mirror of
https://github.com/ArvinLovegood/go-stock.git
synced 2025-07-19 00:00:09 +08:00
- 增加对配置存在的检查,如果存在则更新,不存在则创建默认配置 - 添加日志记录,当配置不存在时创建默认配置的情况 - 通过 Where 子句指定 ID 进行更新,提高更新操作的准确性
54 lines
1.2 KiB
Go
54 lines
1.2 KiB
Go
package data
|
|
|
|
import (
|
|
"go-stock/backend/db"
|
|
"go-stock/backend/logger"
|
|
"gorm.io/gorm"
|
|
)
|
|
|
|
type Settings struct {
|
|
gorm.Model
|
|
LocalPushEnable bool `json:"localPushEnable"`
|
|
DingPushEnable bool `json:"dingPushEnable"`
|
|
DingRobot string `json:"dingRobot"`
|
|
}
|
|
|
|
func (receiver Settings) TableName() string {
|
|
return "settings"
|
|
}
|
|
|
|
type SettingsApi struct {
|
|
Config Settings
|
|
}
|
|
|
|
func NewSettingsApi(settings *Settings) *SettingsApi {
|
|
return &SettingsApi{
|
|
Config: *settings,
|
|
}
|
|
}
|
|
|
|
func (s SettingsApi) UpdateConfig() string {
|
|
count := int64(0)
|
|
db.Dao.Model(s.Config).Count(&count)
|
|
if count > 0 {
|
|
db.Dao.Model(s.Config).Where("id=?", s.Config.ID).Updates(map[string]any{
|
|
"local_push_enable": s.Config.LocalPushEnable,
|
|
"ding_push_enable": s.Config.DingPushEnable,
|
|
"ding_robot": s.Config.DingRobot,
|
|
})
|
|
} else {
|
|
logger.SugaredLogger.Infof("未找到配置,创建默认配置:%+v", s.Config)
|
|
db.Dao.Model(s.Config).Create(&Settings{
|
|
LocalPushEnable: s.Config.LocalPushEnable,
|
|
DingPushEnable: s.Config.DingPushEnable,
|
|
DingRobot: s.Config.DingRobot,
|
|
})
|
|
}
|
|
return "保存成功!"
|
|
}
|
|
func (s SettingsApi) GetConfig() *Settings {
|
|
var settings Settings
|
|
db.Dao.Model(&Settings{}).First(&settings)
|
|
return &settings
|
|
}
|