作者 陈志颖

refactor:优化分红订单导入

... ... @@ -617,7 +617,9 @@ func (dividendsOrderService *DividendsOrderService) ImportDividendsOrder(importD
}, nil
}
// 批量导入创建分红订单
var creatDividendsOrder []*domain.DividendsOrder
// 批量创建分红订单
for _, dividendsOrder := range createDividendsOrderCommands {
// 生成分红订单号
dividendsOrderNumber, err := dividendsOrderDao.GenerateDividendsOrderNumber()
... ... @@ -732,19 +734,14 @@ func (dividendsOrderService *DividendsOrderService) ImportDividendsOrder(importD
Operator: operator,
}
if _, err2 := dividendsOrderRepository.Save(newDividendsOrder); err2 != nil {
row := &domain.ImportInfo{
Error: application.ThrowError(application.INTERNAL_SERVER_ERROR, fmt.Sprintf("保存订单数据失败:%s", err)),
LineNumbers: dividendsOrder.LineNumbers, // 错误影响的行
GoodLine: map[int]interface{}{},
}
errorDataList = append(errorDataList, row)
continue
}
creatDividendsOrder = append(creatDividendsOrder, newDividendsOrder)
}
// 错误报告处理
if len(errorDataList) <= 0 { // 成功返回
if _, err2 := dividendsOrderRepository.SaveMany(creatDividendsOrder); err2 != nil {
return nil, application.ThrowError(application.INTERNAL_SERVER_ERROR, err2.Error())
}
if err3 := transactionContext.CommitTransaction(); err3 != nil {
return nil, application.ThrowError(application.TRANSACTION_ERROR, err3.Error())
}
... ...
... ... @@ -66,6 +66,7 @@ type FailInfo struct {
type DividendsOrderRepository interface {
Save(dividendsOrder *DividendsOrder) (*DividendsOrder, error)
SaveMany(dividendsOrders []*DividendsOrder) ([]*DividendsOrder, error)
UpdateMany(dividendsOrders []*DividendsOrder) ([]*DividendsOrder, error)
Remove(dividendsOrder *DividendsOrder) (*DividendsOrder, error)
BatchRemove(dividendsOrders []*DividendsOrder) ([]*DividendsOrder, error)
... ...
... ... @@ -295,6 +295,80 @@ func (repository *DividendsOrderRepository) Save(dividendsOrder *domain.Dividend
return dividendsOrder, nil
}
func (repository *DividendsOrderRepository) SaveMany(dividendsOrders []*domain.DividendsOrder) ([]*domain.DividendsOrder, error) {
tx := repository.transactionContext.PgTx
var dividendsOrderModels []*models.DividendsOrder
var orderGoodsModel []*models.OrderGood
for _, dividendsOrder := range dividendsOrders {
dividendsOrderModels = append(dividendsOrderModels, &models.DividendsOrder{
DividendsOrderNumber: dividendsOrder.DividendsOrderNumber,
DividendsOriginalOrderNum: dividendsOrder.DividendsOriginalOrderNum,
DividendsOrderAmount: dividendsOrder.DividendsOrderAmount,
OrderTime: dividendsOrder.OrderTime,
DividendTime: dividendsOrder.DividendTime,
DividendStatus: dividendsOrder.DividendStatus,
Region: dividendsOrder.Region,
CustomerName: dividendsOrder.CustomerName,
Org: dividendsOrder.Org,
Company: dividendsOrder.Company,
CreatedAt: time.Now(),
DeletedAt: dividendsOrder.DeletedAt,
UpdatedAt: dividendsOrder.UpdatedAt,
OperateTime: time.Now(),
Operator: dividendsOrder.Operator,
Remarks: dividendsOrder.Remarks,
})
for _, good := range dividendsOrder.Goods {
orderGoodsModel = append(orderGoodsModel, &models.OrderGood{
OrderGoodAmount: good.OrderGoodAmount,
OrderGoodName: good.OrderGoodName,
OrderGoodPrice: good.OrderGoodPrice,
OrderGoodQuantity: good.OrderGoodQuantity,
DividendsOrderNumber: dividendsOrder.DividendsOrderNumber,
DividendsReturnedOrderNumber: "",
CooperationContractNumber: good.CooperationContractNumber,
CompanyId: good.CompanyId,
OrgId: good.OrgId,
OrderGoodExpense: good.OrderGoodExpense,
OrderGoodDividendsStatus: good.OrderGoodDividendsStatus,
CreatedAt: time.Now(),
DeletedAt: time.Time{},
UpdatedAt: time.Time{},
})
}
}
if _, err := tx.Model(&dividendsOrderModels).Insert(); err != nil { // 新增分红订单
return nil, err
} else { // 新增分红订单产品
if _, err := tx.Model(&orderGoodsModel).Insert(); err != nil {
return nil, err
}
}
var dividendsOrdersSaved []*domain.DividendsOrder
for _, dividendsOrderModel := range dividendsOrderModels {
//获取订单产品
var orderGoodModels []*models.OrderGood
orderGoodModelQuery := tx.Model(&orderGoodModels)
if err := orderGoodModelQuery.
Where("company_id = ?", dividendsOrderModel.Company.CompanyId).
Where("org_id = ?", dividendsOrderModel.Org.OrgId).
Where("dividends_order_number = ?", dividendsOrderModel.DividendsOrderNumber).
Select(); err != nil {
log.Logger.Error("分红订单关联的产品不存在", map[string]interface{}{
"dividendsOrderModel": dividendsOrderModel,
})
return nil, fmt.Errorf("分红订单关联的产品不存在")
}
// 聚合分红订单
if dividendsOrder, err := transform.TransformToDividendsOrderDomainModelFromPgModels(dividendsOrderModel, orderGoodModels); err != nil {
return dividendsOrders, err
} else {
dividendsOrders = append(dividendsOrders, dividendsOrder)
}
}
return dividendsOrdersSaved, nil
}
func (repository *DividendsOrderRepository) UpdateMany(dividendsOrders []*domain.DividendsOrder) ([]*domain.DividendsOrder, error) {
tx := repository.transactionContext.PgTx
var dividendsOrderModels []*models.DividendsOrder
... ...