sys_team.go 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179
  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 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. 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. br.Ret = 200
  69. br.Success = true
  70. br.Msg = "新增成功"
  71. }
  72. // @Title 修改分组
  73. // @Description 修改分组接口
  74. // @Param request body system.SysTeamEditReq true "type json string"
  75. // @Success 200 修改成功
  76. // @router /team/edit [post]
  77. func (this *SysTeamController) Edit() {
  78. br := new(models.BaseResponse).Init()
  79. defer func() {
  80. this.Data["json"] = br
  81. this.ServeJSON()
  82. }()
  83. var req system.SysTeamEditReq
  84. err := json.Unmarshal(this.Ctx.Input.RequestBody, &req)
  85. if err != nil {
  86. br.Msg = "参数解析异常!"
  87. br.ErrMsg = "参数解析失败,Err:" + err.Error()
  88. return
  89. }
  90. if req.TeamId <= 0 {
  91. br.Msg = "参数错误"
  92. br.ErrMsg = "参数错误,GroupId 小于等于0 "
  93. return
  94. }
  95. if req.TeamName == "" {
  96. br.Msg = "分组名称不能为空"
  97. return
  98. }
  99. item, err := system.GetSysTeamByName(req.TeamName)
  100. if err != nil && err.Error() != utils.ErrNoRow() {
  101. br.Msg = "获取数据失败"
  102. br.ErrMsg = "获取数据失败,Err:" + err.Error()
  103. return
  104. }
  105. if item != nil {
  106. if item.GroupId != req.TeamId {
  107. br.Msg = "名称已存在,请重新输入"
  108. return
  109. }
  110. }
  111. err = system.ModifySysTeam(req.TeamName, req.TeamId)
  112. if err != nil {
  113. br.Msg = "修改失败"
  114. br.ErrMsg = "修改失败,Err:" + err.Error()
  115. return
  116. }
  117. // 同步分组缓存
  118. var syncData system.SyncGroupData
  119. syncData.Source = utils.SOURCE_ETA_FLAG
  120. syncData.GroupId = item.GroupId
  121. _ = utils.Rc.LPush(utils.CACHE_SYNC_GROUP, syncData)
  122. br.Ret = 200
  123. br.Success = true
  124. br.Msg = "修改成功"
  125. }
  126. // @Title 删除分组
  127. // @Description 删除分组接口
  128. // @Param request body system.SysTeamDeleteReq true "type json string"
  129. // @Success 200 删除成功
  130. // @router /team/delete [post]
  131. func (this *SysTeamController) Delete() {
  132. br := new(models.BaseResponse).Init()
  133. defer func() {
  134. this.Data["json"] = br
  135. this.ServeJSON()
  136. }()
  137. var req system.SysTeamDeleteReq
  138. err := json.Unmarshal(this.Ctx.Input.RequestBody, &req)
  139. if err != nil {
  140. br.Msg = "参数解析异常!"
  141. br.ErrMsg = "参数解析失败,Err:" + err.Error()
  142. return
  143. }
  144. if req.TeamId <= 0 {
  145. br.Msg = "参数错误"
  146. br.ErrMsg = "参数错误,GroupId 小于等于0 "
  147. return
  148. }
  149. err = system.DeleteSysGroup(req.TeamId)
  150. if err != nil {
  151. br.Msg = "删除失败"
  152. br.ErrMsg = "删除失败,Err:" + err.Error()
  153. return
  154. }
  155. err = system.ClearSysUserTeam(req.TeamId)
  156. if err != nil {
  157. br.Msg = "删除失败"
  158. br.ErrMsg = "删除失败,情况分组失败,Err:" + err.Error()
  159. return
  160. }
  161. // 同步分组缓存
  162. var syncData system.SyncGroupData
  163. syncData.Source = utils.SOURCE_ETA_FLAG
  164. syncData.GroupId = req.TeamId
  165. _ = utils.Rc.LPush(utils.CACHE_SYNC_GROUP, syncData)
  166. br.Ret = 200
  167. br.Success = true
  168. br.Msg = "删除成功"
  169. }