chore: 并入 iotlcm-admin-svr 历史到 admin-svr/(原提交 49818c6)
This commit is contained in:
@@ -0,0 +1,17 @@
|
||||
package unsub
|
||||
|
||||
import (
|
||||
"context"
|
||||
|
||||
"git.5iot.com/iotlcm-public/iotlcm-admin-svr/internal/library/batchimport"
|
||||
"git.5iot.com/iotlcm-public/iotlcm-admin-svr/internal/logic/iot/simcardbatch/common/cardsource"
|
||||
)
|
||||
|
||||
func ParseInputRows(ctx context.Context, input string) ([]batchimport.Row, error) {
|
||||
return cardsource.BuildIccidRows(ctx, input, "批量输入退订数据为空", func(line cardsource.IccidLine) *Row {
|
||||
return &Row{
|
||||
RowNo: line.RowNo,
|
||||
Iccid: line.Iccid,
|
||||
}
|
||||
})
|
||||
}
|
||||
@@ -0,0 +1,50 @@
|
||||
package unsub
|
||||
|
||||
import (
|
||||
"context"
|
||||
"io"
|
||||
"git.5iot.com/iotlcm-public/iotlcm-admin-svr/internal/library/batchimport"
|
||||
"git.5iot.com/iotlcm-public/iotlcm-admin-svr/internal/library/batchinput/excel"
|
||||
)
|
||||
|
||||
func ParseExcelFileRows(ctx context.Context, filePath string) ([]batchimport.Row, error) {
|
||||
parsedRows, err := excel.ParseFile(ctx, filePath, excel.ParseOptions{
|
||||
RequiredHeaders: []string{"ICCID"},
|
||||
})
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
return toBatchRows(parsedRows), nil
|
||||
}
|
||||
|
||||
func ParseExcelRows(ctx context.Context, reader io.Reader) ([]*Row, error) {
|
||||
parsedRows, err := excel.ParseReader(ctx, reader, excel.ParseOptions{
|
||||
RequiredHeaders: []string{"ICCID"},
|
||||
})
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
return toRows(parsedRows), nil
|
||||
}
|
||||
|
||||
func toBatchRows(parsedRows []excel.ParsedRow) []batchimport.Row {
|
||||
rows := toRows(parsedRows)
|
||||
result := make([]batchimport.Row, 0, len(rows))
|
||||
for _, row := range rows {
|
||||
result = append(result, row)
|
||||
}
|
||||
return result
|
||||
}
|
||||
|
||||
func toRows(parsedRows []excel.ParsedRow) []*Row {
|
||||
rows := make([]*Row, 0, len(parsedRows))
|
||||
for _, parsed := range parsedRows {
|
||||
rows = append(rows, &Row{
|
||||
RowNo: parsed.RowNo,
|
||||
Iccid: excel.Field(parsed, "ICCID"),
|
||||
Msisdn: excel.Field(parsed, "MSISDN"),
|
||||
Imsi: excel.Field(parsed, "IMSI"),
|
||||
})
|
||||
}
|
||||
return rows
|
||||
}
|
||||
@@ -0,0 +1,230 @@
|
||||
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),
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,148 @@
|
||||
package unsub
|
||||
|
||||
import (
|
||||
"context"
|
||||
"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/model"
|
||||
|
||||
"github.com/gogf/gf/v2/encoding/gjson"
|
||||
"github.com/gogf/gf/v2/frame/g"
|
||||
)
|
||||
|
||||
type Processor struct {
|
||||
config batchimport.ProcessorConfig
|
||||
}
|
||||
|
||||
type submitOptions struct {
|
||||
SourceBatchNo string
|
||||
Remark string
|
||||
}
|
||||
|
||||
func NewProcessor() *Processor {
|
||||
return &Processor{
|
||||
config: batchimport.ProcessorConfig{
|
||||
Code: "sim_card_unsub",
|
||||
BatchType: consts.BatchTypeUnsub,
|
||||
BatchNoPrefix: consts.BatchNoPrefixUnsub,
|
||||
TaskName: consts.ResourceBatchDownloadTaskSimCardUnsub,
|
||||
ExportScene: consts.ResourceExportSceneUnsub,
|
||||
ImportDir: consts.SimCardUnsubSubDirImportExcel,
|
||||
ResultDir: consts.SimCardUnsubSubDirResultExcel,
|
||||
MaxFileSize: consts.SimCardUnsubMaxFileSize,
|
||||
TemplateHeaders: []string{"ICCID", "MSISDN", "IMSI"},
|
||||
RequiredHeaders: []string{"ICCID"},
|
||||
},
|
||||
}
|
||||
}
|
||||
|
||||
func (p *Processor) Config() batchimport.ProcessorConfig {
|
||||
return p.config
|
||||
}
|
||||
|
||||
func (p *Processor) ParseRows(ctx context.Context, filePath string) ([]batchimport.Row, error) {
|
||||
return ParseExcelFileRows(ctx, filePath)
|
||||
}
|
||||
|
||||
func (p *Processor) ValidateRows(ctx context.Context, rows []batchimport.Row) error {
|
||||
unsubRows := make([]*Row, 0, len(rows))
|
||||
for _, item := range rows {
|
||||
row, ok := item.(*Row)
|
||||
if !ok || row == nil {
|
||||
item.AddErrorReason("退订行类型错误")
|
||||
continue
|
||||
}
|
||||
unsubRows = append(unsubRows, row)
|
||||
}
|
||||
return validateRows(unsubRows)
|
||||
}
|
||||
|
||||
func (p *Processor) BuildBatchInfo(ctx context.Context, req batchimport.SubmitRequest, result *batchimport.Result, importOss *model.SysOssSaveContentModel, resultOss *model.SysOssSaveContentModel, downloadId int64) *gjson.Json {
|
||||
info := g.Map{
|
||||
"operation": "unsub",
|
||||
"resultFile": g.Map{"status": "pending"},
|
||||
"submitScene": submitScene(req.BizParams),
|
||||
}
|
||||
if result != nil && result.ImportFileOssId > 0 {
|
||||
info["importFileOssId"] = result.ImportFileOssId
|
||||
} else if importOss != nil && importOss.OssId > 0 {
|
||||
info["importFileOssId"] = importOss.OssId
|
||||
}
|
||||
if opts, ok := maybeUnsubSubmitOptions(req.BizParams); ok {
|
||||
if opts.SourceBatchNo != "" {
|
||||
info["sourceBatchNo"] = opts.SourceBatchNo
|
||||
}
|
||||
if opts.Remark != "" {
|
||||
info["remark"] = opts.Remark
|
||||
}
|
||||
}
|
||||
return gjson.New(info)
|
||||
}
|
||||
|
||||
func submitScene(params any) string {
|
||||
switch params.(type) {
|
||||
case *model.TSimCardUnsubBatchSubmitParam:
|
||||
return "sourceBatch"
|
||||
case *model.TSimCardUnsubExcelSubmitParam:
|
||||
return "excel"
|
||||
case *model.TSimCardUnsubInputSubmitParam:
|
||||
return "input"
|
||||
case *model.TSimCardUnsubRangeSubmitParam:
|
||||
return "range"
|
||||
default:
|
||||
return ""
|
||||
}
|
||||
}
|
||||
|
||||
func validateRows(rows []*Row) error {
|
||||
firstByIccid := make(map[string]*Row, len(rows))
|
||||
for _, row := range rows {
|
||||
if row == nil {
|
||||
continue
|
||||
}
|
||||
row.Iccid = strings.TrimSpace(row.Iccid)
|
||||
if row.Iccid == "" {
|
||||
row.AddReason("ICCID不能为空")
|
||||
continue
|
||||
}
|
||||
if first, ok := firstByIccid[row.Iccid]; ok {
|
||||
row.AddReason("ICCID重复")
|
||||
first.AddReason("ICCID重复")
|
||||
continue
|
||||
}
|
||||
firstByIccid[row.Iccid] = row
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
func maybeUnsubSubmitOptions(params any) (submitOptions, bool) {
|
||||
switch in := params.(type) {
|
||||
case *model.TSimCardUnsubBatchSubmitParam:
|
||||
if in == nil {
|
||||
return submitOptions{}, false
|
||||
}
|
||||
return submitOptions{
|
||||
SourceBatchNo: strings.TrimSpace(in.SourceBatchNo),
|
||||
Remark: strings.TrimSpace(in.Remark),
|
||||
}, true
|
||||
case *model.TSimCardUnsubExcelSubmitParam:
|
||||
if in == nil {
|
||||
return submitOptions{}, false
|
||||
}
|
||||
return submitOptions{}, true
|
||||
case *model.TSimCardUnsubInputSubmitParam:
|
||||
if in == nil {
|
||||
return submitOptions{}, false
|
||||
}
|
||||
return submitOptions{Remark: strings.TrimSpace(in.Remark)}, true
|
||||
case *model.TSimCardUnsubRangeSubmitParam:
|
||||
if in == nil {
|
||||
return submitOptions{}, false
|
||||
}
|
||||
return submitOptions{Remark: strings.TrimSpace(in.Remark)}, true
|
||||
default:
|
||||
return submitOptions{}, false
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,47 @@
|
||||
package unsub
|
||||
|
||||
import (
|
||||
"context"
|
||||
"testing"
|
||||
|
||||
"git.5iot.com/iotlcm-public/iotlcm-admin-svr/internal/library/batchimport"
|
||||
"git.5iot.com/iotlcm-public/iotlcm-admin-svr/internal/model"
|
||||
)
|
||||
|
||||
func TestProcessorConfigTemplateHeaders(t *testing.T) {
|
||||
config := NewProcessor().Config()
|
||||
expected := []string{"ICCID", "MSISDN", "IMSI"}
|
||||
if len(config.TemplateHeaders) != len(expected) {
|
||||
t.Fatalf("unexpected template header count: got %d want %d", len(config.TemplateHeaders), len(expected))
|
||||
}
|
||||
for i, header := range expected {
|
||||
if config.TemplateHeaders[i] != header {
|
||||
t.Fatalf("unexpected template header at %d: got %q want %q", i, config.TemplateHeaders[i], header)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
func TestBuildBatchInfoExcelDoesNotIncludeRemarkOrSourceBatchNo(t *testing.T) {
|
||||
processor := NewProcessor()
|
||||
info := processor.BuildBatchInfo(context.Background(), batchimport.SubmitRequest{
|
||||
BizParams: &model.TSimCardUnsubExcelSubmitParam{},
|
||||
}, nil, nil, nil, 0)
|
||||
|
||||
if info.Get("remark").String() != "" {
|
||||
t.Fatalf("excel batch info should not include remark")
|
||||
}
|
||||
if info.Get("sourceBatchNo").String() != "" {
|
||||
t.Fatalf("excel batch info should not include sourceBatchNo")
|
||||
}
|
||||
}
|
||||
|
||||
func TestBuildBatchInfoInputKeepsRemark(t *testing.T) {
|
||||
processor := NewProcessor()
|
||||
info := processor.BuildBatchInfo(context.Background(), batchimport.SubmitRequest{
|
||||
BizParams: &model.TSimCardUnsubInputSubmitParam{Remark: " note "},
|
||||
}, nil, nil, nil, 0)
|
||||
|
||||
if got := info.Get("remark").String(); got != "note" {
|
||||
t.Fatalf("unexpected remark: got %q", got)
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,32 @@
|
||||
package unsub
|
||||
|
||||
import (
|
||||
"context"
|
||||
|
||||
"git.5iot.com/iotlcm-public/iotlcm-admin-svr/internal/library/batchimport"
|
||||
"git.5iot.com/iotlcm-public/iotlcm-admin-svr/internal/logic/iot/simcardbatch/common/cardsource"
|
||||
)
|
||||
|
||||
const maxRangeRows = 100000
|
||||
|
||||
func BuildRangeRows(ctx context.Context, startIccid, endIccid, startMsisdn, endMsisdn, startImsi, endImsi string) ([]batchimport.Row, error) {
|
||||
return cardsource.BuildRangeRows(ctx, cardsource.RangeConfig{
|
||||
StartIccid: startIccid,
|
||||
EndIccid: endIccid,
|
||||
StartMsisdn: startMsisdn,
|
||||
EndMsisdn: endMsisdn,
|
||||
StartImsi: startImsi,
|
||||
EndImsi: endImsi,
|
||||
MaxRows: maxRangeRows,
|
||||
EmptyMessage: "连号退订数据为空",
|
||||
OverLimitMessage: "连号退订数量不能超过%d条",
|
||||
CountMismatchMessage: "ICCID号段数量(%d)、MSISDN号段数量(%d)、IMSI号段数量(%d)必须一致",
|
||||
}, func(rowNo int, iccid, msisdn, imsi string) *Row {
|
||||
return &Row{
|
||||
RowNo: rowNo,
|
||||
Iccid: iccid,
|
||||
Msisdn: msisdn,
|
||||
Imsi: imsi,
|
||||
}
|
||||
})
|
||||
}
|
||||
@@ -0,0 +1,83 @@
|
||||
package unsub
|
||||
|
||||
import (
|
||||
"context"
|
||||
"strings"
|
||||
|
||||
"git.5iot.com/iotlcm-public/iotlcm-admin-svr/internal/consts"
|
||||
"git.5iot.com/iotlcm-public/iotlcm-admin-svr/internal/dao"
|
||||
"git.5iot.com/iotlcm-public/iotlcm-admin-svr/internal/model/entity"
|
||||
)
|
||||
|
||||
type Repository struct{}
|
||||
|
||||
func NewRepository() *Repository {
|
||||
return &Repository{}
|
||||
}
|
||||
|
||||
func (r *Repository) GetSourceBatch(ctx context.Context, batchNo string) (*entity.BatchMain, error) {
|
||||
batch := &entity.BatchMain{}
|
||||
columns := dao.BatchMain.Columns()
|
||||
err := dao.BatchMain.Ctx(ctx).
|
||||
Fields(
|
||||
columns.Id,
|
||||
columns.BatchNo,
|
||||
columns.BatchType,
|
||||
columns.BatchStatus,
|
||||
columns.RunResult,
|
||||
columns.TenantId,
|
||||
columns.DeptId,
|
||||
columns.CreatedDept,
|
||||
columns.CreatedBy,
|
||||
).
|
||||
Where(columns.BatchNo, strings.TrimSpace(batchNo)).
|
||||
Scan(batch)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
if batch.Id == 0 {
|
||||
return nil, nil
|
||||
}
|
||||
return batch, nil
|
||||
}
|
||||
|
||||
func (r *Repository) GetSourceBatchRows(ctx context.Context, batchNo string) ([]*Row, error) {
|
||||
var details []*entity.BatchDetail
|
||||
columns := dao.BatchDetail.Columns()
|
||||
err := dao.BatchDetail.Ctx(ctx).
|
||||
Fields(columns.RowNo, columns.ItemId, columns.Snapshot).
|
||||
Where(columns.BatchNo, strings.TrimSpace(batchNo)).
|
||||
Where(columns.BatchType, consts.BatchTypeOutbound).
|
||||
WhereIn(columns.DetailStatus, []int{consts.BatchDetailStatusSuccess, consts.BatchAsyncStatusSuccess}).
|
||||
OrderAsc(columns.RowNo).
|
||||
Scan(&details)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
rows := make([]*Row, 0, len(details))
|
||||
for _, detail := range details {
|
||||
if detail == nil {
|
||||
continue
|
||||
}
|
||||
row := &Row{RowNo: detail.RowNo, Iccid: strings.TrimSpace(detail.ItemId)}
|
||||
if detail.Snapshot != nil {
|
||||
if row.Iccid == "" {
|
||||
row.Iccid = strings.TrimSpace(detail.Snapshot.Get("iccid").String())
|
||||
}
|
||||
if row.Iccid == "" {
|
||||
row.Iccid = strings.TrimSpace(detail.Snapshot.Get("ICCID").String())
|
||||
}
|
||||
row.Msisdn = strings.TrimSpace(detail.Snapshot.Get("msisdn").String())
|
||||
if row.Msisdn == "" {
|
||||
row.Msisdn = strings.TrimSpace(detail.Snapshot.Get("MSISDN").String())
|
||||
}
|
||||
row.Imsi = strings.TrimSpace(detail.Snapshot.Get("imsi").String())
|
||||
if row.Imsi == "" {
|
||||
row.Imsi = strings.TrimSpace(detail.Snapshot.Get("IMSI").String())
|
||||
}
|
||||
}
|
||||
rows = append(rows, row)
|
||||
}
|
||||
return rows, nil
|
||||
}
|
||||
@@ -0,0 +1,61 @@
|
||||
package unsub
|
||||
|
||||
import (
|
||||
"github.com/gogf/gf/v2/encoding/gjson"
|
||||
"github.com/gogf/gf/v2/frame/g"
|
||||
)
|
||||
|
||||
type Row struct {
|
||||
RowNo int
|
||||
Iccid string
|
||||
Msisdn string
|
||||
Imsi string
|
||||
Reason string
|
||||
}
|
||||
|
||||
func (r *Row) Valid() bool {
|
||||
return r != nil && r.Reason == ""
|
||||
}
|
||||
|
||||
func (r *Row) AddReason(reason string) {
|
||||
if r == nil || r.Reason != "" || reason == "" {
|
||||
return
|
||||
}
|
||||
r.Reason = reason
|
||||
}
|
||||
|
||||
func (r *Row) GetRowNo() int {
|
||||
if r == nil {
|
||||
return 0
|
||||
}
|
||||
return r.RowNo
|
||||
}
|
||||
|
||||
func (r *Row) GetItemID() string {
|
||||
if r == nil {
|
||||
return ""
|
||||
}
|
||||
return r.Iccid
|
||||
}
|
||||
|
||||
func (r *Row) GetErrorReasons() []string {
|
||||
if r == nil || r.Reason == "" {
|
||||
return nil
|
||||
}
|
||||
return []string{r.Reason}
|
||||
}
|
||||
|
||||
func (r *Row) AddErrorReason(reason string) {
|
||||
r.AddReason(reason)
|
||||
}
|
||||
|
||||
func (r *Row) Snapshot() *gjson.Json {
|
||||
if r == nil {
|
||||
return gjson.New(g.Map{})
|
||||
}
|
||||
return gjson.New(g.Map{
|
||||
"ICCID": r.Iccid,
|
||||
"MSISDN": r.Msisdn,
|
||||
"IMSI": r.Imsi,
|
||||
})
|
||||
}
|
||||
Reference in New Issue
Block a user