sys_dept.go 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443
  1. package service
  2. import (
  3. "Medical_OAuth/common/actions"
  4. cDto "Medical_OAuth/common/dto"
  5. "Medical_OAuth/common/global"
  6. "encoding/json"
  7. "errors"
  8. "fmt"
  9. "gogs.baozhida.cn/zoie/OAuth-core/service"
  10. "gorm.io/gorm"
  11. "gorm.io/gorm/clause"
  12. "Medical_OAuth/app/admin/model"
  13. "Medical_OAuth/app/admin/service/dto"
  14. "gogs.baozhida.cn/zoie/OAuth-core/pkg"
  15. )
  16. type SysDept struct {
  17. service.Service
  18. }
  19. // Get 获取SysDept对象
  20. func (e *SysDept) Get(d *dto.SysDeptGetReq, deptModel *model.SysDept) error {
  21. err := e.Orm.
  22. First(deptModel, d.GetId()).Error
  23. if err != nil {
  24. e.Log.Errorf("db error: %s", err)
  25. if errors.Is(err, gorm.ErrRecordNotFound) {
  26. return global.GetNotFoundOrNoPermissionErr
  27. }
  28. return global.GetFailedErr
  29. }
  30. return nil
  31. }
  32. // Insert 创建SysDept对象
  33. func (e *SysDept) Insert(c *dto.SysDeptInsertReq, deptId int) error {
  34. var err error
  35. var data model.SysDept
  36. c.Generate(&data)
  37. tx := e.Orm.Debug().Begin()
  38. defer func() {
  39. if err != nil {
  40. tx.Rollback()
  41. } else {
  42. tx.Commit()
  43. }
  44. }()
  45. if deptId != 0 && c.ParentId == 0 {
  46. data.ParentId = deptId
  47. }
  48. err = tx.Create(&data).Error
  49. if err != nil {
  50. e.Log.Errorf("db error: %s", err)
  51. return global.CreateFailedErr
  52. }
  53. c.Id = data.Id
  54. deptPath := pkg.IntToString(data.Id) + "/"
  55. if data.ParentId != 0 {
  56. var deptP model.SysDept
  57. tx.First(&deptP, data.ParentId)
  58. deptPath = deptP.DeptPath + deptPath
  59. } else {
  60. deptPath = "/0/" + deptPath
  61. }
  62. var mp = map[string]string{}
  63. mp["dept_path"] = deptPath
  64. err = tx.Model(&data).Update("dept_path", deptPath).Error
  65. if err != nil {
  66. e.Log.Errorf("db error: %s", err)
  67. return global.CreateFailedErr
  68. }
  69. return nil
  70. }
  71. // Update 修改SysDept对象
  72. func (e *SysDept) Update(c *dto.SysDeptUpdateReq) error {
  73. var err error
  74. var deptModel = model.SysDept{}
  75. tx := e.Orm.Debug().Begin()
  76. defer func() {
  77. if err != nil {
  78. tx.Rollback()
  79. } else {
  80. tx.Commit()
  81. }
  82. }()
  83. err = tx.First(&deptModel, c.GetId()).Error
  84. if err != nil {
  85. e.Log.Errorf("db error: %s", err)
  86. if errors.Is(err, gorm.ErrRecordNotFound) {
  87. return global.UpdateNotFoundOrNoPermissionErr
  88. }
  89. return global.UpdateFailedErr
  90. }
  91. c.Generate(&deptModel)
  92. DeptPath := pkg.IntToString(deptModel.Id) + "/"
  93. if deptModel.ParentId != 0 {
  94. var DeptP model.SysDept
  95. tx.First(&DeptP, deptModel.ParentId)
  96. DeptPath = DeptP.DeptPath + DeptPath
  97. } else {
  98. DeptPath = "/0/" + DeptPath
  99. }
  100. deptModel.DeptPath = DeptPath
  101. db := tx.Save(&deptModel)
  102. if err = db.Error; err != nil {
  103. e.Log.Errorf("UpdateSysDept error: %s", err)
  104. return err
  105. }
  106. return nil
  107. }
  108. // Remove 删除SysDept
  109. // 删除部门 同时删除其子部门、部门下的用户
  110. func (e *SysDept) Remove(d *dto.SysDeptDeleteReq, p *actions.DataPermission) error {
  111. var err error
  112. tx := e.Orm.Begin()
  113. defer func() {
  114. if err != nil {
  115. tx.Rollback()
  116. } else {
  117. tx.Commit()
  118. }
  119. }()
  120. var dept model.SysDept
  121. err = e.Orm.
  122. First(&dept, d.GetId()).Error
  123. if err != nil {
  124. e.Log.Errorf("db error: %s", err)
  125. if errors.Is(err, gorm.ErrRecordNotFound) {
  126. return global.GetNotFoundOrNoPermissionErr
  127. }
  128. return global.DeleteFailedErr
  129. }
  130. var deptList = make([]model.SysDept, 0)
  131. var deptIds = make([]int, 0)
  132. tx.Where("dept_path like ?", dept.DeptPath+"%").Find(&deptList)
  133. // 获取部门及其子部门下ID
  134. for _, v := range deptList {
  135. deptIds = append(deptIds, v.Id)
  136. }
  137. db := tx.Select(clause.Associations).Delete(&deptList)
  138. if err = db.Error; err != nil {
  139. e.Log.Errorf("Delete error: %s", err)
  140. return global.DeleteFailedErr
  141. }
  142. if db.RowsAffected == 0 {
  143. return global.DeleteNotFoundOrNoPermissionErr
  144. }
  145. // 删除部门下的用户
  146. dbUser := tx.Where("dept_id in (?)", deptIds).Delete(&model.SysUser{})
  147. if err = dbUser.Error; err != nil {
  148. e.Log.Errorf("Delete User error: %s", err)
  149. return global.DeleteFailedErr
  150. }
  151. return nil
  152. }
  153. // getList 获取组织数据
  154. func (e SysDept) getList(c *dto.SysDeptGetPageReq, list *[]model.SysDept, p *actions.DataPermission) error {
  155. var err error
  156. var data model.SysDept
  157. if len(c.DeptName) == 0 && c.DeptId == 0 {
  158. err = e.Orm.Model(&data).
  159. Scopes(
  160. cDto.MakeCondition(c.GetNeedSearch()),
  161. ).
  162. Find(list).Error
  163. if err != nil {
  164. e.Log.Errorf("db error:%s", err)
  165. return err
  166. }
  167. return nil
  168. }
  169. var tempList []model.SysDept
  170. err = e.Orm.Model(&data).Where("id in (select id from sys_dept where dept_path like ? )", "%/"+pkg.IntToString(p.DeptId)+"/%").
  171. Scopes(
  172. cDto.MakeCondition(c.GetNeedSearch()),
  173. ).
  174. Find(&tempList).Error
  175. if err != nil {
  176. e.Log.Errorf("db error: %s", err)
  177. return global.GetFailedErr
  178. }
  179. // 获取部门及其子部门下ID
  180. for _, dept := range tempList {
  181. var deptList = make([]model.SysDept, 0)
  182. err = e.Orm.Where("dept_path like ?", dept.DeptPath+"%").Order(fmt.Sprintf("sort %s", c.SortOrder)).Find(&deptList).Error
  183. *list = append(*list, deptList...)
  184. }
  185. if err != nil {
  186. e.Log.Errorf("db error: %s", err)
  187. return global.GetFailedErr
  188. }
  189. *list = DeduplicateDept(*list)
  190. return nil
  191. }
  192. // SetDeptTree 设置组织数据
  193. func (e *SysDept) SetDeptTree(c *dto.SysDeptGetPageReq, p *actions.DataPermission) (m []dto.DeptLabel, err error) {
  194. var list []model.SysDept
  195. err = e.getList(c, &list, p)
  196. m = make([]dto.DeptLabel, 0)
  197. for i := 0; i < len(list); i++ {
  198. if list[i].ParentId != 0 {
  199. continue
  200. }
  201. e := dto.DeptLabel{}
  202. e.Id = list[i].Id
  203. e.Label = list[i].DeptName
  204. DeptInfo := DeptTreeCall(&list, e)
  205. m = append(m, DeptInfo)
  206. }
  207. return
  208. }
  209. // DeptTreeCall 递归构造组织数据
  210. func DeptTreeCall(DeptList *[]model.SysDept, Dept dto.DeptLabel) dto.DeptLabel {
  211. list := *DeptList
  212. min := make([]dto.DeptLabel, 0)
  213. for j := 0; j < len(list); j++ {
  214. if Dept.Id != list[j].ParentId {
  215. continue
  216. }
  217. mi := dto.DeptLabel{Id: list[j].Id, Label: list[j].DeptName, Children: []dto.DeptLabel{}}
  218. ms := DeptTreeCall(DeptList, mi)
  219. min = append(min, ms)
  220. }
  221. Dept.Children = min
  222. return Dept
  223. }
  224. // SetDeptPage 设置dept页面数据
  225. //func (e *SysDept) SetDeptPage(c *dto.SysDeptGetPageReq, p *actions.DataPermission) (m []model.SysDept, err error) {
  226. // var list []model.SysDept
  227. // err = e.getList(c, &list, p)
  228. // for i := 0; i < len(list); i++ {
  229. // if list[i].ParentId != 0 {
  230. // continue
  231. // }
  232. // info := e.deptPageCall(&list, list[i])
  233. // m = append(m, info)
  234. // }
  235. // return
  236. //}
  237. //
  238. //func (e *SysDept) deptPageCall(deptlist *[]model.SysDept, dept model.SysDept) model.SysDept {
  239. // list := *deptlist
  240. // min := make([]model.SysDept, 0)
  241. // for j := 0; j < len(list); j++ {
  242. // if dept.Id != list[j].ParentId {
  243. // continue
  244. // }
  245. // mi := model.SysDept{}
  246. // mi.Id = list[j].Id
  247. // mi.ParentId = list[j].ParentId
  248. // mi.DeptPath = list[j].DeptPath
  249. // mi.DeptName = list[j].DeptName
  250. // mi.Sort = list[j].Sort
  251. // mi.Status = list[j].Status
  252. // mi.CreatedAt = list[j].CreatedAt
  253. // mi.Children = []model.SysDept{}
  254. // ms := e.deptPageCall(deptlist, mi)
  255. // min = append(min, ms)
  256. // }
  257. // dept.Children = min
  258. // return dept
  259. //}
  260. // SetDeptPage 设置Dept页面数据
  261. func (e *SysDept) SetDeptPage(c *dto.SysDeptGetPageReq, p *actions.DataPermission) (m []model.SysDept, err error) {
  262. var list []model.SysDept
  263. err = e.getList(c, &list, p)
  264. for i := 0; i < len(list); i++ {
  265. if list[i].ParentId != 0 {
  266. continue
  267. }
  268. info := e.DeptPageCall(&list, list[i])
  269. m = append(m, info)
  270. }
  271. if len(m) == 0 {
  272. parentMap, flag := getDeptParent(list)
  273. if flag {
  274. for i := 0; i < len(list); i++ {
  275. if parentMap[list[i].ParentId] {
  276. continue
  277. }
  278. info := e.DeptPageCall(&list, list[i])
  279. m = append(m, info)
  280. }
  281. } else {
  282. for i := 0; i < len(list); i++ {
  283. info := e.DeptPageCall(&list, list[i])
  284. m = append(m, info)
  285. }
  286. }
  287. }
  288. return
  289. }
  290. func getDeptParent(deptList []model.SysDept) (map[int]bool, bool) {
  291. list := deptList
  292. var flag = false
  293. var parentMap = map[int]bool{}
  294. for j := 0; j < len(list); j++ {
  295. parentMap[list[j].ParentId] = false
  296. }
  297. for j := 0; j < len(list); j++ {
  298. if _, ok := parentMap[list[j].Id]; !ok {
  299. continue
  300. }
  301. parentMap[list[j].Id] = true
  302. flag = true
  303. }
  304. return parentMap, flag
  305. }
  306. func (e *SysDept) DeptPageCall(deptList *[]model.SysDept, menu model.SysDept) model.SysDept {
  307. list := *deptList
  308. min := make([]model.SysDept, 0)
  309. for j := 0; j < len(list); j++ {
  310. if menu.Id != list[j].ParentId {
  311. continue
  312. }
  313. mi := model.SysDept{}
  314. mi.Id = list[j].Id
  315. mi.ParentId = list[j].ParentId
  316. mi.DeptPath = list[j].DeptPath
  317. mi.DeptName = list[j].DeptName
  318. mi.Sort = list[j].Sort
  319. mi.Status = list[j].Status
  320. mi.CreatedAt = list[j].CreatedAt
  321. mi.Children = []model.SysDept{}
  322. ms := e.DeptPageCall(deptList, mi)
  323. min = append(min, ms)
  324. }
  325. menu.Children = min
  326. return menu
  327. }
  328. // GetWithRoleId 获取角色的部门ID集合
  329. func (e *SysDept) GetWithRoleId(roleId int) ([]int, error) {
  330. DeptIds := make([]int, 0)
  331. DeptList := make([]dto.DeptIdList, 0)
  332. if err := e.Orm.Table("sys_role_dept").
  333. Select("sys_role_dept.dept_id").
  334. Joins("LEFT JOIN sys_dept on sys_dept.id=sys_role_dept.dept_id").
  335. Where("role_id = ? ", roleId).
  336. Where(" sys_role_dept.dept_id not in(select sys_dept.parent_id from sys_role_dept LEFT JOIN sys_dept on sys_dept.id=sys_role_dept.dept_id where role_id =? )", roleId).
  337. Find(&DeptList).Error; err != nil {
  338. e.Log.Errorf("db error: %s", err)
  339. return nil, global.GetFailedErr
  340. }
  341. for i := 0; i < len(DeptList); i++ {
  342. DeptIds = append(DeptIds, DeptList[i].DeptId)
  343. }
  344. return DeptIds, nil
  345. }
  346. func (e *SysDept) SetDeptLabel() (m []dto.DeptLabel, err error) {
  347. list := make([]model.SysDept, 0)
  348. err = e.Orm.Find(&list).Error
  349. if err != nil {
  350. e.Log.Error("find Dept list error, %s", err)
  351. err = global.GetFailedErr
  352. return
  353. }
  354. m = make([]dto.DeptLabel, 0)
  355. var item dto.DeptLabel
  356. for i := range list {
  357. if list[i].ParentId != 0 {
  358. continue
  359. }
  360. item = dto.DeptLabel{}
  361. item.Id = list[i].Id
  362. item.Label = list[i].DeptName
  363. DeptInfo := DeptLabelCall(&list, item)
  364. m = append(m, DeptInfo)
  365. }
  366. return
  367. }
  368. // DeptLabelCall
  369. func DeptLabelCall(DeptList *[]model.SysDept, Dept dto.DeptLabel) dto.DeptLabel {
  370. list := *DeptList
  371. var mi dto.DeptLabel
  372. min := make([]dto.DeptLabel, 0)
  373. for j := 0; j < len(list); j++ {
  374. if Dept.Id != list[j].ParentId {
  375. continue
  376. }
  377. mi = dto.DeptLabel{Id: list[j].Id, Label: list[j].DeptName, Children: []dto.DeptLabel{}}
  378. ms := DeptLabelCall(DeptList, mi)
  379. min = append(min, ms)
  380. }
  381. Dept.Children = min
  382. return Dept
  383. }
  384. func DeduplicateDept(list []model.SysDept) []model.SysDept {
  385. seen := make(map[string]bool)
  386. deduplicated := []model.SysDept{}
  387. for _, v := range list {
  388. // 将结构体编码为 JSON 字符串
  389. key, err := json.Marshal(v)
  390. if err != nil {
  391. // 处理错误
  392. continue
  393. }
  394. // 将 JSON 字符串作为键插入到 map 中
  395. if _, ok := seen[string(key)]; !ok {
  396. seen[string(key)] = true
  397. deduplicated = append(deduplicated, v)
  398. }
  399. }
  400. return deduplicated
  401. }