215 lines
5.8 KiB
Go
215 lines
5.8 KiB
Go
package warningconfig
|
|
|
|
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 warningConfigProcessingRetryAfter = 2 * time.Minute
|
|
|
|
type executor struct {
|
|
repository *Repository
|
|
}
|
|
|
|
func NewExecutor() *executor {
|
|
return &executor{repository: NewRepository()}
|
|
}
|
|
|
|
func (e *executor) CanExecuteProcessing(_ context.Context, main *entity.BatchMain) (bool, error) {
|
|
if main == nil || main.BatchType != consts.BatchTypeSimWarningConfig || main.BatchAsyncStatus != consts.BatchAsyncStatusProcessing {
|
|
return false, nil
|
|
}
|
|
if main.StartTime == nil || main.StartTime.IsZero() {
|
|
return true, nil
|
|
}
|
|
return gtime.Now().Sub(main.StartTime) >= warningConfigProcessingRetryAfter, 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 warning config batch failed")
|
|
}
|
|
validateTask(task)
|
|
return gerror.Wrap(e.repository.Commit(ctx, task), "commit warning config batch failed")
|
|
}
|
|
|
|
func validateTask(task *Task) {
|
|
if task == nil || len(task.Details) == 0 {
|
|
if task != nil {
|
|
task.FailureReason = "预警配置批次没有可处理明细"
|
|
}
|
|
return
|
|
}
|
|
for _, detail := range task.Details {
|
|
validateDetail(task, detail)
|
|
}
|
|
_, failCount := countResult(task)
|
|
if failCount > 0 {
|
|
markAtomicFailure(task, "预警配置批次存在失败明细,整批未执行")
|
|
}
|
|
}
|
|
|
|
func markAtomicFailure(task *Task, reason string) {
|
|
if task == nil {
|
|
return
|
|
}
|
|
reason = strings.TrimSpace(reason)
|
|
if reason == "" {
|
|
reason = "本批次存在失败明细,整批未执行"
|
|
}
|
|
task.FailureReason = reason
|
|
for _, detail := range task.Details {
|
|
if detail != nil && strings.TrimSpace(detail.Reason) == "" {
|
|
detail.Reason = "本批次存在失败明细,整批未执行"
|
|
}
|
|
}
|
|
}
|
|
|
|
func validateDetail(task *Task, detail *Detail) {
|
|
if detail == nil || detail.Reason != "" {
|
|
return
|
|
}
|
|
config := detail.Config
|
|
if config.ItemType == warnItemTypeFlowPool {
|
|
validateFlowPoolDetail(config, detail)
|
|
return
|
|
}
|
|
validateCardDetail(config, detail)
|
|
}
|
|
|
|
func validateCardDetail(config Config, detail *Detail) {
|
|
detail.ICCID = strings.TrimSpace(detail.ICCID)
|
|
detail.MSISDN = strings.TrimSpace(detail.MSISDN)
|
|
detail.IMSI = strings.TrimSpace(detail.IMSI)
|
|
if detail.ICCID == "" {
|
|
detail.Reason = "ICCID不能为空"
|
|
return
|
|
}
|
|
if !detail.Authorized {
|
|
detail.Reason = "ICCID不在当前操作员授权范围内"
|
|
return
|
|
}
|
|
if detail.Card == nil {
|
|
detail.Reason = "ICCID不存在或不属于当前租户"
|
|
return
|
|
}
|
|
if detail.MSISDN != "" && detail.MSISDN != detail.Card.MSISDN {
|
|
detail.Reason = "MSISDN与卡档案不一致"
|
|
return
|
|
}
|
|
if detail.IMSI != "" && detail.IMSI != detail.Card.IMSI {
|
|
detail.Reason = "IMSI与卡档案不一致"
|
|
return
|
|
}
|
|
if detail.Card.BossCardStatus == consts.SimBossCardStatusStopped {
|
|
detail.Reason = "卡已停机,不能配置预警"
|
|
return
|
|
}
|
|
if detail.Card.BossCardStatus != consts.SimBossCardStatusActivated {
|
|
detail.Reason = "当前卡状态为" + bossCardStatusText(detail.Card.BossCardStatus) + ",只有已激活卡可以配置预警"
|
|
return
|
|
}
|
|
if detail.Card.PackageStatus != packageStatusEffective {
|
|
detail.Reason = "套餐未生效,不能配置预警"
|
|
return
|
|
}
|
|
if detail.Card.IsFlowPool != 0 {
|
|
detail.Reason = "流量池卡暂不支持单卡预警配置"
|
|
return
|
|
}
|
|
if strings.TrimSpace(detail.Card.CurrentPackageCode) == "" {
|
|
detail.Reason = "当前套餐编号为空,不能配置预警"
|
|
return
|
|
}
|
|
switch config.WarnType {
|
|
case warnTypeCardTraffic:
|
|
validateTrafficDetail(config, detail)
|
|
case warnTypeExpireDay, warnTypeExpireMonth:
|
|
if detail.Card.FlowPkgEndAt == nil || detail.Card.FlowPkgEndAt.IsZero() {
|
|
detail.Reason = "套餐到期时间为空,不能配置到期预警"
|
|
}
|
|
default:
|
|
detail.Reason = "预警类型无效"
|
|
}
|
|
}
|
|
|
|
func validateFlowPoolDetail(config Config, detail *Detail) {
|
|
detail.FlowPoolCode = strings.TrimSpace(detail.FlowPoolCode)
|
|
if detail.FlowPoolCode == "" {
|
|
detail.Reason = "流量池编号不能为空"
|
|
return
|
|
}
|
|
if !detail.Authorized {
|
|
detail.Reason = "流量池不在当前操作员授权范围内"
|
|
return
|
|
}
|
|
if detail.Pool == nil {
|
|
detail.Reason = "流量池不存在或不属于当前租户"
|
|
return
|
|
}
|
|
if detail.Pool.PoolStatus != flowPoolStatusNormal {
|
|
detail.Reason = "流量池状态不是正常,不能配置预警"
|
|
return
|
|
}
|
|
if detail.Pool.TotalFlowMb <= 0 {
|
|
detail.Reason = "流量池总流量为空,不能配置预警"
|
|
return
|
|
}
|
|
if config.WarnType != warnTypeFlowPoolPercent || config.WarnUnit != warnUnitPercent {
|
|
detail.Reason = "流量池预警仅支持百分比配置"
|
|
return
|
|
}
|
|
if config.AutoAction != 0 {
|
|
detail.Reason = "流量池预警暂不支持第三次关停"
|
|
}
|
|
}
|
|
|
|
func validateTrafficDetail(config Config, detail *Detail) {
|
|
if detail.Card.PackageTotalFlowMb <= 0 {
|
|
detail.Reason = "套餐总流量为空,不能配置流量预警"
|
|
return
|
|
}
|
|
if config.WarnUnit != warnUnitMb {
|
|
return
|
|
}
|
|
total := detail.Card.PackageTotalFlowMb
|
|
if config.FirstWarn > total || config.SecondWarn > total || config.ThirdWarn > total {
|
|
detail.Reason = "流量阈值不能大于套餐总流量"
|
|
}
|
|
}
|
|
|
|
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
|
|
}
|
|
|
|
func bossCardStatusText(status int) string {
|
|
switch status {
|
|
case consts.SimBossCardStatusActivated:
|
|
return "已激活"
|
|
case consts.SimBossCardStatusStopped:
|
|
return "已停机"
|
|
case 1:
|
|
return "待激活"
|
|
default:
|
|
return "未知状态"
|
|
}
|
|
}
|