广告平台(站长使用)
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.
 
 
 
 
 

125 lines
2.6 KiB

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