数据采集
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.
 
 
 
 
 
 

133 lines
3.1 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 := sysCfgDb.Db.Cols("k,v,memo").Find(&cfgList); err != nil {
  19. return nil, logx.Error(err)
  20. }
  21. return &cfgList, nil
  22. }
  23. func (sysCfgDb *SysCfgDb) SysCfgGetCron() (*[]model.SysCfg, error) {
  24. var cfgList []model.SysCfg
  25. if err := sysCfgDb.Db.Where("`k` LIKE 'data_collect_cron\\_%' AND v != ''").Cols("k,v,memo").Find(&cfgList); err != nil {
  26. return nil, logx.Error(err)
  27. }
  28. return &cfgList, nil
  29. }
  30. func (sysCfgDb *SysCfgDb) SysCfgGetOneNoDataNoErr(key string) (*model.SysCfg, error) {
  31. var cfgList model.SysCfg
  32. has, err := sysCfgDb.Db.Where("`k`=?", key).Get(&cfgList)
  33. if err != nil {
  34. return nil, logx.Error(err)
  35. }
  36. if !has {
  37. return nil, nil
  38. }
  39. return &cfgList, nil
  40. }
  41. func (sysCfgDb *SysCfgDb) SysCfgGetOne(key string) (*model.SysCfg, error) {
  42. var cfgList model.SysCfg
  43. if has, err := sysCfgDb.Db.Where("`k`=?", key).Get(&cfgList); err != nil || has == false {
  44. return nil, logx.Error(err)
  45. }
  46. return &cfgList, nil
  47. }
  48. func (sysCfgDb *SysCfgDb) SysCfgGetOneData(key string) string {
  49. var cfgList model.SysCfg
  50. if has, err := sysCfgDb.Db.Where("`k`=?", key).Get(&cfgList); err != nil || has == false {
  51. return ""
  52. }
  53. return cfgList.V
  54. }
  55. func (sysCfgDb *SysCfgDb) SysCfgInsert(key, val, memo string) bool {
  56. cfg := model.SysCfg{K: key, V: val, Memo: memo}
  57. _, err := sysCfgDb.Db.InsertOne(&cfg)
  58. if err != nil {
  59. logx.Error(err)
  60. return false
  61. }
  62. return true
  63. }
  64. func (sysCfgDb *SysCfgDb) SysCfgUpdate(key, val string) bool {
  65. cfg := model.SysCfg{K: key, V: val}
  66. _, err := Db.Where("`key`=?", key).Cols("val").Update(&cfg)
  67. if err != nil {
  68. logx.Error(err)
  69. return false
  70. }
  71. sysCfgDb.SysCfgDel(key)
  72. return true
  73. }
  74. func (sysCfgDb *SysCfgDb) SysCfgGetWithDb(HKey string) string {
  75. cacheKey := fmt.Sprintf(md.AppCfgCacheKey, HKey[0:1])
  76. get, err := cache.HGetString(cacheKey, HKey)
  77. if err != nil || get == "" {
  78. cfg, err := sysCfgDb.SysCfgGetOne(HKey)
  79. if err != nil || cfg == nil {
  80. _ = logx.Error(err)
  81. return ""
  82. }
  83. // key是否存在
  84. cacheKeyExist := false
  85. if cache.Exists(cacheKey) {
  86. cacheKeyExist = true
  87. }
  88. // 设置缓存
  89. _, err = cache.HSet(cacheKey, HKey, cfg.V)
  90. if err != nil {
  91. _ = logx.Error(err)
  92. return ""
  93. }
  94. if !cacheKeyExist { // 如果是首次设置 设置过期时间
  95. _, err := cache.Expire(cacheKey, md.CfgCacheTime)
  96. if err != nil {
  97. _ = logx.Error(err)
  98. return ""
  99. }
  100. }
  101. return cfg.V
  102. }
  103. return get
  104. }
  105. func (sysCfgDb *SysCfgDb) SysCfgDel(HKey string) error {
  106. cacheKey := fmt.Sprintf(md.AppCfgCacheKey, HKey[0:1])
  107. _, err := cache.HDel(cacheKey, HKey)
  108. if err != nil {
  109. return err
  110. }
  111. return nil
  112. }
  113. func (sysCfgDb *SysCfgDb) SysCfgFindWithDb(keys ...string) map[string]string {
  114. res := map[string]string{}
  115. for _, v := range keys {
  116. val := sysCfgDb.SysCfgGetWithDb(v)
  117. res[v] = val
  118. }
  119. return res
  120. }