sys_group.go 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213
  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. type SysGroupController struct {
  11. BaseAuthController
  12. }
  13. // @router /group/add [post]
  14. func (this *SysGroupController) Add() {
  15. br := new(models.BaseResponse).Init()
  16. defer func() {
  17. this.Data["json"] = br
  18. this.ServeJSON()
  19. }()
  20. var req system.SysGroupAddReq
  21. err := json.Unmarshal(this.Ctx.Input.RequestBody, &req)
  22. if err != nil {
  23. br.Msg = "参数解析异常!"
  24. br.ErrMsg = "参数解析失败,Err:" + err.Error()
  25. return
  26. }
  27. if req.DepartmentId <= 0 {
  28. br.Msg = "请选择部门"
  29. br.ErrMsg = "部门ID不可为空"
  30. return
  31. }
  32. if req.GroupName == "" {
  33. br.Msg = "分组名称不能为空"
  34. return
  35. }
  36. groupNameArr := strings.Split(req.GroupName, ",")
  37. for _, v := range groupNameArr {
  38. count, err := system.GetSysGroupCount(req.DepartmentId, v)
  39. if err != nil {
  40. br.Msg = "获取数据失败"
  41. br.ErrMsg = "获取数据失败,Err:" + err.Error()
  42. return
  43. }
  44. if count <= 0 {
  45. item := new(system.SysGroup)
  46. item.GroupName = v
  47. item.DepartmentId = req.DepartmentId
  48. item.CreateTime = time.Now()
  49. _, e := system.AddSysGroup(item)
  50. if e != nil {
  51. br.Msg = "新增失败"
  52. br.ErrMsg = "新增失败,Err:" + e.Error()
  53. return
  54. }
  55. }
  56. }
  57. br.Ret = 200
  58. br.Success = true
  59. br.Msg = "新增成功"
  60. }
  61. // @router /group/edit [post]
  62. func (this *SysGroupController) Edit() {
  63. br := new(models.BaseResponse).Init()
  64. defer func() {
  65. this.Data["json"] = br
  66. this.ServeJSON()
  67. }()
  68. var req system.SysGroupEditReq
  69. err := json.Unmarshal(this.Ctx.Input.RequestBody, &req)
  70. if err != nil {
  71. br.Msg = "参数解析异常!"
  72. br.ErrMsg = "参数解析失败,Err:" + err.Error()
  73. return
  74. }
  75. if req.GroupId <= 0 {
  76. br.Msg = "参数错误"
  77. br.ErrMsg = "参数错误,GroupId 小于等于0 "
  78. return
  79. }
  80. if req.GroupName == "" {
  81. br.Msg = "分组名称不能为空"
  82. return
  83. }
  84. item, err := system.GetSysGroupByName(req.GroupName)
  85. if err != nil && !utils.IsErrNoRow(err) {
  86. br.Msg = "获取数据失败"
  87. br.ErrMsg = "获取数据失败,Err:" + err.Error()
  88. return
  89. }
  90. if item != nil && item.GroupId > 0 {
  91. if item.GroupId != req.GroupId {
  92. br.Msg = "名称已存在,请重新输入"
  93. return
  94. }
  95. }
  96. err = system.ModifySysGroup(req.GroupName, req.GroupId)
  97. if err != nil {
  98. br.Msg = "修改失败"
  99. br.ErrMsg = "修改失败,Err:" + err.Error()
  100. return
  101. }
  102. br.Ret = 200
  103. br.Success = true
  104. br.Msg = "修改成功"
  105. }
  106. // @router /group/delete [post]
  107. func (this *SysGroupController) Delete() {
  108. br := new(models.BaseResponse).Init()
  109. defer func() {
  110. this.Data["json"] = br
  111. this.ServeJSON()
  112. }()
  113. var req system.SysGroupDeleteReq
  114. err := json.Unmarshal(this.Ctx.Input.RequestBody, &req)
  115. if err != nil {
  116. br.Msg = "参数解析异常!"
  117. br.ErrMsg = "参数解析失败,Err:" + err.Error()
  118. return
  119. }
  120. if req.GroupId <= 0 {
  121. br.Msg = "参数错误"
  122. br.ErrMsg = "参数错误,GroupId 小于等于0 "
  123. return
  124. }
  125. err = system.DeleteSysGroup(req.GroupId)
  126. if err != nil {
  127. br.Msg = "删除失败"
  128. br.ErrMsg = "删除失败,Err:" + err.Error()
  129. return
  130. }
  131. err = system.ClearSysUserGroup(req.GroupId)
  132. if err != nil {
  133. br.Msg = "删除失败"
  134. br.ErrMsg = "删除失败,情况分组失败,Err:" + err.Error()
  135. return
  136. }
  137. br.Ret = 200
  138. br.Success = true
  139. br.Msg = "删除成功"
  140. }
  141. // @router /group/set_sort [post]
  142. func (this *SysGroupController) SetSort() {
  143. br := new(models.BaseResponse).Init()
  144. defer func() {
  145. if br.ErrMsg == "" {
  146. br.IsSendEmail = false
  147. }
  148. this.Data["json"] = br
  149. this.ServeJSON()
  150. }()
  151. var req system.SysGroupSortReq
  152. err := json.Unmarshal(this.Ctx.Input.RequestBody, &req)
  153. if err != nil {
  154. br.Msg = "参数解析异常!"
  155. br.ErrMsg = "参数解析失败,Err:" + err.Error()
  156. return
  157. }
  158. if len(req.DepartmentIds) > 0 {
  159. updateArr := make([]*system.DepartmentSort, 0)
  160. for k, d := range req.DepartmentIds {
  161. updateArr = append(updateArr, &system.DepartmentSort{
  162. DepartmentId: d,
  163. Sort: k + 1,
  164. })
  165. }
  166. if e := system.MultiUpdateDepartmentSort(updateArr); e != nil {
  167. br.Msg = "操作失败"
  168. br.ErrMsg = "批量更新部门排序失败, Err: " + e.Error()
  169. return
  170. }
  171. }
  172. if len(req.GroupIds) > 0 || len(req.TeamIds) > 0 {
  173. updateArr := make([]*system.GroupSort, 0)
  174. if len(req.GroupIds) > 0 {
  175. for k, g := range req.GroupIds {
  176. updateArr = append(updateArr, &system.GroupSort{
  177. GroupId: g,
  178. Sort: k + 1,
  179. })
  180. }
  181. }
  182. if len(req.TeamIds) > 0 {
  183. for k, t := range req.TeamIds {
  184. updateArr = append(updateArr, &system.GroupSort{
  185. GroupId: t,
  186. Sort: k + 1,
  187. })
  188. }
  189. }
  190. if len(updateArr) > 0 {
  191. if e := system.MultiUpdateGroupSort(updateArr); e != nil {
  192. br.Msg = "操作失败"
  193. br.ErrMsg = "批量更新分组排序失败, Err: " + e.Error()
  194. return
  195. }
  196. }
  197. }
  198. br.Ret = 200
  199. br.Success = true
  200. br.Msg = "操作成功"
  201. }