sys_department.go 6.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246
  1. package controllers
  2. import (
  3. "encoding/json"
  4. "eta/eta_mini_crm/models"
  5. "eta/eta_mini_crm/models/request"
  6. "eta/eta_mini_crm/models/response"
  7. "eta/eta_mini_crm/services"
  8. "github.com/beego/beego/v2/client/orm"
  9. )
  10. type SysDepartmentController struct {
  11. BaseAuthController
  12. }
  13. // @Title 获取部门列表
  14. // @Description 获取部门列表接口
  15. // @Success 200 {object} system.SysDepartmentListResp
  16. // @router /list [get]
  17. func (this *SysDepartmentController) List() {
  18. br := new(models.BaseResponse).Init()
  19. defer func() {
  20. this.Data["json"] = br
  21. this.ServeJSON()
  22. }()
  23. list, err := services.GetSysDepartmentList()
  24. if err != nil {
  25. br.Msg = "获取部门列表失败"
  26. br.ErrMsg = "获取部门列表失败,Err" + err.Error()
  27. return
  28. }
  29. resp := new(response.SysDepartmentListResp)
  30. resp.List = list
  31. br.Ret = 200
  32. br.Success = true
  33. br.Msg = "获取成功"
  34. br.Data = resp
  35. }
  36. // @Title 新增部门
  37. // @Description 新增部门接口
  38. // @Param request body system.SysDepartmentAddReq true "type json string"
  39. // @Success 200 新增成功
  40. // @router /add [post]
  41. func (this *SysDepartmentController) Add() {
  42. br := new(models.BaseResponse).Init()
  43. defer func() {
  44. this.Data["json"] = br
  45. this.ServeJSON()
  46. }()
  47. var req request.SysDepartmentAddReq
  48. err := json.Unmarshal(this.Ctx.Input.RequestBody, &req)
  49. if err != nil {
  50. br.Msg = "参数解析异常!"
  51. br.ErrMsg = "参数解析失败,Err:" + err.Error()
  52. return
  53. }
  54. if len(req.SysDepartmentNames) <= 0 {
  55. br.Msg = "部门名称不能为空"
  56. return
  57. }
  58. for _, dep := range req.SysDepartmentNames {
  59. count, err := models.GetSysDepartmentCountByParentId(req.SysDepartmentId, dep)
  60. if err != nil {
  61. br.Msg = "获取数据失败"
  62. br.ErrMsg = "获取数据失败,Err:" + err.Error()
  63. return
  64. }
  65. if count <= 0 {
  66. sysDepartment := &models.SysDepartment{}
  67. sysDepartment.SysDepartmentName = dep
  68. sysDepartment.Level = req.Level
  69. sysDepartment.ParentId = req.SysDepartmentId
  70. err = sysDepartment.Add()
  71. if err != nil {
  72. br.Msg = "新增失败"
  73. br.ErrMsg = "新增失败,Err:" + err.Error()
  74. return
  75. }
  76. }
  77. }
  78. br.Ret = 200
  79. br.Success = true
  80. br.Msg = "新增成功"
  81. }
  82. // @Title 编辑部门
  83. // @Description 编辑部门接口
  84. // @Param request body request.SysDepartmentEditReq true "type json string"
  85. // @Success 200 编辑成功
  86. // @router /edit [post]
  87. func (this *SysDepartmentController) Edit() {
  88. br := new(models.BaseResponse).Init()
  89. defer func() {
  90. this.Data["json"] = br
  91. this.ServeJSON()
  92. }()
  93. var req request.SysDepartmentEditReq
  94. err := json.Unmarshal(this.Ctx.Input.RequestBody, &req)
  95. if err != nil {
  96. br.Msg = "参数解析异常!"
  97. br.ErrMsg = "参数解析失败,Err:" + err.Error()
  98. return
  99. }
  100. if req.SysDepartmentId <= 0 {
  101. br.Msg = "参数错误"
  102. br.ErrMsg = "参数错误,SysDepartmentId 小于等于0 "
  103. return
  104. }
  105. if req.SysDepartmentName == "" {
  106. br.Msg = "名称不能为空"
  107. return
  108. }
  109. count, err := models.GetSysDepartmentCountById(req.SysDepartmentId)
  110. if err != nil && err != orm.ErrNoRows {
  111. br.Msg = "获取数据失败"
  112. br.ErrMsg = "获取数据失败,Err:" + err.Error()
  113. return
  114. }
  115. if count <= 0 {
  116. br.Msg = "分组不存在,请刷新重试"
  117. return
  118. }
  119. count, err = models.GetSysDepartmentCountByName(req.SysDepartmentName)
  120. if err != nil && err != orm.ErrNoRows {
  121. br.Msg = "获取数据失败"
  122. br.ErrMsg = "获取数据失败,Err:" + err.Error()
  123. return
  124. }
  125. if count > 0 {
  126. br.Msg = "名称已存在,请重新输入"
  127. return
  128. }
  129. sysDepartment := &models.SysDepartment{}
  130. sysDepartment.SysDepartmentId = req.SysDepartmentId
  131. sysDepartment.SysDepartmentName = req.SysDepartmentName
  132. err = sysDepartment.Update([]string{"sys_department_name"})
  133. if err != nil {
  134. br.Msg = "编辑失败,系统错误"
  135. br.Msg = "编辑失败,系统错误,Err:" + err.Error()
  136. return
  137. }
  138. br.Ret = 200
  139. br.Success = true
  140. br.Msg = "编辑成功"
  141. }
  142. // @Title 删除分组
  143. // @Description 删除分组接口
  144. // @Param request body request.SysDepartmentDeleteReq true "type json string"
  145. // @Success 200 删除成功
  146. // @router /delete [post]
  147. func (this *SysDepartmentController) Delete() {
  148. br := new(models.BaseResponse).Init()
  149. defer func() {
  150. this.Data["json"] = br
  151. this.ServeJSON()
  152. }()
  153. var req request.SysDepartmentDeleteReq
  154. err := json.Unmarshal(this.Ctx.Input.RequestBody, &req)
  155. if err != nil {
  156. br.Msg = "参数解析异常!"
  157. br.ErrMsg = "参数解析失败,Err:" + err.Error()
  158. return
  159. }
  160. if req.SysDepartmentId <= 0 {
  161. br.Msg = "参数错误"
  162. br.ErrMsg = "参数错误,SysDepartmentId 小于等于0 "
  163. return
  164. }
  165. if req.Level <= 0 {
  166. br.Msg = "参数错误"
  167. br.ErrMsg = "参数错误,Level 小于等于0 "
  168. return
  169. }
  170. err = services.DeleteSysDepartmentById(req.SysDepartmentId, req.Level)
  171. if err != nil {
  172. br.Msg = "删除失败"
  173. br.ErrMsg = "删除失败,系统异常,Err:" + err.Error()
  174. return
  175. }
  176. br.Ret = 200
  177. br.Msg = "删除成功"
  178. br.Success = true
  179. }
  180. // SetSort
  181. // @Title 分组排序
  182. // @Description 分组排序
  183. // @Param request body request.SysDepartmentSortReq true "type json string"
  184. // @Success 200 修改成功
  185. // @router /set_sort [post]
  186. func (this *SysDepartmentController) SetSort() {
  187. br := new(models.BaseResponse).Init()
  188. defer func() {
  189. if br.ErrMsg == "" {
  190. br.IsSendEmail = false
  191. }
  192. this.Data["json"] = br
  193. this.ServeJSON()
  194. }()
  195. var req request.SysDepartmentSortReq
  196. err := json.Unmarshal(this.Ctx.Input.RequestBody, &req)
  197. if err != nil {
  198. br.Msg = "参数解析异常!"
  199. br.ErrMsg = "参数解析失败,Err:" + err.Error()
  200. return
  201. }
  202. // 一级排序
  203. if len(req.DepartmentIds1) > 0 {
  204. err = models.UpdateDepartmentSortByIds(req.DepartmentIds1)
  205. if err != nil {
  206. br.Msg = "操作失败"
  207. br.ErrMsg = "批量更新部门排序失败, Err: " + err.Error()
  208. return
  209. }
  210. }
  211. // 二级级排序
  212. if len(req.DepartmentIds2) > 0 {
  213. err = models.UpdateDepartmentSortByIds(req.DepartmentIds2)
  214. if err != nil {
  215. br.Msg = "操作失败"
  216. br.ErrMsg = "批量更新部门排序失败, Err: " + err.Error()
  217. return
  218. }
  219. }
  220. // 三级级排序
  221. if len(req.DepartmentIds3) > 0 {
  222. err = models.UpdateDepartmentSortByIds(req.DepartmentIds3)
  223. if err != nil {
  224. br.Msg = "操作失败"
  225. br.ErrMsg = "批量更新部门排序失败, Err: " + err.Error()
  226. return
  227. }
  228. }
  229. br.Ret = 200
  230. br.Success = true
  231. br.Msg = "操作成功"
  232. }