sys_department.go 6.6 KB

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