|
- package svc
-
- import (
- "context"
- "fmt"
- "gim/internal/business/comm/db"
- "gim/internal/business/comm/md"
- "gim/internal/business/comm/utils"
- db2 "gim/pkg/db"
- "gim/pkg/grpclib"
- )
-
- // 单挑记录获取
- func SysCfgGet(masterId, key string) string {
- return db.DbSysCfg.SysCfgGetWithDb(masterId, key)
- }
-
- // 多条记录获取
- func SysCfgFind(ctx context.Context, keys ...string) map[string]string {
- masterId, _ := grpclib.GetCtxMasterId(ctx)
- tmp := SysCfgFindComm(masterId, keys...)
- return tmp
- }
-
- // SysCfgGetByMasterId get one config by master id
- func SysCfgGetByMasterId(masterId, key string) string {
- res := SysCfgFindComm(masterId, key)
- if _, ok := res[key]; !ok {
- return ""
- }
- return res[key]
- }
-
- // SysCfgFindComm get cfg by master id
- func SysCfgFindComm(masterId string, keys ...string) map[string]string {
- res := map[string]string{}
- cfgKey := fmt.Sprintf("%s:gim_cfg_cache", masterId)
- err := db2.RedisUtil.GetJson(cfgKey, &res)
- if err != nil || len(res) == 0 {
- cfgList, _ := db.DbSysCfg.SysCfgGetAll(utils.StrToInt64(masterId))
- if cfgList == nil {
- return nil
- }
- for _, v := range *cfgList {
- res[v.Key] = v.Val
- }
- db2.RedisUtil.SetJson(cfgKey, res, 1800)
- }
- if len(keys) == 0 {
- return res
- }
- tmp := map[string]string{}
- for _, v := range keys {
- if val, ok := res[v]; ok {
- tmp[v] = val
- } else {
- tmp[v] = ""
- }
- }
- return tmp
- }
-
- // EgSysCfgFind 多条记录获取
- func EgSysCfgFind(masterId string, keys ...string) map[string]string {
- res := map[string]string{}
- if len(res) == 0 {
- cfgList, _ := db.DbSysCfg.SysCfgGetAll(utils.StrToInt64(masterId))
- if cfgList == nil {
- return nil
- }
- for _, v := range *cfgList {
- res[v.Key] = v.Val
- }
- }
- if len(keys) == 0 {
- return res
- }
- tmp := map[string]string{}
- for _, v := range keys {
- if val, ok := res[v]; ok {
- tmp[v] = val
- } else {
- tmp[v] = ""
- }
- }
- return tmp
- }
-
- // SysCfgCleanCache 清理系统配置信息
- func SysCfgCleanCache() {
- db2.RedisUtil.Del(md.KEY_SYS_CFG_CACHE)
- }
-
- // SysCfgSet 写入系统设置
- func SysCfgSet(ctx context.Context, key, val, memo string) bool {
- masterId, _ := grpclib.GetCtxMasterId(ctx)
- cfg, err := db.DbSysCfg.SysCfgGetOne(key, utils.StrToInt64(masterId))
- if err != nil || cfg == nil {
- return db.DbSysCfg.SysCfgInsert(key, val, memo, utils.StrToInt64(masterId))
- }
- if memo != "" && cfg.Memo != memo {
- cfg.Memo = memo
- }
- SysCfgCleanCache()
- return db.DbSysCfg.SysCfgUpdate(key, val, cfg.Memo, utils.StrToInt64(masterId))
- }
|