// Code scaffolded by goctl. Safe to edit. // goctl 1.10.2 package applicationconfig import ( "context" "errors" "strings" "udeServer/internal/apierr" "udeServer/internal/audit" "udeServer/internal/log" "udeServer/internal/model" "udeServer/internal/svc" "udeServer/internal/types" "github.com/zeromicro/go-zero/core/logx" ) type UpdateConfigLogic struct { logx.Logger ctx context.Context svcCtx *svc.ServiceContext } // 更新配置(默认配置 edit_flag=1 不允许改配置项名) func NewUpdateConfigLogic(ctx context.Context, svcCtx *svc.ServiceContext) *UpdateConfigLogic { return &UpdateConfigLogic{ Logger: logx.WithContext(ctx), ctx: ctx, svcCtx: svcCtx, } } func (l *UpdateConfigLogic) UpdateConfig(req *types.ConfigUpdateReq) (*types.ConfigUpdateResp, error) { if err := checkId(req.Id); err != nil { return nil, err } req.ConfigItem = trimSpace(req.ConfigItem) if req.ConfigItem == "" { return nil, apierr.BadRequest("配置项不能为空") } if strings.TrimSpace(req.ConfigValue) == "" { return nil, apierr.BadRequest("配置值不能为空") } config, err := l.svcCtx.ConfigModel.FindOne(l.ctx, req.Id) if errors.Is(err, model.ErrNotFound) { return nil, apierr.NotFound("config", req.Id) } if err != nil { log.RecordError(l.ctx, err) return nil, apierr.New(500, "查询系统配置失败") } // 默认配置只能改配置值,不允许换配置项名 if config.EditFlag == editFlagProhibited && config.ConfigItem != req.ConfigItem { return nil, apierr.BadRequest("ID:[%d]的默认配置不能被编辑", req.Id) } // 更新之前校验这个配置项是否已经存在 existing, err := l.svcCtx.ConfigModel.FindOneByConfigItem(l.ctx, req.ConfigItem) if err != nil && !errors.Is(err, model.ErrNotFound) { log.RecordError(l.ctx, err) return nil, apierr.New(500, "查询系统配置失败") } if existing != nil && existing.Id != req.Id { return nil, apierr.BadRequest("已存在同名:[%s]的应用配置项", req.ConfigItem) } if req.ConfigItem != "" { config.ConfigItem = req.ConfigItem } if req.ConfigValue != "" { config.ConfigValue = req.ConfigValue } config.Description = nullStr(req.Description) config.UpdatedBy = configOperatorName if err := l.svcCtx.ConfigModel.Update(l.ctx, config); err != nil { log.RecordError(l.ctx, err) return nil, apierr.New(500, "更新系统配置失败") } // 处于热加载白名单里的键,更新后重载 MinIO 客户端与审计日志配置 if containsConfigKey(reloadableKeys, req.ConfigItem) { minioClient, logConfig := audit.GetGlobalConfig(l.svcCtx.ConfigModel) l.svcCtx.MinioClient = minioClient l.svcCtx.LogConfigManager = logConfig } return &types.ConfigUpdateResp{Id: req.Id}, nil }