sys_group.go 6.8 KB

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