智慧食堂
Nie możesz wybrać więcej, niż 25 tematów Tematy muszą się zaczynać od litery lub cyfry, mogą zawierać myślniki ('-') i mogą mieć do 35 znaków.

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