sys_team.go 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185
  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 SysTeamController struct {
  12. BaseAuthController
  13. }
  14. // @Title 新增分组
  15. // @Description 新增分组接口
  16. // @Param request body system.SysTeamAddReq true "type json string"
  17. // @Success 200 新增成功
  18. // @router /team/add [post]
  19. func (this *SysTeamController) Add() {
  20. br := new(models.BaseResponse).Init()
  21. defer func() {
  22. this.Data["json"] = br
  23. this.ServeJSON()
  24. }()
  25. var req system.SysTeamAddReq
  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.GroupId <= 0 {
  33. br.Msg = "请选择部门"
  34. br.ErrMsg = "大组ID不可为空"
  35. return
  36. }
  37. if req.TeamName == "" {
  38. br.Msg = "分组名称不能为空"
  39. return
  40. }
  41. teamNameArr := strings.Split(req.TeamName, ",")
  42. for _, v := range teamNameArr {
  43. count, err := system.GetSysTeamCount(req.GroupId, 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.ParentId = req.GroupId
  54. item.CreateTime = time.Now()
  55. groupId, e := system.AddSysGroup(item)
  56. if e != nil {
  57. br.Msg = "新增失败"
  58. br.ErrMsg = "新增失败,Err:" + e.Error()
  59. return
  60. }
  61. // 同步分组缓存
  62. if utils.BusinessCode == utils.BusinessCodeRelease {
  63. var syncData system.SyncGroupData
  64. syncData.Source = utils.SOURCE_ETA_FLAG
  65. syncData.GroupId = int(groupId)
  66. _ = utils.Rc.LPush(utils.CACHE_SYNC_GROUP, syncData)
  67. }
  68. }
  69. }
  70. br.Ret = 200
  71. br.Success = true
  72. br.Msg = "新增成功"
  73. }
  74. // @Title 修改分组
  75. // @Description 修改分组接口
  76. // @Param request body system.SysTeamEditReq true "type json string"
  77. // @Success 200 修改成功
  78. // @router /team/edit [post]
  79. func (this *SysTeamController) Edit() {
  80. br := new(models.BaseResponse).Init()
  81. defer func() {
  82. this.Data["json"] = br
  83. this.ServeJSON()
  84. }()
  85. var req system.SysTeamEditReq
  86. err := json.Unmarshal(this.Ctx.Input.RequestBody, &req)
  87. if err != nil {
  88. br.Msg = "参数解析异常!"
  89. br.ErrMsg = "参数解析失败,Err:" + err.Error()
  90. return
  91. }
  92. if req.TeamId <= 0 {
  93. br.Msg = "参数错误"
  94. br.ErrMsg = "参数错误,GroupId 小于等于0 "
  95. return
  96. }
  97. if req.TeamName == "" {
  98. br.Msg = "分组名称不能为空"
  99. return
  100. }
  101. item, err := system.GetSysTeamByName(req.TeamName)
  102. if err != nil && err.Error() != utils.ErrNoRow() {
  103. br.Msg = "获取数据失败"
  104. br.ErrMsg = "获取数据失败,Err:" + err.Error()
  105. return
  106. }
  107. if item != nil {
  108. if item.GroupId != req.TeamId {
  109. br.Msg = "名称已存在,请重新输入"
  110. return
  111. }
  112. }
  113. err = system.ModifySysTeam(req.TeamName, req.TeamId)
  114. if err != nil {
  115. br.Msg = "修改失败"
  116. br.ErrMsg = "修改失败,Err:" + err.Error()
  117. return
  118. }
  119. // 同步分组缓存
  120. if utils.BusinessCode == utils.BusinessCodeRelease {
  121. var syncData system.SyncGroupData
  122. syncData.Source = utils.SOURCE_ETA_FLAG
  123. syncData.GroupId = item.GroupId
  124. _ = utils.Rc.LPush(utils.CACHE_SYNC_GROUP, syncData)
  125. }
  126. br.Ret = 200
  127. br.Success = true
  128. br.Msg = "修改成功"
  129. }
  130. // @Title 删除分组
  131. // @Description 删除分组接口
  132. // @Param request body system.SysTeamDeleteReq true "type json string"
  133. // @Success 200 删除成功
  134. // @router /team/delete [post]
  135. func (this *SysTeamController) Delete() {
  136. br := new(models.BaseResponse).Init()
  137. defer func() {
  138. this.Data["json"] = br
  139. this.ServeJSON()
  140. }()
  141. var req system.SysTeamDeleteReq
  142. err := json.Unmarshal(this.Ctx.Input.RequestBody, &req)
  143. if err != nil {
  144. br.Msg = "参数解析异常!"
  145. br.ErrMsg = "参数解析失败,Err:" + err.Error()
  146. return
  147. }
  148. if req.TeamId <= 0 {
  149. br.Msg = "参数错误"
  150. br.ErrMsg = "参数错误,GroupId 小于等于0 "
  151. return
  152. }
  153. err = system.DeleteSysGroup(req.TeamId)
  154. if err != nil {
  155. br.Msg = "删除失败"
  156. br.ErrMsg = "删除失败,Err:" + err.Error()
  157. return
  158. }
  159. err = system.ClearSysUserTeam(req.TeamId)
  160. if err != nil {
  161. br.Msg = "删除失败"
  162. br.ErrMsg = "删除失败,情况分组失败,Err:" + err.Error()
  163. return
  164. }
  165. // 同步分组缓存
  166. if utils.BusinessCode == utils.BusinessCodeRelease {
  167. var syncData system.SyncGroupData
  168. syncData.Source = utils.SOURCE_ETA_FLAG
  169. syncData.GroupId = req.TeamId
  170. _ = utils.Rc.LPush(utils.CACHE_SYNC_GROUP, syncData)
  171. }
  172. br.Ret = 200
  173. br.Success = true
  174. br.Msg = "删除成功"
  175. }