feat(publish): 增加等级与特殊皮肤价格修正
This commit is contained in:
@@ -222,9 +222,7 @@ func normalizePublishOptions(options *PublishOptionsDTO) {
|
||||
if len(options.RegionOptions) == 0 {
|
||||
options.RegionOptions = defaults.RegionOptions
|
||||
}
|
||||
if len(options.SkinGroups) == 0 {
|
||||
options.SkinGroups = defaults.SkinGroups
|
||||
}
|
||||
normalizePublishOptionGroups(options, defaults.SkinGroups)
|
||||
if len(options.QuantityItems) == 0 {
|
||||
options.QuantityItems = defaults.QuantityItems
|
||||
}
|
||||
@@ -276,6 +274,10 @@ func normalizePublishOptions(options *PublishOptionsDTO) {
|
||||
if len(options.RatioConfig.InsuranceBaseRatios) == 0 {
|
||||
options.RatioConfig.InsuranceBaseRatios = defaults.RatioConfig.InsuranceBaseRatios
|
||||
}
|
||||
if options.RatioConfig.LevelRatioRules == nil {
|
||||
options.RatioConfig.LevelRatioRules = defaults.RatioConfig.LevelRatioRules
|
||||
}
|
||||
normalizeLevelRatioRules(options.RatioConfig.LevelRatioRules)
|
||||
if len(options.RatioConfig.ConfigItems) == 0 {
|
||||
options.RatioConfig.ConfigItems = defaults.RatioConfig.ConfigItems
|
||||
}
|
||||
@@ -287,6 +289,92 @@ func normalizePublishOptions(options *PublishOptionsDTO) {
|
||||
)
|
||||
}
|
||||
|
||||
// NormalizePublishOptions 为其他模块提供与公开接口一致的配置兼容归一化。
|
||||
func NormalizePublishOptions(options *PublishOptionsDTO) {
|
||||
normalizePublishOptions(options)
|
||||
}
|
||||
|
||||
func normalizePublishOptionGroups(options *PublishOptionsDTO, defaults []PublishOptionGroup) {
|
||||
if len(options.SkinGroups) == 0 {
|
||||
options.SkinGroups = clonePublishOptionGroups(defaults)
|
||||
return
|
||||
}
|
||||
defaultByKey := make(map[string]PublishOptionGroup, len(defaults))
|
||||
for _, group := range defaults {
|
||||
defaultByKey[group.Key] = group
|
||||
}
|
||||
for index := range options.SkinGroups {
|
||||
group := &options.SkinGroups[index]
|
||||
group.Key = strings.TrimSpace(group.Key)
|
||||
group.Title = strings.TrimSpace(group.Title)
|
||||
group.Options = normalizeStringValues(group.Options)
|
||||
if group.SpecialPriceOptions == nil {
|
||||
if fallback, ok := defaultByKey[group.Key]; ok {
|
||||
group.SpecialPriceOptions = append([]string(nil), fallback.SpecialPriceOptions...)
|
||||
group.SpecialRatioAdjustment = fallback.SpecialRatioAdjustment
|
||||
} else {
|
||||
group.SpecialPriceOptions = []string{}
|
||||
group.SpecialRatioAdjustment = -1
|
||||
}
|
||||
} else {
|
||||
group.SpecialPriceOptions = normalizeStringValues(group.SpecialPriceOptions)
|
||||
group.SpecialRatioAdjustment = normalizeSignedRatioAdjustment(
|
||||
group.SpecialRatioAdjustment,
|
||||
)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
func normalizeLevelRatioRules(rules []PublishLevelRatioRule) {
|
||||
for index := range rules {
|
||||
rule := &rules[index]
|
||||
rule.Key = strings.TrimSpace(rule.Key)
|
||||
rule.Field = strings.ToLower(strings.TrimSpace(rule.Field))
|
||||
rule.Level = strings.TrimSpace(rule.Level)
|
||||
rule.RatioAdjustment = normalizeSignedRatioAdjustment(rule.RatioAdjustment)
|
||||
}
|
||||
}
|
||||
|
||||
func normalizeStringValues(values []string) []string {
|
||||
result := make([]string, 0, len(values))
|
||||
seen := make(map[string]struct{}, len(values))
|
||||
for _, value := range values {
|
||||
value = strings.TrimSpace(value)
|
||||
if value == "" {
|
||||
continue
|
||||
}
|
||||
if _, ok := seen[value]; ok {
|
||||
continue
|
||||
}
|
||||
seen[value] = struct{}{}
|
||||
result = append(result, value)
|
||||
}
|
||||
return result
|
||||
}
|
||||
|
||||
func clonePublishOptionGroups(groups []PublishOptionGroup) []PublishOptionGroup {
|
||||
result := make([]PublishOptionGroup, len(groups))
|
||||
for index, group := range groups {
|
||||
result[index] = group
|
||||
result[index].Options = append([]string(nil), group.Options...)
|
||||
result[index].SpecialPriceOptions = append([]string(nil), group.SpecialPriceOptions...)
|
||||
}
|
||||
return result
|
||||
}
|
||||
|
||||
func normalizeSignedRatioAdjustment(value float64) float64 {
|
||||
if math.IsNaN(value) || math.IsInf(value, 0) {
|
||||
return 0
|
||||
}
|
||||
if value < -10 {
|
||||
return -10
|
||||
}
|
||||
if value > 10 {
|
||||
return 10
|
||||
}
|
||||
return math.Round(value*10) / 10
|
||||
}
|
||||
|
||||
func normalizePremiumReviewRatioMarkup(value float64) float64 {
|
||||
if value < 0 || math.IsNaN(value) || math.IsInf(value, 0) {
|
||||
return 0
|
||||
|
||||
Reference in New Issue
Block a user