golang-im聊天
Du kannst nicht mehr als 25 Themen auswählen Themen müssen entweder mit einem Buchstaben oder einer Ziffer beginnen. Sie können Bindestriche („-“) enthalten und bis zu 35 Zeichen lang sein.
 
 
 
 

107 Zeilen
2.4 KiB

  1. package svc
  2. import (
  3. "context"
  4. "fmt"
  5. "gim/internal/business/comm/db"
  6. "gim/internal/business/comm/md"
  7. "gim/internal/business/comm/utils"
  8. db2 "gim/pkg/db"
  9. "gim/pkg/grpclib"
  10. )
  11. // 单挑记录获取
  12. func SysCfgGet(masterId, key string) string {
  13. return db.DbSysCfg.SysCfgGetWithDb(masterId, key)
  14. }
  15. // 多条记录获取
  16. func SysCfgFind(ctx context.Context, keys ...string) map[string]string {
  17. masterId, _ := grpclib.GetCtxMasterId(ctx)
  18. tmp := SysCfgFindComm(masterId, keys...)
  19. return tmp
  20. }
  21. // SysCfgGetByMasterId get one config by master id
  22. func SysCfgGetByMasterId(masterId, key string) string {
  23. res := SysCfgFindComm(masterId, key)
  24. if _, ok := res[key]; !ok {
  25. return ""
  26. }
  27. return res[key]
  28. }
  29. // SysCfgFindComm get cfg by master id
  30. func SysCfgFindComm(masterId string, keys ...string) map[string]string {
  31. res := map[string]string{}
  32. cfgKey := fmt.Sprintf("%s:gim_cfg_cache", masterId)
  33. err := db2.RedisUtil.GetJson(cfgKey, &res)
  34. if err != nil || len(res) == 0 {
  35. cfgList, _ := db.DbSysCfg.SysCfgGetAll(utils.StrToInt64(masterId))
  36. if cfgList == nil {
  37. return nil
  38. }
  39. for _, v := range *cfgList {
  40. res[v.Key] = v.Val
  41. }
  42. db2.RedisUtil.SetJson(cfgKey, res, 1800)
  43. }
  44. if len(keys) == 0 {
  45. return res
  46. }
  47. tmp := map[string]string{}
  48. for _, v := range keys {
  49. if val, ok := res[v]; ok {
  50. tmp[v] = val
  51. } else {
  52. tmp[v] = ""
  53. }
  54. }
  55. return tmp
  56. }
  57. // EgSysCfgFind 多条记录获取
  58. func EgSysCfgFind(masterId string, keys ...string) map[string]string {
  59. res := map[string]string{}
  60. if len(res) == 0 {
  61. cfgList, _ := db.DbSysCfg.SysCfgGetAll(utils.StrToInt64(masterId))
  62. if cfgList == nil {
  63. return nil
  64. }
  65. for _, v := range *cfgList {
  66. res[v.Key] = v.Val
  67. }
  68. }
  69. if len(keys) == 0 {
  70. return res
  71. }
  72. tmp := map[string]string{}
  73. for _, v := range keys {
  74. if val, ok := res[v]; ok {
  75. tmp[v] = val
  76. } else {
  77. tmp[v] = ""
  78. }
  79. }
  80. return tmp
  81. }
  82. // SysCfgCleanCache 清理系统配置信息
  83. func SysCfgCleanCache() {
  84. db2.RedisUtil.Del(md.KEY_SYS_CFG_CACHE)
  85. }
  86. // SysCfgSet 写入系统设置
  87. func SysCfgSet(ctx context.Context, key, val, memo string) bool {
  88. masterId, _ := grpclib.GetCtxMasterId(ctx)
  89. cfg, err := db.DbSysCfg.SysCfgGetOne(key, utils.StrToInt64(masterId))
  90. if err != nil || cfg == nil {
  91. return db.DbSysCfg.SysCfgInsert(key, val, memo, utils.StrToInt64(masterId))
  92. }
  93. if memo != "" && cfg.Memo != memo {
  94. cfg.Memo = memo
  95. }
  96. SysCfgCleanCache()
  97. return db.DbSysCfg.SysCfgUpdate(key, val, cfg.Memo, utils.StrToInt64(masterId))
  98. }