chore: 并入 iotlcm-upstream-chan 历史到 upstream-chan/(原提交 1e74497)
This commit is contained in:
@@ -0,0 +1,82 @@
|
||||
package mingqian
|
||||
|
||||
import (
|
||||
"context"
|
||||
"math"
|
||||
"strings"
|
||||
|
||||
"github.com/gogf/gf/v2/errors/gerror"
|
||||
"git.5iot.com/iotlcm-public/iotlcm-upstream-chan/internal/consts"
|
||||
"git.5iot.com/iotlcm-public/iotlcm-upstream-chan/internal/model"
|
||||
)
|
||||
|
||||
type PackageFlowOutput = []*model.SimBaseRes[model.SimPackageFlowItem]
|
||||
|
||||
func (adapter *Adapter) QueryPackageFlow(ctx context.Context, input *model.SimDataUsageInput) (PackageFlowOutput, error) {
|
||||
if input == nil {
|
||||
return nil, gerror.New("mingqian: input required")
|
||||
}
|
||||
return query(ctx, adapter, input.SimKeys, packageUsagePath, 50, func(item card) (model.SimPackageFlowItem, error) {
|
||||
if item.PackageTotal == nil || item.PackageUsage == nil || *item.PackageTotal < 0 || *item.PackageUsage < 0 {
|
||||
return model.SimPackageFlowItem{}, gerror.New("mingqian: package total or cumulative usage missing or unknown")
|
||||
}
|
||||
var (
|
||||
total = float64(*item.PackageTotal)
|
||||
used = float64(*item.PackageUsage)
|
||||
remaining = math.Max(total-used, 0)
|
||||
)
|
||||
return model.SimPackageFlowItem{TotalFlow: total, DataUsage: used, RemainingFlow: remaining}, nil
|
||||
}), nil
|
||||
}
|
||||
|
||||
func QueryChannelsPackageFlow(ctx context.Context, input *model.SimDataUsageInput, lookup func(string) (*model.SimCardChannelCache, bool)) (PackageFlowOutput, error) {
|
||||
if input == nil || lookup == nil {
|
||||
return nil, gerror.New("mingqian: input and channel lookup required")
|
||||
}
|
||||
var (
|
||||
output = make(PackageFlowOutput, len(input.SimKeys))
|
||||
groups = make(map[string][]int)
|
||||
)
|
||||
for index, key := range input.SimKeys {
|
||||
output[index] = &model.SimBaseRes[model.SimPackageFlowItem]{SimKey: key, Code: consts.ApiStatusError, Message: "mingqian: explicit chanKey and ICCID or MSISDN required"}
|
||||
if key == nil || strings.TrimSpace(key.ChanKey) == "" || identifier(key) == "" {
|
||||
continue
|
||||
}
|
||||
channelKey := strings.TrimSpace(key.ChanKey)
|
||||
groups[channelKey] = append(groups[channelKey], index)
|
||||
}
|
||||
for channelKey, indices := range groups {
|
||||
var (
|
||||
adapter *Adapter
|
||||
message string
|
||||
)
|
||||
cached, ok := lookup(channelKey)
|
||||
if !ok || cached == nil || cached.Instance == nil {
|
||||
message = "mingqian: channel not instantiated"
|
||||
} else {
|
||||
adapter, ok = cached.Instance.(*Adapter)
|
||||
if !ok || adapter == nil {
|
||||
message = "mingqian: package flow endpoint only supports sim_mingqian_v3 channels"
|
||||
}
|
||||
}
|
||||
if message != "" {
|
||||
for _, index := range indices {
|
||||
output[index].Message = message
|
||||
}
|
||||
continue
|
||||
}
|
||||
keys := make([]*model.SimKey, len(indices))
|
||||
for position, index := range indices {
|
||||
keys[position] = input.SimKeys[index]
|
||||
}
|
||||
results, err := adapter.QueryPackageFlow(ctx, &model.SimDataUsageInput{SimKeys: keys})
|
||||
for position, index := range indices {
|
||||
if err != nil {
|
||||
output[index].Message = err.Error()
|
||||
} else {
|
||||
output[index] = results[position]
|
||||
}
|
||||
}
|
||||
}
|
||||
return output, nil
|
||||
}
|
||||
Reference in New Issue
Block a user