sys_department.go 5.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223
  1. package controllers
  2. import (
  3. "encoding/json"
  4. "eta/eta_api/models"
  5. "eta/eta_api/models/system"
  6. "eta/eta_api/utils"
  7. "time"
  8. )
  9. // 系统设置
  10. type SysDepartmentController struct {
  11. BaseAuthController
  12. }
  13. // @Title 新增部门
  14. // @Description 新增部门接口
  15. // @Param request body system.SysDepartmentAddReq true "type json string"
  16. // @Success 200 新增成功
  17. // @router /department/add [post]
  18. func (this *SysDepartmentController) Add() {
  19. br := new(models.BaseResponse).Init()
  20. defer func() {
  21. this.Data["json"] = br
  22. this.ServeJSON()
  23. }()
  24. var req system.SysDepartmentAddReq
  25. err := json.Unmarshal(this.Ctx.Input.RequestBody, &req)
  26. if err != nil {
  27. br.Msg = "参数解析异常!"
  28. br.ErrMsg = "参数解析失败,Err:" + err.Error()
  29. return
  30. }
  31. if req.DepartmentName == "" {
  32. br.Msg = "部门名称不能为空"
  33. return
  34. }
  35. count, err := system.GetSysDepartmentCount(req.DepartmentName)
  36. if err != nil {
  37. br.Msg = "获取数据失败"
  38. br.ErrMsg = "获取数据失败"
  39. return
  40. }
  41. if count > 0 {
  42. br.Msg = "部门名称已存在,请重新输入"
  43. return
  44. }
  45. item := new(system.SysDepartment)
  46. item.DepartmentName = req.DepartmentName
  47. item.CreateTime = time.Now()
  48. departmentId, err := system.AddSysDepartment(item)
  49. if err != nil {
  50. br.Msg = "新增失败"
  51. br.ErrMsg = "新增失败,Err:" + err.Error()
  52. return
  53. }
  54. // 同步部门缓存
  55. if utils.BusinessCode == utils.BusinessCodeRelease {
  56. var syncData system.SyncDepartmentData
  57. syncData.Source = utils.SOURCE_ETA_FLAG
  58. syncData.DepartmentId = int(departmentId)
  59. _ = utils.Rc.LPush(utils.CACHE_SYNC_DEPARTMENT, syncData)
  60. }
  61. br.Ret = 200
  62. br.Success = true
  63. br.Msg = "新增成功"
  64. }
  65. // @Title 修改部门
  66. // @Description 修改部门接口
  67. // @Param request body system.SysDepartmentEditReq true "type json string"
  68. // @Success 200 修改成功
  69. // @router /department/edit [post]
  70. func (this *SysDepartmentController) Edit() {
  71. br := new(models.BaseResponse).Init()
  72. defer func() {
  73. this.Data["json"] = br
  74. this.ServeJSON()
  75. }()
  76. var req system.SysDepartmentEditReq
  77. err := json.Unmarshal(this.Ctx.Input.RequestBody, &req)
  78. if err != nil {
  79. br.Msg = "参数解析异常!"
  80. br.ErrMsg = "参数解析失败,Err:" + err.Error()
  81. return
  82. }
  83. if req.DepartmentId <= 0 {
  84. br.Msg = "参数错误"
  85. br.ErrMsg = "参数错误,DepartmentId 小于等于0 "
  86. return
  87. }
  88. if req.DepartmentName == "" {
  89. br.Msg = "部门名称不能为空"
  90. return
  91. }
  92. item, err := system.GetSysDepartmentByName(req.DepartmentName)
  93. if err != nil && err.Error() != utils.ErrNoRow() {
  94. br.Msg = "获取数据失败"
  95. br.ErrMsg = "获取数据失败,Err:" + err.Error()
  96. return
  97. }
  98. if item != nil {
  99. if item.DepartmentId != req.DepartmentId {
  100. br.Msg = "名称已存在,请重新输入"
  101. return
  102. }
  103. }
  104. err = system.ModifySysDepartment(req.DepartmentName, req.DepartmentId)
  105. if err != nil {
  106. br.Msg = "修改失败"
  107. br.ErrMsg = "修改失败,Err:" + err.Error()
  108. return
  109. }
  110. // 同步部门缓存
  111. if utils.BusinessCode == utils.BusinessCodeRelease {
  112. var syncData system.SyncDepartmentData
  113. syncData.Source = utils.SOURCE_ETA_FLAG
  114. syncData.DepartmentId = req.DepartmentId
  115. _ = utils.Rc.LPush(utils.CACHE_SYNC_DEPARTMENT, syncData)
  116. }
  117. br.Ret = 200
  118. br.Success = true
  119. br.Msg = "修改成功"
  120. }
  121. // @Title 删除部门
  122. // @Description 删除部门接口
  123. // @Param request body system.SysDepartmentDeleteReq true "type json string"
  124. // @Success 200 删除成功
  125. // @router /department/delete [post]
  126. func (this *SysDepartmentController) Delete() {
  127. br := new(models.BaseResponse).Init()
  128. defer func() {
  129. this.Data["json"] = br
  130. this.ServeJSON()
  131. }()
  132. var req system.SysDepartmentDeleteReq
  133. err := json.Unmarshal(this.Ctx.Input.RequestBody, &req)
  134. if err != nil {
  135. br.Msg = "参数解析异常!"
  136. br.ErrMsg = "参数解析失败,Err:" + err.Error()
  137. return
  138. }
  139. if req.DepartmentId <= 0 {
  140. br.Msg = "参数错误"
  141. br.ErrMsg = "参数错误,DepartmentId 小于等于0 "
  142. return
  143. }
  144. err = system.DeleteSysDepartment(req.DepartmentId)
  145. if err != nil {
  146. br.Msg = "删除失败"
  147. br.ErrMsg = "删除失败,Err:" + err.Error()
  148. return
  149. }
  150. // 同步部门缓存
  151. if utils.BusinessCode == utils.BusinessCodeRelease {
  152. var syncData system.SyncDepartmentData
  153. syncData.Source = utils.SOURCE_ETA_FLAG
  154. syncData.DepartmentId = req.DepartmentId
  155. _ = utils.Rc.LPush(utils.CACHE_SYNC_DEPARTMENT, syncData)
  156. }
  157. br.Ret = 200
  158. br.Success = true
  159. br.Msg = "删除成功"
  160. }
  161. // @Title 获取部门列表
  162. // @Description 获取部门列表接口
  163. // @Success 200 {object} system.SysDepartmentListResp
  164. // @router /department/list [get]
  165. func (this *SysDepartmentController) ListDepartment() {
  166. br := new(models.BaseResponse).Init()
  167. defer func() {
  168. this.Data["json"] = br
  169. this.ServeJSON()
  170. }()
  171. sysUser := this.SysUser
  172. if sysUser == nil {
  173. br.Msg = "请登录"
  174. br.ErrMsg = "请登录,SysUser Is Empty"
  175. return
  176. }
  177. list, err := system.GetDepartmentList()
  178. if err != nil {
  179. br.Msg = "获取失败"
  180. br.ErrMsg = "获取失败,Err:" + err.Error()
  181. return
  182. }
  183. lenList := len(list)
  184. for i := 0; i < lenList; i++ {
  185. departmentId := list[i].DepartmentId
  186. groupList, err := system.GetSysGroupByDepartmentId(departmentId)
  187. if err != nil {
  188. br.Msg = "获取数据失败"
  189. br.ErrMsg = "获取数据失败,Err:" + err.Error()
  190. return
  191. }
  192. list[i].Child = groupList
  193. list[i].IsDepartment = true
  194. for i2, sysGroup := range groupList {
  195. teamList, err := system.GetSysTeamByDepartmentId(sysGroup.GroupId)
  196. if err != nil {
  197. br.Msg = "获取数据失败"
  198. br.ErrMsg = "获取数据失败,Err:" + err.Error()
  199. return
  200. }
  201. groupList[i2].Child = teamList
  202. groupList[i2].IsGroup = true
  203. }
  204. }
  205. resp := new(system.SysDepartmentListResp)
  206. resp.List = list
  207. br.Ret = 200
  208. br.Success = true
  209. br.Msg = "获取成功"
  210. br.Data = resp
  211. }