2bec257289
iotlcm-worker-svr:异步执行中枢(RabbitMQ 消费者 + 全量同步编排)。 - Go module:iotlcm-worker-svr → git.5iot.com/iotlcm-public/iotlcm-worker-svr(214 文件) - 凭据与内部标识脱敏:MySQL/Redis 凭据、私网与生产 IP、内网域名、 库名 xiujie_admin → iotlcm、哨兵名占位 - 159 处真实手机号 → 合法 11 位编造号(保留号段与后 4 位,维持互异, 不破坏「校验 MSISDN 格式」等测试断言) - 移除内部资产:CI 触发器、Jenkins 触发测试文档、预演门禁与镜像 digest 记录 - Dockerfile 改用公共基础镜像(golang:1.25-alpine → alpine:3.23), 移除写死的 git 凭据 - README 重写(架构/全量同步模型/配置/已知失败),补 Apache-2.0 LICENSE,LF 行尾 + .gitattributes 已知:3 个测试包在源仓同一提交上同样失败(client/upstream、 service/allocate、service/inbound),属既有缺陷,本仓未引入亦未修复。
295 lines
8.0 KiB
Go
295 lines
8.0 KiB
Go
package allocate
|
|
|
|
import (
|
|
"context"
|
|
"strings"
|
|
"time"
|
|
|
|
"github.com/gogf/gf/v2/errors/gerror"
|
|
"github.com/gogf/gf/v2/frame/g"
|
|
"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"
|
|
"git.5iot.com/iotlcm-public/iotlcm-worker-svr/internal/service/packagecustomer"
|
|
)
|
|
|
|
const defaultAllocateProcessingRetryAfter = 30 * time.Minute
|
|
|
|
const allocateParentJobRole = "allocate_parent"
|
|
|
|
type executor struct {
|
|
repository *Repository
|
|
}
|
|
|
|
func NewExecutor() *executor {
|
|
return &executor{repository: NewRepository()}
|
|
}
|
|
|
|
func (e *executor) CanExecuteProcessing(ctx context.Context, main *entity.BatchMain) (bool, error) {
|
|
if main == nil || main.BatchAsyncStatus != consts.BatchAsyncStatusProcessing {
|
|
return false, nil
|
|
}
|
|
if isAllocateParent(main) {
|
|
return true, nil
|
|
}
|
|
if main.BatchType != consts.BatchTypeAllocate {
|
|
return false, nil
|
|
}
|
|
if main.StartTime == nil || main.StartTime.IsZero() {
|
|
return true, nil
|
|
}
|
|
return gtime.Now().Sub(main.StartTime) >= allocateProcessingRetryAfter(ctx), nil
|
|
}
|
|
|
|
func (e *executor) HandleExecutionFailure(ctx context.Context, batchNo string, batchType int, _ string) error {
|
|
if batchType != consts.BatchTypeAllocate {
|
|
return nil
|
|
}
|
|
main, err := e.repository.LoadMain(ctx, batchNo)
|
|
if err != nil {
|
|
return err
|
|
}
|
|
if main == nil {
|
|
return nil
|
|
}
|
|
if strings.TrimSpace(main.SourceBatchNo) == "" {
|
|
return nil
|
|
}
|
|
return e.repository.AggregateParent(ctx, main.SourceBatchNo)
|
|
}
|
|
|
|
func allocateProcessingRetryAfter(ctx context.Context) time.Duration {
|
|
duration := g.Cfg().MustGet(ctx, "batch.allocate.processingRetryAfter", defaultAllocateProcessingRetryAfter).Duration()
|
|
if duration <= 0 {
|
|
return defaultAllocateProcessingRetryAfter
|
|
}
|
|
return duration
|
|
}
|
|
|
|
func (e *executor) Execute(ctx context.Context, batchNo string) error {
|
|
main, err := e.repository.LoadMain(ctx, strings.TrimSpace(batchNo))
|
|
if err != nil {
|
|
return gerror.Wrap(err, "load allocate batch main failed")
|
|
}
|
|
if isAllocateParent(main) {
|
|
return e.repository.AggregateParent(ctx, main.BatchNo)
|
|
}
|
|
task, err := e.repository.Load(ctx, strings.TrimSpace(batchNo))
|
|
if err != nil {
|
|
return gerror.Wrap(err, "load allocate batch failed")
|
|
}
|
|
validateTask(task)
|
|
if err = e.repository.Commit(ctx, task); err != nil {
|
|
return gerror.Wrap(err, "commit allocate batch failed")
|
|
}
|
|
if strings.TrimSpace(task.Batch.ParentBatchNo) == "" {
|
|
publishPackageCustomerSync(ctx, task)
|
|
}
|
|
return nil
|
|
}
|
|
|
|
func isAllocateParent(main *entity.BatchMain) bool {
|
|
if main == nil || main.BatchType != consts.BatchTypeAllocate || strings.TrimSpace(main.SourceBatchNo) != "" {
|
|
return false
|
|
}
|
|
return main.BatchInfo != nil && main.BatchInfo.Get("jobRole").String() == allocateParentJobRole
|
|
}
|
|
|
|
func validateTask(task *Task) {
|
|
if task == nil || len(task.Details) == 0 {
|
|
if task != nil {
|
|
task.FailureReason = "划拨批次没有可处理明细"
|
|
}
|
|
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)
|
|
detail.TargetDeptKey = strings.TrimSpace(detail.TargetDeptKey)
|
|
detail.TargetDeptName = strings.TrimSpace(detail.TargetDeptName)
|
|
if detail.ICCID != "" {
|
|
iccidCounts[detail.ICCID]++
|
|
}
|
|
}
|
|
|
|
for _, detail := range task.Details {
|
|
if detail == nil {
|
|
continue
|
|
}
|
|
if detail.Reason == "" && iccidCounts[detail.ICCID] > 1 {
|
|
detail.Reason = "ICCID在批次内重复"
|
|
}
|
|
validateDetail(task, detail)
|
|
}
|
|
_, failCount := countResult(task)
|
|
if failCount > 0 {
|
|
markAtomicFailure(task, "划拨批次存在失败明细,整批未执行")
|
|
}
|
|
}
|
|
|
|
func publishPackageCustomerSync(ctx context.Context, task *Task) {
|
|
if task == nil {
|
|
return
|
|
}
|
|
deptIds := []int64{task.Batch.CreatedDept}
|
|
hasSuccess := false
|
|
for _, detail := range task.Details {
|
|
if detail == nil || !detail.Success() {
|
|
continue
|
|
}
|
|
hasSuccess = true
|
|
if detail.TargetDeptID > 0 {
|
|
deptIds = append(deptIds, detail.TargetDeptID)
|
|
}
|
|
}
|
|
if !hasSuccess {
|
|
return
|
|
}
|
|
packagecustomer.PublishSyncForDeptsSafe(ctx, task.Batch.TenantID, task.Batch.CreatedBy, deptIds, "allocate", task.Batch.BatchNo)
|
|
}
|
|
|
|
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 || strings.TrimSpace(detail.Reason) != "" {
|
|
return
|
|
}
|
|
if detail.ICCID == "" {
|
|
detail.Reason = "ICCID不能为空"
|
|
return
|
|
}
|
|
target := resolveTarget(task, detail)
|
|
if target == nil {
|
|
return
|
|
}
|
|
detail.Target = target
|
|
detail.TargetDeptID = target.DeptID
|
|
if detail.TargetDeptKey == "" {
|
|
detail.TargetDeptKey = strings.TrimSpace(target.DeptKey)
|
|
}
|
|
if detail.TargetDeptName == "" {
|
|
detail.TargetDeptName = strings.TrimSpace(target.DeptName)
|
|
}
|
|
if detail.Card == nil {
|
|
detail.Reason = "ICCID不存在或不属于当前租户"
|
|
return
|
|
}
|
|
if strings.TrimSpace(detail.ICCID) != strings.TrimSpace(detail.Card.ICCID) {
|
|
detail.Reason = "ICCID与卡档案不一致"
|
|
return
|
|
}
|
|
detail.MSISDN = coalesce(detail.MSISDN, detail.Card.MSISDN)
|
|
detail.IMSI = coalesce(detail.IMSI, detail.Card.IMSI)
|
|
if detail.Card.DeptID != task.Batch.CreatedDept {
|
|
detail.Reason = "ICCID不在当前操作员授权范围内"
|
|
return
|
|
}
|
|
if strings.TrimSpace(detail.MSISDN) != "" && strings.TrimSpace(detail.MSISDN) != detail.Card.MSISDN {
|
|
detail.Reason = "MSISDN与卡档案不一致"
|
|
return
|
|
}
|
|
if strings.TrimSpace(detail.IMSI) != "" && strings.TrimSpace(detail.IMSI) != detail.Card.IMSI {
|
|
detail.Reason = "IMSI与卡档案不一致"
|
|
return
|
|
}
|
|
if detail.Card.BossCardStatus == consts.SimBossCardStatusStopped {
|
|
detail.Reason = "卡已停机,不能划拨"
|
|
return
|
|
}
|
|
if detail.Card.BossCardStatus == consts.SimBossCardStatusCanceled {
|
|
detail.Reason = "卡已销户,不能划拨"
|
|
return
|
|
}
|
|
if detail.Card.PackageStatus == consts.PackageStatusExpired ||
|
|
(detail.Card.FlowPkgEndAt != nil && !detail.Card.FlowPkgEndAt.IsZero() && detail.Card.FlowPkgEndAt.Before(gtime.Now())) {
|
|
detail.Reason = "套餐已过期,不能划拨"
|
|
return
|
|
}
|
|
if detail.Card.DeptID == target.DeptID {
|
|
detail.Reason = "卡已属于目标客户,无需重复划拨"
|
|
}
|
|
}
|
|
|
|
func resolveTarget(task *Task, detail *Detail) *Target {
|
|
if task == nil || detail == nil {
|
|
return nil
|
|
}
|
|
targetDeptID := detail.TargetDeptID
|
|
if targetDeptID <= 0 {
|
|
targetDeptID = task.Batch.TargetDeptID
|
|
}
|
|
if targetDeptID > 0 {
|
|
target := task.TargetsByID[targetDeptID]
|
|
if target == nil {
|
|
detail.Reason = "划拨目标客户不存在或不在当前部门的下级范围内"
|
|
return nil
|
|
}
|
|
return target
|
|
}
|
|
if detail.TargetDeptKey != "" {
|
|
target := task.TargetsByKey[detail.TargetDeptKey]
|
|
if target == nil {
|
|
detail.Reason = "划拨目标客户编码不存在或不在当前部门的下级范围内"
|
|
return nil
|
|
}
|
|
return target
|
|
}
|
|
if detail.TargetDeptName != "" {
|
|
targets := task.TargetsByName[detail.TargetDeptName]
|
|
if len(targets) == 0 {
|
|
detail.Reason = "划拨目标客户名称不存在或不在当前部门的下级范围内"
|
|
return nil
|
|
}
|
|
if len(targets) > 1 {
|
|
detail.Reason = "划拨目标客户名称不唯一,请使用目标客户编码"
|
|
return nil
|
|
}
|
|
return targets[0]
|
|
}
|
|
detail.Reason = "目标客户不能为空"
|
|
return nil
|
|
}
|
|
|
|
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 coalesce(value, fallback string) string {
|
|
value = strings.TrimSpace(value)
|
|
if value != "" {
|
|
return value
|
|
}
|
|
return strings.TrimSpace(fallback)
|
|
}
|