智慧食堂
選択できるのは25トピックまでです。 トピックは、先頭が英数字で、英数字とダッシュ('-')を使用した35文字以内のものにしてください。

535 行
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. 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. 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. isHasAdmin, err := admDb.GetAdminByUserName(req.Username)
  404. if err != nil {
  405. e.OutErr(c, e.ERR_DB_ORM, err.Error())
  406. return
  407. }
  408. if isHasAdmin != nil {
  409. e.OutErr(c, e.ERR, "当前用户名已存在,请勿重复添加")
  410. return
  411. }
  412. admId, err := admDb.CreateAdminId()
  413. if err != nil {
  414. e.OutErr(c, e.ERR_DB_ORM, err.Error())
  415. return
  416. }
  417. admin := model.Admin{
  418. AdmId: admId,
  419. Username: req.Username,
  420. Password: utils.Md5(req.Password),
  421. State: enum.AdminStateForNormal,
  422. IsSuperAdministrator: 0,
  423. Memo: req.Memo,
  424. CreateAt: time.Now().Format("2006-01-02 15:04:05"),
  425. UpdateAt: time.Now().Format("2006-01-02 15:04:05"),
  426. }
  427. _, err = admDb.AdminInsert(&admin)
  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 DeleteAdmin(c *gin.Context) {
  436. admId := c.Param("adm_id")
  437. admDb := db.AdminDb{}
  438. admDb.Set()
  439. err := svc.AdminDelete([]int{utils.StrToInt(admId)})
  440. if err != nil {
  441. e.OutErr(c, e.ERR_DB_ORM, err.Error())
  442. return
  443. }
  444. e.OutSuc(c, "success", nil)
  445. return
  446. }
  447. func BindAdminRole(c *gin.Context) {
  448. var req md.BindAdminRoleReq
  449. err := c.ShouldBindJSON(&req)
  450. if err != nil {
  451. err = validate.HandleValidateErr(err)
  452. err1 := err.(e.E)
  453. e.OutErr(c, err1.Code, err1.Error())
  454. return
  455. }
  456. err = svc.BindAdminRole(req)
  457. if err != nil {
  458. e.OutErr(c, e.ERR, err.Error())
  459. return
  460. }
  461. e.OutSuc(c, "success", nil)
  462. return
  463. }
  464. func BindAdminWithEnterprise(c *gin.Context) {
  465. var req md.BindAdminWithEnterpriseReq
  466. err := c.ShouldBindJSON(&req)
  467. if err != nil {
  468. err = validate.HandleValidateErr(err)
  469. err1 := err.(e.E)
  470. e.OutErr(c, err1.Code, err1.Error())
  471. return
  472. }
  473. err = svc.BindAdminWithEnterprise(req)
  474. if err != nil {
  475. e.OutErr(c, e.ERR, err.Error())
  476. return
  477. }
  478. e.OutSuc(c, "success", nil)
  479. return
  480. }
  481. func AdminInfo(c *gin.Context) {
  482. admId := c.DefaultQuery("adm_id", "")
  483. admDb := db.AdminDb{}
  484. admDb.Set()
  485. admin, err := admDb.GetAdmin(utils.StrToInt(admId))
  486. if err != nil {
  487. e.OutErr(c, e.ERR_DB_ORM, err.Error())
  488. return
  489. }
  490. admin.Password = ""
  491. e.OutSuc(c, map[string]interface{}{
  492. "info": admin,
  493. "state": []map[string]interface{}{
  494. {
  495. "name": enum.RoleState(enum.RoleStateForNormal).String(),
  496. "value": enum.RoleStateForNormal,
  497. },
  498. {
  499. "name": enum.RoleState(enum.RoleStateForFreeze).String(),
  500. "value": enum.RoleStateForFreeze,
  501. },
  502. },
  503. }, nil)
  504. return
  505. }