增加渠道上传预统计
This commit is contained in:
@@ -5,11 +5,14 @@ import (
|
||||
"encoding/json"
|
||||
"fmt"
|
||||
"math"
|
||||
"regexp"
|
||||
"strconv"
|
||||
"strings"
|
||||
"time"
|
||||
)
|
||||
|
||||
var externalOnlineTimePattern = regexp.MustCompile(`^\D*(\d{1,2})(?:\s*[::点.]\s*(\d{1,2}))?\D+(\d{1,2})(?:\s*[::点.]\s*(\d{1,2}))?\D*$`)
|
||||
|
||||
func (s *Service) ImportExternalUpload(ctx context.Context, req ExternalUploadRequest, meta ExternalUploadMeta) (*ExternalUploadResponse, error) {
|
||||
if s.repo == nil {
|
||||
return nil, ErrDependencyUnavailable
|
||||
@@ -18,6 +21,7 @@ func (s *Service) ImportExternalUpload(ctx context.Context, req ExternalUploadRe
|
||||
if uploaderName == "" {
|
||||
return nil, ErrMissingUploaderName
|
||||
}
|
||||
sourceChannel := req.normalizedSourceChannel()
|
||||
items, err := parseExternalUploadItems(req.Data)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
@@ -45,6 +49,7 @@ func (s *Service) ImportExternalUpload(ctx context.Context, req ExternalUploadRe
|
||||
parsedPayload, _ := json.Marshal(item)
|
||||
dto, err := s.repo.CreateFromExternalUpload(ctx, externalUploadCreate{
|
||||
UploaderName: uploaderName,
|
||||
SourceChannel: sourceChannel,
|
||||
ClientUploadTime: clientUploadTime,
|
||||
ClientIP: meta.IP,
|
||||
RawPayload: meta.RawPayload,
|
||||
@@ -85,6 +90,17 @@ func (s *Service) ImportExternalUpload(ctx context.Context, req ExternalUploadRe
|
||||
return resp, nil
|
||||
}
|
||||
|
||||
func normalizeExternalSourceChannel(value string) string {
|
||||
value = strings.TrimSpace(value)
|
||||
if value == "" {
|
||||
return ""
|
||||
}
|
||||
if len([]rune(value)) > 32 {
|
||||
return string([]rune(value)[:32])
|
||||
}
|
||||
return value
|
||||
}
|
||||
|
||||
func parseExternalUploadItems(raw json.RawMessage) ([]ExternalAccountData, error) {
|
||||
if len(raw) == 0 || strings.TrimSpace(string(raw)) == "" || string(raw) == "null" {
|
||||
return nil, ErrMissingUploadData
|
||||
@@ -228,6 +244,7 @@ func externalAccountToCreateRequest(uploaderName string, uploadTime int64, item
|
||||
loadLevel := levelText(item.DailyConsumption.Weight)
|
||||
skins := cleanStrings(item.Inventory.Skins)
|
||||
remark := strings.TrimSpace(item.Remark)
|
||||
onlineTimeText := normalizeExternalOnlineTimeText(item.OwnerOnlineTime)
|
||||
assetSummary := map[string]any{
|
||||
"face_owner": "",
|
||||
"secret_kd": item.SecretKD,
|
||||
@@ -239,7 +256,7 @@ func externalAccountToCreateRequest(uploaderName string, uploadTime int64, item
|
||||
"load_level": loadLevel,
|
||||
"resources": externalResources(item.Inventory),
|
||||
"skin_groups": externalSkinGroups(skins),
|
||||
"online_time_text": strings.TrimSpace(item.OwnerOnlineTime),
|
||||
"online_time_text": onlineTimeText,
|
||||
"ban_record": normalizeBanRecord(item.BanRecord),
|
||||
"common_regions": commonRegions(item.CommonRegion),
|
||||
"remark": remark,
|
||||
@@ -261,6 +278,12 @@ func externalAccountToCreateRequest(uploaderName string, uploadTime int64, item
|
||||
"platform_rule_type": "external_upload",
|
||||
},
|
||||
}
|
||||
if start, end, ok := parseExternalOnlineTimeRange(onlineTimeText); ok {
|
||||
assetSummary["online_time"] = map[string]any{
|
||||
"start": start,
|
||||
"end": end,
|
||||
}
|
||||
}
|
||||
return CreateRequest{
|
||||
Title: externalUploadTitle(item, insurance, hafCoinM),
|
||||
Description: remark,
|
||||
@@ -275,6 +298,49 @@ func externalAccountToCreateRequest(uploaderName string, uploadTime int64, item
|
||||
}
|
||||
}
|
||||
|
||||
func normalizeExternalOnlineTimeText(value string) string {
|
||||
value = strings.TrimSpace(value)
|
||||
if value == "" {
|
||||
return ""
|
||||
}
|
||||
start, end, ok := parseExternalOnlineTimeRange(value)
|
||||
if !ok {
|
||||
return value
|
||||
}
|
||||
return start + " 至 " + end
|
||||
}
|
||||
|
||||
func parseExternalOnlineTimeRange(value string) (string, string, bool) {
|
||||
matches := externalOnlineTimePattern.FindStringSubmatch(strings.TrimSpace(value))
|
||||
if len(matches) != 5 {
|
||||
return "", "", false
|
||||
}
|
||||
start, ok := normalizeExternalTimePart(matches[1], matches[2])
|
||||
if !ok {
|
||||
return "", "", false
|
||||
}
|
||||
end, ok := normalizeExternalTimePart(matches[3], matches[4])
|
||||
if !ok {
|
||||
return "", "", false
|
||||
}
|
||||
return start, end, true
|
||||
}
|
||||
|
||||
func normalizeExternalTimePart(hourText, minuteText string) (string, bool) {
|
||||
hour, err := strconv.Atoi(strings.TrimSpace(hourText))
|
||||
if err != nil || hour < 0 || hour > 23 {
|
||||
return "", false
|
||||
}
|
||||
minute := 0
|
||||
if strings.TrimSpace(minuteText) != "" {
|
||||
minute, err = strconv.Atoi(strings.TrimSpace(minuteText))
|
||||
if err != nil || minute < 0 || minute > 59 {
|
||||
return "", false
|
||||
}
|
||||
}
|
||||
return fmt.Sprintf("%02d:%02d", hour, minute), true
|
||||
}
|
||||
|
||||
func externalUploadTitle(item ExternalAccountData, insurance string, hafCoinM float64) string {
|
||||
parts := []string{
|
||||
strings.TrimSpace(item.Rank),
|
||||
@@ -402,7 +468,6 @@ func normalizeBanRecord(value string) string {
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
func commonRegions(value string) []string {
|
||||
return cleanStrings(strings.Split(value, ","))
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user