Files

63 lines
2.2 KiB
Go
Raw Permalink Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
package systemconfig
import (
"context"
"strconv"
"strings"
"hfb_sys/backend/internal/modules/rentergrowth"
)
func (s *Service) Update(ctx context.Context, actorID uint64, key string, req UpdateRequest, meta AuditMeta) (*ConfigDTO, error) {
if s.repo == nil {
return nil, ErrDependencyUnavailable
}
// 备份定时只能通过独立接口修改,避免拥有普通系统配置权限的管理员改变备份策略。
if strings.HasPrefix(key, "backup.") {
return nil, ErrInvalidConfig
}
if key == "" || (req.Value == "" && key != "integration.paddle_ocr_token") {
return nil, ErrInvalidConfig
}
if key == "listing.publish_cooldown_minutes" {
minutes, err := strconv.Atoi(strings.TrimSpace(req.Value))
if err != nil || minutes < 0 {
return nil, ErrInvalidConfig
}
}
if key == rentergrowth.ConfigKey {
cfg, err := rentergrowth.ParseConfigValue(req.Value)
if err != nil {
return nil, ErrInvalidConfig
}
value, err := rentergrowth.MarshalConfig(cfg)
if err != nil {
return nil, ErrInvalidConfig
}
req.Value = value
}
return s.repo.Update(ctx, actorID, key, req, meta)
}
func (s *Service) UpdateBackupSchedule(ctx context.Context, actorID uint64, req BackupScheduleDTO, meta AuditMeta) (*BackupScheduleDTO, error) {
if s.repo == nil || req.FullHour < 0 || req.FullHour > 23 || req.FullMinute < 0 || req.FullMinute > 59 || req.BinlogIntervalMinutes < 1 || req.BinlogIntervalMinutes > 5 {
return nil, ErrInvalidConfig
}
updates := []struct {
key string
value string
description string
}{
{"backup.schedule_enabled", strconv.FormatBool(req.Enabled), "在线备份定时任务是否启用"},
{"backup.full_hour", strconv.Itoa(req.FullHour), "每日全量备份小时(Asia/Shanghai"},
{"backup.full_minute", strconv.Itoa(req.FullMinute), "每日全量备份分钟(Asia/Shanghai"},
{"backup.binlog_interval_minutes", strconv.Itoa(req.BinlogIntervalMinutes), "binlog 归档间隔分钟(1-5"},
}
for _, item := range updates {
if _, err := s.repo.Update(ctx, actorID, item.key, UpdateRequest{Value: item.value, Description: item.description}, meta); err != nil {
return nil, err
}
}
return &req, nil
}