sys_post_menu.go 1.9 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677
  1. package model
  2. import (
  3. model2 "cold-delivery/common/model"
  4. "encoding/json"
  5. "errors"
  6. "gogs.baozhida.cn/zoie/OAuth-core/sdk"
  7. "gogs.baozhida.cn/zoie/OAuth-core/sdk/config"
  8. )
  9. // SysPostMenu 岗位-菜单权限关联表
  10. type SysPostMenu struct {
  11. model2.Model
  12. PostId int `json:"postId" gorm:"index;comment:岗位ID"`
  13. MenuId int `json:"menuId" gorm:"index;comment:菜单ID"`
  14. model2.ControlBy
  15. model2.ModelTime
  16. }
  17. func (SysPostMenu) TableName() string {
  18. return "sys_post_menu"
  19. }
  20. func (e *SysPostMenu) Generate() model2.ActiveRecord {
  21. o := *e
  22. return &o
  23. }
  24. func (e *SysPostMenu) GetId() interface{} {
  25. return e.Id
  26. }
  27. // GetPostMenuCacheKey 获取岗位菜单缓存key
  28. func GetPostMenuCacheKey(postId int) string {
  29. return "post_menu-" + string(rune(postId))
  30. }
  31. // DeleteAllPostMenuCache 菜单变更时删除所有岗位菜单缓存
  32. func DeleteAllPostMenuCache() error {
  33. db := sdk.Runtime.GetDbByKey(config.ApplicationConfig.Host)
  34. if db == nil {
  35. return errors.New("db not exist")
  36. }
  37. var list []SysPost
  38. err := db.Model(SysPost{}).Find(&list).Error
  39. if err != nil {
  40. return err
  41. }
  42. for _, post := range list {
  43. _ = sdk.Runtime.GetCacheAdapter().Del(GetPostMenuCacheKey(post.Id))
  44. }
  45. return nil
  46. }
  47. // DeletePostMenuCache 删除单个岗位菜单缓存
  48. func DeletePostMenuCache(postId int) error {
  49. return sdk.Runtime.GetCacheAdapter().Del(GetPostMenuCacheKey(postId))
  50. }
  51. // SetPostMenuCache 设置岗位菜单缓存
  52. func SetPostMenuCache(postId int, menuList []SysMenu) error {
  53. s, err := json.Marshal(menuList)
  54. if err != nil {
  55. return err
  56. }
  57. return sdk.Runtime.GetCacheAdapter().Set(GetPostMenuCacheKey(postId), s, 24*60*60)
  58. }
  59. // GetPostMenuCache 获取岗位菜单缓存
  60. func GetPostMenuCache(postId int, menuList *[]SysMenu) error {
  61. s, err := sdk.Runtime.GetCacheAdapter().Get(GetPostMenuCacheKey(postId))
  62. if err != nil {
  63. return err
  64. }
  65. return json.Unmarshal([]byte(s), &menuList)
  66. }