golang-im聊天
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 line
3.0 KiB

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