sys_group.go 6.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259
  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. "strings"
  8. "time"
  9. )
  10. // 部门组
  11. type SysGroupController struct {
  12. BaseAuthController
  13. }
  14. // @Title 新增分组
  15. // @Description 新增分组接口
  16. // @Param request body system.SysGroupAddReq true "type json string"
  17. // @Success 200 新增成功
  18. // @router /group/add [post]
  19. func (this *SysGroupController) Add() {
  20. br := new(models.BaseResponse).Init()
  21. defer func() {
  22. this.Data["json"] = br
  23. this.ServeJSON()
  24. }()
  25. var req system.SysGroupAddReq
  26. err := json.Unmarshal(this.Ctx.Input.RequestBody, &req)
  27. if err != nil {
  28. br.Msg = "参数解析异常!"
  29. br.ErrMsg = "参数解析失败,Err:" + err.Error()
  30. return
  31. }
  32. if req.DepartmentId <= 0 {
  33. br.Msg = "请选择部门"
  34. br.ErrMsg = "部门ID不可为空"
  35. return
  36. }
  37. if req.GroupName == "" {
  38. br.Msg = "分组名称不能为空"
  39. return
  40. }
  41. groupNameArr := strings.Split(req.GroupName, ",")
  42. for _, v := range groupNameArr {
  43. count, err := system.GetSysGroupCount(req.DepartmentId, v)
  44. if err != nil {
  45. br.Msg = "获取数据失败"
  46. br.ErrMsg = "获取数据失败,Err:" + err.Error()
  47. return
  48. }
  49. if count <= 0 {
  50. item := new(system.SysGroup)
  51. item.GroupName = v
  52. item.DepartmentId = req.DepartmentId
  53. item.CreateTime = time.Now()
  54. groupId, e := system.AddSysGroup(item)
  55. if e != nil {
  56. br.Msg = "新增失败"
  57. br.ErrMsg = "新增失败,Err:" + e.Error()
  58. return
  59. }
  60. // 同步分组缓存
  61. var syncData system.SyncGroupData
  62. syncData.Source = utils.SOURCE_ETA_FLAG
  63. syncData.GroupId = int(groupId)
  64. _ = utils.Rc.LPush(utils.CACHE_SYNC_GROUP, syncData)
  65. }
  66. }
  67. br.Ret = 200
  68. br.Success = true
  69. br.Msg = "新增成功"
  70. }
  71. // @Title 修改分组
  72. // @Description 修改分组接口
  73. // @Param request body system.SysGroupEditReq true "type json string"
  74. // @Success 200 修改成功
  75. // @router /group/edit [post]
  76. func (this *SysGroupController) Edit() {
  77. br := new(models.BaseResponse).Init()
  78. defer func() {
  79. this.Data["json"] = br
  80. this.ServeJSON()
  81. }()
  82. var req system.SysGroupEditReq
  83. err := json.Unmarshal(this.Ctx.Input.RequestBody, &req)
  84. if err != nil {
  85. br.Msg = "参数解析异常!"
  86. br.ErrMsg = "参数解析失败,Err:" + err.Error()
  87. return
  88. }
  89. if req.GroupId <= 0 {
  90. br.Msg = "参数错误"
  91. br.ErrMsg = "参数错误,GroupId 小于等于0 "
  92. return
  93. }
  94. if req.GroupName == "" {
  95. br.Msg = "分组名称不能为空"
  96. return
  97. }
  98. item, err := system.GetSysGroupByName(req.GroupName)
  99. if err != nil && err.Error() != utils.ErrNoRow() {
  100. br.Msg = "获取数据失败"
  101. br.ErrMsg = "获取数据失败,Err:" + err.Error()
  102. return
  103. }
  104. if item != nil {
  105. if item.GroupId != req.GroupId {
  106. br.Msg = "名称已存在,请重新输入"
  107. return
  108. }
  109. }
  110. err = system.ModifySysGroup(req.GroupName, req.GroupId)
  111. if err != nil {
  112. br.Msg = "修改失败"
  113. br.ErrMsg = "修改失败,Err:" + err.Error()
  114. return
  115. }
  116. // 同步分组缓存
  117. var syncData system.SyncGroupData
  118. syncData.Source = utils.SOURCE_ETA_FLAG
  119. syncData.GroupId = req.GroupId
  120. _ = utils.Rc.LPush(utils.CACHE_SYNC_GROUP, syncData)
  121. br.Ret = 200
  122. br.Success = true
  123. br.Msg = "修改成功"
  124. }
  125. // @Title 删除分组
  126. // @Description 删除分组接口
  127. // @Param request body system.SysGroupDeleteReq true "type json string"
  128. // @Success 200 删除成功
  129. // @router /group/delete [post]
  130. func (this *SysGroupController) Delete() {
  131. br := new(models.BaseResponse).Init()
  132. defer func() {
  133. this.Data["json"] = br
  134. this.ServeJSON()
  135. }()
  136. var req system.SysGroupDeleteReq
  137. err := json.Unmarshal(this.Ctx.Input.RequestBody, &req)
  138. if err != nil {
  139. br.Msg = "参数解析异常!"
  140. br.ErrMsg = "参数解析失败,Err:" + err.Error()
  141. return
  142. }
  143. if req.GroupId <= 0 {
  144. br.Msg = "参数错误"
  145. br.ErrMsg = "参数错误,GroupId 小于等于0 "
  146. return
  147. }
  148. err = system.DeleteSysGroup(req.GroupId)
  149. if err != nil {
  150. br.Msg = "删除失败"
  151. br.ErrMsg = "删除失败,Err:" + err.Error()
  152. return
  153. }
  154. err = system.ClearSysUserGroup(req.GroupId)
  155. if err != nil {
  156. br.Msg = "删除失败"
  157. br.ErrMsg = "删除失败,情况分组失败,Err:" + err.Error()
  158. return
  159. }
  160. // 同步分组缓存
  161. var syncData system.SyncGroupData
  162. syncData.Source = utils.SOURCE_ETA_FLAG
  163. syncData.GroupId = req.GroupId
  164. _ = utils.Rc.LPush(utils.CACHE_SYNC_GROUP, syncData)
  165. br.Ret = 200
  166. br.Success = true
  167. br.Msg = "删除成功"
  168. }
  169. // SetSort
  170. // @Title 分组排序
  171. // @Description 分组排序
  172. // @Param request body system.SysGroupEditReq true "type json string"
  173. // @Success 200 修改成功
  174. // @router /group/set_sort [post]
  175. func (this *SysGroupController) SetSort() {
  176. br := new(models.BaseResponse).Init()
  177. defer func() {
  178. if br.ErrMsg == "" {
  179. br.IsSendEmail = false
  180. }
  181. this.Data["json"] = br
  182. this.ServeJSON()
  183. }()
  184. var req system.SysGroupSortReq
  185. err := json.Unmarshal(this.Ctx.Input.RequestBody, &req)
  186. if err != nil {
  187. br.Msg = "参数解析异常!"
  188. br.ErrMsg = "参数解析失败,Err:" + err.Error()
  189. return
  190. }
  191. // 部门排序
  192. if len(req.DepartmentIds) > 0 {
  193. updateArr := make([]*system.DepartmentSort, 0)
  194. for k, d := range req.DepartmentIds {
  195. updateArr = append(updateArr, &system.DepartmentSort{
  196. DepartmentId: d,
  197. Sort: k + 1,
  198. })
  199. }
  200. if e := system.MultiUpdateDepartmentSort(updateArr); e != nil {
  201. br.Msg = "操作失败"
  202. br.ErrMsg = "批量更新部门排序失败, Err: " + e.Error()
  203. return
  204. }
  205. }
  206. // 分组排序/小组排序
  207. if len(req.GroupIds) > 0 || len(req.TeamIds) > 0 {
  208. updateArr := make([]*system.GroupSort, 0)
  209. if len(req.GroupIds) > 0 {
  210. for k, g := range req.GroupIds {
  211. updateArr = append(updateArr, &system.GroupSort{
  212. GroupId: g,
  213. Sort: k + 1,
  214. })
  215. }
  216. }
  217. if len(req.TeamIds) > 0 {
  218. for k, t := range req.TeamIds {
  219. updateArr = append(updateArr, &system.GroupSort{
  220. GroupId: t,
  221. Sort: k + 1,
  222. })
  223. }
  224. }
  225. if len(updateArr) > 0 {
  226. if e := system.MultiUpdateGroupSort(updateArr); e != nil {
  227. br.Msg = "操作失败"
  228. br.ErrMsg = "批量更新分组排序失败, Err: " + e.Error()
  229. return
  230. }
  231. // 同步分组缓存
  232. for _, g := range updateArr {
  233. var syncData system.SyncGroupData
  234. syncData.Source = utils.SOURCE_ETA_FLAG
  235. syncData.GroupId = g.GroupId
  236. _ = utils.Rc.LPush(utils.CACHE_SYNC_GROUP, syncData)
  237. }
  238. }
  239. }
  240. br.Ret = 200
  241. br.Success = true
  242. br.Msg = "操作成功"
  243. }