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 已说明。
This commit is contained in:
@@ -0,0 +1,204 @@
|
||||
package batchimport
|
||||
|
||||
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/dao"
|
||||
"git.5iot.com/iotlcm-public/iotlcm-admin-svr/internal/model"
|
||||
"git.5iot.com/iotlcm-public/iotlcm-admin-svr/internal/model/do"
|
||||
"git.5iot.com/iotlcm-public/iotlcm-admin-svr/internal/service"
|
||||
|
||||
"github.com/gogf/gf/v2/database/gdb"
|
||||
"github.com/gogf/gf/v2/errors/gerror"
|
||||
"github.com/gogf/gf/v2/frame/g"
|
||||
"github.com/gogf/gf/v2/os/gtime"
|
||||
)
|
||||
|
||||
type PendingOptions struct {
|
||||
PrepareRows func(ctx context.Context, rows []Row) error
|
||||
}
|
||||
|
||||
const pendingDetailInsertBatchSize = 500
|
||||
|
||||
func CreatePendingUpload(ctx context.Context, req SubmitRequest, p PendingProcessor, options PendingOptions) (*Result, error) {
|
||||
if err := batchmetadata.ValidateCreation(req.SourceChannel, req.ExecutionMode); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
cfg := p.Config()
|
||||
if req.File == nil {
|
||||
return nil, gerror.New("请选择上传文件")
|
||||
}
|
||||
if err := service.SysOss().CheckType(ctx, consts.SysUploadFileTypeFile, req.File); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
if req.File.Size > cfg.MaxFileSize {
|
||||
return nil, gerror.Newf("上传文件大小超过限制,最大为%dM", cfg.MaxFileSize>>20)
|
||||
}
|
||||
|
||||
batchNo, err := NextBatchNoContext(ctx, cfg.BatchNoPrefix)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
req.BatchNo = batchNo
|
||||
content, err := readUploadContent(req.File)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
importOss, err := service.SysOss().SaveContent(ctx, &model.SysOssSaveContentParam{
|
||||
Content: content,
|
||||
FileName: batchNo + "-import.xlsx",
|
||||
NewFileType: consts.SysUploadNewFileTypeUserFile,
|
||||
SubDirName: cfg.ImportDir,
|
||||
})
|
||||
if err != nil {
|
||||
return nil, gerror.Wrap(err, "保存原始Excel失败")
|
||||
}
|
||||
|
||||
rows, err := p.ParseRows(ctx, importOss.Path)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
return createPending(ctx, &req, p, rows, importOss, options)
|
||||
}
|
||||
|
||||
func CreatePendingRows(ctx context.Context, req SubmitRequest, p PendingProcessor, rows []Row, options PendingOptions) (*Result, error) {
|
||||
if err := batchmetadata.ValidateCreation(req.SourceChannel, req.ExecutionMode); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
return createPending(ctx, &req, p, rows, nil, options)
|
||||
}
|
||||
|
||||
func createPending(ctx context.Context, req *SubmitRequest, p PendingProcessor, rows []Row, importOss *model.SysOssSaveContentModel, options PendingOptions) (*Result, error) {
|
||||
if len(rows) == 0 {
|
||||
return nil, gerror.New("导入数据为空")
|
||||
}
|
||||
if err := validatePendingRowCount(len(rows), p.Config().MaxRows); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
if err := p.ValidateRows(ctx, rows); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
if options.PrepareRows != nil {
|
||||
if err := options.PrepareRows(ctx, rows); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
}
|
||||
if strings.TrimSpace(req.BatchNo) == "" {
|
||||
batchNo, err := NextBatchNoContext(ctx, p.Config().BatchNoPrefix)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
req.BatchNo = batchNo
|
||||
}
|
||||
|
||||
result := &Result{
|
||||
ImportStatus: consts.ImportStatusSuccess,
|
||||
BatchNo: req.BatchNo,
|
||||
BatchType: p.Config().BatchType,
|
||||
TotalRows: len(rows),
|
||||
}
|
||||
if importOss != nil {
|
||||
result.ImportFileOssId = importOss.OssId
|
||||
result.ImportFileUrl = importOss.Url
|
||||
}
|
||||
|
||||
if err := g.DB().Transaction(ctx, func(ctx context.Context, tx gdb.TX) error {
|
||||
sourceBatchNo := strings.TrimSpace(req.SourceBatchNo)
|
||||
if sourceBatchNo == "" {
|
||||
sourceBatchNo = req.BatchNo
|
||||
}
|
||||
if req.BeforeSave != nil {
|
||||
if err := req.BeforeSave(ctx, tx, req, rows); err != nil {
|
||||
return err
|
||||
}
|
||||
}
|
||||
if _, err := tx.Ctx(ctx).Model(dao.BatchMain.Table()).Data(do.BatchMain{
|
||||
BatchNo: req.BatchNo,
|
||||
OrderSn: "",
|
||||
BatchType: p.Config().BatchType,
|
||||
BatchAsyncStatus: consts.BatchAsyncStatusPending,
|
||||
SourceType: req.SourceType,
|
||||
SourceChannel: batchmetadata.Channel(req.SourceChannel),
|
||||
ExecutionMode: batchmetadata.NormalizeMode(req.ExecutionMode),
|
||||
SourceBatchNo: sourceBatchNo,
|
||||
SourceOssId: ossIDValue(sourceOssID(result, importOss)),
|
||||
IsCrossDept: 0,
|
||||
TargetDeptId: req.TargetDeptId,
|
||||
DeptId: req.Operator.DeptId,
|
||||
TotalCount: len(rows),
|
||||
ValidCount: 0,
|
||||
SuccessCount: 0,
|
||||
FailCount: 0,
|
||||
RunResult: consts.BatchRunResultPending,
|
||||
FailReason: "",
|
||||
BatchInfo: p.BuildBatchInfo(ctx, *req, result, importOss, nil, 0),
|
||||
TenantId: req.Operator.TenantId,
|
||||
CreatedDept: req.Operator.DeptId,
|
||||
CreatedBy: req.Operator.UserId,
|
||||
UpdatedBy: req.Operator.UserId,
|
||||
}).Insert(); err != nil {
|
||||
return gerror.Wrap(err, "写入待处理批次失败")
|
||||
}
|
||||
|
||||
details := make([]do.BatchDetail, 0, len(rows))
|
||||
for _, row := range rows {
|
||||
details = append(details, do.BatchDetail{
|
||||
BatchNo: req.BatchNo,
|
||||
BatchType: p.Config().BatchType,
|
||||
DetailStatus: consts.BatchDetailStatusPending,
|
||||
BlankOutStatus: consts.BlankOutStatusNo,
|
||||
RowNo: row.GetRowNo(),
|
||||
ItemId: row.GetItemID(),
|
||||
ErrorCode: "",
|
||||
ErrorReason: strings.Join(row.GetErrorReasons(), ";"),
|
||||
Snapshot: row.Snapshot(),
|
||||
TenantId: req.Operator.TenantId,
|
||||
CreatedDept: req.Operator.DeptId,
|
||||
CreatedBy: req.Operator.UserId,
|
||||
UpdatedBy: req.Operator.UserId,
|
||||
})
|
||||
}
|
||||
for start := 0; start < len(details); start += pendingDetailInsertBatchSize {
|
||||
end := start + pendingDetailInsertBatchSize
|
||||
if end > len(details) {
|
||||
end = len(details)
|
||||
}
|
||||
if _, err := tx.Ctx(ctx).Model(dao.BatchDetail.Table()).Data(details[start:end]).Insert(); err != nil {
|
||||
return gerror.Wrapf(err, "写入待处理批次明细失败,范围:%d-%d", start+1, end)
|
||||
}
|
||||
}
|
||||
return nil
|
||||
}); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
return result, nil
|
||||
}
|
||||
|
||||
func validatePendingRowCount(rowCount, maxRows int) error {
|
||||
if maxRows > 0 && rowCount > maxRows {
|
||||
return gerror.Newf("单批最多支持%d张卡,当前共%d张,请拆分后重试", maxRows, rowCount)
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
func SaveAsyncResultFiles(
|
||||
ctx context.Context,
|
||||
req SubmitRequest,
|
||||
p Processor,
|
||||
result *Result,
|
||||
rows []Row,
|
||||
importOss *model.SysOssSaveContentModel,
|
||||
startTime, endTime *gtime.Time,
|
||||
) (*model.SysOssSaveContentModel, int64, error) {
|
||||
resultOss, downloadId, err := saveResultExcelAndDownload(ctx, req, p, result, rows, importOss, startTime, endTime)
|
||||
if err != nil {
|
||||
return nil, 0, err
|
||||
}
|
||||
result.ResultDownloadId = downloadId
|
||||
result.ResultFileOssId = resultOss.OssId
|
||||
result.ResultFileUrl = resultOss.Url
|
||||
return resultOss, downloadId, nil
|
||||
}
|
||||
Reference in New Issue
Block a user