sync: 对齐小程序 v1.0.2 生产代码并脱敏

This commit is contained in:
MiFaZhan
2026-10-29 15:15:12 +08:00
parent a7abd476f0
commit 5fcf74deb0
423 changed files with 15001 additions and 6382 deletions
+54 -187
View File
@@ -4,12 +4,12 @@ import (
"context"
"fmt"
"strings"
"git.5iot.com/iotlcm-public/iotlcm-app/internal/library/batchmetadata"
"git.5iot.com/iotlcm-public/iotlcm-app/internal/consts"
"git.5iot.com/iotlcm-public/iotlcm-app/internal/dao"
"git.5iot.com/iotlcm-public/iotlcm-app/internal/library/batchimport"
"git.5iot.com/iotlcm-public/iotlcm-app/internal/model"
"git.5iot.com/iotlcm-public/iotlcm-app/internal/model/do"
"git.5iot.com/iotlcm-public/iotlcm-app/internal/model/entity"
"github.com/gogf/gf/v2/database/gdb"
@@ -87,6 +87,7 @@ func (s *sAppCard) loadAllocateTargetDepts(ctx context.Context, user *entity.Sys
err := dao.SysDept.Ctx(ctx).
Where(dao.SysDept.Columns().TenantId, user.TenantId).
Where(dao.SysDept.Columns().ParentId, user.DeptId).
Where(dao.SysDept.Columns().Status, "0").
WhereNull(dao.SysDept.Columns().DeletedAt).
OrderAsc(dao.SysDept.Columns().OrderNum).
OrderAsc(dao.SysDept.Columns().DeptId).
@@ -145,12 +146,17 @@ func (s *sAppCard) AllocateBatchPrepare(ctx context.Context, param *model.AppCar
return nil, err
}
if param.ExecutionMode == batchmetadata.Single && len(batch.Cards) != 1 {
return nil, gerror.New("单卡划拨只能选择一张卡")
}
ctx = appContextWithUser(ctx, user)
processor := newAppAllocateBatchProcessor(batch)
rows := appAllocateBatchRows(batch)
req := batchimport.SubmitRequest{
BizParams: param,
SourceType: consts.BatchSourceTypeBatchSelect,
SourceChannel: batchmetadata.MiniProgram,
ExecutionMode: appAllocateExecutionMode(param.ExecutionMode),
BatchType: consts.BatchTypePrepare,
FailureBatchType: consts.BatchTypeAllocate,
SkipExecute: true,
@@ -177,8 +183,11 @@ func (s *sAppCard) AllocateBatchPrepare(ctx context.Context, param *model.AppCar
return s.buildAllocateBatchModel(ctx, user, batch, batchNo, "", consts.BatchTypePrepare, status), nil
}
// AllocateBatchSubmit 确认划拨预处理批次,并更新卡片归属;划拨后原企业的可见和办理权限仍保留。
// AllocateBatchSubmit 确认预处理批次并提交 worker 执行。
func (s *sAppCard) AllocateBatchSubmit(ctx context.Context, param *model.AppCardAllocateBatchSubmitParam) (res *model.AppCardAllocateBatchModel, err error) {
if err := appEnsureFeatureAccess(ctx, "card:allocate"); err != nil {
return nil, err
}
if param == nil {
param = &model.AppCardAllocateBatchSubmitParam{}
}
@@ -190,7 +199,15 @@ func (s *sAppCard) AllocateBatchSubmit(ctx context.Context, param *model.AppCard
return nil, gerror.New("终端用户不能使用卡划拨功能")
}
now := gtime.Now()
if strings.TrimSpace(param.BatchNo) == "" {
if strings.TrimSpace(param.RequestId) != "" {
return s.submitAppAllocateRequest(ctx, user, param)
}
return s.createAppAllocatePending(ctx, user, param)
}
if !appWorkerBatchAsyncEnabled(ctx) {
return nil, gerror.New("划拨执行服务未启用,请联系管理员")
}
var batch *appAllocateBatchContext
var sourceBatchNo string
batchNo := strings.TrimSpace(param.BatchNo)
@@ -199,6 +216,19 @@ func (s *sAppCard) AllocateBatchSubmit(ctx context.Context, param *model.AppCard
if err != nil {
return err
}
if prepareBatch.CreatedDept != user.DeptId {
return gerror.New("当前机构与划拨批次创建机构不一致")
}
if prepareBatch.BatchType == consts.BatchTypeAllocate {
if prepareBatch.BatchInfo.Get("jobRole").String() != "allocate_parent" {
return gerror.New("旧划拨批次不能重新提交,请查看执行日志")
}
if err := appValidateAllocateRetry(prepareBatch, param); err != nil {
return err
}
res = appAllocateQueuedResponse(prepareBatch)
return nil
}
sourceBatchNo = appAllocateSourceBatchNoFromBatch(prepareBatch)
targetDeptId := prepareBatch.TargetDeptId
if param.TargetDeptId > 0 && param.TargetDeptId != targetDeptId {
@@ -245,10 +275,7 @@ func (s *sAppCard) AllocateBatchSubmit(ctx context.Context, param *model.AppCard
})
}
batch.refreshCounts()
if err = s.updateAllocateCards(ctx, tx, user, batch, prepareBatch.BatchNo, now); err != nil {
return err
}
if err = s.markAppAllocatePrepareBatchConfirmed(ctx, tx, user, prepareBatch, batch, sourceBatchNo, strings.TrimSpace(param.Remark), now); err != nil {
if err = s.queueAppAllocateBatch(ctx, tx, user, prepareBatch, batch, strings.TrimSpace(param.Remark)); err != nil {
return err
}
return nil
@@ -256,19 +283,17 @@ func (s *sAppCard) AllocateBatchSubmit(ctx context.Context, param *model.AppCard
if err != nil {
return nil, err
}
if batch == nil {
return nil, gerror.New("划拨提交结果不完整")
if res == nil {
if batch == nil {
return nil, gerror.New("划拨提交结果不完整")
}
res = s.buildAllocateBatchModel(ctx, user, batch, batchNo, sourceBatchNo, consts.BatchTypeAllocate, "pending")
}
if res.Status == "pending" || res.Status == "processing" {
if err = s.publishAppAllocateBatch(ctx, user, batchNo); err != nil {
res.FailReason = "批次已创建,投递失败,请保留批次号并重试提交"
}
}
res = s.buildAllocateBatchModel(ctx, user, batch, batchNo, sourceBatchNo, consts.BatchTypeAllocate, "success")
_ = s.publishAppBusinessAsyncEvent(ctx, appBusinessAsyncEvent{
EventType: appAsyncEventAllocateCompleted,
BatchNo: batchNo,
BatchType: consts.BatchTypeAllocate,
TenantId: user.TenantId,
UserId: user.UserId,
DeptId: user.DeptId,
Trigger: "allocate_submit",
})
return res, nil
}
@@ -368,75 +393,6 @@ func (item *appAllocateCardContext) allocateItemId() string {
return strings.TrimSpace(item.ItemId)
}
func (s *sAppCard) insertAppAllocatePrepareBatch(ctx context.Context, tx gdb.TX, user *entity.SysUser, batch *appAllocateBatchContext, batchNo string, now *gtime.Time) error {
batch.refreshCounts()
batchStatus := consts.BatchAsyncStatusSuccess
runResult := consts.BatchRunResultSuccess
successCount := batch.ValidCount
if batch.FailCount > 0 {
batchStatus = consts.BatchAsyncStatusFailed
runResult = consts.BatchRunResultFailed
successCount = 0
}
_, err := dao.BatchMain.Ctx(ctx).TX(tx).Data(do.BatchMain{
BatchNo: batchNo,
BatchType: consts.BatchTypePrepare,
BatchStatus: batchStatus,
BatchAsyncStatus: batchStatus,
SourceType: consts.BatchSourceTypeBatchSelect,
SourceBatchNo: batchNo,
IsCrossDept: 0,
TargetDeptId: batch.Target.DeptId,
DeptId: user.DeptId,
TotalCount: batch.TotalCount,
ValidCount: batch.ValidCount,
SuccessCount: successCount,
FailCount: batch.FailCount,
RunResult: runResult,
FailReason: batch.FailReason,
BatchInfo: appAllocatePrepareBatchInfo(batch, batchNo),
StartTime: now,
EndTime: now,
TenantId: user.TenantId,
CreatedDept: user.DeptId,
CreatedBy: user.UserId,
CreatedAt: now,
UpdatedBy: user.UserId,
UpdatedAt: now,
}).Insert()
if err != nil {
return gerror.Wrap(err, "写入划拨预处理主表失败")
}
details := make([]do.BatchDetail, 0, len(batch.Cards))
for _, item := range batch.Cards {
detailStatus := consts.BatchDetailStatusSuccess
if strings.TrimSpace(item.ErrorReason) != "" {
detailStatus = consts.BatchDetailStatusFailed
}
details = append(details, do.BatchDetail{
BatchNo: batchNo,
BatchType: consts.BatchTypePrepare,
DetailStatus: detailStatus,
BlankOutStatus: consts.BlankOutStatusNo,
RowNo: item.RowNo,
ItemId: item.allocateItemId(),
ErrorReason: strings.TrimSpace(item.ErrorReason),
Snapshot: appAllocatePrepareDetailSnapshot(batch, item),
TenantId: user.TenantId,
CreatedDept: user.DeptId,
CreatedBy: user.UserId,
CreatedAt: now,
UpdatedBy: user.UserId,
UpdatedAt: now,
})
}
if _, err = dao.BatchDetail.Ctx(ctx).TX(tx).Data(details).Insert(); err != nil {
return gerror.Wrap(err, "写入划拨预处理明细失败")
}
return nil
}
func (s *sAppCard) lockAppAllocatePrepareBatch(ctx context.Context, tx gdb.TX, user *entity.SysUser, batchNo string) (*entity.BatchMain, error) {
batchNo = strings.TrimSpace(batchNo)
if batchNo == "" {
@@ -446,7 +402,7 @@ func (s *sAppCard) lockAppAllocatePrepareBatch(ctx context.Context, tx gdb.TX, u
columns := dao.BatchMain.Columns()
err := dao.BatchMain.Ctx(ctx).TX(tx).
Where(columns.BatchNo, batchNo).
Where(columns.BatchType, consts.BatchTypePrepare).
WhereIn(columns.BatchType, []int{consts.BatchTypePrepare, consts.BatchTypeAllocate}).
Where(columns.TenantId, user.TenantId).
Where(columns.CreatedBy, user.UserId).
LockUpdate().
@@ -461,6 +417,9 @@ func (s *sAppCard) lockAppAllocatePrepareBatch(ctx context.Context, tx gdb.TX, u
if batch.BatchInfo == nil || batch.BatchInfo.Get("targetBatchType").Int() != consts.BatchTypeAllocate {
return nil, gerror.New("批次不是卡划拨预处理批次")
}
if batch.BatchType == consts.BatchTypeAllocate {
return batch, nil
}
if batch.RunResult != consts.BatchRunResultSuccess || batch.FailCount > 0 {
return nil, gerror.New("划拨预处理批次存在校验失败数据,请重新校验后再提交")
}
@@ -533,6 +492,9 @@ func (s *sAppCard) validateAllocateCardBusinessRules(ctx context.Context, tx gdb
if card.BossCardStatus == 4 {
return gerror.Newf("卡%s当前为停机状态,不能划拨", cardLabel)
}
if card.BossCardStatus == 9 {
return gerror.Newf("卡%s已销户,不能划拨", cardLabel)
}
if card.PackageStatus == consts.PackageStatusExpired ||
(card.FlowPkgEndAt != nil && !card.FlowPkgEndAt.IsZero() && card.FlowPkgEndAt.Before(gtime.Now())) {
return gerror.Newf("卡%s套餐已过期,不能划拨", cardLabel)
@@ -540,104 +502,6 @@ func (s *sAppCard) validateAllocateCardBusinessRules(ctx context.Context, tx gdb
return nil
}
func (s *sAppCard) updateAllocateCards(ctx context.Context, tx gdb.TX, user *entity.SysUser, batch *appAllocateBatchContext, batchNo string, now *gtime.Time) error {
for _, item := range batch.Cards {
result, err := dao.TSimCardInfo.Ctx(ctx).TX(tx).
Where(dao.TSimCardInfo.Columns().Id, item.Card.Id).
Where(dao.TSimCardInfo.Columns().DeptId, item.Card.DeptId).
Data(do.TSimCardInfo{
DeptId: batch.Target.DeptId,
CreatedDept: user.DeptId,
OutboundBatchNo: batchNo,
AllocationBatchNo: batchNo,
UpdatedBy: user.UserId,
UpdatedAt: now,
}).
Update()
if err != nil {
return gerror.Wrapf(err, "更新卡%s归属失败", item.Card.Iccid)
}
if affected, err := result.RowsAffected(); err != nil {
return gerror.Wrap(err, "读取划拨更新数量失败")
} else if affected != 1 {
return gerror.Newf("卡%s归属已变化,请重新校验", item.Card.Iccid)
}
if item.SourceDeptId <= 0 {
item.SourceDeptId = item.Card.DeptId
}
item.Card.DeptId = batch.Target.DeptId
item.Card.CreatedDept = user.DeptId
item.Card.OutboundBatchNo = batchNo
item.Card.AllocationBatchNo = batchNo
}
return nil
}
func (s *sAppCard) markAppAllocatePrepareBatchConfirmed(ctx context.Context, tx gdb.TX, user *entity.SysUser, prepareBatch *entity.BatchMain, batch *appAllocateBatchContext, sourceBatchNo string, remark string, now *gtime.Time) error {
batchNo := strings.TrimSpace(prepareBatch.BatchNo)
for _, item := range batch.Cards {
_, err := dao.BatchDetail.Ctx(ctx).TX(tx).
Where(dao.BatchDetail.Columns().BatchNo, batchNo).
Where(dao.BatchDetail.Columns().BatchType, consts.BatchTypePrepare).
Where(dao.BatchDetail.Columns().DetailStatus, consts.BatchDetailStatusSuccess).
Where(dao.BatchDetail.Columns().ItemId, item.Card.Iccid).
Data(do.BatchDetail{
BatchType: consts.BatchTypeAllocate,
Snapshot: appAllocateSubmitDetailSnapshot(batch, item, batchNo, sourceBatchNo),
UpdatedBy: user.UserId,
UpdatedAt: now,
}).
Update()
if err != nil {
return gerror.Wrap(err, "更新划拨批次明细失败")
}
}
if _, err := dao.BatchDetail.Ctx(ctx).TX(tx).
Where(dao.BatchDetail.Columns().BatchNo, batchNo).
Where(dao.BatchDetail.Columns().BatchType, consts.BatchTypePrepare).
Data(do.BatchDetail{
BatchType: consts.BatchTypeAllocate,
UpdatedBy: user.UserId,
UpdatedAt: now,
}).
Update(); err != nil {
return gerror.Wrap(err, "更新划拨批次明细类型失败")
}
logFiles, err := s.ensureAppAllocateBatchLogFiles(ctx, tx, user, batch, prepareBatch.BatchInfo, batchNo, sourceBatchNo, remark, now)
if err != nil {
return err
}
batchInfo := appAllocateBatchInfoWithLogFiles(
appAllocateSubmitBatchInfo(batch, batchNo, sourceBatchNo, remark, now),
logFiles,
)
_, err = dao.BatchMain.Ctx(ctx).TX(tx).
Where(dao.BatchMain.Columns().Id, prepareBatch.Id).
Where(dao.BatchMain.Columns().BatchType, consts.BatchTypePrepare).
Data(do.BatchMain{
BatchType: consts.BatchTypeAllocate,
BatchStatus: consts.BatchStatusSuccess,
BatchAsyncStatus: consts.BatchAsyncStatusSuccess,
TargetDeptId: batch.Target.DeptId,
TotalCount: len(batch.Cards),
ValidCount: len(batch.Cards),
SuccessCount: len(batch.Cards),
FailCount: 0,
RunResult: consts.BatchRunResultSuccess,
BatchInfo: batchInfo,
EndTime: now,
UpdatedBy: user.UserId,
UpdatedAt: now,
}).
Update()
if err != nil {
return gerror.Wrap(err, "更新划拨批次主表失败")
}
return nil
}
func (s *sAppCard) loadAllocateTarget(ctx context.Context, user *entity.SysUser, targetDeptId int64) (*entity.SysDept, error) {
if targetDeptId <= 0 {
return nil, gerror.New("请选择划拨目标客户")
@@ -649,6 +513,9 @@ func (s *sAppCard) loadAllocateTarget(ctx context.Context, user *entity.SysUser,
if target == nil || target.DeptId == 0 {
return nil, gerror.New("划拨目标客户不存在")
}
if target.Status != "0" {
return nil, gerror.New("划拨目标客户未启用")
}
if target.DeptId == user.DeptId {
return nil, gerror.New("划拨目标不能选择当前企业")
}