96b1edff45
iotlcm-upstream-chan:运营商上游通道适配服务(统一 /sim/* 协议)。 - 内联原私有依赖:yunze-public → internal/pub/(10 包 18 文件), yunze-pb/pb → internal/pb/(9 文件);Consul 注册改用官方 gogf/gf/contrib/registry/consul/v2(API 兼容) - Go module:yz-svr → git.5iot.com/iotlcm-public/iotlcm-upstream-chan - 修复 mingqian 包测试:实现重构后 `adapter.operations` 字段已移除, 测试断言按当前实现语义重写(不改产品行为) - 凭据与内部标识脱敏:MySQL/Redis/Consul 凭据、私网与生产 IP、 内网域名、真实手机号、注释里的真实 token/transid - 移除内部资产:CI 触发器、生产通道种子 SQL、真实凭据测试资源文档 - Dockerfile 改用公共基础镜像,移除写死的 registry 凭据 - 补 Apache-2.0 LICENSE,重写 README,LF 行尾 + .gitattributes
149 lines
5.1 KiB
Go
149 lines
5.1 KiB
Go
package upstreamimpl
|
|
|
|
import (
|
|
"context"
|
|
"fmt"
|
|
"git.5iot.com/iotlcm-public/iotlcm-upstream-chan/internal/model"
|
|
"strings"
|
|
"time"
|
|
)
|
|
|
|
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)
|