sys_group.go 6.6 KB

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