sys_dept.go 6.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295
  1. package service
  2. import (
  3. "errors"
  4. "go-admin/app/admin/models"
  5. log "github.com/go-admin-team/go-admin-core/logger"
  6. "github.com/go-admin-team/go-admin-core/sdk/pkg"
  7. "gorm.io/gorm"
  8. "go-admin/app/admin/service/dto"
  9. cDto "go-admin/common/dto"
  10. "github.com/go-admin-team/go-admin-core/sdk/service"
  11. )
  12. type SysDept struct {
  13. service.Service
  14. }
  15. // GetPage 获取SysDept列表
  16. //func (e *SysDept) GetPage(c *dto.SysDeptGetPageReq, list *[]models.SysDept) error {
  17. // var err error
  18. // var data models.SysDept
  19. //
  20. // err = e.Orm.Model(&data).
  21. // Scopes(
  22. // cDto.MakeCondition(c.GetNeedSearch()),
  23. // ).
  24. // Find(list).Error
  25. // if err != nil {
  26. // e.Log.Errorf("db error:%s", err)
  27. // return err
  28. // }
  29. // return nil
  30. //}
  31. // Get 获取SysDept对象
  32. func (e *SysDept) Get(d *dto.SysDeptGetReq, model *models.SysDept) error {
  33. var err error
  34. var data models.SysDept
  35. db := e.Orm.Model(&data).
  36. First(model, d.GetId())
  37. err = db.Error
  38. if err != nil && errors.Is(err, gorm.ErrRecordNotFound) {
  39. err = errors.New("查看对象不存在或无权查看")
  40. e.Log.Errorf("db error:%s", err)
  41. return err
  42. }
  43. if err = db.Error; err != nil {
  44. e.Log.Errorf("db error:%s", err)
  45. return err
  46. }
  47. return nil
  48. }
  49. // Insert 创建SysDept对象
  50. func (e *SysDept) Insert(c *dto.SysDeptInsertReq) error {
  51. var err error
  52. var data models.SysDept
  53. c.Generate(&data)
  54. tx := e.Orm.Debug().Begin()
  55. defer func() {
  56. if err != nil {
  57. tx.Rollback()
  58. } else {
  59. tx.Commit()
  60. }
  61. }()
  62. err = tx.Create(&data).Error
  63. if err != nil {
  64. e.Log.Errorf("db error:%s", err)
  65. return err
  66. }
  67. deptPath := pkg.IntToString(data.DeptId) + "/"
  68. if data.ParentId != 0 {
  69. var deptP models.SysDept
  70. tx.First(&deptP, data.ParentId)
  71. deptPath = deptP.DeptPath + deptPath
  72. } else {
  73. deptPath = "/0/" + deptPath
  74. }
  75. var mp = map[string]string{}
  76. mp["dept_path"] = deptPath
  77. if err := tx.Model(&data).Update("dept_path", deptPath).Error; err != nil {
  78. e.Log.Errorf("db error:%s", err)
  79. return err
  80. }
  81. return nil
  82. }
  83. // Update 修改SysDept对象
  84. func (e *SysDept) Update(c *dto.SysDeptUpdateReq) error {
  85. var err error
  86. var model = models.SysDept{}
  87. tx := e.Orm.Debug().Begin()
  88. defer func() {
  89. if err != nil {
  90. tx.Rollback()
  91. } else {
  92. tx.Commit()
  93. }
  94. }()
  95. tx.First(&model, c.GetId())
  96. c.Generate(&model)
  97. deptPath := pkg.IntToString(model.DeptId) + "/"
  98. if model.ParentId != 0 {
  99. var deptP models.SysDept
  100. tx.First(&deptP, model.ParentId)
  101. deptPath = deptP.DeptPath + deptPath
  102. } else {
  103. deptPath = "/0/" + deptPath
  104. }
  105. model.DeptPath = deptPath
  106. db := tx.Save(&model)
  107. if err = db.Error; err != nil {
  108. e.Log.Errorf("UpdateSysDept error:%s", err)
  109. return err
  110. }
  111. if db.RowsAffected == 0 {
  112. return errors.New("无权更新该数据")
  113. }
  114. return nil
  115. }
  116. // Remove 删除SysDept
  117. func (e *SysDept) Remove(d *dto.SysDeptDeleteReq) error {
  118. var err error
  119. var data models.SysDept
  120. db := e.Orm.Model(&data).Delete(&data, d.GetId())
  121. if err = db.Error; err != nil {
  122. err = db.Error
  123. e.Log.Errorf("Delete error: %s", err)
  124. return err
  125. }
  126. if db.RowsAffected == 0 {
  127. err = errors.New("无权删除该数据")
  128. return err
  129. }
  130. return nil
  131. }
  132. // GetSysDeptList 获取组织数据
  133. func (e *SysDept) getList(c *dto.SysDeptGetPageReq, list *[]models.SysDept) error {
  134. var err error
  135. var data models.SysDept
  136. err = e.Orm.Model(&data).
  137. Scopes(
  138. cDto.MakeCondition(c.GetNeedSearch()),
  139. ).
  140. Find(list).Error
  141. if err != nil {
  142. e.Log.Errorf("db error:%s", err)
  143. return err
  144. }
  145. return nil
  146. }
  147. // SetDeptTree 设置组织数据
  148. func (e *SysDept) SetDeptTree(c *dto.SysDeptGetPageReq) (m []dto.DeptLabel, err error) {
  149. var list []models.SysDept
  150. err = e.getList(c, &list)
  151. m = make([]dto.DeptLabel, 0)
  152. for i := 0; i < len(list); i++ {
  153. if list[i].ParentId != 0 {
  154. continue
  155. }
  156. e := dto.DeptLabel{}
  157. e.Id = list[i].DeptId
  158. e.Label = list[i].DeptName
  159. deptsInfo := deptTreeCall(&list, e)
  160. m = append(m, deptsInfo)
  161. }
  162. return
  163. }
  164. // Call 递归构造组织数据
  165. func deptTreeCall(deptList *[]models.SysDept, dept dto.DeptLabel) dto.DeptLabel {
  166. list := *deptList
  167. min := make([]dto.DeptLabel, 0)
  168. for j := 0; j < len(list); j++ {
  169. if dept.Id != list[j].ParentId {
  170. continue
  171. }
  172. mi := dto.DeptLabel{Id: list[j].DeptId, Label: list[j].DeptName, Children: []dto.DeptLabel{}}
  173. ms := deptTreeCall(deptList, mi)
  174. min = append(min, ms)
  175. }
  176. dept.Children = min
  177. return dept
  178. }
  179. // SetDeptPage 设置dept页面数据
  180. func (e *SysDept) SetDeptPage(c *dto.SysDeptGetPageReq) (m []models.SysDept, err error) {
  181. var list []models.SysDept
  182. err = e.getList(c, &list)
  183. for i := 0; i < len(list); i++ {
  184. if list[i].ParentId != 0 {
  185. continue
  186. }
  187. info := e.deptPageCall(&list, list[i])
  188. m = append(m, info)
  189. }
  190. return
  191. }
  192. func (e *SysDept) deptPageCall(deptlist *[]models.SysDept, menu models.SysDept) models.SysDept {
  193. list := *deptlist
  194. min := make([]models.SysDept, 0)
  195. for j := 0; j < len(list); j++ {
  196. if menu.DeptId != list[j].ParentId {
  197. continue
  198. }
  199. mi := models.SysDept{}
  200. mi.DeptId = list[j].DeptId
  201. mi.ParentId = list[j].ParentId
  202. mi.DeptPath = list[j].DeptPath
  203. mi.DeptName = list[j].DeptName
  204. mi.Sort = list[j].Sort
  205. mi.Leader = list[j].Leader
  206. mi.Phone = list[j].Phone
  207. mi.Email = list[j].Email
  208. mi.Status = list[j].Status
  209. mi.CreatedAt = list[j].CreatedAt
  210. mi.Children = []models.SysDept{}
  211. ms := e.deptPageCall(deptlist, mi)
  212. min = append(min, ms)
  213. }
  214. menu.Children = min
  215. return menu
  216. }
  217. // GetRoleDeptId 获取角色的部门ID集合
  218. func (e *SysDept) GetWithRoleId(roleId int) ([]int, error) {
  219. deptIds := make([]int, 0)
  220. deptList := make([]dto.DeptIdList, 0)
  221. if err := e.Orm.Table("sys_role_dept").
  222. Select("sys_role_dept.dept_id").
  223. Joins("LEFT JOIN sys_dept on sys_dept.dept_id=sys_role_dept.dept_id").
  224. Where("role_id = ? ", roleId).
  225. Where(" sys_role_dept.dept_id not in(select sys_dept.parent_id from sys_role_dept LEFT JOIN sys_dept on sys_dept.dept_id=sys_role_dept.dept_id where role_id =? )", roleId).
  226. Find(&deptList).Error; err != nil {
  227. return nil, err
  228. }
  229. for i := 0; i < len(deptList); i++ {
  230. deptIds = append(deptIds, deptList[i].DeptId)
  231. }
  232. return deptIds, nil
  233. }
  234. func (e *SysDept) SetDeptLabel() (m []dto.DeptLabel, err error) {
  235. list := make([]models.SysDept, 0)
  236. err = e.Orm.Find(&list).Error
  237. if err != nil {
  238. log.Error("find dept list error, %s", err.Error())
  239. return
  240. }
  241. m = make([]dto.DeptLabel, 0)
  242. var item dto.DeptLabel
  243. for i := range list {
  244. if list[i].ParentId != 0 {
  245. continue
  246. }
  247. item = dto.DeptLabel{}
  248. item.Id = list[i].DeptId
  249. item.Label = list[i].DeptName
  250. deptInfo := deptLabelCall(&list, item)
  251. m = append(m, deptInfo)
  252. }
  253. return
  254. }
  255. // deptLabelCall
  256. func deptLabelCall(deptList *[]models.SysDept, dept dto.DeptLabel) dto.DeptLabel {
  257. list := *deptList
  258. var mi dto.DeptLabel
  259. min := make([]dto.DeptLabel, 0)
  260. for j := 0; j < len(list); j++ {
  261. if dept.Id != list[j].ParentId {
  262. continue
  263. }
  264. mi = dto.DeptLabel{Id: list[j].DeptId, Label: list[j].DeptName, Children: []dto.DeptLabel{}}
  265. ms := deptLabelCall(deptList, mi)
  266. min = append(min, ms)
  267. }
  268. dept.Children = min
  269. return dept
  270. }