Files
EdgeAPI/internal/db/models/sys_setting_dao.go

265 lines
6.4 KiB
Go
Raw Normal View History

2020-09-26 08:06:40 +08:00
package models
import (
"encoding/json"
2020-09-26 08:06:40 +08:00
"fmt"
2024-07-27 14:15:25 +08:00
"strconv"
"time"
2021-12-14 12:44:57 +08:00
"github.com/TeaOSLab/EdgeAPI/internal/remotelogs"
2022-01-09 10:44:17 +08:00
"github.com/TeaOSLab/EdgeAPI/internal/utils"
2021-12-14 12:44:57 +08:00
"github.com/TeaOSLab/EdgeAPI/internal/zero"
"github.com/TeaOSLab/EdgeCommon/pkg/nodeconfigs"
2021-01-19 12:05:35 +08:00
"github.com/TeaOSLab/EdgeCommon/pkg/systemconfigs"
"github.com/TeaOSLab/EdgeCommon/pkg/userconfigs"
2020-09-26 08:06:40 +08:00
_ "github.com/go-sql-driver/mysql"
"github.com/iwind/TeaGo/Tea"
"github.com/iwind/TeaGo/dbs"
"github.com/iwind/TeaGo/types"
2020-09-26 08:06:40 +08:00
)
type SysSettingDAO dbs.DAO
func NewSysSettingDAO() *SysSettingDAO {
return dbs.NewDAO(&SysSettingDAO{
DAOObject: dbs.DAOObject{
DB: Tea.Env,
Table: "edgeSysSettings",
Model: new(SysSetting),
PkName: "id",
},
}).(*SysSettingDAO)
}
2020-10-13 20:05:13 +08:00
var SharedSysSettingDAO *SysSettingDAO
func init() {
dbs.OnReady(func() {
SharedSysSettingDAO = NewSysSettingDAO()
})
}
2020-09-26 08:06:40 +08:00
2021-09-14 15:27:48 +08:00
// UpdateSetting 设置配置
func (this *SysSettingDAO) UpdateSetting(tx *dbs.Tx, codeFormat string, valueJSON []byte, codeFormatArgs ...interface{}) error {
2020-10-02 17:22:32 +08:00
if len(codeFormatArgs) > 0 {
codeFormat = fmt.Sprintf(codeFormat, codeFormatArgs...)
2020-09-26 08:06:40 +08:00
}
countRetries := 3
var lastErr error
2021-12-14 12:44:57 +08:00
defer func() {
if lastErr == nil {
err := this.NotifyUpdate(tx, codeFormat)
if err != nil {
remotelogs.Error("SysSettingDAO", "notify update failed: "+err.Error())
}
}
}()
2020-09-26 08:06:40 +08:00
for i := 0; i < countRetries; i++ {
settingId, err := this.Query(tx).
2020-10-02 17:22:32 +08:00
Attr("code", codeFormat).
2020-09-26 08:06:40 +08:00
ResultPk().
FindInt64Col(0)
if err != nil {
return err
}
if settingId == 0 {
// 新建
var op = NewSysSettingOperator()
2020-10-02 17:22:32 +08:00
op.Code = codeFormat
2020-09-26 08:06:40 +08:00
op.Value = valueJSON
err = this.Save(tx, op)
2020-09-26 08:06:40 +08:00
if err != nil {
lastErr = err
// 因为错误的原因可能是因为code冲突所以这里我们继续执行
continue
}
2021-12-14 12:44:57 +08:00
lastErr = nil
2020-09-26 08:06:40 +08:00
return nil
}
// 修改
var op = NewSysSettingOperator()
2020-09-26 08:06:40 +08:00
op.Id = settingId
op.Value = valueJSON
err = this.Save(tx, op)
2020-09-26 08:06:40 +08:00
if err != nil {
return err
}
2021-12-14 12:44:57 +08:00
lastErr = nil
break
2020-09-26 08:06:40 +08:00
}
return lastErr
}
2021-09-14 15:27:48 +08:00
// ReadSetting 读取配置
func (this *SysSettingDAO) ReadSetting(tx *dbs.Tx, code string, codeFormatArgs ...interface{}) (valueJSON []byte, err error) {
if len(codeFormatArgs) > 0 {
code = fmt.Sprintf(code, codeFormatArgs...)
2020-09-26 08:06:40 +08:00
}
col, err := this.Query(tx).
2020-09-26 08:06:40 +08:00
Attr("code", code).
Result("value").
FindStringCol("")
return []byte(col), err
}
2021-09-14 15:27:48 +08:00
// CompareInt64Setting 对比配置中的数字大小
func (this *SysSettingDAO) CompareInt64Setting(tx *dbs.Tx, code string, anotherValue int64) (int8, error) {
valueJSON, err := this.ReadSetting(tx, code)
if err != nil {
return 0, err
}
value := types.Int64(string(valueJSON))
if value > anotherValue {
return 1, nil
}
if value < anotherValue {
return -1, nil
}
return 0, nil
}
2022-01-09 10:44:17 +08:00
// ReadAdminUIConfig 读取管理员界面配置
func (this *SysSettingDAO) ReadAdminUIConfig(tx *dbs.Tx, cacheMap *utils.CacheMap) (*systemconfigs.AdminUIConfig, error) {
var cacheKey = this.Table + ":ReadAdminUIConfig"
if cacheMap != nil {
cache, ok := cacheMap.Get(cacheKey)
if ok && cache != nil {
return cache.(*systemconfigs.AdminUIConfig), nil
}
}
valueJSON, err := this.ReadSetting(tx, systemconfigs.SettingCodeAdminUIConfig)
if err != nil {
return nil, err
}
if len(valueJSON) > 0 {
var config = &systemconfigs.AdminUIConfig{}
err = json.Unmarshal(valueJSON, config)
if err != nil {
return nil, err
}
if cacheMap != nil {
cacheMap.Put(cacheKey, config)
}
return config, nil
}
return &systemconfigs.AdminUIConfig{}, nil
}
// ReadProductName 读取设置的产品名称
func (this *SysSettingDAO) ReadProductName(tx *dbs.Tx) (string, error) {
productName, err := this.Query(tx).
Attr("code", systemconfigs.SettingCodeAdminUIConfig).
Result("JSON_EXTRACT(value, '$.productName')").
FindStringCol("")
if err != nil {
return "", err
}
if len(productName) > 0 {
return strconv.Unquote(productName)
}
return "", nil
}
2022-11-04 17:39:53 +08:00
// ReadUserUIConfig 读取用户UI配置
func (this *SysSettingDAO) ReadUserUIConfig(tx *dbs.Tx) (*systemconfigs.UserUIConfig, error) {
valueJSON, err := this.ReadSetting(tx, systemconfigs.SettingCodeUserUIConfig)
if err != nil {
return nil, err
}
if len(valueJSON) == 0 {
2024-03-10 16:26:03 +08:00
return systemconfigs.NewUserUIConfig(), nil
2022-11-04 17:39:53 +08:00
}
2024-03-10 16:26:03 +08:00
var config = systemconfigs.NewUserUIConfig()
2022-11-04 17:39:53 +08:00
err = json.Unmarshal(valueJSON, config)
if err != nil {
return nil, err
}
return config, nil
}
2021-12-14 12:44:57 +08:00
// NotifyUpdate 通知更改
func (this *SysSettingDAO) NotifyUpdate(tx *dbs.Tx, code string) error {
switch code {
case systemconfigs.SettingCodeAccessLogQueue:
accessLogQueueChanged <- zero.New()
case systemconfigs.SettingCodeAdminUIConfig:
// 修改当前时区
config, err := this.ReadAdminUIConfig(nil, nil)
if err == nil && config != nil {
if len(config.TimeZone) == 0 {
config.TimeZone = nodeconfigs.DefaultTimeZoneLocation
}
location, err := time.LoadLocation(config.TimeZone)
if err == nil && time.Local != location {
time.Local = location
}
}
2021-12-14 12:44:57 +08:00
}
return nil
}
2022-10-14 10:03:29 +08:00
// ReadUserServerConfig 读取用户服务配置
func (this *SysSettingDAO) ReadUserServerConfig(tx *dbs.Tx) (*userconfigs.UserServerConfig, error) {
valueJSON, err := this.ReadSetting(tx, systemconfigs.SettingCodeUserServerConfig)
if err != nil {
return nil, err
}
if len(valueJSON) == 0 {
return userconfigs.DefaultUserServerConfig(), nil
}
var config = userconfigs.DefaultUserServerConfig()
err = json.Unmarshal(valueJSON, config)
if err != nil {
return nil, err
}
return config, nil
}
// ReadUserRegisterConfig 读取用户注册配置
func (this *SysSettingDAO) ReadUserRegisterConfig(tx *dbs.Tx) (*userconfigs.UserRegisterConfig, error) {
valueJSON, err := this.ReadSetting(tx, systemconfigs.SettingCodeUserRegisterConfig)
if err != nil {
return nil, err
}
if len(valueJSON) == 0 {
return userconfigs.DefaultUserRegisterConfig(), nil
}
var config = userconfigs.DefaultUserRegisterConfig()
err = json.Unmarshal(valueJSON, config)
if err != nil {
return nil, err
}
return config, nil
}
2023-07-01 17:54:40 +08:00
func (this *SysSettingDAO) ReadDatabaseConfig(tx *dbs.Tx) (config *systemconfigs.DatabaseConfig, err error) {
valueJSON, err := this.ReadSetting(tx, systemconfigs.SettingCodeDatabaseConfigSetting)
if err != nil {
return nil, err
}
if len(valueJSON) == 0 {
return systemconfigs.NewDatabaseConfig(), nil
}
config = systemconfigs.NewDatabaseConfig()
err = json.Unmarshal(valueJSON, config)
if err != nil {
return nil, err
}
return config, nil
}