package title import ( "bytes" "encoding/json" "fmt" ) func (s *service) ValidateEffectParams(effectType int32, raw string) (string, error) { dec := json.NewDecoder(bytes.NewBufferString(raw)) dec.DisallowUnknownFields() switch effectType { case 1: var p struct { TemplateID int64 `json:"template_id"` Frequency struct { Period string `json:"period"` Times int `json:"times"` } `json:"frequency"` } if err := dec.Decode(&p); err != nil { return "", err } if p.TemplateID < 0 { return "", fmt.Errorf("template_id无效") } if p.Frequency.Times < 1 || p.Frequency.Times > 100 { return "", fmt.Errorf("times范围错误") } if p.Frequency.Period != "day" && p.Frequency.Period != "week" && p.Frequency.Period != "month" { return "", fmt.Errorf("period无效") } b, _ := json.Marshal(p); return string(b), nil case 2: var p struct { DiscountType string `json:"discount_type"` ValueX1000 int32 `json:"value_x1000"` MaxDiscountX1000 int32 `json:"max_discount_x1000"` } if err := dec.Decode(&p); err != nil { return "", err } if p.DiscountType != "percentage" && p.DiscountType != "fixed" { return "", fmt.Errorf("discount_type无效") } if p.ValueX1000 < 0 || p.MaxDiscountX1000 < 0 { return "", fmt.Errorf("数值必须>=0") } b, _ := json.Marshal(p); return string(b), nil case 3: var p struct { MultiplierX1000 int32 `json:"multiplier_x1000"` DailyCapPoints int32 `json:"daily_cap_points"` } if err := dec.Decode(&p); err != nil { return "", err } if p.MultiplierX1000 < 0 || p.DailyCapPoints < 0 { return "", fmt.Errorf("数值必须>=0") } b, _ := json.Marshal(p); return string(b), nil case 4: var p struct { TemplateID int64 `json:"template_id"` Frequency struct { Period string `json:"period"` Times int `json:"times"` } `json:"frequency"` } if err := dec.Decode(&p); err != nil { return "", err } if p.TemplateID < 0 { return "", fmt.Errorf("template_id无效") } if p.Frequency.Times < 1 || p.Frequency.Times > 100 { return "", fmt.Errorf("times范围错误") } if p.Frequency.Period != "week" && p.Frequency.Period != "month" { return "", fmt.Errorf("period无效") } b, _ := json.Marshal(p); return string(b), nil case 5: var p struct { TargetPrizeIDs []int64 `json:"target_prize_ids"` BoostX1000 int32 `json:"boost_x1000"` CapX1000 *int32 `json:"cap_x1000"` } if err := dec.Decode(&p); err != nil { return "", err } if p.BoostX1000 < 0 || p.BoostX1000 > 100000 { return "", fmt.Errorf("boost_x1000范围错误") } if p.CapX1000 != nil && *p.CapX1000 < 0 { return "", fmt.Errorf("cap_x1000必须>=0") } if len(p.TargetPrizeIDs) > 200 { return "", fmt.Errorf("target_prize_ids数量过多") } m := make(map[int64]struct{}) out := make([]int64, 0, len(p.TargetPrizeIDs)) for _, id := range p.TargetPrizeIDs { if _, ok := m[id]; !ok { m[id] = struct{}{}; out = append(out, id) } } p.TargetPrizeIDs = out b, _ := json.Marshal(p); return string(b), nil case 6: var p struct { TargetPrizeIDs []int64 `json:"target_prize_ids"` ChanceX1000 int32 `json:"chance_x1000"` PeriodCapTimes *int32 `json:"period_cap_times"` } if err := dec.Decode(&p); err != nil { return "", err } if p.ChanceX1000 < 0 || p.ChanceX1000 > 100000 { return "", fmt.Errorf("chance_x1000范围错误") } if p.PeriodCapTimes != nil && *p.PeriodCapTimes < 0 { return "", fmt.Errorf("period_cap_times必须>=0") } if len(p.TargetPrizeIDs) > 200 { return "", fmt.Errorf("target_prize_ids数量过多") } m := make(map[int64]struct{}) out := make([]int64, 0, len(p.TargetPrizeIDs)) for _, id := range p.TargetPrizeIDs { if _, ok := m[id]; !ok { m[id] = struct{}{}; out = append(out, id) } } p.TargetPrizeIDs = out b, _ := json.Marshal(p); return string(b), nil default: return raw, nil } }