智慧食堂
Não pode escolher mais do que 25 tópicos Os tópicos devem começar com uma letra ou um número, podem incluir traços ('-') e podem ter até 35 caracteres.

491 linhas
11 KiB

  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. adminRole, err1 := adminRoleDb.GetAdminRoleByRole(v.Id)
  101. if err1 != nil {
  102. e.OutErr(c, e.ERR_DB_ORM, err1.Error())
  103. return
  104. }
  105. if adminRole != nil {
  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. admId, err := admDb.CreateAdminId()
  389. if err != nil {
  390. e.OutErr(c, e.ERR_DB_ORM, err.Error())
  391. return
  392. }
  393. admin := model.Admin{
  394. AdmId: admId,
  395. Username: req.Username,
  396. Password: utils.Md5(req.Password),
  397. State: enum.AdminStateForNormal,
  398. IsSuperAdministrator: 0,
  399. Memo: req.Memo,
  400. CreateAt: time.Now().Format("2006-01-02 15:04:05"),
  401. UpdateAt: time.Now().Format("2006-01-02 15:04:05"),
  402. }
  403. _, err = admDb.AdminInsert(&admin)
  404. if err != nil {
  405. e.OutErr(c, e.ERR_DB_ORM, err.Error())
  406. return
  407. }
  408. e.OutSuc(c, "success", nil)
  409. return
  410. }
  411. func DeleteAdmin(c *gin.Context) {
  412. admId := c.Param("adm_id")
  413. admDb := db.AdminDb{}
  414. admDb.Set()
  415. err := svc.AdminDelete([]int{utils.StrToInt(admId)})
  416. if err != nil {
  417. e.OutErr(c, e.ERR_DB_ORM, err.Error())
  418. return
  419. }
  420. e.OutSuc(c, "success", nil)
  421. return
  422. }
  423. func BindAdminRole(c *gin.Context) {
  424. var req md.BindAdminRoleReq
  425. err := c.ShouldBindJSON(&req)
  426. if err != nil {
  427. err = validate.HandleValidateErr(err)
  428. err1 := err.(e.E)
  429. e.OutErr(c, err1.Code, err1.Error())
  430. return
  431. }
  432. err = svc.BindAdminRole(req)
  433. if err != nil {
  434. e.OutErr(c, e.ERR, err.Error())
  435. return
  436. }
  437. e.OutSuc(c, "success", nil)
  438. return
  439. }
  440. func AdminInfo(c *gin.Context) {
  441. admId := c.DefaultQuery("adm_id", "")
  442. admDb := db.AdminDb{}
  443. admDb.Set()
  444. admin, err := admDb.GetAdmin(utils.StrToInt(admId))
  445. if err != nil {
  446. e.OutErr(c, e.ERR_DB_ORM, err.Error())
  447. return
  448. }
  449. admin.Password = ""
  450. e.OutSuc(c, map[string]interface{}{
  451. "info": admin,
  452. "state": []map[string]interface{}{
  453. {
  454. "name": enum.RoleState(enum.RoleStateForNormal).String(),
  455. "value": enum.RoleStateForNormal,
  456. },
  457. {
  458. "name": enum.RoleState(enum.RoleStateForFreeze).String(),
  459. "value": enum.RoleStateForFreeze,
  460. },
  461. },
  462. }, nil)
  463. return
  464. }