140 lines
4.3 KiB
Go
140 lines
4.3 KiB
Go
package addpackage
|
|
|
|
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/logic/iot/simcardbatch/common"
|
|
"git.5iot.com/iotlcm-public/iotlcm-admin-svr/utility/iot_excelize"
|
|
|
|
"github.com/gogf/gf/v2/net/ghttp"
|
|
"github.com/xuri/excelize/v2"
|
|
)
|
|
|
|
type Submitter struct {
|
|
operation Operation
|
|
}
|
|
|
|
func NewCardSubmitter() *Submitter {
|
|
return &Submitter{operation: OperationCard}
|
|
}
|
|
|
|
func NewPoolSubmitter() *Submitter {
|
|
return &Submitter{operation: OperationPool}
|
|
}
|
|
|
|
func (s *Submitter) DownloadTemplate(ctx context.Context) error {
|
|
processor, err := NewProcessor(s.operation, SubmitOptions{})
|
|
if err != nil {
|
|
return err
|
|
}
|
|
f := excelize.NewFile()
|
|
defer func() { _ = f.Close() }()
|
|
|
|
headers := processor.TemplateHeaders()
|
|
if err := f.SetSheetRow("Sheet1", "A1", &headers); err != nil {
|
|
return err
|
|
}
|
|
if err := iot_excelize.ApplyHeaderColumnsTextStyle(f); err != nil {
|
|
return err
|
|
}
|
|
if err := f.SetColWidth("Sheet1", "A", "B", 24); err != nil {
|
|
return err
|
|
}
|
|
if err := f.SetColWidth("Sheet1", "C", "C", 32); err != nil {
|
|
return err
|
|
}
|
|
fileName := "sim_card_add_package_template"
|
|
if s.operation == OperationPool {
|
|
fileName = "sim_pool_add_package_template"
|
|
}
|
|
return iot_excelize.WriteHTTP(ctx, f, fileName)
|
|
}
|
|
|
|
func (s *Submitter) SubmitExcel(ctx context.Context, file *ghttp.UploadFile, options SubmitOptions) (*SubmitResult, error) {
|
|
options.AutoConfirm = !options.ManualConfirm
|
|
processor, err := NewPrepareProcessor(s.operation, options)
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
req := batchimport.SubmitRequest{
|
|
SourceChannel: batchmetadata.Console,
|
|
ExecutionMode: batchmetadata.Bulk,
|
|
File: file,
|
|
BizParams: &processor.inner.options,
|
|
SourceType: consts.BatchSourceTypeFile,
|
|
Operator: common.CurrentOperator(ctx),
|
|
}
|
|
result, err := batchimport.CreatePendingUpload(ctx, req, processor, batchimport.PendingOptions{})
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
common.PublishBatchMessage(ctx, result, consts.QueueBatchExecute, string(s.operation)+"_add_package_prepare")
|
|
res := pendingResultFromBatch(result, consts.BatchTypePrepare)
|
|
res.TargetBatchType = processor.targetBatchType
|
|
return res, nil
|
|
}
|
|
|
|
func (s *Submitter) SubmitInput(ctx context.Context, input string, options SubmitOptions) (*SubmitResult, error) {
|
|
// Keep the legacy input/submit endpoint from bypassing async validation.
|
|
return s.PrepareInput(ctx, input, options)
|
|
}
|
|
|
|
func (s *Submitter) PrepareInput(ctx context.Context, input string, options SubmitOptions) (*SubmitResult, error) {
|
|
if strings.TrimSpace(options.PackageCode) != "" || strings.TrimSpace(options.EffectiveMode) != "" {
|
|
options.DefaultEffectiveMode = true
|
|
}
|
|
processor, err := NewPrepareProcessor(s.operation, options)
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
rows, err := parseInputRows(ctx, input, processor.inner.options)
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
req := batchimport.SubmitRequest{
|
|
SourceChannel: batchmetadata.Console,
|
|
ExecutionMode: batchmetadata.Bulk,
|
|
BizParams: &processor.inner.options,
|
|
SourceType: consts.BatchSourceTypeBatchInput,
|
|
Operator: common.CurrentOperator(ctx),
|
|
}
|
|
result, err := batchimport.CreatePendingRows(ctx, req, processor, rows, batchimport.PendingOptions{})
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
common.PublishBatchMessage(ctx, result, consts.QueueBatchExecute, string(s.operation)+"_add_package_prepare")
|
|
res := pendingResultFromBatch(result, consts.BatchTypePrepare)
|
|
res.TargetBatchType = processor.targetBatchType
|
|
return res, nil
|
|
}
|
|
|
|
func pendingResultFromBatch(result *batchimport.Result, batchType int) *SubmitResult {
|
|
res := &SubmitResult{
|
|
BatchType: batchType,
|
|
BatchAsyncStatus: consts.BatchAsyncStatusPending,
|
|
}
|
|
if result == nil {
|
|
return res
|
|
}
|
|
valid := result.ValidRows
|
|
if result.TotalRows > 0 && valid == 0 {
|
|
valid = result.TotalRows - result.ErrorRows
|
|
if valid < 0 {
|
|
valid = 0
|
|
}
|
|
}
|
|
res.BatchNo = result.BatchNo
|
|
res.BatchType = result.BatchType
|
|
if batchType != result.BatchType {
|
|
res.TargetBatchType = batchType
|
|
}
|
|
res.Total = result.TotalRows
|
|
res.ValidCount = valid
|
|
res.FailCount = result.ErrorRows
|
|
return res
|
|
}
|