chore: 并入 iotlcm-upstream-chan 历史到 upstream-chan/(原提交 1e74497)
This commit is contained in:
@@ -0,0 +1,148 @@
|
||||
package upstreamimpl
|
||||
|
||||
import (
|
||||
"context"
|
||||
"fmt"
|
||||
"strings"
|
||||
"time"
|
||||
"git.5iot.com/iotlcm-public/iotlcm-upstream-chan/internal/model"
|
||||
)
|
||||
|
||||
const dxV3BatchLimit = 100
|
||||
|
||||
// dxV3BatchMonthAvailable 电信 v3 月批量接口仅支持最近六个含当月的月份。
|
||||
func dxV3BatchMonthAvailable(month time.Time) bool {
|
||||
now := time.Now().In(dxBatchLocation)
|
||||
current := time.Date(now.Year(), now.Month(), 1, 0, 0, 0, 0, dxBatchLocation)
|
||||
return !month.Before(current.AddDate(0, -5, 0)) && !month.After(current)
|
||||
}
|
||||
|
||||
func dxV3Batch[T any](ctx context.Context, s *sSimDXV3, keys []*model.SimKey, path, listName, dateName, date string,
|
||||
convert func(map[string]any) (T, error)) []*model.SimBaseRes[T] {
|
||||
output := make([]*model.SimBaseRes[T], len(keys))
|
||||
pending := make([]int, 0, len(keys))
|
||||
for index, key := range keys {
|
||||
if key == nil || strings.TrimSpace(key.MSISDN) == "" {
|
||||
output[index] = dxV3Failure[T](key, fmt.Errorf("telecom v3 batch requires msisdn"))
|
||||
continue
|
||||
}
|
||||
pending = append(pending, index)
|
||||
}
|
||||
for offset := 0; offset < len(pending); offset += dxV3BatchLimit {
|
||||
end := offset + dxV3BatchLimit
|
||||
if end > len(pending) {
|
||||
end = len(pending)
|
||||
}
|
||||
chunk := pending[offset:end]
|
||||
msisdns := make([]string, 0, len(chunk))
|
||||
for _, index := range chunk {
|
||||
msisdns = append(msisdns, strings.TrimSpace(keys[index].MSISDN))
|
||||
}
|
||||
body := map[string]any{"msisdnList": msisdns, "custNbr": s.custNbr}
|
||||
if dateName != "" {
|
||||
body[dateName] = date
|
||||
}
|
||||
var err error
|
||||
var result map[string]any
|
||||
if s.custNbr == "" {
|
||||
err = fmt.Errorf("telecom v3 custNbr not configured")
|
||||
} else {
|
||||
result, err = s.request(ctx, path, nil, body)
|
||||
}
|
||||
byNumber := make(map[string]map[string]any)
|
||||
if err == nil {
|
||||
list, ok := result[listName].([]any)
|
||||
if !ok {
|
||||
err = fmt.Errorf("telecom v3 result is missing %s", listName)
|
||||
} else {
|
||||
for _, entry := range list {
|
||||
item, ok := entry.(map[string]any)
|
||||
if !ok {
|
||||
err = fmt.Errorf("invalid telecom v3 batch entry")
|
||||
break
|
||||
}
|
||||
number := dxV3String(item, "msisdn")
|
||||
if number == "" {
|
||||
err = fmt.Errorf("telecom v3 batch entry is missing msisdn")
|
||||
break
|
||||
}
|
||||
if _, duplicate := byNumber[number]; duplicate {
|
||||
err = fmt.Errorf("duplicate telecom v3 batch result for msisdn")
|
||||
break
|
||||
}
|
||||
byNumber[number] = item
|
||||
}
|
||||
}
|
||||
}
|
||||
for _, index := range chunk {
|
||||
key := keys[index]
|
||||
itemErr := err
|
||||
item := byNumber[strings.TrimSpace(key.MSISDN)]
|
||||
if itemErr == nil && item == nil {
|
||||
itemErr = fmt.Errorf("telecom v3 batch omitted requested card")
|
||||
}
|
||||
if itemErr == nil {
|
||||
code := dxV3String(item, "result")
|
||||
if code == "" {
|
||||
itemErr = fmt.Errorf("telecom v3 batch entry is missing result")
|
||||
} else if code != "0" {
|
||||
itemErr = &dxV3Error{code: code, message: firstNonEmpty(dxV3String(item, "resultDesc"), "telecom v3 card query failed")}
|
||||
}
|
||||
}
|
||||
var value T
|
||||
if itemErr == nil && dateName != "" && dxV3String(item, dateName) != date {
|
||||
itemErr = fmt.Errorf("telecom v3 batch returned a different query period")
|
||||
}
|
||||
if itemErr == nil {
|
||||
value, itemErr = convert(item)
|
||||
}
|
||||
if itemErr != nil {
|
||||
output[index] = dxV3Failure[T](key, itemErr)
|
||||
} else {
|
||||
output[index] = &model.SimBaseRes[T]{SimKey: key, ResData: value}
|
||||
}
|
||||
}
|
||||
}
|
||||
return output
|
||||
}
|
||||
|
||||
func (s *sSimDXV3) QuerySimCardStatusBatch(ctx context.Context, input *model.SimCardStatusInput) (model.SimCardStatusOutput, error) {
|
||||
if input == nil {
|
||||
return model.SimCardStatusOutput{}, nil
|
||||
}
|
||||
return dxV3Batch(ctx, s, input.SimKeys, "getSimStatusBatch", "simStatusList", "", "", dxV3Status), nil
|
||||
}
|
||||
|
||||
func (s *sSimDXV3) QuerySimDataUsageMonthlyBatch(ctx context.Context, input *model.SimDataUsageMonthlyInput) (model.SimDataUsageMonthlyOutput, error) {
|
||||
if input == nil {
|
||||
return model.SimDataUsageMonthlyOutput{}, nil
|
||||
}
|
||||
month, err := time.ParseInLocation("200601", input.ReqData.QueryDate, dxBatchLocation)
|
||||
if err != nil {
|
||||
return nil, fmt.Errorf("invalid queryDate: expected yyyyMM")
|
||||
}
|
||||
if !dxV3BatchMonthAvailable(month) {
|
||||
return nil, fmt.Errorf("telecom v3 monthly batch supports the latest six months")
|
||||
}
|
||||
return dxV3Batch(ctx, s, input.SimKeys, "getDataUsageByMonthBatch", "batchTrafficList", "month", input.ReqData.QueryDate,
|
||||
func(item map[string]any) (model.SimDataUsageMonthlyOutputItem, error) {
|
||||
amount, err := dxV3Amount(item)
|
||||
return model.SimDataUsageMonthlyOutputItem{DataAmount: amount}, err
|
||||
}), nil
|
||||
}
|
||||
|
||||
func (s *sSimDXV3) QuerySimDataUsageDailyBatch(ctx context.Context, input *model.SimDataUsageDailyInput) (model.SimDataUsageDailyOutput, error) {
|
||||
if input == nil {
|
||||
return model.SimDataUsageDailyOutput{}, nil
|
||||
}
|
||||
if !dxBatchDayAvailable(input.ReqData.QueryDate, time.Now()) {
|
||||
return nil, fmt.Errorf("telecom v3 daily batch requires a completed day within the latest seven days")
|
||||
}
|
||||
return dxV3Batch(ctx, s, input.SimKeys, "getDataUsageByDayBatch", "batchTrafficList", "date", input.ReqData.QueryDate,
|
||||
func(item map[string]any) (model.SimDataUsageDailyOutputItem, error) {
|
||||
amount, err := dxV3Amount(item)
|
||||
return model.SimDataUsageDailyOutputItem{QueryDate: input.ReqData.QueryDate, DataAmount: amount}, err
|
||||
}), nil
|
||||
}
|
||||
|
||||
var _ model.SimCardBatchQueryI = (*sSimDXV3)(nil)
|
||||
Reference in New Issue
Block a user