一物一码
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.
 
 
 
 
 
 

120 lines
2.6 KiB

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