chore: 初始开源发布
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),属既有缺陷,本仓未引入亦未修复。
This commit is contained in:
@@ -0,0 +1,147 @@
|
||||
package allocate
|
||||
|
||||
import (
|
||||
"context"
|
||||
"sort"
|
||||
"strings"
|
||||
|
||||
"github.com/gogf/gf/v2/database/gdb"
|
||||
"github.com/gogf/gf/v2/errors/gerror"
|
||||
|
||||
"git.5iot.com/iotlcm-public/iotlcm-worker-svr/internal/consts"
|
||||
"git.5iot.com/iotlcm-public/iotlcm-worker-svr/internal/dao"
|
||||
"git.5iot.com/iotlcm-public/iotlcm-worker-svr/internal/model/entity"
|
||||
)
|
||||
|
||||
func (r *Repository) loadParentTaskTx(ctx context.Context, tx gdb.TX, parent *entity.BatchMain) (*Task, error) {
|
||||
var batchDetails []*entity.BatchDetail
|
||||
if err := tx.Ctx(ctx).Model(dao.BatchDetail.Table()).
|
||||
Where("batch_no IN (SELECT batch_no FROM batch_main WHERE source_batch_no = ? AND batch_type = ? AND tenant_id = ?)", parent.BatchNo, consts.BatchTypeAllocate, parent.TenantId).
|
||||
Where(dao.BatchDetail.Columns().BatchType, consts.BatchTypeAllocate).
|
||||
OrderAsc(dao.BatchDetail.Columns().RowNo).
|
||||
OrderAsc(dao.BatchDetail.Columns().BatchNo).
|
||||
OrderAsc(dao.BatchDetail.Columns().Id).
|
||||
Scan(&batchDetails); err != nil {
|
||||
return nil, gerror.Wrap(err, "load allocate parent details failed")
|
||||
}
|
||||
if len(batchDetails) == 0 {
|
||||
return nil, gerror.New("allocate parent has no child details")
|
||||
}
|
||||
|
||||
task := &Task{
|
||||
Batch: Batch{
|
||||
ID: parent.Id, BatchNo: parent.BatchNo, BatchType: consts.BatchTypeAllocate,
|
||||
TenantID: parent.TenantId, TargetDeptID: targetDeptIDFromBatch(parent),
|
||||
CreatedDept: parent.CreatedDept, CreatedBy: parent.CreatedBy,
|
||||
StartTime: parent.StartTime, BatchInfo: parent.BatchInfo,
|
||||
},
|
||||
Details: make([]*Detail, 0, len(batchDetails)),
|
||||
TargetsByID: make(map[int64]*Target), TargetsByKey: make(map[string]*Target), TargetsByName: make(map[string][]*Target),
|
||||
}
|
||||
cardIDs := make([]int64, 0, len(batchDetails))
|
||||
targetIDs := make([]int64, 0, len(batchDetails))
|
||||
cardIDByDetailID := make(map[int64]int64, len(batchDetails))
|
||||
for _, batchDetail := range batchDetails {
|
||||
if batchDetail == nil {
|
||||
continue
|
||||
}
|
||||
detail := &Detail{OriginalSnapshot: batchDetail.Snapshot, ID: batchDetail.Id, BatchNo: batchDetail.BatchNo, RowNo: batchDetail.RowNo, DetailStatus: batchDetail.DetailStatus, ICCID: strings.TrimSpace(batchDetail.ItemId), Reason: strings.TrimSpace(batchDetail.ErrorReason)}
|
||||
if detail.DetailStatus != consts.BatchDetailStatusSuccess && detail.Reason == "" {
|
||||
detail.Reason = "子批次明细尚未全部验证成功"
|
||||
}
|
||||
if snapshot := batchDetail.Snapshot; snapshot != nil {
|
||||
detail.Remark = firstSnapshot(snapshot, "remark")
|
||||
detail.MSISDN = firstSnapshot(snapshot, "MSISDN", "msisdn")
|
||||
detail.IMSI = firstSnapshot(snapshot, "IMSI", "imsi")
|
||||
detail.TargetDeptID = snapshot.Get("targetDeptId").Int64()
|
||||
detail.TargetDeptKey = firstSnapshot(snapshot, "targetDeptKey", "target_dept_key")
|
||||
detail.TargetDeptName = firstSnapshot(snapshot, "targetDeptName", "target_dept_name")
|
||||
if cardID := snapshot.Get("cardId").Int64(); cardID > 0 {
|
||||
cardIDs = append(cardIDs, cardID)
|
||||
cardIDByDetailID[batchDetail.Id] = cardID
|
||||
} else if detail.Reason == "" {
|
||||
detail.Reason = "卡片快照缺少有效卡片ID"
|
||||
}
|
||||
}
|
||||
if detail.TargetDeptID > 0 {
|
||||
targetIDs = append(targetIDs, detail.TargetDeptID)
|
||||
}
|
||||
task.Details = append(task.Details, detail)
|
||||
}
|
||||
|
||||
cardsByID := make(map[int64]*entity.TSimCardInfo, len(cardIDs))
|
||||
cardIDs = uniqueInt64s(cardIDs)
|
||||
sort.Slice(cardIDs, func(i, j int) bool { return cardIDs[i] < cardIDs[j] })
|
||||
for start := 0; start < len(cardIDs); start += allocateCardQueryBatchSize {
|
||||
end := min(start+allocateCardQueryBatchSize, len(cardIDs))
|
||||
var cards []*entity.TSimCardInfo
|
||||
if err := tx.Ctx(ctx).Model(dao.TSimCardInfo.Table()).Safe().
|
||||
Where(dao.TSimCardInfo.Columns().TenantId, parent.TenantId).
|
||||
WhereIn(dao.TSimCardInfo.Columns().Id, cardIDs[start:end]).
|
||||
LockUpdate().Scan(&cards); err != nil {
|
||||
return nil, gerror.Wrap(err, "lock allocate parent cards failed")
|
||||
}
|
||||
for _, card := range cards {
|
||||
if card != nil {
|
||||
cardsByID[card.Id] = card
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
targetsByID := make(map[int64]*Target, len(targetIDs))
|
||||
targetIDs = uniqueInt64s(targetIDs)
|
||||
sort.Slice(targetIDs, func(i, j int) bool { return targetIDs[i] < targetIDs[j] })
|
||||
for start := 0; start < len(targetIDs); start += allocateCardQueryBatchSize {
|
||||
end := min(start+allocateCardQueryBatchSize, len(targetIDs))
|
||||
var depts []*entity.SysDept
|
||||
if err := tx.Ctx(ctx).Model(dao.SysDept.Table()).
|
||||
Where(dao.SysDept.Columns().TenantId, parent.TenantId).
|
||||
Where(dao.SysDept.Columns().ParentId, parent.CreatedDept).
|
||||
Where(dao.SysDept.Columns().Status, "0").
|
||||
WhereNull(dao.SysDept.Columns().DeletedAt).
|
||||
WhereIn(dao.SysDept.Columns().DeptId, targetIDs[start:end]).
|
||||
LockUpdate().Scan(&depts); err != nil {
|
||||
return nil, gerror.Wrap(err, "lock allocate parent targets failed")
|
||||
}
|
||||
for _, dept := range depts {
|
||||
if dept == nil {
|
||||
continue
|
||||
}
|
||||
target := &Target{DeptID: dept.DeptId, ParentID: dept.ParentId, DeptKey: strings.TrimSpace(dept.DeptKey), DeptName: strings.TrimSpace(dept.DeptName)}
|
||||
targetsByID[target.DeptID] = target
|
||||
task.TargetsByID[target.DeptID] = target
|
||||
if target.DeptKey != "" {
|
||||
task.TargetsByKey[target.DeptKey] = target
|
||||
}
|
||||
if target.DeptName != "" {
|
||||
task.TargetsByName[target.DeptName] = append(task.TargetsByName[target.DeptName], target)
|
||||
}
|
||||
}
|
||||
}
|
||||
for _, detail := range task.Details {
|
||||
if detail == nil {
|
||||
continue
|
||||
}
|
||||
if detail.Target == nil {
|
||||
detail.Target = targetsByID[detail.TargetDeptID]
|
||||
}
|
||||
cardID := cardIDByDetailID[detail.ID]
|
||||
if card := cardsByID[cardID]; card != nil {
|
||||
detail.Card = &Card{ID: card.Id, ICCID: strings.TrimSpace(card.Iccid), MSISDN: strings.TrimSpace(card.Msisdn), IMSI: strings.TrimSpace(card.Imsi), DeptID: card.DeptId, AllocationBatchNo: strings.TrimSpace(card.AllocationBatchNo), PackageStatus: card.PackageStatus, BossCardStatus: card.BossCardStatus, FlowPkgEndAt: card.FlowPkgEndAt}
|
||||
}
|
||||
}
|
||||
return task, nil
|
||||
}
|
||||
|
||||
func uniqueInt64s(values []int64) []int64 {
|
||||
seen := make(map[int64]struct{}, len(values))
|
||||
unique := make([]int64, 0, len(values))
|
||||
for _, value := range values {
|
||||
if _, exists := seen[value]; exists {
|
||||
continue
|
||||
}
|
||||
seen[value] = struct{}{}
|
||||
unique = append(unique, value)
|
||||
}
|
||||
return unique
|
||||
}
|
||||
@@ -0,0 +1,88 @@
|
||||
package allocate
|
||||
|
||||
import (
|
||||
"context"
|
||||
"strings"
|
||||
"sync"
|
||||
"time"
|
||||
|
||||
"github.com/gogf/gf/v2/frame/g"
|
||||
)
|
||||
|
||||
const (
|
||||
defaultAllocateDeadlockMaxRetries = 3
|
||||
defaultAllocateDeadlockRetryDelay = 200 * time.Millisecond
|
||||
)
|
||||
|
||||
type allocateParentLock struct {
|
||||
mu sync.Mutex
|
||||
refs int
|
||||
}
|
||||
|
||||
var allocateParentLocks = struct {
|
||||
mu sync.Mutex
|
||||
values map[string]*allocateParentLock
|
||||
}{values: make(map[string]*allocateParentLock)}
|
||||
|
||||
func acquireAllocateParentLock(batchNo string) func() {
|
||||
allocateParentLocks.mu.Lock()
|
||||
lock := allocateParentLocks.values[batchNo]
|
||||
if lock == nil {
|
||||
lock = &allocateParentLock{}
|
||||
allocateParentLocks.values[batchNo] = lock
|
||||
}
|
||||
lock.refs++
|
||||
allocateParentLocks.mu.Unlock()
|
||||
|
||||
lock.mu.Lock()
|
||||
return func() {
|
||||
lock.mu.Unlock()
|
||||
allocateParentLocks.mu.Lock()
|
||||
lock.refs--
|
||||
if lock.refs == 0 {
|
||||
delete(allocateParentLocks.values, batchNo)
|
||||
}
|
||||
allocateParentLocks.mu.Unlock()
|
||||
}
|
||||
}
|
||||
|
||||
func allocateDeadlockMaxRetries(ctx context.Context) int {
|
||||
retries := g.Cfg().MustGet(ctx, "batch.allocate.deadlockMaxRetries", defaultAllocateDeadlockMaxRetries).Int()
|
||||
if retries < 0 {
|
||||
return 0
|
||||
}
|
||||
return retries
|
||||
}
|
||||
|
||||
func allocateDeadlockRetryDelay(ctx context.Context, attempt int) time.Duration {
|
||||
delay := g.Cfg().MustGet(ctx, "batch.allocate.deadlockRetryDelay", defaultAllocateDeadlockRetryDelay).Duration()
|
||||
if delay <= 0 {
|
||||
delay = defaultAllocateDeadlockRetryDelay
|
||||
}
|
||||
for i := 0; i < attempt; i++ {
|
||||
if delay >= time.Second {
|
||||
return time.Second
|
||||
}
|
||||
delay *= 2
|
||||
}
|
||||
return delay
|
||||
}
|
||||
|
||||
func waitAllocateDeadlockRetry(ctx context.Context, attempt int) error {
|
||||
timer := time.NewTimer(allocateDeadlockRetryDelay(ctx, attempt))
|
||||
defer timer.Stop()
|
||||
select {
|
||||
case <-ctx.Done():
|
||||
return ctx.Err()
|
||||
case <-timer.C:
|
||||
return nil
|
||||
}
|
||||
}
|
||||
|
||||
func isAllocateDeadlock(err error) bool {
|
||||
if err == nil {
|
||||
return false
|
||||
}
|
||||
message := strings.ToLower(err.Error())
|
||||
return strings.Contains(message, "deadlock found") || strings.Contains(message, "error 1213") || strings.Contains(message, "1213 (40001)")
|
||||
}
|
||||
@@ -0,0 +1,119 @@
|
||||
package allocate
|
||||
|
||||
import (
|
||||
"context"
|
||||
"git.5iot.com/iotlcm-public/iotlcm-worker-svr/internal/consts"
|
||||
"git.5iot.com/iotlcm-public/iotlcm-worker-svr/internal/model/do"
|
||||
"git.5iot.com/iotlcm-public/iotlcm-worker-svr/internal/model/entity"
|
||||
_ "github.com/gogf/gf/contrib/drivers/sqlite/v2"
|
||||
"github.com/gogf/gf/v2/database/gdb"
|
||||
"github.com/gogf/gf/v2/encoding/gjson"
|
||||
"github.com/gogf/gf/v2/os/gtime"
|
||||
"testing"
|
||||
)
|
||||
|
||||
func TestPersistedAllocateDetailsRequireExactBusinessResult(t *testing.T) {
|
||||
task := &Task{Batch: Batch{BatchNo: "ALC-child", TenantID: "000000", CreatedBy: 133, CreatedDept: 175}}
|
||||
detail := &Detail{ID: 1, ICCID: "89860401790000277308", TargetDeptID: 185}
|
||||
good := entity.BatchDetail{Id: 1, BatchNo: task.Batch.BatchNo, BatchType: consts.BatchTypeAllocate, TenantId: task.Batch.TenantID, DetailStatus: consts.BatchDetailStatusSuccess, UpdatedBy: 133, Snapshot: gjson.New(detail.Snapshot(task.Batch).String())}
|
||||
if err := validatePersistedDetailResults(task, []*Detail{detail}, []*entity.BatchDetail{&good}); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
for _, name := range []string{"missing", "tenant", "batch", "type", "status", "reason", "snapshot", "operator", "duplicate"} {
|
||||
t.Run(name, func(t *testing.T) {
|
||||
row := good
|
||||
rows := []*entity.BatchDetail{&row}
|
||||
details := []*Detail{detail}
|
||||
switch name {
|
||||
case "missing":
|
||||
rows = nil
|
||||
case "tenant":
|
||||
row.TenantId = "other"
|
||||
case "batch":
|
||||
row.BatchNo = "other"
|
||||
case "type":
|
||||
row.BatchType = 4
|
||||
case "status":
|
||||
row.DetailStatus = consts.BatchDetailStatusRunning
|
||||
case "reason":
|
||||
row.ErrorReason = "unexpected"
|
||||
case "snapshot":
|
||||
row.Snapshot = gjson.New(map[string]any{"targetDeptId": 999})
|
||||
case "operator":
|
||||
row.UpdatedBy = 999
|
||||
case "duplicate":
|
||||
details = append(details, detail)
|
||||
rows = append(rows, &row)
|
||||
}
|
||||
if validatePersistedDetailResults(task, details, rows) == nil {
|
||||
t.Fatal("inconsistent detail accepted")
|
||||
}
|
||||
})
|
||||
}
|
||||
detail.Reason = "整批校验失败"
|
||||
good.DetailStatus = consts.BatchDetailStatusFailed
|
||||
good.ErrorReason = detail.Reason
|
||||
good.Snapshot = gjson.New(detail.Snapshot(task.Batch).String())
|
||||
if err := validatePersistedDetailResults(task, []*Detail{detail}, []*entity.BatchDetail{&good}); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
}
|
||||
|
||||
func TestDetailChunkZeroAffectedChecksPersistedResult(t *testing.T) {
|
||||
ctx := context.Background()
|
||||
db, err := gdb.New(gdb.ConfigNode{Type: "sqlite", Link: "sqlite::@file(:memory:)"})
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
defer db.Close(ctx)
|
||||
for _, sql := range []string{
|
||||
"CREATE TABLE batch_detail (id INTEGER PRIMARY KEY, batch_no TEXT, batch_type INTEGER, tenant_id TEXT, detail_status INTEGER, error_reason TEXT, snapshot TEXT, updated_by INTEGER, updated_at DATETIME)",
|
||||
"CREATE TABLE card_test (id INTEGER PRIMARY KEY, dept_id INTEGER)",
|
||||
"INSERT INTO card_test VALUES (1,175)",
|
||||
} {
|
||||
if _, err = db.Exec(ctx, sql); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
}
|
||||
task := &Task{Batch: Batch{BatchNo: "ALC-child", TenantID: "000000", CreatedBy: 133, CreatedDept: 175}}
|
||||
detail := &Detail{ID: 1, ICCID: "CARD", TargetDeptID: 185, Remark: "保留备注"}
|
||||
if _, err = db.Model("batch_detail").Data(do.BatchDetail{Id: 1, BatchNo: task.Batch.BatchNo, BatchType: 3, TenantId: task.Batch.TenantID, DetailStatus: 2, ErrorReason: "", Snapshot: detail.Snapshot(task.Batch), UpdatedBy: 133}).Insert(); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
// 强制模拟驱动返回零修改行,不依赖 SQLite 与 MySQL 行数语义差异。
|
||||
if _, err = db.Exec(ctx, "CREATE TRIGGER ignore_update BEFORE UPDATE ON batch_detail BEGIN SELECT RAISE(IGNORE); END"); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
repo := &Repository{db: db}
|
||||
if err = db.Transaction(ctx, func(ctx context.Context, tx gdb.TX) error {
|
||||
return repo.updateDetailChunk(ctx, tx, task, []*Detail{detail}, gtime.Now())
|
||||
}); err != nil {
|
||||
t.Fatalf("identical persisted result rejected: %v", err)
|
||||
}
|
||||
for _, name := range []string{"mismatch", "missing", "tenant"} {
|
||||
t.Run(name, func(t *testing.T) {
|
||||
changed := *detail
|
||||
changed.Reason = "unexpected"
|
||||
if name == "missing" {
|
||||
changed.ID = 999
|
||||
}
|
||||
scopedTask := *task
|
||||
if name == "tenant" {
|
||||
scopedTask.Batch.TenantID = "other"
|
||||
}
|
||||
err := db.Transaction(ctx, func(ctx context.Context, tx gdb.TX) error {
|
||||
if _, err := tx.Exec("UPDATE card_test SET dept_id=185 WHERE id=1"); err != nil {
|
||||
return err
|
||||
}
|
||||
return repo.updateDetailChunk(ctx, tx, &scopedTask, []*Detail{&changed}, gtime.Now())
|
||||
})
|
||||
if err == nil {
|
||||
t.Fatal("unsafe update accepted")
|
||||
}
|
||||
dept, err := db.Model("card_test").Value("dept_id")
|
||||
if err != nil || dept.Int() != 175 {
|
||||
t.Fatalf("transaction failed to rollback ownership: %v %v", dept, err)
|
||||
}
|
||||
})
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,294 @@
|
||||
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)
|
||||
}
|
||||
@@ -0,0 +1,197 @@
|
||||
package allocate
|
||||
|
||||
import (
|
||||
"context"
|
||||
"errors"
|
||||
"strings"
|
||||
"testing"
|
||||
"time"
|
||||
|
||||
"github.com/gogf/gf/v2/encoding/gjson"
|
||||
"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"
|
||||
)
|
||||
|
||||
func TestAllocateProcessingRetryAfterHasLargeBatchDefault(t *testing.T) {
|
||||
if got := allocateProcessingRetryAfter(context.Background()); got < 30*time.Minute {
|
||||
t.Fatalf("allocateProcessingRetryAfter() = %s, want at least 30m", got)
|
||||
}
|
||||
}
|
||||
|
||||
func TestIsAllocateParentRequiresTypeAndRole(t *testing.T) {
|
||||
parent := &entity.BatchMain{
|
||||
BatchType: consts.BatchTypeAllocate,
|
||||
BatchInfo: gjson.New(`{"jobRole":"allocate_parent"}`),
|
||||
}
|
||||
if !isAllocateParent(parent) {
|
||||
t.Fatal("expected allocate parent metadata to be recognized")
|
||||
}
|
||||
parent.SourceBatchNo = "ALC-CHILD"
|
||||
if isAllocateParent(parent) {
|
||||
t.Fatal("child batch must not be recognized as allocate parent")
|
||||
}
|
||||
}
|
||||
|
||||
func TestIsAllocateDeadlock(t *testing.T) {
|
||||
if !isAllocateDeadlock(errors.New("Error 1213 (40001): Deadlock found when trying to get lock")) {
|
||||
t.Fatal("expected MySQL deadlock to be retryable")
|
||||
}
|
||||
if isAllocateDeadlock(errors.New("validation failed")) {
|
||||
t.Fatal("validation errors must not be treated as deadlocks")
|
||||
}
|
||||
}
|
||||
|
||||
func TestResolveAllocateParentStartTime(t *testing.T) {
|
||||
parentStart := gtime.New("2026-09-29 12:59:00")
|
||||
childStart := gtime.New("2026-09-29 12:59:32")
|
||||
fallback := gtime.New("2026-09-29 13:00:21")
|
||||
|
||||
if got := resolveAllocateParentStartTime(parentStart, childStart, fallback); got != parentStart {
|
||||
t.Fatalf("parent start time = %v, want existing parent time %v", got, parentStart)
|
||||
}
|
||||
if got := resolveAllocateParentStartTime(nil, childStart, fallback); got != childStart {
|
||||
t.Fatalf("parent start time = %v, want earliest child time %v", got, childStart)
|
||||
}
|
||||
if got := resolveAllocateParentStartTime(nil, nil, fallback); got != fallback {
|
||||
t.Fatalf("parent start time = %v, want fallback time %v", got, fallback)
|
||||
}
|
||||
}
|
||||
|
||||
func TestAllocateBatchSizesRemainBounded(t *testing.T) {
|
||||
if allocateCardQueryBatchSize <= 0 || allocateCardQueryBatchSize > 1000 {
|
||||
t.Fatalf("allocateCardQueryBatchSize = %d", allocateCardQueryBatchSize)
|
||||
}
|
||||
if allocateCardUpdateBatchSize <= 0 || allocateCardUpdateBatchSize > 1000 {
|
||||
t.Fatalf("allocateCardUpdateBatchSize = %d", allocateCardUpdateBatchSize)
|
||||
}
|
||||
if allocateDetailUpdateBatchSize <= 0 || allocateDetailUpdateBatchSize > 1000 {
|
||||
t.Fatalf("allocateDetailUpdateBatchSize = %d", allocateDetailUpdateBatchSize)
|
||||
}
|
||||
}
|
||||
|
||||
func TestBuildDetailUpdateStatementUsesBoundParameters(t *testing.T) {
|
||||
task := &Task{Batch: Batch{BatchNo: "ALC-1", CreatedBy: 7}}
|
||||
details := []*Detail{
|
||||
{ID: 11, DetailStatus: consts.BatchDetailStatusPending, Reason: "unsafe ' reason"},
|
||||
{ID: 12, DetailStatus: consts.BatchDetailStatusRunning, Reason: "second"},
|
||||
}
|
||||
statement, args := buildDetailUpdateStatement(task, details, gtime.Now())
|
||||
if strings.Contains(statement, details[0].Reason) {
|
||||
t.Fatal("detail reason was interpolated into SQL")
|
||||
}
|
||||
if !strings.Contains(statement, "CASE") || !strings.Contains(statement, "IN (?,?)") {
|
||||
t.Fatalf("unexpected statement: %s", statement)
|
||||
}
|
||||
if len(args) != len(details)*7+9 {
|
||||
t.Fatalf("argument count = %d, want %d", len(args), len(details)*7+9)
|
||||
}
|
||||
}
|
||||
|
||||
func TestNormalizeAtomicParentResultRejectsPartialSuccess(t *testing.T) {
|
||||
task := &Task{Details: []*Detail{
|
||||
{ICCID: "8986000000000000001"},
|
||||
{ICCID: "8986000000000000002", Reason: "目标客户不存在"},
|
||||
}}
|
||||
|
||||
successCount, failCount := normalizeAtomicParentResult(task)
|
||||
if successCount != 0 || failCount != len(task.Details) {
|
||||
t.Fatalf("normalizeAtomicParentResult() = %d, %d; want 0, %d", successCount, failCount, len(task.Details))
|
||||
}
|
||||
for _, detail := range task.Details {
|
||||
if detail.Success() {
|
||||
t.Fatalf("detail %s remained successful", detail.ICCID)
|
||||
}
|
||||
}
|
||||
status, result, _ := parentBatchResult(successCount, failCount)
|
||||
if status != consts.BatchAsyncStatusFailed || result != consts.BatchRunResultFailed {
|
||||
t.Fatalf("parentBatchResult() = %d, %d; want failed, failed", status, result)
|
||||
}
|
||||
}
|
||||
|
||||
func TestValidateDetailAllowsCurrentHolderReallocation(t *testing.T) {
|
||||
task := &Task{
|
||||
Batch: Batch{
|
||||
CreatedDept: 100,
|
||||
},
|
||||
TargetsByID: map[int64]*Target{
|
||||
200: {DeptID: 200, ParentID: 100},
|
||||
},
|
||||
}
|
||||
detail := &Detail{
|
||||
ICCID: "8986000000000000000",
|
||||
TargetDeptID: 200,
|
||||
Card: &Card{
|
||||
ICCID: "8986000000000000000",
|
||||
DeptID: 100,
|
||||
AllocationBatchNo: "ALC202608050001",
|
||||
PackageStatus: consts.PackageStatusEffective,
|
||||
},
|
||||
}
|
||||
|
||||
validateDetail(task, detail)
|
||||
if detail.Reason != "" {
|
||||
t.Fatalf("expected current holder reallocation to pass, got %q", detail.Reason)
|
||||
}
|
||||
}
|
||||
|
||||
func TestValidateDetailRejectsAncestorReallocationAfterCardMoved(t *testing.T) {
|
||||
task := &Task{
|
||||
Batch: Batch{
|
||||
CreatedDept: 100,
|
||||
},
|
||||
TargetsByID: map[int64]*Target{
|
||||
300: {DeptID: 300, ParentID: 100},
|
||||
},
|
||||
}
|
||||
detail := &Detail{
|
||||
ICCID: "8986000000000000000",
|
||||
TargetDeptID: 300,
|
||||
Card: &Card{
|
||||
ICCID: "8986000000000000000",
|
||||
DeptID: 200,
|
||||
AllocationBatchNo: "ALC202608050001",
|
||||
PackageStatus: consts.PackageStatusEffective,
|
||||
},
|
||||
}
|
||||
|
||||
validateDetail(task, detail)
|
||||
if detail.Reason == "" {
|
||||
t.Fatal("expected ancestor reallocation to be rejected after card moved")
|
||||
}
|
||||
}
|
||||
|
||||
func TestValidateTaskMarksWholeBatchFailed(t *testing.T) {
|
||||
task := &Task{
|
||||
Details: []*Detail{
|
||||
{ICCID: "8986000000000000001"},
|
||||
{ICCID: "8986000000000000002", Reason: "目标客户不存在"},
|
||||
},
|
||||
}
|
||||
|
||||
validateTask(task)
|
||||
|
||||
for _, detail := range task.Details {
|
||||
if detail.Success() {
|
||||
t.Fatalf("detail %s remained successful after atomic failure", detail.ICCID)
|
||||
}
|
||||
}
|
||||
if task.FailureReason == "" {
|
||||
t.Fatal("expected batch failure reason")
|
||||
}
|
||||
status, result := batchResult(countResult(task))
|
||||
if status != consts.BatchAsyncStatusFailed || result != consts.BatchRunResultFailed {
|
||||
t.Fatalf("batchResult() = %d, %d; want failed, failed", status, result)
|
||||
}
|
||||
}
|
||||
|
||||
func TestValidateDetailRejectsChangedCardICCID(t *testing.T) {
|
||||
task := &Task{Batch: Batch{CreatedDept: 100}, TargetsByID: map[int64]*Target{200: {DeptID: 200, ParentID: 100}}}
|
||||
detail := &Detail{ICCID: "8986000000000000001", TargetDeptID: 200, Card: &Card{ICCID: "8986000000000000002", DeptID: 100, PackageStatus: consts.PackageStatusEffective}}
|
||||
|
||||
validateDetail(task, detail)
|
||||
if detail.Reason == "" {
|
||||
t.Fatal("expected changed card ICCID to be rejected")
|
||||
}
|
||||
}
|
||||
File diff suppressed because it is too large
Load Diff
@@ -0,0 +1,162 @@
|
||||
package allocate
|
||||
|
||||
import (
|
||||
"strings"
|
||||
|
||||
"github.com/gogf/gf/v2/encoding/gjson"
|
||||
"github.com/gogf/gf/v2/os/gtime"
|
||||
|
||||
"git.5iot.com/iotlcm-public/iotlcm-worker-svr/internal/service/batchresult"
|
||||
)
|
||||
|
||||
type Batch struct {
|
||||
ID int64
|
||||
BatchNo string
|
||||
BatchType int
|
||||
ParentBatchNo string
|
||||
TenantID string
|
||||
TargetDeptID int64
|
||||
CreatedDept int64
|
||||
CreatedBy int64
|
||||
StartTime *gtime.Time
|
||||
BatchInfo *gjson.Json
|
||||
}
|
||||
|
||||
type Card struct {
|
||||
ID int64
|
||||
ICCID string
|
||||
MSISDN string
|
||||
IMSI string
|
||||
DeptID int64
|
||||
AllocationBatchNo string
|
||||
PackageStatus int
|
||||
BossCardStatus int
|
||||
FlowPkgEndAt *gtime.Time
|
||||
}
|
||||
|
||||
type Target struct {
|
||||
DeptID int64
|
||||
ParentID int64
|
||||
DeptKey string
|
||||
DeptName string
|
||||
}
|
||||
|
||||
type Detail struct {
|
||||
OriginalSnapshot *gjson.Json
|
||||
ID int64
|
||||
BatchNo string
|
||||
RowNo int
|
||||
DetailStatus int
|
||||
ICCID string
|
||||
MSISDN string
|
||||
IMSI string
|
||||
TargetDeptID int64
|
||||
TargetDeptKey string
|
||||
TargetDeptName string
|
||||
Remark string
|
||||
Card *Card
|
||||
Target *Target
|
||||
Reason string
|
||||
}
|
||||
|
||||
type Task struct {
|
||||
Batch Batch
|
||||
Details []*Detail
|
||||
TargetsByID map[int64]*Target
|
||||
TargetsByKey map[string]*Target
|
||||
TargetsByName map[string][]*Target
|
||||
FailureReason string
|
||||
}
|
||||
|
||||
func (d *Detail) Success() bool {
|
||||
return d != nil && strings.TrimSpace(d.Reason) == ""
|
||||
}
|
||||
|
||||
func (d *Detail) DisplayCode() string {
|
||||
if d == nil {
|
||||
return ""
|
||||
}
|
||||
if d.Card != nil && strings.TrimSpace(d.Card.ICCID) != "" {
|
||||
return strings.TrimSpace(d.Card.ICCID)
|
||||
}
|
||||
return strings.TrimSpace(d.ICCID)
|
||||
}
|
||||
|
||||
func (d *Detail) Snapshot(batch Batch) *gjson.Json {
|
||||
if d == nil {
|
||||
return gjson.New(nil)
|
||||
}
|
||||
data := map[string]any{}
|
||||
if d.OriginalSnapshot != nil {
|
||||
for k, v := range d.OriginalSnapshot.Map() {
|
||||
data[k] = v
|
||||
}
|
||||
}
|
||||
sourceDeptID := batch.CreatedDept
|
||||
if d.OriginalSnapshot != nil && d.OriginalSnapshot.Get("sourceDeptId").Int64() > 0 {
|
||||
sourceDeptID = d.OriginalSnapshot.Get("sourceDeptId").Int64()
|
||||
}
|
||||
current := map[string]any{
|
||||
"rowNo": d.RowNo,
|
||||
"ICCID": strings.TrimSpace(d.ICCID),
|
||||
"MSISDN": strings.TrimSpace(d.MSISDN),
|
||||
"IMSI": strings.TrimSpace(d.IMSI),
|
||||
"sourceDeptId": sourceDeptID,
|
||||
"targetDeptId": d.TargetDeptID,
|
||||
"targetDeptKey": strings.TrimSpace(d.TargetDeptKey),
|
||||
"targetDeptName": strings.TrimSpace(d.TargetDeptName),
|
||||
"remark": strings.TrimSpace(d.Remark),
|
||||
"errorReason": strings.TrimSpace(d.Reason),
|
||||
}
|
||||
for k, v := range current {
|
||||
data[k] = v
|
||||
}
|
||||
if d.Card != nil {
|
||||
data["cardId"] = d.Card.ID
|
||||
data["cardIccid"] = d.Card.ICCID
|
||||
data["cardMsisdn"] = d.Card.MSISDN
|
||||
data["cardImsi"] = d.Card.IMSI
|
||||
data["cardDeptIdBefore"] = d.Card.DeptID
|
||||
data["allocationBatchNoBefore"] = strings.TrimSpace(d.Card.AllocationBatchNo)
|
||||
data["packageStatus"] = d.Card.PackageStatus
|
||||
data["bossCardStatus"] = d.Card.BossCardStatus
|
||||
if d.Card.FlowPkgEndAt != nil {
|
||||
data["flowPkgEndAt"] = d.Card.FlowPkgEndAt.String()
|
||||
}
|
||||
}
|
||||
if d.Target != nil {
|
||||
data["targetDeptId"] = d.Target.DeptID
|
||||
if strings.TrimSpace(d.TargetDeptKey) == "" {
|
||||
data["targetDeptKey"] = d.Target.DeptKey
|
||||
}
|
||||
if strings.TrimSpace(d.TargetDeptName) == "" {
|
||||
data["targetDeptName"] = d.Target.DeptName
|
||||
}
|
||||
}
|
||||
return gjson.New(data)
|
||||
}
|
||||
|
||||
func (t *Task) ResultRows() []batchresult.Row {
|
||||
rows := make([]batchresult.Row, 0, len(t.Details))
|
||||
for _, detail := range t.Details {
|
||||
if detail == nil {
|
||||
continue
|
||||
}
|
||||
targetName := strings.TrimSpace(detail.TargetDeptName)
|
||||
if targetName == "" && detail.Target != nil && strings.TrimSpace(detail.Target.DeptName) != "" {
|
||||
targetName = strings.TrimSpace(detail.Target.DeptName)
|
||||
}
|
||||
rows = append(rows, batchresult.Row{
|
||||
RowNo: detail.RowNo,
|
||||
ObjectCode: detail.DisplayCode(),
|
||||
ICCID: strings.TrimSpace(detail.ICCID),
|
||||
MSISDN: strings.TrimSpace(detail.MSISDN),
|
||||
IMSI: strings.TrimSpace(detail.IMSI),
|
||||
TargetDeptName: targetName,
|
||||
Operation: "卡划拨",
|
||||
Success: detail.Success(),
|
||||
Reason: strings.TrimSpace(detail.Reason),
|
||||
})
|
||||
}
|
||||
return rows
|
||||
}
|
||||
@@ -0,0 +1,34 @@
|
||||
package allocate
|
||||
|
||||
import (
|
||||
"github.com/gogf/gf/v2/encoding/gjson"
|
||||
"testing"
|
||||
)
|
||||
|
||||
func TestUnitSnapshotSurvivesRenameAndWriteback(t *testing.T) {
|
||||
detail := &Detail{OriginalSnapshot: gjson.New(map[string]any{"sourceDeptName": "划拨旧名称", "sourceDeptKey": "source", "auditMarker": "keep"}), TargetDeptID: 2, TargetDeptName: "接收旧名称", TargetDeptKey: "target", Target: &Target{DeptID: 2, DeptName: "接收新名称", DeptKey: "new"}}
|
||||
info := detail.Snapshot(Batch{CreatedDept: 1})
|
||||
if info.Get("sourceDeptName").String() != "划拨旧名称" || info.Get("targetDeptName").String() != "接收旧名称" || info.Get("targetDeptKey").String() != "target" || info.Get("auditMarker").String() != "keep" {
|
||||
t.Fatal(info)
|
||||
}
|
||||
task := &Task{Details: []*Detail{detail}}
|
||||
if task.ResultRows()[0].TargetDeptName != "接收旧名称" {
|
||||
t.Fatal("结果文件覆盖历史名称")
|
||||
}
|
||||
parent := mergeAllocateParentInfo(gjson.New(map[string]any{"sourceDeptName": "划拨旧名称", "targetDeptName": "接收旧名称"}), nil)
|
||||
if parent.Get("sourceDeptName").String() != "划拨旧名称" {
|
||||
t.Fatal(parent)
|
||||
}
|
||||
}
|
||||
|
||||
func TestUnitTargetUsesStableIDAfterRename(t *testing.T) {
|
||||
target := &Target{DeptID: 2, DeptName: "接收单位新名称", DeptKey: "new-key"}
|
||||
detail := &Detail{OriginalSnapshot: gjson.New(map[string]any{"sourceDeptId": 9}), TargetDeptID: 2, TargetDeptName: "接收单位旧名称"}
|
||||
task := &Task{TargetsByID: map[int64]*Target{2: target}}
|
||||
if resolveTarget(task, detail) != target {
|
||||
t.Fatal("单位改名后未优先使用稳定 ID")
|
||||
}
|
||||
if detail.Snapshot(Batch{CreatedDept: 1}).Get("sourceDeptId").Int64() != 9 {
|
||||
t.Fatal("执行写回覆盖原划拨单位 ID")
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user