143 lines
3.9 KiB
Go
143 lines
3.9 KiB
Go
package trafficcutoffconfig
|
|
|
|
import (
|
|
"context"
|
|
"strings"
|
|
"time"
|
|
|
|
"github.com/gogf/gf/v2/errors/gerror"
|
|
"github.com/gogf/gf/v2/os/gtime"
|
|
|
|
"git.5iot.com/iotlcm-public/iotlcm-worker-svr/internal/consts"
|
|
"git.5iot.com/iotlcm-public/iotlcm-worker-svr/internal/model/entity"
|
|
)
|
|
|
|
const (
|
|
noDetailsReason = "达量断网配置批次没有可处理明细"
|
|
validationFailureReason = "部分达量断网配置校验失败"
|
|
emptyICCIDReason = "ICCID不能为空"
|
|
duplicateICCIDReason = "ICCID在批次内重复"
|
|
unauthorizedReason = "ICCID不在当前操作员授权范围内"
|
|
cardMissingReason = "ICCID不存在或不属于当前租户"
|
|
msisdnMismatchReason = "MSISDN与卡档案不一致"
|
|
imsiMismatchReason = "IMSI与卡档案不一致"
|
|
flowPoolCardReason = "流量池卡不支持达量断网配置"
|
|
packageTotalExceededReason = "达量断网阈值不能大于套餐总流量"
|
|
)
|
|
|
|
type executor struct {
|
|
repository *Repository
|
|
}
|
|
|
|
const trafficCutoffConfigProcessingRetryAfter = 2 * time.Minute
|
|
|
|
func NewExecutor() *executor {
|
|
return &executor{repository: NewRepository()}
|
|
}
|
|
|
|
func (e *executor) CanExecuteProcessing(_ context.Context, main *entity.BatchMain) (bool, error) {
|
|
if main == nil || main.BatchType != consts.BatchTypeTrafficCutoffConfig || main.BatchAsyncStatus != consts.BatchAsyncStatusProcessing {
|
|
return false, nil
|
|
}
|
|
if main.StartTime == nil || main.StartTime.IsZero() {
|
|
return true, nil
|
|
}
|
|
return gtime.Now().Sub(main.StartTime) >= trafficCutoffConfigProcessingRetryAfter, nil
|
|
}
|
|
|
|
func (e *executor) Execute(ctx context.Context, batchNo string) error {
|
|
task, err := e.repository.Load(ctx, strings.TrimSpace(batchNo))
|
|
if err != nil {
|
|
return gerror.Wrap(err, "load traffic cutoff config batch failed")
|
|
}
|
|
validateTask(task)
|
|
return gerror.Wrap(e.repository.Commit(ctx, task), "commit traffic cutoff config batch failed")
|
|
}
|
|
|
|
func validateTask(task *Task) {
|
|
if task == nil || len(task.Details) == 0 {
|
|
if task != nil {
|
|
task.FailureReason = noDetailsReason
|
|
}
|
|
return
|
|
}
|
|
|
|
iccidCounts := make(map[string]int, len(task.Details))
|
|
for _, detail := range task.Details {
|
|
if detail == nil {
|
|
continue
|
|
}
|
|
detail.ICCID = strings.TrimSpace(detail.ICCID)
|
|
detail.MSISDN = strings.TrimSpace(detail.MSISDN)
|
|
detail.IMSI = strings.TrimSpace(detail.IMSI)
|
|
if detail.ICCID != "" {
|
|
iccidCounts[detail.ICCID]++
|
|
}
|
|
}
|
|
|
|
for _, detail := range task.Details {
|
|
validateDetail(task, detail)
|
|
if detail != nil && detail.Reason == "" && iccidCounts[detail.ICCID] > 1 {
|
|
detail.Reason = duplicateICCIDReason
|
|
}
|
|
}
|
|
_, failCount := countResult(task)
|
|
if failCount > 0 {
|
|
task.FailureReason = validationFailureReason
|
|
} else {
|
|
task.FailureReason = ""
|
|
}
|
|
}
|
|
|
|
func validateDetail(task *Task, detail *Detail) {
|
|
if detail == nil || detail.Reason != "" {
|
|
return
|
|
}
|
|
if detail.ICCID == "" {
|
|
detail.Reason = emptyICCIDReason
|
|
return
|
|
}
|
|
if !detail.Authorized {
|
|
detail.Reason = unauthorizedReason
|
|
return
|
|
}
|
|
if detail.Card == nil {
|
|
detail.Reason = cardMissingReason
|
|
return
|
|
}
|
|
detail.Card.ICCID = strings.TrimSpace(detail.Card.ICCID)
|
|
detail.Card.MSISDN = strings.TrimSpace(detail.Card.MSISDN)
|
|
detail.Card.IMSI = strings.TrimSpace(detail.Card.IMSI)
|
|
if detail.MSISDN != "" && detail.MSISDN != detail.Card.MSISDN {
|
|
detail.Reason = msisdnMismatchReason
|
|
return
|
|
}
|
|
if detail.IMSI != "" && detail.IMSI != detail.Card.IMSI {
|
|
detail.Reason = imsiMismatchReason
|
|
return
|
|
}
|
|
if detail.Card.IsFlowPool != 0 {
|
|
detail.Reason = flowPoolCardReason
|
|
return
|
|
}
|
|
if task.Config.TrafficCutoffStatus == 1 &&
|
|
detail.Card.PackageTotalFlowMb > 0 &&
|
|
task.Config.TrafficCutoffThreshold > detail.Card.PackageTotalFlowMb {
|
|
detail.Reason = packageTotalExceededReason
|
|
}
|
|
}
|
|
|
|
func countResult(task *Task) (successCount, failCount int) {
|
|
if task == nil {
|
|
return 0, 0
|
|
}
|
|
for _, detail := range task.Details {
|
|
if detail != nil && detail.Success() {
|
|
successCount++
|
|
} else {
|
|
failCount++
|
|
}
|
|
}
|
|
return successCount, failCount
|
|
}
|