sys_role_admin.go 13 KB

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