sys_department.go 10 KB

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