103 lines
3.1 KiB
Go
103 lines
3.1 KiB
Go
package common
|
|
|
|
import (
|
|
"context"
|
|
"strings"
|
|
|
|
"github.com/gogf/gf/v2/database/gdb"
|
|
"github.com/gogf/gf/v2/errors/gerror"
|
|
"github.com/gogf/gf/v2/util/gconv"
|
|
|
|
"git.5iot.com/iotlcm-public/iotlcm-openapi/internal/dao"
|
|
"git.5iot.com/iotlcm-public/iotlcm-openapi/internal/library/batchimport"
|
|
"git.5iot.com/iotlcm-public/iotlcm-openapi/internal/library/batchmetadata"
|
|
"git.5iot.com/iotlcm-public/iotlcm-openapi/internal/library/rabbitmq"
|
|
"git.5iot.com/iotlcm-public/iotlcm-openapi/internal/model/do"
|
|
"git.5iot.com/iotlcm-public/iotlcm-openapi/internal/model/entity"
|
|
"git.5iot.com/iotlcm-public/iotlcm-openapi/internal/service"
|
|
)
|
|
|
|
type sCommon struct{}
|
|
|
|
func New() *sCommon {
|
|
return &sCommon{}
|
|
}
|
|
|
|
func init() {
|
|
service.RegisterCommon(New())
|
|
}
|
|
|
|
// BuildBatchNo 与控制台、小程序共用按日递增规则。
|
|
func (s *sCommon) BuildBatchNo(ctx context.Context, prefix string) (string, error) {
|
|
return batchimport.NextBatchNoContext(ctx, prefix)
|
|
}
|
|
|
|
// SaveBatchMain 写入批次主表。
|
|
func (s *sCommon) SaveBatchMain(ctx context.Context, tx gdb.TX, batch *entity.BatchMain, executionMode int) error {
|
|
if tx == nil {
|
|
return gerror.New("transaction is empty")
|
|
}
|
|
if batch == nil {
|
|
return gerror.New("batch main is empty")
|
|
}
|
|
if strings.TrimSpace(batch.BatchNo) == "" {
|
|
return gerror.New("batch no is empty")
|
|
}
|
|
if executionMode != batchmetadata.Single && executionMode != batchmetadata.Bulk {
|
|
return gerror.New("batch execution mode is invalid")
|
|
}
|
|
data := do.BatchMain{}
|
|
if err := gconv.Struct(batch, &data); err != nil {
|
|
return gerror.Wrap(err, "convert batch main failed")
|
|
}
|
|
data.SourceChannel = batchmetadata.API
|
|
data.ExecutionMode = executionMode
|
|
data.Id = nil
|
|
data.CreatedAt = nil
|
|
data.UpdatedAt = nil
|
|
_, err := tx.Ctx(ctx).Model(dao.BatchMain.Table()).Data(data).Insert()
|
|
if err != nil {
|
|
return gerror.Wrap(err, "save batch main failed")
|
|
}
|
|
return nil
|
|
}
|
|
|
|
// SaveBatchDetail 写入单条批次明细。
|
|
func (s *sCommon) SaveBatchDetail(ctx context.Context, tx gdb.TX, detail *entity.BatchDetail) error {
|
|
if tx == nil {
|
|
return gerror.New("transaction is empty")
|
|
}
|
|
if detail == nil {
|
|
return gerror.New("batch detail is empty")
|
|
}
|
|
if strings.TrimSpace(detail.BatchNo) == "" {
|
|
return gerror.New("batch no is empty")
|
|
}
|
|
data := do.BatchDetail{}
|
|
if err := gconv.Struct(detail, &data); err != nil {
|
|
return gerror.Wrap(err, "convert batch detail failed")
|
|
}
|
|
data.Id = nil
|
|
data.CreatedAt = nil
|
|
data.UpdatedAt = nil
|
|
_, err := tx.Ctx(ctx).Model(dao.BatchDetail.Table()).Data(data).Insert()
|
|
if err != nil {
|
|
return gerror.Wrap(err, "save batch detail failed")
|
|
}
|
|
return nil
|
|
}
|
|
|
|
// SaveBatchDetails 批量写入批次明细。为了保持行为稳定,这里按行写入,避免不同数据库驱动对批量结构体的差异。
|
|
func (s *sCommon) SaveBatchDetails(ctx context.Context, tx gdb.TX, details []*entity.BatchDetail) error {
|
|
for _, detail := range details {
|
|
if err := s.SaveBatchDetail(ctx, tx, detail); err != nil {
|
|
return err
|
|
}
|
|
}
|
|
return nil
|
|
}
|
|
|
|
func (s *sCommon) Publish(ctx context.Context, queue string, data any) error {
|
|
return rabbitmq.Publish(ctx, queue, data)
|
|
}
|