english_auth.go 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474
  1. package english_report
  2. import (
  3. "encoding/json"
  4. "github.com/rdlucklib/rdluck_tools/paging"
  5. "hongze/hz_crm_api/controllers"
  6. "hongze/hz_crm_api/models"
  7. "hongze/hz_crm_api/models/system"
  8. "hongze/hz_crm_api/models/system/request"
  9. "hongze/hz_crm_api/models/system/response"
  10. "hongze/hz_crm_api/utils"
  11. "strconv"
  12. "strings"
  13. "time"
  14. )
  15. var EnglishAuthRoles = []string{
  16. system.EnglishAuthCompanyRole, system.EnglishAuthReportRole, system.EnglishAuthPPTRole, system.EnglishAuthDayNewRole,
  17. }
  18. // EnglishAuthController 英文销售权限
  19. type EnglishAuthController struct {
  20. controllers.BaseAuthController
  21. }
  22. // List
  23. // @Title 权限列表
  24. // @Description 权限列表
  25. // @Success 200 {object} response.RoleAdminItemResp
  26. // @router /auth/list [get]
  27. func (this *EnglishAuthController) List() {
  28. br := new(models.BaseResponse).Init()
  29. defer func() {
  30. this.Data["json"] = br
  31. this.ServeJSON()
  32. }()
  33. sysUser := this.SysUser
  34. if sysUser == nil {
  35. br.Msg = "请登录"
  36. br.ErrMsg = "请登录,SysUser is Empty"
  37. br.Ret = 408
  38. return
  39. }
  40. pageSize, _ := this.GetInt("PageSize")
  41. currentIndex, _ := this.GetInt("CurrentIndex")
  42. var startSize int
  43. if pageSize <= 0 {
  44. pageSize = utils.PageSize20
  45. }
  46. if currentIndex <= 0 {
  47. currentIndex = 1
  48. }
  49. startSize = paging.StartIndex(currentIndex, pageSize)
  50. // 获取权限列表
  51. var cond string
  52. var pars []interface{}
  53. cond += ` AND r.role_type_code IN (` + utils.GetOrmInReplace(len(EnglishAuthRoles)) + `)`
  54. pars = append(pars, EnglishAuthRoles)
  55. total, adminList, err := system.GetEnglishAuthRoleAdminList(cond, pars, startSize, pageSize)
  56. if err != nil {
  57. br.Msg = "获取管理账号列表失败!"
  58. br.ErrMsg = "获取管理账号列表失败,Err:" + err.Error()
  59. return
  60. }
  61. // 查询分组信息
  62. departmentGroupNameMap := make(map[int]string)
  63. departmentNameMap := make(map[int]string)
  64. groupList, err := system.GetFullGroup()
  65. if err != nil {
  66. br.Msg = "查询分组信息失败!"
  67. br.ErrMsg = "查询分组信息失败,Err:" + err.Error()
  68. return
  69. }
  70. departmentList, err := system.GetDepartmentList()
  71. if err != nil {
  72. br.Msg = "查询部门信息失败!"
  73. br.ErrMsg = "查询部门信息失败,Err:" + err.Error()
  74. return
  75. }
  76. for _, d := range departmentList {
  77. departmentNameMap[d.DepartmentId] = d.DepartmentName
  78. }
  79. for _, g := range groupList {
  80. if g.ParentGroupName != "" {
  81. departmentGroupNameMap[g.GroupId] = g.DepartmentName + "/" + g.ParentGroupName + "/" + g.GroupName
  82. } else {
  83. departmentGroupNameMap[g.GroupId] = g.DepartmentName + "/" + g.GroupName
  84. }
  85. }
  86. // 查询角色信息
  87. roles, err := system.GetSysRoleAll(false)
  88. if err != nil {
  89. br.Msg = "查询角色信息失败!"
  90. br.ErrMsg = "查询角色信息失败,Err:" + err.Error()
  91. return
  92. }
  93. rolesNameMap := make(map[int]string)
  94. for _, v := range roles {
  95. rolesNameMap[v.RoleId] = v.RoleName
  96. }
  97. var list []*response.RoleAdminItemResp
  98. for _, v := range adminList {
  99. item := new(response.RoleAdminItemResp)
  100. item.SysRoleAdminId = v.SysRoleAdminId
  101. item.AdminId = v.AdminId
  102. item.Mobile = v.Mobile
  103. item.RealName = v.RealName
  104. item.Enabled = v.Enabled
  105. if v.GroupId > 0 {
  106. if departmentGroup, ok := departmentGroupNameMap[v.GroupId]; ok {
  107. item.DepartmentGroup = departmentGroup
  108. }
  109. } else {
  110. if departmentName, ok := departmentNameMap[v.DepartmentId]; ok {
  111. item.DepartmentGroup = departmentName
  112. }
  113. }
  114. // 整理角色名称
  115. item.RoleId = v.RoleId
  116. if rolesName, ok := rolesNameMap[v.RoleId]; ok {
  117. item.RoleName = rolesName
  118. }
  119. // 整理标签
  120. if v.RoleTypeCode == utils.ROLE_TYPE_CODE_ADMIN {
  121. item.LabelVal = 1
  122. } else if v.RoleTypeCode == utils.ROLE_TYPE_CODE_FICC_ADMIN ||
  123. v.RoleTypeCode == utils.ROLE_TYPE_CODE_RAI_ADMIN {
  124. item.LabelVal = 2
  125. } else {
  126. if v.Authority == 1 {
  127. item.LabelVal = 3
  128. } else if v.Authority == 2 && v.RoleTypeCode == utils.ROLE_TYPE_CODE_RAI_GROUP {
  129. item.LabelVal = 4
  130. } else if v.Authority == 2 && v.RoleTypeCode == utils.ROLE_TYPE_CODE_FICC_GROUP {
  131. item.LabelVal = 5
  132. } else if v.Authority == 4 {
  133. item.LabelVal = 4
  134. }
  135. }
  136. list = append(list, item)
  137. }
  138. page := paging.GetPaging(currentIndex, pageSize, total)
  139. resp := response.RoleAdminListResp{
  140. List: list,
  141. Paging: page,
  142. }
  143. br.Ret = 200
  144. br.Success = true
  145. br.Msg = "获取成功"
  146. br.Data = resp
  147. }
  148. // BatchAdd
  149. // @Title 批量新增权限
  150. // @Description 批量新增权限
  151. // @Param request body request.AddEnglishAuthRoleAdminReq true "type json string"
  152. // @Success Ret=200 操作成功
  153. // @router /auth/batch_add [post]
  154. func (this *EnglishAuthController) BatchAdd() {
  155. br := new(models.BaseResponse).Init()
  156. defer func() {
  157. this.Data["json"] = br
  158. this.ServeJSON()
  159. }()
  160. sysUser := this.SysUser
  161. if sysUser == nil {
  162. br.Msg = "请登录"
  163. br.ErrMsg = "请登录,SysUser Is Empty"
  164. br.Ret = 408
  165. return
  166. }
  167. var req request.AddEnglishAuthRoleAdminReq
  168. err := json.Unmarshal(this.Ctx.Input.RequestBody, &req)
  169. if err != nil {
  170. br.Msg = "参数解析异常!"
  171. br.ErrMsg = "参数解析失败,Err:" + err.Error()
  172. return
  173. }
  174. if req.AdminIds == "" {
  175. br.Msg = "请选择用户"
  176. return
  177. }
  178. // 校验参数
  179. adminIdArr := strings.Split(req.AdminIds, ",")
  180. if len(adminIdArr) == 0 {
  181. br.Msg = "请选择用户"
  182. return
  183. }
  184. adminIds := make([]int, 0)
  185. for i := range adminIdArr {
  186. v, e := strconv.Atoi(adminIdArr[i])
  187. if e != nil {
  188. br.Msg = "请选择用户"
  189. br.ErrMsg = "参数解析异常, Err:" + e.Error()
  190. return
  191. }
  192. adminIds = append(adminIds, v)
  193. }
  194. if len(adminIds) == 0 {
  195. br.Msg = "请选择有效用户"
  196. return
  197. }
  198. // 获取英文权限角色IDs
  199. roleIds, e := system.GetEnglishAuthRoleIds(EnglishAuthRoles)
  200. if e != nil {
  201. br.Msg = "新增失败"
  202. br.ErrMsg = "获取英文权限角色IDs失败, Err: " + e.Error()
  203. return
  204. }
  205. if len(roleIds) == 0 || len(roleIds) != len(EnglishAuthRoles) {
  206. br.Msg = "英文权限角色信息有误"
  207. return
  208. }
  209. // 查询已绑定的管理员账号
  210. var noBindIds []int
  211. bindMap := make(map[int]bool)
  212. hasBindList, e := system.CheckEnglishAuthRoleAdminByAdminIds(adminIds, EnglishAuthRoles)
  213. if e != nil {
  214. br.Msg = "新增失败"
  215. br.ErrMsg = "查询已绑定英文权限的账号失败, Err:" + e.Error()
  216. return
  217. }
  218. for i := range hasBindList {
  219. bindMap[hasBindList[i].AdminId] = true
  220. }
  221. for i := range adminIds {
  222. if !bindMap[adminIds[i]] {
  223. noBindIds = append(noBindIds, adminIds[i])
  224. }
  225. }
  226. if len(noBindIds) == 0 {
  227. br.Msg = "账号已绑定,无需重复绑定"
  228. br.ErrMsg = "账号已绑定,无需重复绑定"
  229. return
  230. }
  231. // 新增绑定关系
  232. nowTime := time.Now().Local()
  233. newBinds := make([]*system.SysRoleAdmin, 0)
  234. for i := range noBindIds {
  235. for r := range roleIds {
  236. v := &system.SysRoleAdmin{
  237. AdminId: noBindIds[i],
  238. RoleId: roleIds[r],
  239. CreateTime: nowTime,
  240. }
  241. newBinds = append(newBinds, v)
  242. }
  243. }
  244. if e = system.AddRoleAdminBatch(newBinds); e != nil {
  245. br.Msg = "新增失败"
  246. br.ErrMsg = "新增英文权限绑定关系失败, Err:" + e.Error()
  247. return
  248. }
  249. br.Ret = 200
  250. br.Success = true
  251. br.Msg = "操作成功"
  252. }
  253. // Detail
  254. // @Title 权限详情
  255. // @Description 权限详情
  256. // @Param AdminId query string int "管理员ID"
  257. // @Success 200 {object} response.EnglishAuthRoleDetailResp
  258. // @router /auth/detail [get]
  259. func (this *EnglishAuthController) Detail() {
  260. br := new(models.BaseResponse).Init()
  261. defer func() {
  262. this.Data["json"] = br
  263. this.ServeJSON()
  264. }()
  265. sysUser := this.SysUser
  266. if sysUser == nil {
  267. br.Msg = "请登录"
  268. br.ErrMsg = "请登录,SysUser is Empty"
  269. br.Ret = 408
  270. return
  271. }
  272. adminId, _ := this.GetInt("AdminId", 0)
  273. if adminId <= 0 {
  274. br.Msg = "参数有误"
  275. return
  276. }
  277. resp := make([]*response.EnglishAuthRoleDetailResp, 0)
  278. authList, e := system.GetAdminEnglishAuthRoleDetail(adminId, EnglishAuthRoles)
  279. if e != nil {
  280. br.Msg = "获取失败"
  281. br.ErrMsg = "获取管理员英文权限详情失败, Err:" + e.Error()
  282. return
  283. }
  284. authMap := make(map[string]bool, 0)
  285. for i := range authList {
  286. authMap[authList[i].RoleTypeCode] = true
  287. }
  288. var englishAuthRolesMap = map[string]string{
  289. system.EnglishAuthCompanyRole: "英文客户列表",
  290. system.EnglishAuthReportRole: "英文研报",
  291. system.EnglishAuthPPTRole: "英文PPT",
  292. system.EnglishAuthDayNewRole: "每日资讯",
  293. }
  294. for i, v := range englishAuthRolesMap {
  295. resp = append(resp, &response.EnglishAuthRoleDetailResp{
  296. AdminId: adminId,
  297. AuthName: v,
  298. RoleTypeCode: i,
  299. AuthOk: authMap[i],
  300. })
  301. }
  302. br.Ret = 200
  303. br.Success = true
  304. br.Msg = "获取成功"
  305. br.Data = resp
  306. }
  307. // Edit
  308. // @Title 编辑权限
  309. // @Description 编辑权限
  310. // @Param request body request.EditEnglishAuthRoleAdminReq true "type json string"
  311. // @Success Ret=200 操作成功
  312. // @router /auth/edit [post]
  313. func (this *EnglishAuthController) Edit() {
  314. br := new(models.BaseResponse).Init()
  315. defer func() {
  316. this.Data["json"] = br
  317. this.ServeJSON()
  318. }()
  319. sysUser := this.SysUser
  320. if sysUser == nil {
  321. br.Msg = "请登录"
  322. br.ErrMsg = "请登录,SysUser Is Empty"
  323. br.Ret = 408
  324. return
  325. }
  326. var req request.EditEnglishAuthRoleAdminReq
  327. err := json.Unmarshal(this.Ctx.Input.RequestBody, &req)
  328. if err != nil {
  329. br.Msg = "参数解析异常!"
  330. br.ErrMsg = "参数解析失败,Err:" + err.Error()
  331. return
  332. }
  333. if req.AdminId <= 0 {
  334. br.Msg = "管理员ID有误"
  335. return
  336. }
  337. if req.RoleTypeCodes == "" {
  338. br.Msg = "请选择权限"
  339. return
  340. }
  341. roleArr := strings.Split(req.RoleTypeCodes, ",")
  342. if len(roleArr) == 0 {
  343. br.Msg = "请选择权限"
  344. return
  345. }
  346. for i := range roleArr {
  347. if !utils.InArrayByStr(EnglishAuthRoles, roleArr[i]) {
  348. br.Msg = "权限信息有误"
  349. return
  350. }
  351. }
  352. // 获取英文权限角色IDs
  353. roleIds, e := system.GetEnglishAuthRoleIds(EnglishAuthRoles)
  354. if e != nil {
  355. br.Msg = "新增失败"
  356. br.ErrMsg = "获取英文权限角色IDs失败, Err: " + e.Error()
  357. return
  358. }
  359. if len(roleIds) == 0 {
  360. br.Msg = "英文权限角色信息有误"
  361. return
  362. }
  363. // 获取编辑的权限角色IDs
  364. editRoleIds, e := system.GetEnglishAuthRoleIds(roleArr)
  365. if e != nil {
  366. br.Msg = "新增失败"
  367. br.ErrMsg = "获取编辑英文权限角色IDs失败, Err: " + e.Error()
  368. return
  369. }
  370. if len(editRoleIds) != len(roleArr) {
  371. br.Msg = "权限角色信息有误"
  372. return
  373. }
  374. // 编辑权限
  375. nowTime := time.Now().Local()
  376. newBinds := make([]*system.SysRoleAdmin, 0)
  377. for r := range editRoleIds {
  378. v := &system.SysRoleAdmin{
  379. AdminId: req.AdminId,
  380. RoleId: editRoleIds[r],
  381. CreateTime: nowTime,
  382. }
  383. newBinds = append(newBinds, v)
  384. }
  385. if e = system.EditEnglishAuthRoleAdmin(req.AdminId, roleIds, newBinds); e != nil {
  386. br.Msg = "编辑失败"
  387. br.ErrMsg = "编辑管理员英文权限绑定关系失败, Err:" + e.Error()
  388. return
  389. }
  390. br.Ret = 200
  391. br.Success = true
  392. br.Msg = "操作成功"
  393. }
  394. // Del
  395. // @Title 删除权限
  396. // @Description 删除权限
  397. // @Param request body request.DelEnglishAuthRoleAdminReq true "type json string"
  398. // @Success Ret=200 操作成功
  399. // @router /auth/del [post]
  400. func (this *EnglishAuthController) Del() {
  401. br := new(models.BaseResponse).Init()
  402. defer func() {
  403. this.Data["json"] = br
  404. this.ServeJSON()
  405. }()
  406. sysUser := this.SysUser
  407. if sysUser == nil {
  408. br.Msg = "请登录"
  409. br.ErrMsg = "请登录,SysUser Is Empty"
  410. br.Ret = 408
  411. return
  412. }
  413. var req request.DelEnglishAuthRoleAdminReq
  414. err := json.Unmarshal(this.Ctx.Input.RequestBody, &req)
  415. if err != nil {
  416. br.Msg = "参数解析异常!"
  417. br.ErrMsg = "参数解析失败,Err:" + err.Error()
  418. return
  419. }
  420. if req.AdminId <= 0 {
  421. br.Msg = "参数有误"
  422. return
  423. }
  424. // 获取英文权限角色IDs
  425. roleIds, e := system.GetEnglishAuthRoleIds(EnglishAuthRoles)
  426. if e != nil {
  427. br.Msg = "删除失败"
  428. br.ErrMsg = "获取英文权限角色IDs失败, Err: " + e.Error()
  429. return
  430. }
  431. if len(roleIds) == 0 {
  432. br.Msg = "删除失败"
  433. br.ErrMsg = "解除英文权限角色绑定关系失败, 角色信息有误"
  434. return
  435. }
  436. // 解除绑定关系
  437. if e = system.DeleteRoleAdminByAdminIdAndRoleIds(req.AdminId, roleIds); e != nil {
  438. br.Msg = "删除失败"
  439. br.ErrMsg = "解除英文权限角色绑定关系失败, Err:" + e.Error()
  440. return
  441. }
  442. br.Ret = 200
  443. br.Success = true
  444. br.Msg = "操作成功"
  445. }