kefu/models/ent_configs.go

113 lines
3.1 KiB
Go
Raw Normal View History

2024-12-10 02:50:12 +00:00
package models
import (
"fmt"
"strings"
)
type EntConfig struct {
ID uint `gorm:"primary_key" json:"id"`
ConfName string `json:"conf_name"`
ConfKey string `json:"conf_key"`
ConfValue string `json:"conf_value"`
EntId string `json:"ent_id"`
}
func CreateMany(entId interface{}, configs []EntConfig) {
sql := "insert into ent_config (conf_name,conf_key,conf_value,ent_id) values "
for _, config := range configs {
sql += fmt.Sprintf("('%s','%s','%s','%v'),", config.ConfName, config.ConfKey, config.ConfValue, entId)
}
sql = strings.TrimRight(sql, ",")
Execute(sql)
GetEntConfigFromCache(entId, true)
}
func CreateEntConfig(kefuId interface{}, name, key, value string) {
c := &EntConfig{
ConfName: name,
ConfKey: key,
ConfValue: value,
EntId: fmt.Sprintf("%v", kefuId),
}
DB.Create(c)
GetEntConfigFromCache(kefuId, true)
}
func FindEntConfigs(kefuId interface{}) []EntConfig {
configs := GetEntConfigFromCache(kefuId, false)
//var configs []EntConfig
//DB.Where("ent_id = ?", kefuId).Find(&configs)
return configs
}
func FindEntConfigByEntid(entId interface{}) []EntConfig {
configs := GetEntConfigFromCache(entId, false)
//var configs []EntConfig
//DB.Where("ent_id = ?", entId).Find(&configs)
return configs
}
func FindEntConfig(entId interface{}, key string) EntConfig {
var config EntConfig
configs := GetEntConfigFromCache(entId, false)
for _, row := range configs {
if row.ConfKey == key {
return row
}
}
//DB.Where("ent_id = ? and conf_key=?", kefuId, key).Find(&config)
return config
}
func UpdateEntConfig(kefuId interface{}, name, key, value string) {
c := map[string]string{
"conf_name": name,
"conf_key": key,
"conf_value": value,
"ent_id": fmt.Sprintf("%v", kefuId),
}
DB.Model(&EntConfig{}).Where("ent_id = ? and conf_key = ?", fmt.Sprintf("%v", kefuId), key).Update(c)
GetEntConfigFromCache(kefuId, true)
}
// 更新,不存在的就插入
func UpdateInsertEntConfig(kefuId interface{}, name, key, value string) {
var res EntConfig
DB.Model(&EntConfig{}).Where("ent_id = ? and conf_key = ?", fmt.Sprintf("%v", kefuId), key).First(&res)
if res.ID == 0 {
CreateEntConfig(kefuId, name, key, value)
} else {
UpdateEntConfig(kefuId, name, key, value)
}
}
func DelEntConfig(query interface{}, args ...interface{}) {
DB.Model(&EntConfig{}).Where(query, args...).Delete(&EntConfig{})
GetEntConfigFromCache(args, true)
}
// 从缓存中获取配置数据
func GetEntConfigFromCache(entId interface{}, forceUpdate bool) []EntConfig {
cacheKey := fmt.Sprintf("configs_%v", entId)
configs, ok := DBcache.Get(cacheKey)
if !ok || forceUpdate {
var res []EntConfig
DB.Where("ent_id = ?", entId).Find(&res)
DBcache.Set(cacheKey, res, 0)
return res
}
return configs.([]EntConfig)
}
// 获取企业配置信息
func GetEntConfigsMap(entId string, keys ...string) map[string]string {
configs := FindEntConfigByEntid(entId)
result := make(map[string]string)
for _, key := range keys {
result[key] = ""
}
for _, config := range configs {
mapKey := config.ConfKey
if _, ok := result[mapKey]; ok {
result[mapKey] = config.ConfValue
}
}
return result
}