Files
nl_cms-api/internal/dao/config_methods.go

161 lines
4.4 KiB
Go
Raw Normal View History

2025-07-29 12:45:07 +08:00
package dao
import (
"context"
"cms-api/internal/model"
)
// GetByKey 根据配置键获取配置
func (dao *ConfigDao) GetByKey(ctx context.Context, key string) (*model.Config, error) {
var config *model.Config
err := dao.Ctx(ctx).Where(dao.Columns().ConfigKey, key).Scan(&config)
if err != nil {
return nil, err
}
return config, nil
}
// List 获取配置列表
func (dao *ConfigDao) List(ctx context.Context, req *model.ConfigListRequest) ([]*model.Config, int, error) {
var (
configs []*model.Config
total int
db = dao.Ctx(ctx)
)
// 添加查询条件
if req.GroupName != "" {
db = db.Where(dao.Columns().GroupName, req.GroupName)
}
if req.IsSystem >= 0 {
db = db.Where(dao.Columns().IsSystem, req.IsSystem)
}
if req.Keyword != "" {
db = db.Where(dao.Columns().ConfigKey+" LIKE ? OR "+dao.Columns().Description+" LIKE ?", "%"+req.Keyword+"%", "%"+req.Keyword+"%")
}
// 获取总数
count, err := db.Count()
if err != nil {
return nil, 0, err
}
total = count
// 分页查询
offset := (req.Page - 1) * req.PageSize
err = db.Order(dao.Columns().GroupName + " ASC, " + dao.Columns().SortOrder + " ASC").
Limit(req.PageSize).
Offset(offset).
Scan(&configs)
if err != nil {
return nil, 0, err
}
return configs, total, nil
}
// GetByGroup 根据分组获取配置
func (dao *ConfigDao) GetByGroup(ctx context.Context, groupName string) ([]*model.Config, error) {
var configs []*model.Config
err := dao.Ctx(ctx).Where(dao.Columns().GroupName, groupName).Order(dao.Columns().SortOrder + " ASC").Scan(&configs)
if err != nil {
return nil, err
}
return configs, nil
}
// GetGroups 获取所有配置分组
func (dao *ConfigDao) GetGroups(ctx context.Context) ([]string, error) {
var groups []string
err := dao.Ctx(ctx).Fields("DISTINCT " + dao.Columns().GroupName).Scan(&groups)
if err != nil {
return nil, err
}
return groups, nil
}
// Create 创建配置
func (dao *ConfigDao) Create(ctx context.Context, config *model.Config) (int64, error) {
result, err := dao.Ctx(ctx).Data(config).Insert()
if err != nil {
return 0, err
}
id, err := result.LastInsertId()
if err != nil {
return 0, err
}
return id, nil
}
// Update 更新配置 - 根据ID更新
func (dao *ConfigDao) Update(ctx context.Context, id int, data interface{}) error {
_, err := dao.Ctx(ctx).Where(dao.Columns().Id, id).Data(data).Update()
return err
}
// UpdateByKey 更新配置 - 根据配置键更新
func (dao *ConfigDao) UpdateByKey(ctx context.Context, key string, data interface{}) error {
_, err := dao.Ctx(ctx).Where(dao.Columns().ConfigKey, key).Data(data).Update()
return err
}
// UpdateValue 更新配置值
func (dao *ConfigDao) UpdateValue(ctx context.Context, key string, value string) error {
_, err := dao.Ctx(ctx).Where(dao.Columns().ConfigKey, key).Data(map[string]interface{}{
dao.Columns().ConfigValue: value,
}).Update()
return err
}
// Delete 删除配置 - 根据ID删除
func (dao *ConfigDao) Delete(ctx context.Context, id int) error {
_, err := dao.Ctx(ctx).Where(dao.Columns().Id, id).Delete()
return err
}
// DeleteByKey 删除配置 - 根据配置键删除
func (dao *ConfigDao) DeleteByKey(ctx context.Context, key string) error {
_, err := dao.Ctx(ctx).Where(dao.Columns().ConfigKey, key).Delete()
return err
}
// GetById 根据ID获取配置
func (dao *ConfigDao) GetById(ctx context.Context, id int) (*model.Config, error) {
var config *model.Config
err := dao.Ctx(ctx).Where(dao.Columns().Id, id).Scan(&config)
if err != nil {
return nil, err
}
return config, nil
}
// BatchUpdate 批量更新配置
func (dao *ConfigDao) BatchUpdate(ctx context.Context, configs map[string]string) error {
for key, value := range configs {
err := dao.UpdateValue(ctx, key, value)
if err != nil {
return err
}
}
return nil
}
// GetSystemConfigs 获取系统配置
func (dao *ConfigDao) GetSystemConfigs(ctx context.Context) ([]*model.Config, error) {
var configs []*model.Config
err := dao.Ctx(ctx).Where(dao.Columns().IsSystem, 1).Order(dao.Columns().GroupName + " ASC, " + dao.Columns().SortOrder + " ASC").Scan(&configs)
if err != nil {
return nil, err
}
return configs, nil
}
// GetUserConfigs 获取用户配置
func (dao *ConfigDao) GetUserConfigs(ctx context.Context) ([]*model.Config, error) {
var configs []*model.Config
err := dao.Ctx(ctx).Where(dao.Columns().IsSystem, 0).Order(dao.Columns().GroupName + " ASC, " + dao.Columns().SortOrder + " ASC").Scan(&configs)
if err != nil {
return nil, err
}
return configs, nil
}