智慧食堂
Non puoi selezionare più di 25 argomenti Gli argomenti devono iniziare con una lettera o un numero, possono includere trattini ('-') e possono essere lunghi fino a 35 caratteri.

505 righe
12 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. adminEnterpriseList, _, err1 := adminDb.FindAdminWithEnterprise(v.AdmId)
  291. if err != nil {
  292. e.OutErr(c, e.ERR_DB_ORM, err1.Error())
  293. return
  294. }
  295. var enterpriseList []map[string]interface{}
  296. for _, v1 := range adminEnterpriseList {
  297. enterpriseList = append(enterpriseList, map[string]interface{}{
  298. "name": v1.Enterprise.Name,
  299. "id": v1.Enterprise.Id,
  300. })
  301. }
  302. result = append(result, md.AdminListResp{
  303. AdmId: v.AdmId,
  304. Username: v.Username,
  305. State: v.State,
  306. IsSuperAdministrator: v.IsSuperAdministrator,
  307. Memo: v.Memo,
  308. CreateAt: v.CreateAt,
  309. UpdateAt: v.UpdateAt,
  310. RoleList: roleList,
  311. })
  312. }
  313. e.OutSuc(c, map[string]interface{}{
  314. "list": result,
  315. "total": total,
  316. "state": []map[string]interface{}{
  317. {
  318. "name": enum.RoleState(enum.RoleStateForNormal).String(),
  319. "value": enum.RoleStateForNormal,
  320. },
  321. {
  322. "name": enum.RoleState(enum.RoleStateForFreeze).String(),
  323. "value": enum.RoleStateForFreeze,
  324. },
  325. },
  326. }, nil)
  327. return
  328. }
  329. func UpdateAdminState(c *gin.Context) {
  330. var req md.UpdateAdminStateReq
  331. err := c.ShouldBindJSON(&req)
  332. if err != nil {
  333. err = validate.HandleValidateErr(err)
  334. err1 := err.(e.E)
  335. e.OutErr(c, err1.Code, err1.Error())
  336. return
  337. }
  338. admDb := db.AdminDb{}
  339. admDb.Set()
  340. admin, err := admDb.GetAdmin(req.AdmId)
  341. if err != nil {
  342. e.OutErr(c, e.ERR_DB_ORM, err.Error())
  343. return
  344. }
  345. if admin == nil {
  346. e.OutErr(c, e.ERR_NO_DATA, "未查询到相应记录")
  347. return
  348. }
  349. admin.State = req.State
  350. _, err = admDb.UpdateAdmin(admin, "state")
  351. if err != nil {
  352. e.OutErr(c, e.ERR_DB_ORM, err.Error())
  353. return
  354. }
  355. e.OutSuc(c, "success", nil)
  356. return
  357. }
  358. func UpdateAdmin(c *gin.Context) {
  359. var req md.UpdateAdminReq
  360. err := c.ShouldBindJSON(&req)
  361. if err != nil {
  362. err = validate.HandleValidateErr(err)
  363. err1 := err.(e.E)
  364. e.OutErr(c, err1.Code, err1.Error())
  365. return
  366. }
  367. admDb := db.AdminDb{}
  368. admDb.Set()
  369. admin, err := admDb.GetAdmin(req.AdmId)
  370. if err != nil {
  371. e.OutErr(c, e.ERR_DB_ORM, err.Error())
  372. return
  373. }
  374. if admin == nil {
  375. e.OutErr(c, e.ERR_NO_DATA, "未查询到相应记录")
  376. return
  377. }
  378. admin.Username = req.Username
  379. admin.Memo = req.Memo
  380. admin.Password = utils.Md5(req.Password)
  381. _, err = admDb.UpdateAdmin(admin, "username", "memo", "password")
  382. if err != nil {
  383. e.OutErr(c, e.ERR_DB_ORM, err.Error())
  384. return
  385. }
  386. e.OutSuc(c, "success", nil)
  387. return
  388. }
  389. func AddAdmin(c *gin.Context) {
  390. var req md.AddAdminReq
  391. err := c.ShouldBindJSON(&req)
  392. if err != nil {
  393. err = validate.HandleValidateErr(err)
  394. err1 := err.(e.E)
  395. e.OutErr(c, err1.Code, err1.Error())
  396. return
  397. }
  398. admDb := db.AdminDb{}
  399. admDb.Set()
  400. admId, err := admDb.CreateAdminId()
  401. if err != nil {
  402. e.OutErr(c, e.ERR_DB_ORM, err.Error())
  403. return
  404. }
  405. admin := model.Admin{
  406. AdmId: admId,
  407. Username: req.Username,
  408. Password: utils.Md5(req.Password),
  409. State: enum.AdminStateForNormal,
  410. IsSuperAdministrator: 0,
  411. Memo: req.Memo,
  412. CreateAt: time.Now().Format("2006-01-02 15:04:05"),
  413. UpdateAt: time.Now().Format("2006-01-02 15:04:05"),
  414. }
  415. _, err = admDb.AdminInsert(&admin)
  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 DeleteAdmin(c *gin.Context) {
  424. admId := c.Param("adm_id")
  425. admDb := db.AdminDb{}
  426. admDb.Set()
  427. err := svc.AdminDelete([]int{utils.StrToInt(admId)})
  428. if err != nil {
  429. e.OutErr(c, e.ERR_DB_ORM, err.Error())
  430. return
  431. }
  432. e.OutSuc(c, "success", nil)
  433. return
  434. }
  435. func BindAdminRole(c *gin.Context) {
  436. var req md.BindAdminRoleReq
  437. err := c.ShouldBindJSON(&req)
  438. if err != nil {
  439. err = validate.HandleValidateErr(err)
  440. err1 := err.(e.E)
  441. e.OutErr(c, err1.Code, err1.Error())
  442. return
  443. }
  444. err = svc.BindAdminRole(req)
  445. if err != nil {
  446. e.OutErr(c, e.ERR, err.Error())
  447. return
  448. }
  449. e.OutSuc(c, "success", nil)
  450. return
  451. }
  452. func AdminInfo(c *gin.Context) {
  453. admId := c.DefaultQuery("adm_id", "")
  454. admDb := db.AdminDb{}
  455. admDb.Set()
  456. admin, err := admDb.GetAdmin(utils.StrToInt(admId))
  457. if err != nil {
  458. e.OutErr(c, e.ERR_DB_ORM, err.Error())
  459. return
  460. }
  461. admin.Password = ""
  462. e.OutSuc(c, map[string]interface{}{
  463. "info": admin,
  464. "state": []map[string]interface{}{
  465. {
  466. "name": enum.RoleState(enum.RoleStateForNormal).String(),
  467. "value": enum.RoleStateForNormal,
  468. },
  469. {
  470. "name": enum.RoleState(enum.RoleStateForFreeze).String(),
  471. "value": enum.RoleStateForFreeze,
  472. },
  473. },
  474. }, nil)
  475. return
  476. }