sys_department.go 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217
  1. package controllers
  2. import (
  3. "encoding/json"
  4. "hongze/hz_eta_api/models"
  5. "hongze/hz_eta_api/models/system"
  6. "hongze/hz_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. var syncData system.SyncDepartmentData
  56. syncData.Source = utils.SOURCE_ETA_FLAG
  57. syncData.DepartmentId = int(departmentId)
  58. _ = utils.Rc.LPush(utils.CACHE_SYNC_DEPARTMENT, syncData)
  59. br.Ret = 200
  60. br.Success = true
  61. br.Msg = "新增成功"
  62. }
  63. // @Title 修改部门
  64. // @Description 修改部门接口
  65. // @Param request body system.SysDepartmentEditReq true "type json string"
  66. // @Success 200 修改成功
  67. // @router /department/edit [post]
  68. func (this *SysDepartmentController) Edit() {
  69. br := new(models.BaseResponse).Init()
  70. defer func() {
  71. this.Data["json"] = br
  72. this.ServeJSON()
  73. }()
  74. var req system.SysDepartmentEditReq
  75. err := json.Unmarshal(this.Ctx.Input.RequestBody, &req)
  76. if err != nil {
  77. br.Msg = "参数解析异常!"
  78. br.ErrMsg = "参数解析失败,Err:" + err.Error()
  79. return
  80. }
  81. if req.DepartmentId <= 0 {
  82. br.Msg = "参数错误"
  83. br.ErrMsg = "参数错误,DepartmentId 小于等于0 "
  84. return
  85. }
  86. if req.DepartmentName == "" {
  87. br.Msg = "部门名称不能为空"
  88. return
  89. }
  90. item, err := system.GetSysDepartmentByName(req.DepartmentName)
  91. if err != nil && err.Error() != utils.ErrNoRow() {
  92. br.Msg = "获取数据失败"
  93. br.ErrMsg = "获取数据失败,Err:" + err.Error()
  94. return
  95. }
  96. if item != nil {
  97. if item.DepartmentId != req.DepartmentId {
  98. br.Msg = "名称已存在,请重新输入"
  99. return
  100. }
  101. }
  102. err = system.ModifySysDepartment(req.DepartmentName, req.DepartmentId)
  103. if err != nil {
  104. br.Msg = "修改失败"
  105. br.ErrMsg = "修改失败,Err:" + err.Error()
  106. return
  107. }
  108. // 同步部门缓存
  109. var syncData system.SyncDepartmentData
  110. syncData.Source = utils.SOURCE_ETA_FLAG
  111. syncData.DepartmentId = req.DepartmentId
  112. _ = utils.Rc.LPush(utils.CACHE_SYNC_DEPARTMENT, syncData)
  113. br.Ret = 200
  114. br.Success = true
  115. br.Msg = "修改成功"
  116. }
  117. // @Title 删除部门
  118. // @Description 删除部门接口
  119. // @Param request body system.SysDepartmentDeleteReq true "type json string"
  120. // @Success 200 删除成功
  121. // @router /department/delete [post]
  122. func (this *SysDepartmentController) Delete() {
  123. br := new(models.BaseResponse).Init()
  124. defer func() {
  125. this.Data["json"] = br
  126. this.ServeJSON()
  127. }()
  128. var req system.SysDepartmentDeleteReq
  129. err := json.Unmarshal(this.Ctx.Input.RequestBody, &req)
  130. if err != nil {
  131. br.Msg = "参数解析异常!"
  132. br.ErrMsg = "参数解析失败,Err:" + err.Error()
  133. return
  134. }
  135. if req.DepartmentId <= 0 {
  136. br.Msg = "参数错误"
  137. br.ErrMsg = "参数错误,DepartmentId 小于等于0 "
  138. return
  139. }
  140. err = system.DeleteSysDepartment(req.DepartmentId)
  141. if err != nil {
  142. br.Msg = "删除失败"
  143. br.ErrMsg = "删除失败,Err:" + err.Error()
  144. return
  145. }
  146. // 同步部门缓存
  147. var syncData system.SyncDepartmentData
  148. syncData.Source = utils.SOURCE_ETA_FLAG
  149. syncData.DepartmentId = req.DepartmentId
  150. _ = utils.Rc.LPush(utils.CACHE_SYNC_DEPARTMENT, syncData)
  151. br.Ret = 200
  152. br.Success = true
  153. br.Msg = "删除成功"
  154. }
  155. // @Title 获取部门列表
  156. // @Description 获取部门列表接口
  157. // @Success 200 {object} system.SysDepartmentListResp
  158. // @router /department/list [get]
  159. func (this *SysDepartmentController) ListDepartment() {
  160. br := new(models.BaseResponse).Init()
  161. defer func() {
  162. this.Data["json"] = br
  163. this.ServeJSON()
  164. }()
  165. sysUser := this.SysUser
  166. if sysUser == nil {
  167. br.Msg = "请登录"
  168. br.ErrMsg = "请登录,SysUser Is Empty"
  169. return
  170. }
  171. list, err := system.GetDepartmentList()
  172. if err != nil {
  173. br.Msg = "获取失败"
  174. br.ErrMsg = "获取失败,Err:" + err.Error()
  175. return
  176. }
  177. lenList := len(list)
  178. for i := 0; i < lenList; i++ {
  179. departmentId := list[i].DepartmentId
  180. groupList, err := system.GetSysGroupByDepartmentId(departmentId)
  181. if err != nil {
  182. br.Msg = "获取数据失败"
  183. br.ErrMsg = "获取数据失败,Err:" + err.Error()
  184. return
  185. }
  186. list[i].Child = groupList
  187. list[i].IsDepartment = true
  188. for i2, sysGroup := range groupList {
  189. teamList, err := system.GetSysTeamByDepartmentId(sysGroup.GroupId)
  190. if err != nil {
  191. br.Msg = "获取数据失败"
  192. br.ErrMsg = "获取数据失败,Err:" + err.Error()
  193. return
  194. }
  195. groupList[i2].Child = teamList
  196. groupList[i2].IsGroup = true
  197. }
  198. }
  199. resp := new(system.SysDepartmentListResp)
  200. resp.List = list
  201. br.Ret = 200
  202. br.Success = true
  203. br.Msg = "获取成功"
  204. br.Data = resp
  205. }