sys_role_admin.go 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466
  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/models/system/request"
  7. "eta_gn/eta_api/models/system/response"
  8. "eta_gn/eta_api/utils"
  9. "github.com/rdlucklib/rdluck_tools/paging"
  10. "strconv"
  11. "strings"
  12. "time"
  13. )
  14. type SysRoleAdminController struct {
  15. BaseAuthController
  16. }
  17. // List
  18. // @Title 获取特定角色下的管理员列表
  19. // @Description 获取特定角色下的管理员列表
  20. // @Param RoleTypeCode query string false "角色类型编码"
  21. // @Success 200 {object} response.RoleAdminItemResp
  22. // @router /role/admin/list [get]
  23. func (this *SysRoleAdminController) List() {
  24. br := new(models.BaseResponse).Init()
  25. defer func() {
  26. this.Data["json"] = br
  27. this.ServeJSON()
  28. }()
  29. sysUser := this.SysUser
  30. if sysUser == nil {
  31. br.Msg = "请登录"
  32. br.ErrMsg = "请登录,SysUser is Empty"
  33. br.Ret = 408
  34. return
  35. }
  36. pageSize, _ := this.GetInt("PageSize")
  37. currentIndex, _ := this.GetInt("CurrentIndex")
  38. var startSize int
  39. if pageSize <= 0 {
  40. pageSize = utils.PageSize20
  41. }
  42. if currentIndex <= 0 {
  43. currentIndex = 1
  44. }
  45. startSize = paging.StartIndex(currentIndex, pageSize)
  46. roleTypeCode := this.GetString("RoleTypeCode")
  47. if roleTypeCode == "" {
  48. br.Msg = "请输入查询的角色类型"
  49. br.ErrMsg = "请输入查询的角色类型"
  50. return
  51. }
  52. //根据搜索条件查询账号列表
  53. // 查询留言管理员的角色ID:yb_comment_admin
  54. roleInfo, err := system.GetSysRoleByRoleTypeCode(roleTypeCode)
  55. if err != nil {
  56. if utils.IsErrNoRow(err) {
  57. br.Msg = "该角色不存在!"
  58. br.ErrMsg = "该角色不存在,Err:" + err.Error()
  59. return
  60. } else {
  61. br.Msg = "查询角色信息失败!"
  62. br.ErrMsg = "查询角色信息失败,Err:" + err.Error()
  63. return
  64. }
  65. }
  66. var pars []interface{}
  67. condition := " and s.role_id = ? "
  68. pars = append(pars, roleInfo.RoleId)
  69. reqKeyword := this.GetString("Keyword")
  70. if reqKeyword != "" {
  71. condition += " AND (a.real_name LIKE ? OR a.mobile LIKE ?)"
  72. pars = utils.GetLikeKeywordPars(pars, reqKeyword, 2)
  73. }
  74. adminList, err := system.GetRoleAdminList(condition, pars, startSize, pageSize)
  75. if err != nil {
  76. br.Msg = "获取管理账号列表失败!"
  77. br.ErrMsg = "获取管理账号列表失败,Err:" + err.Error()
  78. return
  79. }
  80. //查询分组信息
  81. departmentGroupNameMap := make(map[int]string)
  82. departmentNameMap := make(map[int]string)
  83. groupList, err := system.GetFullGroup()
  84. if err != nil {
  85. br.Msg = "查询分组信息失败!"
  86. br.ErrMsg = "查询分组信息失败,Err:" + err.Error()
  87. return
  88. }
  89. departmentList, err := system.GetDepartmentList()
  90. if err != nil {
  91. br.Msg = "查询部门信息失败!"
  92. br.ErrMsg = "查询部门信息失败,Err:" + err.Error()
  93. return
  94. }
  95. for _, d := range departmentList {
  96. departmentNameMap[d.DepartmentId] = d.DepartmentName
  97. }
  98. for _, g := range groupList {
  99. if g.ParentGroupName != "" {
  100. departmentGroupNameMap[g.GroupId] = g.DepartmentName + "/" + g.ParentGroupName + "/" + g.GroupName
  101. } else {
  102. departmentGroupNameMap[g.GroupId] = g.DepartmentName + "/" + g.GroupName
  103. }
  104. }
  105. // 查询角色信息
  106. roles, err := system.GetSysRoleAll(false)
  107. if err != nil {
  108. br.Msg = "查询角色信息失败!"
  109. br.ErrMsg = "查询角色信息失败,Err:" + err.Error()
  110. return
  111. }
  112. rolesNameMap := make(map[int]string)
  113. for _, v := range roles {
  114. rolesNameMap[v.RoleId] = v.RoleName
  115. }
  116. var list []*response.RoleAdminItemResp
  117. for _, v := range adminList {
  118. item := new(response.RoleAdminItemResp)
  119. item.SysRoleAdminId = v.SysRoleAdminId
  120. item.AdminId = v.AdminId
  121. item.Mobile = v.Mobile
  122. item.RealName = v.RealName
  123. item.Enabled = v.Enabled
  124. if v.GroupId > 0 {
  125. if departmentGroup, ok := departmentGroupNameMap[v.GroupId]; ok {
  126. item.DepartmentGroup = departmentGroup
  127. }
  128. } else {
  129. if departmentName, ok := departmentNameMap[v.DepartmentId]; ok {
  130. item.DepartmentGroup = departmentName
  131. }
  132. }
  133. // 整理角色名称
  134. item.RoleId = v.RoleId
  135. if rolesName, ok := rolesNameMap[v.RoleId]; ok {
  136. item.RoleName = rolesName
  137. }
  138. // 整理标签
  139. if v.RoleTypeCode == utils.ROLE_TYPE_CODE_ADMIN {
  140. item.LabelVal = 1
  141. } else if v.RoleTypeCode == utils.ROLE_TYPE_CODE_FICC_ADMIN ||
  142. v.RoleTypeCode == utils.ROLE_TYPE_CODE_RAI_ADMIN {
  143. item.LabelVal = 2
  144. } else {
  145. if v.Authority == 1 {
  146. item.LabelVal = 3
  147. } else if v.Authority == 2 && v.RoleTypeCode == utils.ROLE_TYPE_CODE_RAI_GROUP {
  148. item.LabelVal = 4
  149. } else if v.Authority == 2 && v.RoleTypeCode == utils.ROLE_TYPE_CODE_FICC_GROUP {
  150. item.LabelVal = 5
  151. } else if v.Authority == 4 {
  152. item.LabelVal = 4
  153. }
  154. }
  155. list = append(list, item)
  156. }
  157. // 获取列表分页总数
  158. total, err := system.GetRoleAdminListTotal(condition, pars)
  159. if err != nil {
  160. br.Msg = "获取留言列表总数失败!"
  161. br.ErrMsg = "获取留言列表总数失败,Err:" + err.Error()
  162. return
  163. }
  164. page := paging.GetPaging(currentIndex, pageSize, int(total))
  165. resp := response.RoleAdminListResp{
  166. List: list,
  167. Paging: page,
  168. }
  169. br.Ret = 200
  170. br.Success = true
  171. br.Msg = "获取成功"
  172. br.Data = resp
  173. return
  174. }
  175. // AddAdmin
  176. // @Title 新增留言管理管理员
  177. // @Description 新增留言管理管理员
  178. // @Param request body request.AddRoleAdminReq true "type json string"
  179. // @Success Ret=200 操作成功
  180. // @router /role/admin/add [post]
  181. func (this *SysRoleAdminController) AddAdmin() {
  182. br := new(models.BaseResponse).Init()
  183. defer func() {
  184. this.Data["json"] = br
  185. this.ServeJSON()
  186. }()
  187. sysUser := this.SysUser
  188. if sysUser == nil {
  189. br.Msg = "请登录"
  190. br.ErrMsg = "请登录,SysUser Is Empty"
  191. br.Ret = 408
  192. return
  193. }
  194. var req request.AddRoleAdminReq
  195. err := json.Unmarshal(this.Ctx.Input.RequestBody, &req)
  196. if err != nil {
  197. br.Msg = "参数解析异常!"
  198. br.ErrMsg = "参数解析失败,Err:" + err.Error()
  199. return
  200. }
  201. // 校验参数
  202. sliceAdmin := strings.Split(req.AdminIds, ",")
  203. if len(sliceAdmin) > 0 {
  204. for _, v := range sliceAdmin {
  205. _, tErr := strconv.Atoi(v)
  206. if tErr != nil {
  207. br.Msg = "请先选择用户!"
  208. br.ErrMsg = "参数解析异常,Err:" + tErr.Error()
  209. return
  210. }
  211. }
  212. }
  213. if req.RoleTypeCode == "" {
  214. br.Msg = "请输入角色类型"
  215. br.ErrMsg = "请输入角色类型"
  216. return
  217. }
  218. // 查询留言管理员的角色ID
  219. roleInfo, err := system.GetSysRoleByRoleTypeCode(req.RoleTypeCode)
  220. if err != nil {
  221. if utils.IsErrNoRow(err) {
  222. br.Msg = "该角色不存在!"
  223. br.ErrMsg = "该角色不存在,Err:" + err.Error()
  224. return
  225. } else {
  226. br.Msg = "查询角色信息失败!"
  227. br.ErrMsg = "查询角色信息失败,Err:" + err.Error()
  228. return
  229. }
  230. }
  231. // 查询已绑定的管理员账号
  232. var unBindAdminIds []int
  233. bindAdminMap := make(map[int]bool)
  234. hasBindList, err := system.CheckRoleAdminByAdminIds(req.AdminIds, roleInfo.RoleId)
  235. if err != nil {
  236. br.Msg = "查询已有的账号失败!"
  237. br.ErrMsg = "查询已有的账号失败,Err:" + err.Error()
  238. return
  239. }
  240. for _, v := range hasBindList {
  241. bindAdminMap[v.AdminId] = true
  242. }
  243. for _, v := range sliceAdmin {
  244. adminId, _ := strconv.Atoi(v)
  245. if _, ok := bindAdminMap[adminId]; !ok {
  246. unBindAdminIds = append(unBindAdminIds, adminId)
  247. }
  248. }
  249. if len(unBindAdminIds) == 0 {
  250. br.Msg = "账号已绑定,无需重复绑定"
  251. br.ErrMsg = "账号已绑定,无需重复绑定"
  252. return
  253. }
  254. //组装批量新增的信息
  255. admins := make([]*system.SysRoleAdmin, 0)
  256. for _, v := range unBindAdminIds {
  257. adminAdd := &system.SysRoleAdmin{
  258. AdminId: v,
  259. RoleId: roleInfo.RoleId,
  260. CreateTime: time.Now(),
  261. }
  262. admins = append(admins, adminAdd)
  263. }
  264. err = system.AddRoleAdminBatch(admins)
  265. if err != nil {
  266. br.Msg = "绑定操作失败"
  267. br.ErrMsg = "绑定操作失败,Err:" + err.Error()
  268. return
  269. }
  270. br.Ret = 200
  271. br.Success = true
  272. br.Msg = "操作成功"
  273. return
  274. }
  275. // DeleteAdmin
  276. // @Title 删除留言管理管理员
  277. // @Description 删除留言管理管理员
  278. // @Param request body request.DelCommentAdminReq true "type json string"
  279. // @Success Ret=200 操作成功
  280. // @router /role/admin/del [post]
  281. func (this *SysRoleAdminController) DeleteAdmin() {
  282. br := new(models.BaseResponse).Init()
  283. defer func() {
  284. this.Data["json"] = br
  285. this.ServeJSON()
  286. }()
  287. sysUser := this.SysUser
  288. if sysUser == nil {
  289. br.Msg = "请登录"
  290. br.ErrMsg = "请登录,SysUser Is Empty"
  291. br.Ret = 408
  292. return
  293. }
  294. var req request.DelRoleAdminReq
  295. err := json.Unmarshal(this.Ctx.Input.RequestBody, &req)
  296. if err != nil {
  297. br.Msg = "参数解析异常!"
  298. br.ErrMsg = "参数解析失败,Err:" + err.Error()
  299. return
  300. }
  301. if req.SysRoleAdminId <= 0 {
  302. br.Msg = "请输入需要删除的绑定ID!"
  303. br.ErrMsg = "请输入需要删除的绑定ID!"
  304. return
  305. }
  306. err = system.DeleteRoleAdmin(req.SysRoleAdminId)
  307. if err != nil {
  308. br.Msg = "删除失败"
  309. br.ErrMsg = "删除失败,Err:" + err.Error()
  310. return
  311. }
  312. br.Ret = 200
  313. br.Success = true
  314. br.Msg = "操作成功"
  315. return
  316. }
  317. // SellerList
  318. // @Title 获取未配置权限的管理员(根据部门、分组)
  319. // @Description 获取未配置权限的管理员(根据部门、分组)接口
  320. // @Success 200 {object} response.DepartmentGroupSellersResp
  321. // @router /role/seller/list [get]
  322. func (this *SysRoleAdminController) SellerList() {
  323. br := new(models.BaseResponse).Init()
  324. defer func() {
  325. this.Data["json"] = br
  326. this.ServeJSON()
  327. }()
  328. sysUser := this.SysUser
  329. if sysUser == nil {
  330. br.Msg = "请登录"
  331. br.ErrMsg = "请登录,SysUser Is Empty"
  332. br.Ret = 408
  333. return
  334. }
  335. departmentList, err := system.GetDepartmentList()
  336. if err != nil {
  337. br.Msg = "获取失败"
  338. br.ErrMsg = "获取部门失败,Err:" + err.Error()
  339. return
  340. }
  341. departmentMap := make(map[int]*system.SysDepartmentList)
  342. for _, v := range departmentList {
  343. departmentMap[v.DepartmentId] = v
  344. }
  345. fullGroups, err := system.GetFullGroup()
  346. if err != nil {
  347. br.Msg = "获取分组失败"
  348. br.ErrMsg = "获取分组失败,Err:" + err.Error()
  349. return
  350. }
  351. fullGroupMap := make(map[int]*system.SysFullGroup)
  352. for _, v := range fullGroups {
  353. fullGroupMap[v.GroupId] = v
  354. }
  355. var list []response.DepartmentGroupSellers
  356. departmentListMap := make(map[int][]response.DepartmentGroupSellers)
  357. groupListMap := make(map[int][]response.DepartmentGroupSellers)
  358. teamListMap := make(map[int][]response.DepartmentGroupSellers)
  359. departmentHasMap := make(map[int]bool)
  360. groupHasMap := make(map[int]bool)
  361. teamHasMap := make(map[int]bool)
  362. condition := " and enabled = 1 "
  363. sellerList, err := system.GetSysUserItems(condition, []interface{}{})
  364. if err != nil {
  365. br.Msg = "获取管理账号失败"
  366. br.ErrMsg = "获取管理账号失败,Err:" + err.Error()
  367. return
  368. }
  369. for _, v := range sellerList {
  370. tmp := response.DepartmentGroupSellers{
  371. AdminId: strconv.Itoa(v.AdminId),
  372. RealName: v.RealName,
  373. }
  374. if v.GroupId > 0 {
  375. if groupInfo, ok := fullGroupMap[v.GroupId]; ok {
  376. if groupInfo.ParentId > 0 {
  377. teamListMap[v.GroupId] = append(teamListMap[v.GroupId], tmp)
  378. } else {
  379. groupListMap[groupInfo.GroupId] = append(groupListMap[groupInfo.GroupId], tmp)
  380. }
  381. }
  382. } else {
  383. departmentListMap[v.DepartmentId] = append(departmentListMap[v.DepartmentId], tmp)
  384. }
  385. }
  386. for _, groupInfo := range fullGroups {
  387. var team1 response.DepartmentGroupSellers
  388. //处理小组
  389. if groupInfo.ParentId > 0 {
  390. if _, ok2 := teamHasMap[groupInfo.GroupId]; !ok2 {
  391. if len(teamListMap[groupInfo.GroupId]) > 0 {
  392. team1 = response.DepartmentGroupSellers{
  393. AdminId: "team_" + strconv.Itoa(groupInfo.GroupId),
  394. RealName: groupInfo.GroupName,
  395. ChildrenList: teamListMap[groupInfo.GroupId],
  396. }
  397. teamHasMap[groupInfo.GroupId] = true
  398. groupListMap[groupInfo.ParentId] = append(groupListMap[groupInfo.ParentId], team1)
  399. }
  400. }
  401. }
  402. }
  403. for _, groupInfo := range fullGroups {
  404. var group1 response.DepartmentGroupSellers
  405. //处理大组
  406. if groupInfo.ParentId == 0 {
  407. if _, ok2 := groupHasMap[groupInfo.GroupId]; !ok2 {
  408. if len(groupListMap[groupInfo.GroupId]) > 0 {
  409. group1 = response.DepartmentGroupSellers{
  410. AdminId: "group_" + strconv.Itoa(groupInfo.GroupId),
  411. RealName: groupInfo.GroupName,
  412. ChildrenList: groupListMap[groupInfo.GroupId],
  413. }
  414. groupHasMap[groupInfo.GroupId] = true
  415. departmentListMap[groupInfo.DepartmentId] = append(departmentListMap[groupInfo.DepartmentId], group1)
  416. }
  417. }
  418. }
  419. }
  420. for _, groupInfo := range departmentList {
  421. var department1 response.DepartmentGroupSellers
  422. //处理部门
  423. if _, ok1 := departmentHasMap[groupInfo.DepartmentId]; !ok1 {
  424. if len(departmentListMap[groupInfo.DepartmentId]) > 0 {
  425. department1 = response.DepartmentGroupSellers{
  426. AdminId: "department_" + strconv.Itoa(groupInfo.DepartmentId),
  427. RealName: groupInfo.DepartmentName,
  428. ChildrenList: departmentListMap[groupInfo.DepartmentId],
  429. }
  430. departmentHasMap[groupInfo.DepartmentId] = true
  431. list = append(list, department1)
  432. }
  433. }
  434. }
  435. br.Ret = 200
  436. br.Success = true
  437. br.Msg = "获取成功"
  438. br.Data = response.DepartmentGroupSellersResp{
  439. List: list,
  440. }
  441. return
  442. }