dept.go 6.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276
  1. package models
  2. import (
  3. "errors"
  4. _ "time"
  5. orm "device-manage/common/global"
  6. "device-manage/tools"
  7. )
  8. type SysDept struct {
  9. DeptId int `json:"deptId" gorm:"primary_key;auto_increment;"` //部门编码
  10. ParentId int `json:"parentId" gorm:""` //上级部门
  11. DeptPath string `json:"deptPath" gorm:"size:255;"` //
  12. DeptName string `json:"deptName" gorm:"size:128;"` //部门名称
  13. Sort int `json:"sort" gorm:""` //排序
  14. Leader string `json:"leader" gorm:"size:128;"` //负责人
  15. Phone string `json:"phone" gorm:"size:11;"` //手机
  16. Email string `json:"email" gorm:"size:64;"` //邮箱
  17. Status string `json:"status" gorm:"size:4;"` //状态
  18. CreateBy string `json:"createBy" gorm:"size:64;"`
  19. UpdateBy string `json:"updateBy" gorm:"size:64;"`
  20. BaseModel
  21. DataScope string `json:"dataScope" gorm:"-"`
  22. Params string `json:"params" gorm:"-"`
  23. Children []SysDept `json:"children" gorm:"-"`
  24. }
  25. func (SysDept) TableName() string {
  26. return "sys_dept"
  27. }
  28. type DeptLable struct {
  29. Id int `gorm:"-" json:"id"`
  30. Label string `gorm:"-" json:"label"`
  31. Children []DeptLable `gorm:"-" json:"children"`
  32. }
  33. func (e *SysDept) Create() (SysDept, error) {
  34. var doc SysDept
  35. result := orm.Eloquent.Table(e.TableName()).Create(&e)
  36. if result.Error != nil {
  37. err := result.Error
  38. return doc, err
  39. }
  40. deptPath := "/" + tools.IntToString(e.DeptId)
  41. if int(e.ParentId) != 0 {
  42. var deptP SysDept
  43. orm.Eloquent.Table(e.TableName()).Where("dept_id = ?", e.ParentId).First(&deptP)
  44. deptPath = deptP.DeptPath + deptPath
  45. } else {
  46. deptPath = "/0" + deptPath
  47. }
  48. var mp = map[string]string{}
  49. mp["deptPath"] = deptPath
  50. if err := orm.Eloquent.Table(e.TableName()).Where("dept_id = ?", e.DeptId).Updates(mp).Error; err != nil {
  51. err := result.Error
  52. return doc, err
  53. }
  54. doc = *e
  55. doc.DeptPath = deptPath
  56. return doc, nil
  57. }
  58. func (e *SysDept) Get() (SysDept, error) {
  59. var doc SysDept
  60. table := orm.Eloquent.Table(e.TableName())
  61. if e.DeptId != 0 {
  62. table = table.Where("dept_id = ?", e.DeptId)
  63. }
  64. if e.DeptName != "" {
  65. table = table.Where("dept_name = ?", e.DeptName)
  66. }
  67. if err := table.First(&doc).Error; err != nil {
  68. return doc, err
  69. }
  70. return doc, nil
  71. }
  72. func (e *SysDept) GetList() ([]SysDept, error) {
  73. var doc []SysDept
  74. table := orm.Eloquent.Table(e.TableName())
  75. if e.DeptId != 0 {
  76. table = table.Where("dept_id = ?", e.DeptId)
  77. }
  78. if e.DeptName != "" {
  79. table = table.Where("dept_name = ?", e.DeptName)
  80. }
  81. if e.Status != "" {
  82. table = table.Where("status = ?", e.Status)
  83. }
  84. if err := table.Order("sort").Find(&doc).Error; err != nil {
  85. return doc, err
  86. }
  87. return doc, nil
  88. }
  89. func (e *SysDept) GetPage(bl bool) ([]SysDept, error) {
  90. var doc []SysDept
  91. table := orm.Eloquent.Table(e.TableName())
  92. if e.DeptId != 0 {
  93. table = table.Where("dept_id = ?", e.DeptId)
  94. }
  95. if e.DeptName != "" {
  96. table = table.Where("dept_name = ?", e.DeptName)
  97. }
  98. if e.Status != "" {
  99. table = table.Where("status = ?", e.Status)
  100. }
  101. if e.DeptPath != "" {
  102. table = table.Where("deptPath like %?%", e.DeptPath)
  103. }
  104. if bl {
  105. // 数据权限控制
  106. dataPermission := new(DataPermission)
  107. dataPermission.UserId, _ = tools.StringToInt(e.DataScope)
  108. tableper, err := dataPermission.GetDataScope("sys_dept", table)
  109. if err != nil {
  110. return nil, err
  111. }
  112. table = tableper
  113. }
  114. if err := table.Order("sort").Find(&doc).Error; err != nil {
  115. return nil, err
  116. }
  117. return doc, nil
  118. }
  119. func (e *SysDept) SetDept(bl bool) ([]SysDept, error) {
  120. list, err := e.GetPage(bl)
  121. m := make([]SysDept, 0)
  122. for i := 0; i < len(list); i++ {
  123. if list[i].ParentId != 0 {
  124. continue
  125. }
  126. info := Digui(&list, list[i])
  127. m = append(m, info)
  128. }
  129. return m, err
  130. }
  131. func Digui(deptlist *[]SysDept, menu SysDept) SysDept {
  132. list := *deptlist
  133. min := make([]SysDept, 0)
  134. for j := 0; j < len(list); j++ {
  135. if menu.DeptId != list[j].ParentId {
  136. continue
  137. }
  138. mi := SysDept{}
  139. mi.DeptId = list[j].DeptId
  140. mi.ParentId = list[j].ParentId
  141. mi.DeptPath = list[j].DeptPath
  142. mi.DeptName = list[j].DeptName
  143. mi.Sort = list[j].Sort
  144. mi.Leader = list[j].Leader
  145. mi.Phone = list[j].Phone
  146. mi.Email = list[j].Email
  147. mi.Status = list[j].Status
  148. mi.Children = []SysDept{}
  149. ms := Digui(deptlist, mi)
  150. min = append(min, ms)
  151. }
  152. menu.Children = min
  153. return menu
  154. }
  155. func (e *SysDept) Update(id int) (update SysDept, err error) {
  156. if err = orm.Eloquent.Table(e.TableName()).Where("dept_id = ?", id).First(&update).Error; err != nil {
  157. return
  158. }
  159. deptPath := "/" + tools.IntToString(e.DeptId)
  160. if int(e.ParentId) != 0 {
  161. var deptP SysDept
  162. orm.Eloquent.Table(e.TableName()).Where("dept_id = ?", e.ParentId).First(&deptP)
  163. deptPath = deptP.DeptPath + deptPath
  164. } else {
  165. deptPath = "/0" + deptPath
  166. }
  167. e.DeptPath = deptPath
  168. if e.DeptPath != "" && e.DeptPath != update.DeptPath {
  169. return update, errors.New("上级部门不允许修改!")
  170. }
  171. //参数1:是要修改的数据
  172. //参数2:是修改的数据
  173. if err = orm.Eloquent.Table(e.TableName()).Model(&update).Updates(&e).Error; err != nil {
  174. return
  175. }
  176. return
  177. }
  178. func (e *SysDept) Delete(id int) (success bool, err error) {
  179. user := SysUser{}
  180. user.DeptId = id
  181. userlist, err := user.GetList()
  182. tools.HasError(err, "", 500)
  183. tools.Assert(len(userlist) <= 0, "当前部门存在用户,不能删除!", 500)
  184. tx := orm.Eloquent.Begin()
  185. defer func() {
  186. if r := recover(); r != nil {
  187. tx.Rollback()
  188. }
  189. }()
  190. if err = tx.Error; err != nil {
  191. success = false
  192. return
  193. }
  194. if err = tx.Table(e.TableName()).Where("dept_id = ?", id).Delete(&SysDept{}).Error; err != nil {
  195. success = false
  196. tx.Rollback()
  197. return
  198. }
  199. if err = tx.Commit().Error; err != nil {
  200. success = false
  201. return
  202. }
  203. success = true
  204. return
  205. }
  206. func (dept *SysDept) SetDeptLable() (m []DeptLable, err error) {
  207. deptlist, err := dept.GetList()
  208. m = make([]DeptLable, 0)
  209. for i := 0; i < len(deptlist); i++ {
  210. if deptlist[i].ParentId != 0 {
  211. continue
  212. }
  213. e := DeptLable{}
  214. e.Id = deptlist[i].DeptId
  215. e.Label = deptlist[i].DeptName
  216. deptsInfo := DiguiDeptLable(&deptlist, e)
  217. m = append(m, deptsInfo)
  218. }
  219. return
  220. }
  221. func DiguiDeptLable(deptlist *[]SysDept, dept DeptLable) DeptLable {
  222. list := *deptlist
  223. min := make([]DeptLable, 0)
  224. for j := 0; j < len(list); j++ {
  225. if dept.Id != list[j].ParentId {
  226. continue
  227. }
  228. mi := DeptLable{list[j].DeptId, list[j].DeptName, []DeptLable{}}
  229. ms := DiguiDeptLable(deptlist, mi)
  230. min = append(min, ms)
  231. }
  232. dept.Children = min
  233. return dept
  234. }