sys_role_admin.go 12 KB

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