Files
iotlcm/server/internal/logic/iot/simcardbatch/unsub/pending.go
T
MiFaZhan 59eb34976a chore: 初始开源发布
iotlcm-admin-svr:管理后台服务。

- Go module:iotlcm → git.5iot.com/iotlcm-public/iotlcm-admin-svr(729 文件)
- 凭据与地址脱敏:JWT signingKey(含代码里的 MustGet 默认值)、
  MySQL DSN、Redis 口令、告警邮件发件人邮箱全部改为占位;
  私网 IP → 文档地址段;库名 xiujie_admin → iotlcm
- 真实手机号/邮箱 → 合法 11 位编造号 / example.com
- 移除内部资产:CI 触发器、过时 DDL(sql/db_iotlcm.sql)、
  私有依赖 go get 行、明文 registry 登录口令
- 启动 banner 统一 IOTLCM 方块字 + 社区惯例署名
- Dockerfile 改用公共基础镜像
- 拼错的 RADEME.md → README.md,README 重写
- 需要真实数据库的用例改为可用 IOTLCM_TEST_WITH_DB=1 显式启用
- 补 Apache-2.0 LICENSE,LF 行尾 + .gitattributes

数据库 schema 不随本仓发布(生产 DDL 已过时),README 已说明。
2026-10-23 14:48:30 +08:00

231 lines
6.8 KiB
Go

package unsub
import (
"context"
"git.5iot.com/iotlcm-public/iotlcm-admin-svr/internal/library/batchmetadata"
"strings"
"git.5iot.com/iotlcm-public/iotlcm-admin-svr/internal/consts"
"git.5iot.com/iotlcm-public/iotlcm-admin-svr/internal/library/batchimport"
"git.5iot.com/iotlcm-public/iotlcm-admin-svr/internal/library/contexts"
"git.5iot.com/iotlcm-public/iotlcm-admin-svr/internal/library/rabbitmq"
"git.5iot.com/iotlcm-public/iotlcm-admin-svr/internal/model"
"github.com/gogf/gf/v2/errors/gerror"
"github.com/gogf/gf/v2/frame/g"
"github.com/gogf/gf/v2/net/ghttp"
)
type BatchExecuteMessage struct {
BatchNo string `json:"batchNo"`
BatchType int `json:"batchType"`
}
type PendingResult struct {
BatchNo string
BatchType int
BatchAsyncStatus int
Total int
ValidCount int
FailCount int
}
type Publisher interface {
PublishBatch(ctx context.Context, msg BatchExecuteMessage) error
}
type rabbitPublisher struct{}
func (rabbitPublisher) PublishBatch(ctx context.Context, msg BatchExecuteMessage) error {
return rabbitmq.Publish(ctx, consts.QueueBatchExecute, msg)
}
var publisher Publisher = rabbitPublisher{}
func SetPublisher(p Publisher) {
if p == nil {
publisher = rabbitPublisher{}
return
}
publisher = p
}
func CreatePendingExcel(ctx context.Context, file *ghttp.UploadFile, in *model.TSimCardUnsubExcelSubmitParam) (*PendingResult, error) {
if in == nil {
in = &model.TSimCardUnsubExcelSubmitParam{}
}
processor := NewProcessor()
req := batchimport.SubmitRequest{
SourceChannel: batchmetadata.Console,
ExecutionMode: batchmetadata.Bulk,
File: file,
BizParams: in,
SourceType: consts.BatchSourceTypeFile,
Operator: operatorContext(ctx),
}
result, err := batchimport.CreatePendingUpload(ctx, req, processor, batchimport.PendingOptions{})
if err != nil {
return nil, err
}
return finalizePending(ctx, processor, result), nil
}
func CreatePendingRange(ctx context.Context, in *model.TSimCardUnsubRangeSubmitParam) (*PendingResult, error) {
if in == nil {
in = &model.TSimCardUnsubRangeSubmitParam{}
}
rows, err := BuildRangeRows(ctx, in.StartIccid, in.EndIccid, in.StartMsisdn, in.EndMsisdn, in.StartImsi, in.EndImsi)
if err != nil {
return nil, err
}
processor := NewProcessor()
req := batchimport.SubmitRequest{
SourceChannel: batchmetadata.Console,
ExecutionMode: batchmetadata.Bulk,
BizParams: in,
SourceType: consts.BatchSourceTypeRange,
Operator: operatorContext(ctx),
}
result, err := batchimport.CreatePendingRows(ctx, req, processor, rows, batchimport.PendingOptions{})
if err != nil {
return nil, err
}
return finalizePending(ctx, processor, result), nil
}
func CreatePendingInput(ctx context.Context, in *model.TSimCardUnsubInputSubmitParam) (*PendingResult, error) {
if in == nil {
in = &model.TSimCardUnsubInputSubmitParam{}
}
rows, err := ParseInputRows(ctx, in.Input)
if err != nil {
return nil, err
}
processor := NewProcessor()
req := batchimport.SubmitRequest{
SourceChannel: batchmetadata.Console,
ExecutionMode: batchmetadata.Bulk,
BizParams: in,
SourceType: consts.BatchSourceTypeBatchInput,
Operator: operatorContext(ctx),
}
result, err := batchimport.CreatePendingRows(ctx, req, processor, rows, batchimport.PendingOptions{})
if err != nil {
return nil, err
}
return finalizePending(ctx, processor, result), nil
}
func CreatePendingBatch(ctx context.Context, in *model.TSimCardUnsubBatchSubmitParam) (*PendingResult, error) {
if in == nil {
in = &model.TSimCardUnsubBatchSubmitParam{}
}
in.SourceBatchNo = strings.TrimSpace(in.SourceBatchNo)
if in.SourceBatchNo == "" {
return nil, gerror.New("来源批次号不能为空")
}
processor := NewProcessor()
repo := NewRepository()
sourceBatch, err := repo.GetSourceBatch(ctx, in.SourceBatchNo)
if err != nil {
return nil, gerror.Wrap(err, "查询来源批次失败")
}
if sourceBatch == nil {
return nil, gerror.New("来源批次不存在")
}
if tenantId := contexts.GetTenantId(ctx); tenantId != "" && sourceBatch.TenantId != tenantId {
return nil, gerror.New("来源批次不存在")
}
if sourceBatch.BatchType != consts.BatchTypeOutbound {
return nil, gerror.New("来源批次不是出库批次")
}
if sourceBatch.RunResult != consts.BatchRunResultSuccess {
return nil, gerror.New("来源批次不是成功批次")
}
rows, err := repo.GetSourceBatchRows(ctx, in.SourceBatchNo)
if err != nil {
return nil, gerror.Wrap(err, "查询来源批次卡明细失败")
}
if len(rows) == 0 {
return nil, gerror.New("来源批次没有可退订卡")
}
importRows := make([]batchimport.Row, 0, len(rows))
for _, row := range rows {
importRows = append(importRows, row)
}
req := batchimport.SubmitRequest{
SourceChannel: batchmetadata.Console,
ExecutionMode: batchmetadata.Bulk,
BizParams: in,
SourceType: consts.BatchSourceTypeBatchSelect,
SourceBatchNo: in.SourceBatchNo,
Operator: operatorContext(ctx),
}
result, err := batchimport.CreatePendingRows(ctx, req, processor, importRows, batchimport.PendingOptions{})
if err != nil {
return nil, err
}
return finalizePending(ctx, processor, result), nil
}
func finalizePending(ctx context.Context, p *Processor, result *batchimport.Result) *PendingResult {
if result == nil {
result = &batchimport.Result{}
}
if result.BatchType == 0 {
result.BatchType = p.Config().BatchType
}
publishPending(ctx, result.BatchNo, result.BatchType)
return pendingResultFromBatch(result, p)
}
func pendingResultFromBatch(result *batchimport.Result, p *Processor) *PendingResult {
if result == nil {
return &PendingResult{
BatchType: p.Config().BatchType,
BatchAsyncStatus: consts.BatchAsyncStatusPending,
}
}
valid := result.ValidRows
if result.TotalRows > 0 && valid == 0 {
valid = result.TotalRows - result.ErrorRows
if valid < 0 {
valid = 0
}
}
return &PendingResult{
BatchNo: result.BatchNo,
BatchType: result.BatchType,
BatchAsyncStatus: consts.BatchAsyncStatusPending,
Total: result.TotalRows,
ValidCount: valid,
FailCount: result.ErrorRows,
}
}
func publishPending(ctx context.Context, batchNo string, batchType int) {
batchNo = strings.TrimSpace(batchNo)
if batchNo == "" {
return
}
if batchType == 0 {
batchType = consts.BatchTypeUnsub
}
msg := BatchExecuteMessage{BatchNo: batchNo, BatchType: batchType}
if err := publisher.PublishBatch(ctx, msg); err != nil {
g.Log().Errorf(ctx, "unsub batch async publish failed, batchNo=%s, batchType=%d, tenantId=%s, error=%+v",
batchNo, batchType, contexts.GetTenantId(ctx), err)
}
}
func operatorContext(ctx context.Context) batchimport.OperatorContext {
return batchimport.OperatorContext{
TenantId: contexts.GetTenantId(ctx),
DeptId: contexts.GetDeptId(ctx),
UserId: contexts.GetUserId(ctx),
}
}