mirror of
https://github.com/ArvinLovegood/go-stock.git
synced 2025-07-19 00:00:09 +08:00
feat(settings): 优化配置更新逻辑
- 增加对配置存在的检查,如果存在则更新,不存在则创建默认配置 - 添加日志记录,当配置不存在时创建默认配置的情况 - 通过 Where 子句指定 ID 进行更新,提高更新操作的准确性
This commit is contained in:
parent
1fd149bbd5
commit
ce29514b54
@ -2,6 +2,7 @@ package data
|
||||
|
||||
import (
|
||||
"go-stock/backend/db"
|
||||
"go-stock/backend/logger"
|
||||
"gorm.io/gorm"
|
||||
)
|
||||
|
||||
@ -27,13 +28,21 @@ func NewSettingsApi(settings *Settings) *SettingsApi {
|
||||
}
|
||||
|
||||
func (s SettingsApi) UpdateConfig() string {
|
||||
err := db.Dao.Model(s.Config).Updates(map[string]any{
|
||||
"local_push_enable": s.Config.LocalPushEnable,
|
||||
"ding_push_enable": s.Config.DingPushEnable,
|
||||
"ding_robot": s.Config.DingRobot,
|
||||
}).Error
|
||||
if err != nil {
|
||||
return err.Error()
|
||||
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 "保存成功!"
|
||||
}
|
||||
|
Loading…
x
Reference in New Issue
Block a user