sys_group.go 5.5 KB

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