蛋蛋星球 后台端
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 
 
 

119 lines
2.7 KiB

  1. package sys_cfg
  2. import (
  3. "applet/app/utils/cache"
  4. "code.fnuoos.com/EggPlanet/egg_models.git/md"
  5. "code.fnuoos.com/EggPlanet/egg_models.git/src/model"
  6. zhios_order_relate_logx "code.fnuoos.com/EggPlanet/egg_models.git/utils/logx"
  7. "fmt"
  8. "xorm.io/xorm"
  9. )
  10. func NewSysCfgDb(engine *xorm.Engine) SysCfgDao {
  11. return &SysCfgDb{
  12. Db: engine,
  13. }
  14. }
  15. type SysCfgDb struct {
  16. Db *xorm.Engine
  17. }
  18. func (s SysCfgDb) SysCfgGetAll() (*[]model.SysCfg, error) {
  19. var cfgList []model.SysCfg
  20. if err := s.Db.Cols("key,val,memo").Find(&cfgList); err != nil {
  21. return nil, zhios_order_relate_logx.Error(err)
  22. }
  23. return &cfgList, nil
  24. }
  25. func (s SysCfgDb) SysCfgGetOneNoDataNoErr(key string) (*model.SysCfg, error) {
  26. var cfgList model.SysCfg
  27. _, err := s.Db.Where("`key`=?", key).Get(&cfgList)
  28. if err != nil {
  29. return nil, zhios_order_relate_logx.Error(err)
  30. }
  31. return &cfgList, nil
  32. }
  33. func (s SysCfgDb) SysCfgGetOne(key string) (*model.SysCfg, error) {
  34. var cfgList model.SysCfg
  35. if has, err := s.Db.Where("`key`=?", key).Get(&cfgList); err != nil || has == false {
  36. return nil, zhios_order_relate_logx.Error(err)
  37. }
  38. return &cfgList, nil
  39. }
  40. func (s SysCfgDb) SysCfgInsert(key, val, memo string) bool {
  41. cfg := model.SysCfg{Key: key, Val: val, Memo: memo}
  42. _, err := s.Db.InsertOne(&cfg)
  43. if err != nil {
  44. zhios_order_relate_logx.Error(err)
  45. return false
  46. }
  47. return true
  48. }
  49. func (s SysCfgDb) SysCfgUpdate(key, val string) bool {
  50. cfg := model.SysCfg{Key: key, Val: val}
  51. _, err := s.Db.Where("`key`=?", key).Cols("val").Update(&cfg)
  52. if err != nil {
  53. zhios_order_relate_logx.Error(err)
  54. return false
  55. }
  56. s.SysCfgDel(key)
  57. return true
  58. }
  59. func (s SysCfgDb) SysCfgGetWithDb(HKey string) string {
  60. cacheKey := fmt.Sprintf(md.AppCfgCacheKey, HKey[0:1])
  61. get, err := cache.HGetString(cacheKey, HKey)
  62. if err != nil || get == "" {
  63. cfg, err := s.SysCfgGetOne(HKey)
  64. if err != nil || cfg == nil {
  65. _ = zhios_order_relate_logx.Error(err)
  66. return ""
  67. }
  68. // key是否存在
  69. cacheKeyExist := false
  70. if cache.Exists(cacheKey) {
  71. cacheKeyExist = true
  72. }
  73. // 设置缓存
  74. _, err = cache.HSet(cacheKey, HKey, cfg.Val)
  75. if err != nil {
  76. _ = zhios_order_relate_logx.Error(err)
  77. return ""
  78. }
  79. if !cacheKeyExist { // 如果是首次设置 设置过期时间
  80. _, err := cache.Expire(cacheKey, md.CfgCacheTime)
  81. if err != nil {
  82. _ = zhios_order_relate_logx.Error(err)
  83. return ""
  84. }
  85. }
  86. return cfg.Val
  87. }
  88. return get
  89. }
  90. func (s SysCfgDb) SysCfgDel(HKey string) error {
  91. cacheKey := fmt.Sprintf(md.AppCfgCacheKey, HKey[0:1])
  92. _, err := cache.HDel(cacheKey, HKey)
  93. if err != nil {
  94. return err
  95. }
  96. return nil
  97. }
  98. func (s SysCfgDb) SysCfgFindWithDb(keys ...string) map[string]string {
  99. res := map[string]string{}
  100. for _, v := range keys {
  101. val := s.SysCfgGetWithDb(v)
  102. res[v] = val
  103. }
  104. return res
  105. }