updateconfiglogic.go 2.7 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394
  1. // Code scaffolded by goctl. Safe to edit.
  2. // goctl 1.10.2
  3. package applicationconfig
  4. import (
  5. "context"
  6. "errors"
  7. "strings"
  8. "udeServer/internal/apierr"
  9. "udeServer/internal/audit"
  10. "udeServer/internal/log"
  11. "udeServer/internal/model"
  12. "udeServer/internal/svc"
  13. "udeServer/internal/types"
  14. "github.com/zeromicro/go-zero/core/logx"
  15. )
  16. type UpdateConfigLogic struct {
  17. logx.Logger
  18. ctx context.Context
  19. svcCtx *svc.ServiceContext
  20. }
  21. // 更新配置(默认配置 edit_flag=1 不允许改配置项名)
  22. func NewUpdateConfigLogic(ctx context.Context, svcCtx *svc.ServiceContext) *UpdateConfigLogic {
  23. return &UpdateConfigLogic{
  24. Logger: logx.WithContext(ctx),
  25. ctx: ctx,
  26. svcCtx: svcCtx,
  27. }
  28. }
  29. func (l *UpdateConfigLogic) UpdateConfig(req *types.ConfigUpdateReq) (*types.ConfigUpdateResp, error) {
  30. if err := checkId(req.Id); err != nil {
  31. return nil, err
  32. }
  33. req.ConfigItem = trimSpace(req.ConfigItem)
  34. if req.ConfigItem == "" {
  35. return nil, apierr.BadRequest("配置项不能为空")
  36. }
  37. if strings.TrimSpace(req.ConfigValue) == "" {
  38. return nil, apierr.BadRequest("配置值不能为空")
  39. }
  40. config, err := l.svcCtx.ConfigModel.FindOne(l.ctx, req.Id)
  41. if errors.Is(err, model.ErrNotFound) {
  42. return nil, apierr.NotFound("config", req.Id)
  43. }
  44. if err != nil {
  45. log.RecordError(l.ctx, err)
  46. return nil, apierr.New(500, "查询系统配置失败")
  47. }
  48. // 默认配置只能改配置值,不允许换配置项名
  49. if config.EditFlag == editFlagProhibited && config.ConfigItem != req.ConfigItem {
  50. return nil, apierr.BadRequest("ID:[%d]的默认配置不能被编辑", req.Id)
  51. }
  52. // 更新之前校验这个配置项是否已经存在
  53. existing, err := l.svcCtx.ConfigModel.FindOneByConfigItem(l.ctx, req.ConfigItem)
  54. if err != nil && !errors.Is(err, model.ErrNotFound) {
  55. log.RecordError(l.ctx, err)
  56. return nil, apierr.New(500, "查询系统配置失败")
  57. }
  58. if existing != nil && existing.Id != req.Id {
  59. return nil, apierr.BadRequest("已存在同名:[%s]的应用配置项", req.ConfigItem)
  60. }
  61. if req.ConfigItem != "" {
  62. config.ConfigItem = req.ConfigItem
  63. }
  64. if req.ConfigValue != "" {
  65. config.ConfigValue = req.ConfigValue
  66. }
  67. config.Description = nullStr(req.Description)
  68. config.UpdatedBy = configOperatorName
  69. if err := l.svcCtx.ConfigModel.Update(l.ctx, config); err != nil {
  70. log.RecordError(l.ctx, err)
  71. return nil, apierr.New(500, "更新系统配置失败")
  72. }
  73. // 处于热加载白名单里的键,更新后重载 MinIO 客户端与审计日志配置
  74. if containsConfigKey(reloadableKeys, req.ConfigItem) {
  75. minioClient, logConfig := audit.GetGlobalConfig(l.svcCtx.ConfigModel)
  76. l.svcCtx.MinioClient = minioClient
  77. l.svcCtx.LogConfigManager = logConfig
  78. }
  79. return &types.ConfigUpdateResp{Id: req.Id}, nil
  80. }