面包店
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

hdl_role.go 11 KiB

9 months ago
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500
  1. package hdl
  2. import (
  3. "applet/app/admin/enum"
  4. "applet/app/admin/lib/validate"
  5. "applet/app/admin/md"
  6. "applet/app/admin/svc"
  7. "applet/app/db"
  8. "applet/app/db/model"
  9. "applet/app/e"
  10. "applet/app/utils"
  11. "github.com/gin-gonic/gin"
  12. "time"
  13. )
  14. func PermissionGroupList(c *gin.Context) {
  15. roleId := c.DefaultQuery("role_id", "")
  16. qrcodeWithBatchRecordsDb := db.PermissionGroupDb{}
  17. qrcodeWithBatchRecordsDb.Set()
  18. groupList, err := qrcodeWithBatchRecordsDb.FindPermissionGroup()
  19. if err != nil {
  20. e.OutErr(c, e.ERR_DB_ORM, err.Error())
  21. return
  22. }
  23. roleDb := db.RoleDb{}
  24. roleDb.Set(0)
  25. list, _, err := roleDb.FindPermissionGroupByRole(utils.StrToInt(roleId))
  26. if err != nil {
  27. e.OutErr(c, e.ERR_DB_ORM, err.Error())
  28. return
  29. }
  30. var isHasPermissionGroupId []string
  31. for _, v := range list {
  32. isHasPermissionGroupId = append(isHasPermissionGroupId, utils.IntToStr(v.PermissionGroup.Id))
  33. }
  34. var tempRespMap = map[string]*md.PermissionGroupListResp{}
  35. var tempRespMapKeys []string
  36. for _, v := range *groupList {
  37. isCheck := false
  38. if utils.InArr(utils.IntToStr(v.Id), isHasPermissionGroupId) {
  39. isCheck = true
  40. }
  41. tempRespMap[utils.IntToStr(v.Id)] = &md.PermissionGroupListResp{
  42. Id: v.Id,
  43. Name: v.Name,
  44. Key: v.Key,
  45. State: v.State,
  46. ParentId: v.ParentId,
  47. CreateAt: v.CreateAt,
  48. UpdateAt: v.UpdateAt,
  49. IsCheck: isCheck,
  50. }
  51. tempRespMapKeys = append(tempRespMapKeys, utils.IntToStr(v.Id))
  52. }
  53. for _, v := range tempRespMap {
  54. if v.ParentId != 0 && tempRespMap[utils.IntToStr(v.ParentId)].ParentId != 0 {
  55. tempRespMap[utils.IntToStr(v.ParentId)].SubPermissionGroupList = append(tempRespMap[utils.IntToStr(v.ParentId)].SubPermissionGroupList, *v)
  56. }
  57. }
  58. for _, v := range tempRespMap {
  59. if v.ParentId != 0 && tempRespMap[utils.IntToStr(v.ParentId)].ParentId == 0 {
  60. tempRespMap[utils.IntToStr(v.ParentId)].SubPermissionGroupList = append(tempRespMap[utils.IntToStr(v.ParentId)].SubPermissionGroupList, *v)
  61. }
  62. }
  63. var resp []*md.PermissionGroupListResp
  64. for _, v := range tempRespMapKeys {
  65. if tempRespMap[v].ParentId == 0 {
  66. resp = append(resp, tempRespMap[v])
  67. }
  68. }
  69. e.OutSuc(c, map[string]interface{}{
  70. "list": resp,
  71. "state": []map[string]interface{}{
  72. {
  73. "name": enum.PermissionGroupState(enum.PermissionGroupStateForNormal).String(),
  74. "value": enum.PermissionGroupStateForNormal,
  75. },
  76. {
  77. "name": enum.PermissionGroupState(enum.PermissionGroupStateForDiscard).String(),
  78. "value": enum.PermissionGroupStateForDiscard,
  79. },
  80. },
  81. }, nil)
  82. return
  83. }
  84. func RoleList(c *gin.Context) {
  85. roleDb := db.RoleDb{}
  86. roleDb.Set(0)
  87. roleList, err := roleDb.FindRole()
  88. if err != nil {
  89. e.OutErr(c, e.ERR_DB_ORM, err.Error())
  90. return
  91. }
  92. adminRoleDb := db.AdminRoleDb{}
  93. adminRoleDb.Set()
  94. adminDb := db.AdminDb{}
  95. adminDb.Set()
  96. var result []*md.RoleListResp
  97. for _, v := range *roleList {
  98. var temp md.RoleListResp
  99. temp.Data = v
  100. adminRoles, err1 := adminRoleDb.FindAdminRoleByRoleId(v.Id)
  101. if err1 != nil {
  102. e.OutErr(c, e.ERR_DB_ORM, err1.Error())
  103. return
  104. }
  105. for _, adminRole := range *adminRoles {
  106. admin, err2 := adminDb.GetAdmin(adminRole.AdmId)
  107. if err2 != nil {
  108. e.OutErr(c, e.ERR_DB_ORM, err2.Error())
  109. return
  110. }
  111. temp.AdminList = append(temp.AdminList, struct {
  112. Name string `json:"name"`
  113. }{
  114. Name: admin.Username,
  115. })
  116. }
  117. result = append(result, &temp)
  118. }
  119. e.OutSuc(c, map[string]interface{}{
  120. "list": result,
  121. "state": []map[string]interface{}{
  122. {
  123. "name": enum.RoleState(enum.RoleStateForNormal).String(),
  124. "value": enum.RoleStateForNormal,
  125. },
  126. {
  127. "name": enum.RoleState(enum.RoleStateForFreeze).String(),
  128. "value": enum.RoleStateForFreeze,
  129. },
  130. },
  131. }, nil)
  132. return
  133. }
  134. func AddRole(c *gin.Context) {
  135. var req md.AddRoleReq
  136. err := c.ShouldBindJSON(&req)
  137. if err != nil {
  138. err = validate.HandleValidateErr(err)
  139. err1 := err.(e.E)
  140. e.OutErr(c, err1.Code, err1.Error())
  141. return
  142. }
  143. roleDb := db.RoleDb{}
  144. roleDb.Set(0)
  145. now := time.Now()
  146. _, err = roleDb.RoleInsert(&model.Role{
  147. Name: req.Name,
  148. State: enum.RoleStateForNormal,
  149. Memo: req.Memo,
  150. CreateAt: now.Format("2006-01-02 15:04:05"),
  151. UpdateAt: now.Format("2006-01-02 15:04:05"),
  152. })
  153. if err != nil {
  154. e.OutErr(c, e.ERR_DB_ORM, err.Error())
  155. return
  156. }
  157. e.OutSuc(c, "success", nil)
  158. return
  159. }
  160. func UpdateRole(c *gin.Context) {
  161. var req md.UpdateRoleReq
  162. err := c.ShouldBindJSON(&req)
  163. if err != nil {
  164. err = validate.HandleValidateErr(err)
  165. err1 := err.(e.E)
  166. e.OutErr(c, err1.Code, err1.Error())
  167. return
  168. }
  169. roleDb := db.RoleDb{}
  170. roleDb.Set(req.RoleId)
  171. role, err := roleDb.GetRole()
  172. if err != nil {
  173. e.OutErr(c, e.ERR_DB_ORM, err.Error())
  174. return
  175. }
  176. if role == nil {
  177. e.OutErr(c, e.ERR_NO_DATA, "未查询到相应记录")
  178. return
  179. }
  180. role.Name = req.Name
  181. role.Memo = req.Memo
  182. _, err = roleDb.UpdateRole(role, "name", "memo")
  183. if err != nil {
  184. e.OutErr(c, e.ERR_DB_ORM, err.Error())
  185. return
  186. }
  187. e.OutSuc(c, "success", nil)
  188. return
  189. }
  190. func RoleBindPermissionGroup(c *gin.Context) {
  191. var req md.RoleBindPermissionGroupReq
  192. err := c.ShouldBindJSON(&req)
  193. if err != nil {
  194. err = validate.HandleValidateErr(err)
  195. err1 := err.(e.E)
  196. e.OutErr(c, err1.Code, err1.Error())
  197. return
  198. }
  199. err = svc.RoleBindPermissionGroup(req)
  200. if err != nil {
  201. e.OutErr(c, e.ERR, err.Error())
  202. return
  203. }
  204. e.OutSuc(c, "success", nil)
  205. return
  206. }
  207. func UpdateRoleState(c *gin.Context) {
  208. var req md.UpdateRoleStateReq
  209. err := c.ShouldBindJSON(&req)
  210. if err != nil {
  211. err = validate.HandleValidateErr(err)
  212. err1 := err.(e.E)
  213. e.OutErr(c, err1.Code, err1.Error())
  214. return
  215. }
  216. roleDb := db.RoleDb{}
  217. roleDb.Set(req.RoleId)
  218. role, err := roleDb.GetRole()
  219. if err != nil {
  220. e.OutErr(c, e.ERR_DB_ORM, err.Error())
  221. return
  222. }
  223. if role == nil {
  224. e.OutErr(c, e.ERR_NO_DATA, "未查询到相应记录")
  225. return
  226. }
  227. role.State = req.State
  228. _, err = roleDb.UpdateRole(role, "state")
  229. if err != nil {
  230. e.OutErr(c, e.ERR_DB_ORM, err.Error())
  231. return
  232. }
  233. e.OutSuc(c, "success", nil)
  234. return
  235. }
  236. func DeleteRole(c *gin.Context) {
  237. id := c.Param("id")
  238. roleDb := db.RoleDb{}
  239. roleDb.Set(utils.StrToInt(id))
  240. role, err := roleDb.GetRole()
  241. if err != nil {
  242. e.OutErr(c, e.ERR_DB_ORM, err.Error())
  243. return
  244. }
  245. if role == nil {
  246. e.OutErr(c, e.ERR_NO_DATA, "未查询到相应记录")
  247. return
  248. }
  249. err = svc.DeleteRole(utils.StrToInt(id))
  250. if err != nil {
  251. e.OutErr(c, e.ERR, err.Error())
  252. return
  253. }
  254. e.OutSuc(c, "success", nil)
  255. return
  256. }
  257. func AdminList(c *gin.Context) {
  258. var req md.AdminListReq
  259. err := c.ShouldBindJSON(&req)
  260. if err != nil {
  261. err = validate.HandleValidateErr(err)
  262. err1 := err.(e.E)
  263. e.OutErr(c, err1.Code, err1.Error())
  264. return
  265. }
  266. if req.Limit == 0 {
  267. req.Limit = 10
  268. }
  269. if req.Page == 0 {
  270. req.Page = 10
  271. }
  272. adminDb := db.AdminDb{}
  273. adminDb.Set()
  274. adminList, total, err := adminDb.FindAdmin(req.UserName, req.State, req.Page, req.Limit)
  275. if err != nil {
  276. e.OutErr(c, e.ERR_DB_ORM, err.Error())
  277. return
  278. }
  279. var result []md.AdminListResp
  280. for _, v := range adminList {
  281. permissionGroupList, _, err1 := adminDb.FindAdminRolePermissionGroup(v.AdmId)
  282. if err != nil {
  283. e.OutErr(c, e.ERR_DB_ORM, err1.Error())
  284. return
  285. }
  286. var roleList []string
  287. for _, v1 := range permissionGroupList {
  288. roleList = append(roleList, v1.Role.Name)
  289. }
  290. result = append(result, md.AdminListResp{
  291. AdmId: v.AdmId,
  292. Username: v.Username,
  293. State: v.State,
  294. IsSuperAdministrator: v.IsSuperAdministrator,
  295. Memo: v.Memo,
  296. CreateAt: v.CreateAt,
  297. UpdateAt: v.UpdateAt,
  298. RoleList: roleList,
  299. })
  300. }
  301. e.OutSuc(c, map[string]interface{}{
  302. "list": result,
  303. "total": total,
  304. "state": []map[string]interface{}{
  305. {
  306. "name": enum.RoleState(enum.RoleStateForNormal).String(),
  307. "value": enum.RoleStateForNormal,
  308. },
  309. {
  310. "name": enum.RoleState(enum.RoleStateForFreeze).String(),
  311. "value": enum.RoleStateForFreeze,
  312. },
  313. },
  314. }, nil)
  315. return
  316. }
  317. func UpdateAdminState(c *gin.Context) {
  318. var req md.UpdateAdminStateReq
  319. err := c.ShouldBindJSON(&req)
  320. if err != nil {
  321. err = validate.HandleValidateErr(err)
  322. err1 := err.(e.E)
  323. e.OutErr(c, err1.Code, err1.Error())
  324. return
  325. }
  326. admDb := db.AdminDb{}
  327. admDb.Set()
  328. admin, err := admDb.GetAdmin(req.AdmId)
  329. if err != nil {
  330. e.OutErr(c, e.ERR_DB_ORM, err.Error())
  331. return
  332. }
  333. if admin == nil {
  334. e.OutErr(c, e.ERR_NO_DATA, "未查询到相应记录")
  335. return
  336. }
  337. admin.State = req.State
  338. _, err = admDb.UpdateAdmin(admin, "state")
  339. if err != nil {
  340. e.OutErr(c, e.ERR_DB_ORM, err.Error())
  341. return
  342. }
  343. e.OutSuc(c, "success", nil)
  344. return
  345. }
  346. func UpdateAdmin(c *gin.Context) {
  347. var req md.UpdateAdminReq
  348. err := c.ShouldBindJSON(&req)
  349. if err != nil {
  350. err = validate.HandleValidateErr(err)
  351. err1 := err.(e.E)
  352. e.OutErr(c, err1.Code, err1.Error())
  353. return
  354. }
  355. admDb := db.AdminDb{}
  356. admDb.Set()
  357. admin, err := admDb.GetAdmin(req.AdmId)
  358. if err != nil {
  359. e.OutErr(c, e.ERR_DB_ORM, err.Error())
  360. return
  361. }
  362. if admin == nil {
  363. e.OutErr(c, e.ERR_NO_DATA, "未查询到相应记录")
  364. return
  365. }
  366. admin.Username = req.Username
  367. admin.Memo = req.Memo
  368. admin.Password = utils.Md5(req.Password)
  369. _, err = admDb.UpdateAdmin(admin, "username", "memo", "password")
  370. if err != nil {
  371. e.OutErr(c, e.ERR_DB_ORM, err.Error())
  372. return
  373. }
  374. e.OutSuc(c, "success", nil)
  375. return
  376. }
  377. func AddAdmin(c *gin.Context) {
  378. var req md.AddAdminReq
  379. err := c.ShouldBindJSON(&req)
  380. if err != nil {
  381. err = validate.HandleValidateErr(err)
  382. err1 := err.(e.E)
  383. e.OutErr(c, err1.Code, err1.Error())
  384. return
  385. }
  386. admDb := db.AdminDb{}
  387. admDb.Set()
  388. isHasAdmin, err := admDb.GetAdminByUserName(req.Username)
  389. if err != nil {
  390. e.OutErr(c, e.ERR_DB_ORM, err.Error())
  391. return
  392. }
  393. if isHasAdmin != nil {
  394. e.OutErr(c, e.ERR, "当前用户名已存在,请勿重复添加")
  395. return
  396. }
  397. admId, err := admDb.CreateAdminId()
  398. if err != nil {
  399. e.OutErr(c, e.ERR_DB_ORM, err.Error())
  400. return
  401. }
  402. admin := model.Admin{
  403. AdmId: admId,
  404. Username: req.Username,
  405. Password: utils.Md5(req.Password),
  406. State: enum.AdminStateForNormal,
  407. IsSuperAdministrator: 0,
  408. Memo: req.Memo,
  409. CreateAt: time.Now().Format("2006-01-02 15:04:05"),
  410. UpdateAt: time.Now().Format("2006-01-02 15:04:05"),
  411. }
  412. _, err = admDb.AdminInsert(&admin)
  413. if err != nil {
  414. e.OutErr(c, e.ERR_DB_ORM, err.Error())
  415. return
  416. }
  417. e.OutSuc(c, "success", nil)
  418. return
  419. }
  420. func DeleteAdmin(c *gin.Context) {
  421. admId := c.Param("adm_id")
  422. admDb := db.AdminDb{}
  423. admDb.Set()
  424. err := svc.AdminDelete([]int{utils.StrToInt(admId)})
  425. if err != nil {
  426. e.OutErr(c, e.ERR_DB_ORM, err.Error())
  427. return
  428. }
  429. e.OutSuc(c, "success", nil)
  430. return
  431. }
  432. func BindAdminRole(c *gin.Context) {
  433. var req md.BindAdminRoleReq
  434. err := c.ShouldBindJSON(&req)
  435. if err != nil {
  436. err = validate.HandleValidateErr(err)
  437. err1 := err.(e.E)
  438. e.OutErr(c, err1.Code, err1.Error())
  439. return
  440. }
  441. err = svc.BindAdminRole(req)
  442. if err != nil {
  443. e.OutErr(c, e.ERR, err.Error())
  444. return
  445. }
  446. e.OutSuc(c, "success", nil)
  447. return
  448. }
  449. func AdminInfo(c *gin.Context) {
  450. admId := c.DefaultQuery("adm_id", "")
  451. admDb := db.AdminDb{}
  452. admDb.Set()
  453. admin, err := admDb.GetAdmin(utils.StrToInt(admId))
  454. if err != nil {
  455. e.OutErr(c, e.ERR_DB_ORM, err.Error())
  456. return
  457. }
  458. admin.Password = ""
  459. e.OutSuc(c, map[string]interface{}{
  460. "info": admin,
  461. "state": []map[string]interface{}{
  462. {
  463. "name": enum.RoleState(enum.RoleStateForNormal).String(),
  464. "value": enum.RoleStateForNormal,
  465. },
  466. {
  467. "name": enum.RoleState(enum.RoleStateForFreeze).String(),
  468. "value": enum.RoleStateForFreeze,
  469. },
  470. },
  471. }, nil)
  472. return
  473. }