sys_department.go 9.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374
  1. package controllers
  2. import (
  3. "encoding/json"
  4. "eta/eta_api/models"
  5. "eta/eta_api/models/system"
  6. "eta/eta_api/utils"
  7. "fmt"
  8. "strings"
  9. "time"
  10. )
  11. // 系统设置
  12. type SysDepartmentController struct {
  13. BaseAuthController
  14. }
  15. // @Title 新增部门
  16. // @Description 新增部门接口
  17. // @Param request body system.SysDepartmentAddReq true "type json string"
  18. // @Success 200 新增成功
  19. // @router /department/add [post]
  20. func (this *SysDepartmentController) Add() {
  21. br := new(models.BaseResponse).Init()
  22. defer func() {
  23. this.Data["json"] = br
  24. this.ServeJSON()
  25. }()
  26. var req system.SysDepartmentAddReq
  27. err := json.Unmarshal(this.Ctx.Input.RequestBody, &req)
  28. if err != nil {
  29. br.Msg = "参数解析异常!"
  30. br.ErrMsg = "参数解析失败,Err:" + err.Error()
  31. return
  32. }
  33. if req.DepartmentName == "" {
  34. br.Msg = "部门名称不能为空"
  35. return
  36. }
  37. count, err := system.GetSysDepartmentCount(req.DepartmentName)
  38. if err != nil {
  39. br.Msg = "获取数据失败"
  40. br.ErrMsg = "获取数据失败"
  41. return
  42. }
  43. if count > 0 {
  44. br.Msg = "部门名称已存在,请重新输入"
  45. return
  46. }
  47. item := new(system.SysDepartment)
  48. item.DepartmentName = req.DepartmentName
  49. item.CreateTime = time.Now()
  50. departmentId, err := system.AddSysDepartment(item)
  51. if err != nil {
  52. br.Msg = "新增失败"
  53. br.ErrMsg = "新增失败,Err:" + err.Error()
  54. return
  55. }
  56. // 同步部门缓存
  57. if utils.BusinessCode == utils.BusinessCodeRelease {
  58. var syncData system.SyncDepartmentData
  59. syncData.Source = utils.SOURCE_ETA_FLAG
  60. syncData.DepartmentId = int(departmentId)
  61. _ = utils.Rc.LPush(utils.CACHE_SYNC_DEPARTMENT, syncData)
  62. }
  63. br.Ret = 200
  64. br.Success = true
  65. br.Msg = "新增成功"
  66. }
  67. // @Title 修改部门
  68. // @Description 修改部门接口
  69. // @Param request body system.SysDepartmentEditReq true "type json string"
  70. // @Success 200 修改成功
  71. // @router /department/edit [post]
  72. func (this *SysDepartmentController) Edit() {
  73. br := new(models.BaseResponse).Init()
  74. defer func() {
  75. this.Data["json"] = br
  76. this.ServeJSON()
  77. }()
  78. var req system.SysDepartmentEditReq
  79. err := json.Unmarshal(this.Ctx.Input.RequestBody, &req)
  80. if err != nil {
  81. br.Msg = "参数解析异常!"
  82. br.ErrMsg = "参数解析失败,Err:" + err.Error()
  83. return
  84. }
  85. if req.DepartmentId <= 0 {
  86. br.Msg = "参数错误"
  87. br.ErrMsg = "参数错误,DepartmentId 小于等于0 "
  88. return
  89. }
  90. if req.DepartmentName == "" {
  91. br.Msg = "部门名称不能为空"
  92. return
  93. }
  94. item, err := system.GetSysDepartmentByName(req.DepartmentName)
  95. if err != nil && err.Error() != utils.ErrNoRow() {
  96. br.Msg = "获取数据失败"
  97. br.ErrMsg = "获取数据失败,Err:" + err.Error()
  98. return
  99. }
  100. if item != nil {
  101. if item.DepartmentId != req.DepartmentId {
  102. br.Msg = "名称已存在,请重新输入"
  103. return
  104. }
  105. }
  106. err = system.ModifySysDepartment(req.DepartmentName, req.DepartmentId)
  107. if err != nil {
  108. br.Msg = "修改失败"
  109. br.ErrMsg = "修改失败,Err:" + err.Error()
  110. return
  111. }
  112. // 同步部门缓存
  113. if utils.BusinessCode == utils.BusinessCodeRelease {
  114. var syncData system.SyncDepartmentData
  115. syncData.Source = utils.SOURCE_ETA_FLAG
  116. syncData.DepartmentId = req.DepartmentId
  117. _ = utils.Rc.LPush(utils.CACHE_SYNC_DEPARTMENT, syncData)
  118. }
  119. br.Ret = 200
  120. br.Success = true
  121. br.Msg = "修改成功"
  122. }
  123. // @Title 删除部门
  124. // @Description 删除部门接口
  125. // @Param request body system.SysDepartmentDeleteReq true "type json string"
  126. // @Success 200 删除成功
  127. // @router /department/delete [post]
  128. func (this *SysDepartmentController) Delete() {
  129. br := new(models.BaseResponse).Init()
  130. defer func() {
  131. this.Data["json"] = br
  132. this.ServeJSON()
  133. }()
  134. var req system.SysDepartmentDeleteReq
  135. err := json.Unmarshal(this.Ctx.Input.RequestBody, &req)
  136. if err != nil {
  137. br.Msg = "参数解析异常!"
  138. br.ErrMsg = "参数解析失败,Err:" + err.Error()
  139. return
  140. }
  141. if req.DepartmentId <= 0 {
  142. br.Msg = "参数错误"
  143. br.ErrMsg = "参数错误,DepartmentId 小于等于0 "
  144. return
  145. }
  146. err = system.DeleteSysDepartment(req.DepartmentId)
  147. if err != nil {
  148. br.Msg = "删除失败"
  149. br.ErrMsg = "删除失败,Err:" + err.Error()
  150. return
  151. }
  152. // 同步部门缓存
  153. if utils.BusinessCode == utils.BusinessCodeRelease {
  154. var syncData system.SyncDepartmentData
  155. syncData.Source = utils.SOURCE_ETA_FLAG
  156. syncData.DepartmentId = req.DepartmentId
  157. _ = utils.Rc.LPush(utils.CACHE_SYNC_DEPARTMENT, syncData)
  158. }
  159. br.Ret = 200
  160. br.Success = true
  161. br.Msg = "删除成功"
  162. }
  163. // @Title 获取部门列表
  164. // @Description 获取部门列表接口
  165. // @Success 200 {object} system.SysDepartmentListResp
  166. // @router /department/list [get]
  167. func (this *SysDepartmentController) ListDepartment() {
  168. br := new(models.BaseResponse).Init()
  169. defer func() {
  170. this.Data["json"] = br
  171. this.ServeJSON()
  172. }()
  173. sysUser := this.SysUser
  174. if sysUser == nil {
  175. br.Msg = "请登录"
  176. br.ErrMsg = "请登录,SysUser Is Empty"
  177. return
  178. }
  179. list, err := system.GetDepartmentList()
  180. if err != nil {
  181. br.Msg = "获取失败"
  182. br.ErrMsg = "获取失败,Err:" + err.Error()
  183. return
  184. }
  185. lenList := len(list)
  186. for i := 0; i < lenList; i++ {
  187. departmentId := list[i].DepartmentId
  188. groupList, err := system.GetSysGroupByDepartmentId(departmentId)
  189. if err != nil {
  190. br.Msg = "获取数据失败"
  191. br.ErrMsg = "获取数据失败,Err:" + err.Error()
  192. return
  193. }
  194. list[i].Child = groupList
  195. list[i].IsDepartment = true
  196. for i2, sysGroup := range groupList {
  197. teamList, err := system.GetSysTeamByDepartmentId(sysGroup.GroupId)
  198. if err != nil {
  199. br.Msg = "获取数据失败"
  200. br.ErrMsg = "获取数据失败,Err:" + err.Error()
  201. return
  202. }
  203. groupList[i2].Child = teamList
  204. groupList[i2].IsGroup = true
  205. }
  206. }
  207. resp := new(system.SysDepartmentListResp)
  208. resp.List = list
  209. br.Ret = 200
  210. br.Success = true
  211. br.Msg = "获取成功"
  212. br.Data = resp
  213. }
  214. // DepartmentUserTree
  215. // @Title 获取部门及分组用户树
  216. // @Description 获取部门列表接口
  217. // @Success 200 {object} system.DepartmentUserTree
  218. // @router /department/user_tree [get]
  219. func (this *SysDepartmentController) DepartmentUserTree() {
  220. br := new(models.BaseResponse).Init()
  221. defer func() {
  222. if br.ErrMsg == "" {
  223. br.IsSendEmail = false
  224. }
  225. this.Data["json"] = br
  226. this.ServeJSON()
  227. }()
  228. sysUser := this.SysUser
  229. if sysUser == nil {
  230. br.Msg = "请登录"
  231. br.ErrMsg = "请登录,SysUser Is Empty"
  232. return
  233. }
  234. keywords := this.GetString("Keywords", "")
  235. keywords = strings.TrimSpace(keywords)
  236. // 获取部门/分组/用户
  237. departments, e := system.GetDepartmentList()
  238. if e != nil {
  239. br.Msg = "获取失败"
  240. br.ErrMsg = "获取部门失败,Err:" + e.Error()
  241. return
  242. }
  243. groups, e := system.GetFullGroup()
  244. if e != nil {
  245. br.Msg = "获取失败"
  246. br.ErrMsg = "获取分组失败,Err:" + e.Error()
  247. return
  248. }
  249. cond := ` AND enabled = 1`
  250. pars := make([]interface{}, 0)
  251. if keywords != "" {
  252. kw := fmt.Sprint("%", keywords, "%")
  253. cond += ` AND real_name LIKE ?`
  254. pars = append(pars, kw)
  255. }
  256. admins, e := system.GetSysAdminList(cond, pars, []string{}, "")
  257. if e != nil {
  258. br.Msg = "获取失败"
  259. br.ErrMsg = "获取用户失败,Err:" + e.Error()
  260. return
  261. }
  262. // 用户map
  263. departmentAdmins := make(map[int][]*system.DepartmentUserTree, 0)
  264. groupAdmins := make(map[int][]*system.DepartmentUserTree, 0)
  265. for _, v := range admins {
  266. t := new(system.DepartmentUserTree)
  267. t.NodeId = v.AdminId
  268. t.NodeType = 3
  269. t.NodeName = v.RealName
  270. t.Children = make([]*system.DepartmentUserTree, 0)
  271. if groupAdmins[v.GroupId] == nil {
  272. groupAdmins[v.GroupId] = make([]*system.DepartmentUserTree, 0)
  273. }
  274. groupAdmins[v.GroupId] = append(groupAdmins[v.GroupId], t)
  275. // 直属于部门
  276. if v.GroupId == 0 {
  277. if departmentAdmins[v.DepartmentId] == nil {
  278. departmentAdmins[v.DepartmentId] = make([]*system.DepartmentUserTree, 0)
  279. }
  280. departmentAdmins[v.DepartmentId] = append(departmentAdmins[v.DepartmentId], t)
  281. }
  282. }
  283. // 小组
  284. groupTeams := make(map[int][]*system.DepartmentUserTree, 0)
  285. for _, v := range groups {
  286. if v.ParentId == 0 {
  287. continue
  288. }
  289. // 关键词查询时不显示空组
  290. if keywords != "" && groupAdmins[v.GroupId] == nil {
  291. continue
  292. }
  293. t := new(system.DepartmentUserTree)
  294. t.NodeId = v.GroupId
  295. t.NodeName = v.GroupName
  296. t.NodeType = 2
  297. t.Children = make([]*system.DepartmentUserTree, 0)
  298. t.Children = groupAdmins[v.GroupId]
  299. if groupTeams[v.ParentId] == nil {
  300. groupTeams[v.ParentId] = make([]*system.DepartmentUserTree, 0)
  301. }
  302. groupTeams[v.ParentId] = append(groupTeams[v.ParentId], t)
  303. }
  304. // 大组
  305. departmentGroups := make(map[int][]*system.DepartmentUserTree, 0)
  306. for _, v := range groups {
  307. if v.ParentId > 0 {
  308. continue
  309. }
  310. // 关键词查询时不显示空组
  311. if keywords != "" && groupAdmins[v.GroupId] == nil && groupTeams[v.GroupId] == nil {
  312. continue
  313. }
  314. t := new(system.DepartmentUserTree)
  315. t.NodeId = v.GroupId
  316. t.NodeName = v.GroupName
  317. t.NodeType = 2
  318. t.Children = make([]*system.DepartmentUserTree, 0)
  319. if groupTeams[v.GroupId] != nil {
  320. t.Children = append(t.Children, groupTeams[v.GroupId]...)
  321. }
  322. if groupAdmins[v.GroupId] != nil {
  323. t.Children = append(t.Children, groupAdmins[v.GroupId]...)
  324. }
  325. if departmentGroups[v.DepartmentId] == nil {
  326. departmentGroups[v.DepartmentId] = make([]*system.DepartmentUserTree, 0)
  327. }
  328. departmentGroups[v.DepartmentId] = append(departmentGroups[v.DepartmentId], t)
  329. }
  330. // 部门
  331. list := make([]*system.DepartmentUserTree, 0)
  332. for _, v := range departments {
  333. // 关键词查询时不显示空部门
  334. if keywords != "" && departmentGroups[v.DepartmentId] == nil && departmentAdmins[v.DepartmentId] == nil {
  335. continue
  336. }
  337. t := new(system.DepartmentUserTree)
  338. t.NodeId = v.DepartmentId
  339. t.NodeType = 1
  340. t.NodeName = v.DepartmentName
  341. t.Children = make([]*system.DepartmentUserTree, 0)
  342. if departmentGroups[v.DepartmentId] != nil {
  343. t.Children = append(t.Children, departmentGroups[v.DepartmentId]...)
  344. }
  345. if departmentAdmins[v.DepartmentId] != nil {
  346. t.Children = append(t.Children, departmentAdmins[v.DepartmentId]...)
  347. }
  348. list = append(list, t)
  349. }
  350. br.Ret = 200
  351. br.Success = true
  352. br.Msg = "获取成功"
  353. br.Data = list
  354. }