sys_department.go 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430
  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. "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. _, err = system.AddSysDepartment(item)
  51. if err != nil {
  52. br.Msg = "新增失败"
  53. br.ErrMsg = "新增失败,Err:" + err.Error()
  54. return
  55. }
  56. br.Ret = 200
  57. br.Success = true
  58. br.Msg = "新增成功"
  59. }
  60. // @Title 修改部门
  61. // @Description 修改部门接口
  62. // @Param request body system.SysDepartmentEditReq true "type json string"
  63. // @Success 200 修改成功
  64. // @router /department/edit [post]
  65. func (this *SysDepartmentController) Edit() {
  66. br := new(models.BaseResponse).Init()
  67. defer func() {
  68. this.Data["json"] = br
  69. this.ServeJSON()
  70. }()
  71. var req system.SysDepartmentEditReq
  72. err := json.Unmarshal(this.Ctx.Input.RequestBody, &req)
  73. if err != nil {
  74. br.Msg = "参数解析异常!"
  75. br.ErrMsg = "参数解析失败,Err:" + err.Error()
  76. return
  77. }
  78. if req.DepartmentId <= 0 {
  79. br.Msg = "参数错误"
  80. br.ErrMsg = "参数错误,DepartmentId 小于等于0 "
  81. return
  82. }
  83. if req.DepartmentName == "" {
  84. br.Msg = "部门名称不能为空"
  85. return
  86. }
  87. item, err := system.GetSysDepartmentByName(req.DepartmentName)
  88. if err != nil && !utils.IsErrNoRow(err) {
  89. br.Msg = "获取数据失败"
  90. br.ErrMsg = "获取数据失败,Err:" + err.Error()
  91. return
  92. }
  93. if item != nil && item.DepartmentId > 0 {
  94. if item.DepartmentId != req.DepartmentId {
  95. br.Msg = "名称已存在,请重新输入"
  96. return
  97. }
  98. }
  99. err = system.ModifySysDepartment(req.DepartmentName, req.DepartmentId)
  100. if err != nil {
  101. br.Msg = "修改失败"
  102. br.ErrMsg = "修改失败,Err:" + err.Error()
  103. return
  104. }
  105. br.Ret = 200
  106. br.Success = true
  107. br.Msg = "修改成功"
  108. }
  109. // @Title 删除部门
  110. // @Description 删除部门接口
  111. // @Param request body system.SysDepartmentDeleteReq true "type json string"
  112. // @Success 200 删除成功
  113. // @router /department/delete [post]
  114. func (this *SysDepartmentController) Delete() {
  115. br := new(models.BaseResponse).Init()
  116. defer func() {
  117. this.Data["json"] = br
  118. this.ServeJSON()
  119. }()
  120. var req system.SysDepartmentDeleteReq
  121. err := json.Unmarshal(this.Ctx.Input.RequestBody, &req)
  122. if err != nil {
  123. br.Msg = "参数解析异常!"
  124. br.ErrMsg = "参数解析失败,Err:" + err.Error()
  125. return
  126. }
  127. if req.DepartmentId <= 0 {
  128. br.Msg = "参数错误"
  129. br.ErrMsg = "参数错误,DepartmentId 小于等于0 "
  130. return
  131. }
  132. err = system.DeleteSysDepartment(req.DepartmentId)
  133. if err != nil {
  134. br.Msg = "删除失败"
  135. br.ErrMsg = "删除失败,Err:" + err.Error()
  136. return
  137. }
  138. br.Ret = 200
  139. br.Success = true
  140. br.Msg = "删除成功"
  141. }
  142. // @Title 获取部门列表
  143. // @Description 获取部门列表接口
  144. // @Success 200 {object} system.SysDepartmentListResp
  145. // @router /department/list [get]
  146. func (this *SysDepartmentController) ListDepartment() {
  147. br := new(models.BaseResponse).Init()
  148. defer func() {
  149. this.Data["json"] = br
  150. this.ServeJSON()
  151. }()
  152. sysUser := this.SysUser
  153. if sysUser == nil {
  154. br.Msg = "请登录"
  155. br.ErrMsg = "请登录,SysUser Is Empty"
  156. return
  157. }
  158. //list, err := system.GetDepartmentList()
  159. //if err != nil {
  160. // br.Msg = "获取失败"
  161. // br.ErrMsg = "获取失败,Err:" + err.Error()
  162. // return
  163. //}
  164. //lenList := len(list)
  165. //for i := 0; i < lenList; i++ {
  166. // departmentId := list[i].DepartmentId
  167. // groupList, err := system.GetSysGroupByDepartmentId(departmentId)
  168. // if err != nil {
  169. // br.Msg = "获取数据失败"
  170. // br.ErrMsg = "获取数据失败,Err:" + err.Error()
  171. // return
  172. // }
  173. // list[i].Child = groupList
  174. // list[i].IsDepartment = true
  175. // for i2, sysGroup := range groupList {
  176. // teamList, err := system.GetSysTeamByDepartmentId(sysGroup.GroupId)
  177. // if err != nil {
  178. // br.Msg = "获取数据失败"
  179. // br.ErrMsg = "获取数据失败,Err:" + err.Error()
  180. // return
  181. // }
  182. // groupList[i2].Child = teamList
  183. // groupList[i2].IsGroup = true
  184. // }
  185. //}
  186. // 获取部门
  187. departments, e := system.GetDepartmentList()
  188. if e != nil {
  189. br.Msg = "获取失败"
  190. br.ErrMsg = fmt.Sprintf("获取部门失败, %v", e)
  191. return
  192. }
  193. // 获取分组和小组
  194. groups, e := system.GetSysGroupList()
  195. if e != nil {
  196. br.Msg = "获取失败"
  197. br.ErrMsg = fmt.Sprintf("获取分组失败, %v", e)
  198. return
  199. }
  200. departmentGroup := make(map[int][]*system.SysGroupList)
  201. groupTeam := make(map[int][]*system.SysTeamList)
  202. for _, v := range groups {
  203. if departmentGroup[v.DepartmentId] == nil {
  204. departmentGroup[v.DepartmentId] = make([]*system.SysGroupList, 0)
  205. }
  206. if v.ParentId == 0 {
  207. departmentGroup[v.DepartmentId] = append(departmentGroup[v.DepartmentId], v)
  208. }
  209. if v.ParentId > 0 {
  210. if groupTeam[v.ParentId] == nil {
  211. groupTeam[v.ParentId] = make([]*system.SysTeamList, 0)
  212. }
  213. groupTeam[v.ParentId] = append(groupTeam[v.ParentId], &system.SysTeamList{
  214. GroupId: v.GroupId,
  215. ParentId: v.ParentId,
  216. GroupName: v.GroupName,
  217. CreateTime: v.CreateTime,
  218. })
  219. }
  220. }
  221. // 数据重组
  222. for _, groups := range departmentGroup {
  223. for _, g := range groups {
  224. g.IsGroup = true
  225. if groupTeam[g.GroupId] != nil {
  226. g.Child = groupTeam[g.GroupId]
  227. } else {
  228. g.Child = make([]*system.SysTeamList, 0)
  229. }
  230. }
  231. }
  232. for _, v := range departments {
  233. v.IsDepartment = true
  234. if departmentGroup[v.DepartmentId] != nil {
  235. v.Child = departmentGroup[v.DepartmentId]
  236. } else {
  237. v.Child = make([]*system.SysGroupList, 0)
  238. }
  239. }
  240. //lenList := len(list)
  241. //for i := 0; i < lenList; i++ {
  242. // departmentId := list[i].DepartmentId
  243. // groupList, err := system.GetSysGroupByDepartmentId(departmentId)
  244. // if err != nil {
  245. // br.Msg = "获取数据失败"
  246. // br.ErrMsg = "获取数据失败,Err:" + err.Error()
  247. // return
  248. // }
  249. // list[i].Child = groupList
  250. // list[i].IsDepartment = true
  251. // for i2, sysGroup := range groupList {
  252. // teamList, err := system.GetSysTeamByDepartmentId(sysGroup.GroupId)
  253. // if err != nil {
  254. // br.Msg = "获取数据失败"
  255. // br.ErrMsg = "获取数据失败,Err:" + err.Error()
  256. // return
  257. // }
  258. // groupList[i2].Child = teamList
  259. // groupList[i2].IsGroup = true
  260. // }
  261. //}
  262. resp := new(system.SysDepartmentListResp)
  263. resp.List = departments
  264. br.Ret = 200
  265. br.Success = true
  266. br.Msg = "获取成功"
  267. br.Data = resp
  268. }
  269. // DepartmentUserTree
  270. // @Title 获取部门及分组用户树
  271. // @Description 获取部门列表接口
  272. // @Success 200 {object} system.DepartmentUserTree
  273. // @router /department/user_tree [get]
  274. func (this *SysDepartmentController) DepartmentUserTree() {
  275. br := new(models.BaseResponse).Init()
  276. defer func() {
  277. if br.ErrMsg == "" {
  278. br.IsSendEmail = false
  279. }
  280. this.Data["json"] = br
  281. this.ServeJSON()
  282. }()
  283. sysUser := this.SysUser
  284. if sysUser == nil {
  285. br.Msg = "请登录"
  286. br.ErrMsg = "请登录,SysUser Is Empty"
  287. return
  288. }
  289. keywords := this.GetString("Keywords", "")
  290. keywords = strings.TrimSpace(keywords)
  291. // 获取部门/分组/用户
  292. departments, e := system.GetDepartmentList()
  293. if e != nil {
  294. br.Msg = "获取失败"
  295. br.ErrMsg = "获取部门失败,Err:" + e.Error()
  296. return
  297. }
  298. groups, e := system.GetFullGroup()
  299. if e != nil {
  300. br.Msg = "获取失败"
  301. br.ErrMsg = "获取分组失败,Err:" + e.Error()
  302. return
  303. }
  304. cond := ` AND enabled = 1`
  305. pars := make([]interface{}, 0)
  306. if keywords != "" {
  307. kw := fmt.Sprint("%", keywords, "%")
  308. cond += ` AND real_name LIKE ?`
  309. pars = append(pars, kw)
  310. }
  311. admins, e := system.GetSysAdminList(cond, pars, []string{}, "")
  312. if e != nil {
  313. br.Msg = "获取失败"
  314. br.ErrMsg = "获取用户失败,Err:" + e.Error()
  315. return
  316. }
  317. // 用户map
  318. departmentAdmins := make(map[int][]*system.DepartmentUserTree, 0)
  319. groupAdmins := make(map[int][]*system.DepartmentUserTree, 0)
  320. for _, v := range admins {
  321. t := new(system.DepartmentUserTree)
  322. t.NodeId = v.AdminId
  323. t.NodeType = 3
  324. t.NodeName = v.RealName
  325. t.Children = make([]*system.DepartmentUserTree, 0)
  326. if groupAdmins[v.GroupId] == nil {
  327. groupAdmins[v.GroupId] = make([]*system.DepartmentUserTree, 0)
  328. }
  329. groupAdmins[v.GroupId] = append(groupAdmins[v.GroupId], t)
  330. // 直属于部门
  331. if v.GroupId == 0 {
  332. if departmentAdmins[v.DepartmentId] == nil {
  333. departmentAdmins[v.DepartmentId] = make([]*system.DepartmentUserTree, 0)
  334. }
  335. departmentAdmins[v.DepartmentId] = append(departmentAdmins[v.DepartmentId], t)
  336. }
  337. }
  338. // 小组
  339. groupTeams := make(map[int][]*system.DepartmentUserTree, 0)
  340. for _, v := range groups {
  341. if v.ParentId == 0 {
  342. continue
  343. }
  344. // 关键词查询时不显示空组
  345. if keywords != "" && groupAdmins[v.GroupId] == nil {
  346. continue
  347. }
  348. t := new(system.DepartmentUserTree)
  349. t.NodeId = v.GroupId
  350. t.NodeName = v.GroupName
  351. t.NodeType = 2
  352. t.Children = make([]*system.DepartmentUserTree, 0)
  353. t.Children = groupAdmins[v.GroupId]
  354. if groupTeams[v.ParentId] == nil {
  355. groupTeams[v.ParentId] = make([]*system.DepartmentUserTree, 0)
  356. }
  357. groupTeams[v.ParentId] = append(groupTeams[v.ParentId], t)
  358. }
  359. // 大组
  360. departmentGroups := make(map[int][]*system.DepartmentUserTree, 0)
  361. for _, v := range groups {
  362. if v.ParentId > 0 {
  363. continue
  364. }
  365. // 关键词查询时不显示空组
  366. if keywords != "" && groupAdmins[v.GroupId] == nil && groupTeams[v.GroupId] == nil {
  367. continue
  368. }
  369. t := new(system.DepartmentUserTree)
  370. t.NodeId = v.GroupId
  371. t.NodeName = v.GroupName
  372. t.NodeType = 2
  373. t.Children = make([]*system.DepartmentUserTree, 0)
  374. if groupTeams[v.GroupId] != nil {
  375. t.Children = append(t.Children, groupTeams[v.GroupId]...)
  376. }
  377. if groupAdmins[v.GroupId] != nil {
  378. t.Children = append(t.Children, groupAdmins[v.GroupId]...)
  379. }
  380. if departmentGroups[v.DepartmentId] == nil {
  381. departmentGroups[v.DepartmentId] = make([]*system.DepartmentUserTree, 0)
  382. }
  383. departmentGroups[v.DepartmentId] = append(departmentGroups[v.DepartmentId], t)
  384. }
  385. // 部门
  386. list := make([]*system.DepartmentUserTree, 0)
  387. for _, v := range departments {
  388. // 关键词查询时不显示空部门
  389. if keywords != "" && departmentGroups[v.DepartmentId] == nil && departmentAdmins[v.DepartmentId] == nil {
  390. continue
  391. }
  392. t := new(system.DepartmentUserTree)
  393. t.NodeId = v.DepartmentId
  394. t.NodeType = 1
  395. t.NodeName = v.DepartmentName
  396. t.Children = make([]*system.DepartmentUserTree, 0)
  397. if departmentGroups[v.DepartmentId] != nil {
  398. t.Children = append(t.Children, departmentGroups[v.DepartmentId]...)
  399. }
  400. if departmentAdmins[v.DepartmentId] != nil {
  401. t.Children = append(t.Children, departmentAdmins[v.DepartmentId]...)
  402. }
  403. list = append(list, t)
  404. }
  405. br.Ret = 200
  406. br.Success = true
  407. br.Msg = "获取成功"
  408. br.Data = list
  409. }