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.

db_sys_cfg.go 2.7 KiB

3 days ago
2 days ago
3 days ago
3 days ago
3 days ago
3 days ago
3 days ago
3 days ago
3 days ago
3 days ago
3 days ago
3 days ago
3 days ago
3 days ago
3 days ago
3 days ago
3 days ago
3 days ago
3 days ago
3 days ago
3 days ago
3 days ago
3 days ago
3 days ago
3 days ago
3 days ago
3 days ago
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118
  1. package db
  2. import (
  3. "egg-im/internal/business/comm/db/model"
  4. "egg-im/internal/business/comm/md"
  5. "egg-im/internal/business/comm/utils/logx"
  6. "egg-im/pkg/db"
  7. "egg-im/pkg/gerrors"
  8. "fmt"
  9. )
  10. type dbSysCfg struct{}
  11. var DbSysCfg = new(dbSysCfg)
  12. // SysCfgGetAll 获取系统配置
  13. func (*dbSysCfg) SysCfgGetAll() (*[]model.SysCfg, error) {
  14. var cfgList []model.SysCfg
  15. if err := db.DB.Where("1=1").Find(&cfgList).Error; err != nil {
  16. return nil, gerrors.WrapError(err)
  17. }
  18. return &cfgList, nil
  19. }
  20. // SysCfgGetOne 获取一条记录
  21. func (*dbSysCfg) SysCfgGetOne(key string) (*model.SysCfg, error) {
  22. var cfgList model.SysCfg
  23. if err := db.DB.Where("`key` = ?", key).First(&cfgList).Error; err != nil {
  24. return nil, gerrors.WrapError(err)
  25. }
  26. return &cfgList, nil
  27. }
  28. // SysCfgInsert 插入一条配置信息
  29. func (*dbSysCfg) SysCfgInsert(key, val, memo string) bool {
  30. cfg := model.SysCfg{Key: key, Val: val, Memo: memo}
  31. err := db.DB.Create(&cfg).Error
  32. if err != nil {
  33. logx.Error(err)
  34. return false
  35. }
  36. return true
  37. }
  38. // SysCfgUpdate 修改某条配置信息
  39. func (*dbSysCfg) SysCfgUpdate(key, val, memo string) bool {
  40. cfg := model.SysCfg{Key: key, Val: val, Memo: memo}
  41. err := db.DB.Where("`key` = ?", key).Update(&cfg).Error
  42. if err != nil {
  43. logx.Error(err)
  44. return false
  45. }
  46. return true
  47. }
  48. // SysCfgGetWithDb 单条记录获取
  49. func (*dbSysCfg) SysCfgGetWithDb(HKey string) string {
  50. cacheKey := fmt.Sprintf(md.AppCfgCacheKey, HKey[0:1])
  51. get, err := db.RedisUtil.HGetString(cacheKey, HKey)
  52. if err != nil || get == "" {
  53. cfg, err := DbSysCfg.SysCfgGetOne(HKey)
  54. if err != nil || cfg == nil {
  55. _ = logx.Error(err)
  56. return ""
  57. }
  58. // key是否存在
  59. cacheKeyExist := false
  60. if db.RedisUtil.Exists(cacheKey) {
  61. cacheKeyExist = true
  62. }
  63. // 设置缓存
  64. _, err = db.RedisUtil.HSet(cacheKey, HKey, cfg.Val)
  65. if err != nil {
  66. _ = logx.Error(err)
  67. return ""
  68. }
  69. if !cacheKeyExist { // 如果是首次设置 设置过期时间
  70. _, err := db.RedisUtil.Expire(cacheKey, md.CfgCacheTime)
  71. if err != nil {
  72. _ = logx.Error(err)
  73. return ""
  74. }
  75. }
  76. return cfg.Val
  77. }
  78. return get
  79. }
  80. // SysCfgFindWithDb 多条记录获取
  81. func (*dbSysCfg) SysCfgFindWithDb(keys ...string) map[string]string {
  82. res := map[string]string{}
  83. cacheKey := fmt.Sprintf("egg_im_cfg_cache")
  84. err := db.RedisUtil.GetJson(cacheKey, &res)
  85. if err != nil || len(res) == 0 {
  86. cfgList, _ := DbSysCfg.SysCfgGetAll()
  87. if cfgList == nil {
  88. return nil
  89. }
  90. for _, v := range *cfgList {
  91. res[v.Key] = v.Val
  92. }
  93. db.RedisUtil.SetJson(cacheKey, res, md.CfgCacheTime)
  94. }
  95. if len(keys) == 0 {
  96. return res
  97. }
  98. tmp := map[string]string{}
  99. for _, v := range keys {
  100. if val, ok := res[v]; ok {
  101. tmp[v] = val
  102. } else {
  103. tmp[v] = ""
  104. }
  105. }
  106. return tmp
  107. }