彻底优化数据库字段
This commit is contained in:
@@ -3,6 +3,8 @@ package listing
|
||||
import (
|
||||
"encoding/json"
|
||||
"errors"
|
||||
"math"
|
||||
"regexp"
|
||||
"strconv"
|
||||
"strings"
|
||||
)
|
||||
@@ -15,6 +17,7 @@ var (
|
||||
ErrMissingServerRegion = errors.New("missing server region")
|
||||
ErrInvalidPrice = errors.New("invalid listing price")
|
||||
ErrInvalidDeposit = errors.New("invalid listing deposit")
|
||||
ErrDepositTooLow = errors.New("listing deposit too low")
|
||||
ErrInvalidHafCoin = errors.New("invalid haf coin amount")
|
||||
ErrMissingScreenshot = errors.New("missing screenshot")
|
||||
)
|
||||
@@ -34,6 +37,8 @@ const (
|
||||
defaultFireLevelMin = 38
|
||||
)
|
||||
|
||||
var priceNumberPattern = regexp.MustCompile(`(\d+(?:\.\d+)?)`)
|
||||
|
||||
type FireLevelTooLowError struct {
|
||||
Min int
|
||||
}
|
||||
@@ -217,6 +222,9 @@ func validateRequest(req CreateRequest, rules publishRules) error {
|
||||
if req.DepositAmount < 0 {
|
||||
return ErrInvalidDeposit
|
||||
}
|
||||
if consumables := consumableValue(req.AssetSummary); consumables > 0 && req.DepositAmount <= consumables {
|
||||
return ErrDepositTooLow
|
||||
}
|
||||
if req.HafCoinAmount < 0 {
|
||||
return ErrInvalidHafCoin
|
||||
}
|
||||
@@ -229,6 +237,85 @@ func validateRequest(req CreateRequest, rules publishRules) error {
|
||||
return nil
|
||||
}
|
||||
|
||||
func consumableValue(summary map[string]any) float64 {
|
||||
if summary == nil {
|
||||
return 0
|
||||
}
|
||||
rawResources, ok := summary["resources"]
|
||||
if !ok {
|
||||
return 0
|
||||
}
|
||||
resources, ok := rawResources.([]any)
|
||||
if !ok {
|
||||
return 0
|
||||
}
|
||||
total := 0.0
|
||||
for _, raw := range resources {
|
||||
resource, ok := raw.(map[string]any)
|
||||
if !ok {
|
||||
continue
|
||||
}
|
||||
mode, _ := resource["mode"].(string)
|
||||
if strings.TrimSpace(mode) != "收费" {
|
||||
continue
|
||||
}
|
||||
quantity := readSummaryFloat(resource["quantity"])
|
||||
if quantity <= 0 {
|
||||
continue
|
||||
}
|
||||
priceText, _ := resource["price"].(string)
|
||||
total += quantity * readUnitPrice(priceText)
|
||||
}
|
||||
return roundMoney(total)
|
||||
}
|
||||
|
||||
func readSummaryFloat(value any) float64 {
|
||||
switch current := value.(type) {
|
||||
case float64:
|
||||
return current
|
||||
case int:
|
||||
return float64(current)
|
||||
case int64:
|
||||
return float64(current)
|
||||
case json.Number:
|
||||
parsed, err := current.Float64()
|
||||
if err != nil {
|
||||
return 0
|
||||
}
|
||||
return parsed
|
||||
case string:
|
||||
parsed, err := strconv.ParseFloat(strings.TrimSpace(current), 64)
|
||||
if err != nil {
|
||||
return 0
|
||||
}
|
||||
return parsed
|
||||
default:
|
||||
return 0
|
||||
}
|
||||
}
|
||||
|
||||
func readUnitPrice(priceText string) float64 {
|
||||
numbers := priceNumberPattern.FindAllString(priceText, -1)
|
||||
if len(numbers) == 0 {
|
||||
return 0
|
||||
}
|
||||
amount, err := strconv.ParseFloat(numbers[0], 64)
|
||||
if err != nil {
|
||||
return 0
|
||||
}
|
||||
if len(numbers) >= 2 {
|
||||
count, err := strconv.ParseFloat(numbers[1], 64)
|
||||
if err == nil && count > 0 {
|
||||
return amount / count
|
||||
}
|
||||
}
|
||||
return amount
|
||||
}
|
||||
|
||||
func roundMoney(value float64) float64 {
|
||||
return math.Round(value*100) / 100
|
||||
}
|
||||
|
||||
func readFireLevel(summary map[string]any) (int, bool) {
|
||||
if summary == nil {
|
||||
return 0, false
|
||||
|
||||
Reference in New Issue
Block a user