作者 陈志颖

refactor:优化分红订单导入

@@ -617,7 +617,9 @@ func (dividendsOrderService *DividendsOrderService) ImportDividendsOrder(importD @@ -617,7 +617,9 @@ func (dividendsOrderService *DividendsOrderService) ImportDividendsOrder(importD
617 }, nil 617 }, nil
618 } 618 }
619 619
620 - // 批量导入创建分红订单 620 + var creatDividendsOrder []*domain.DividendsOrder
  621 +
  622 + // 批量创建分红订单
621 for _, dividendsOrder := range createDividendsOrderCommands { 623 for _, dividendsOrder := range createDividendsOrderCommands {
622 // 生成分红订单号 624 // 生成分红订单号
623 dividendsOrderNumber, err := dividendsOrderDao.GenerateDividendsOrderNumber() 625 dividendsOrderNumber, err := dividendsOrderDao.GenerateDividendsOrderNumber()
@@ -732,19 +734,14 @@ func (dividendsOrderService *DividendsOrderService) ImportDividendsOrder(importD @@ -732,19 +734,14 @@ func (dividendsOrderService *DividendsOrderService) ImportDividendsOrder(importD
732 Operator: operator, 734 Operator: operator,
733 } 735 }
734 736
735 - if _, err2 := dividendsOrderRepository.Save(newDividendsOrder); err2 != nil {  
736 - row := &domain.ImportInfo{  
737 - Error: application.ThrowError(application.INTERNAL_SERVER_ERROR, fmt.Sprintf("保存订单数据失败:%s", err)),  
738 - LineNumbers: dividendsOrder.LineNumbers, // 错误影响的行  
739 - GoodLine: map[int]interface{}{},  
740 - }  
741 - errorDataList = append(errorDataList, row)  
742 - continue  
743 - } 737 + creatDividendsOrder = append(creatDividendsOrder, newDividendsOrder)
744 } 738 }
745 739
746 // 错误报告处理 740 // 错误报告处理
747 if len(errorDataList) <= 0 { // 成功返回 741 if len(errorDataList) <= 0 { // 成功返回
  742 + if _, err2 := dividendsOrderRepository.SaveMany(creatDividendsOrder); err2 != nil {
  743 + return nil, application.ThrowError(application.INTERNAL_SERVER_ERROR, err2.Error())
  744 + }
748 if err3 := transactionContext.CommitTransaction(); err3 != nil { 745 if err3 := transactionContext.CommitTransaction(); err3 != nil {
749 return nil, application.ThrowError(application.TRANSACTION_ERROR, err3.Error()) 746 return nil, application.ThrowError(application.TRANSACTION_ERROR, err3.Error())
750 } 747 }
@@ -66,6 +66,7 @@ type FailInfo struct { @@ -66,6 +66,7 @@ type FailInfo struct {
66 66
67 type DividendsOrderRepository interface { 67 type DividendsOrderRepository interface {
68 Save(dividendsOrder *DividendsOrder) (*DividendsOrder, error) 68 Save(dividendsOrder *DividendsOrder) (*DividendsOrder, error)
  69 + SaveMany(dividendsOrders []*DividendsOrder) ([]*DividendsOrder, error)
69 UpdateMany(dividendsOrders []*DividendsOrder) ([]*DividendsOrder, error) 70 UpdateMany(dividendsOrders []*DividendsOrder) ([]*DividendsOrder, error)
70 Remove(dividendsOrder *DividendsOrder) (*DividendsOrder, error) 71 Remove(dividendsOrder *DividendsOrder) (*DividendsOrder, error)
71 BatchRemove(dividendsOrders []*DividendsOrder) ([]*DividendsOrder, error) 72 BatchRemove(dividendsOrders []*DividendsOrder) ([]*DividendsOrder, error)
@@ -295,6 +295,80 @@ func (repository *DividendsOrderRepository) Save(dividendsOrder *domain.Dividend @@ -295,6 +295,80 @@ func (repository *DividendsOrderRepository) Save(dividendsOrder *domain.Dividend
295 return dividendsOrder, nil 295 return dividendsOrder, nil
296 } 296 }
297 297
  298 +func (repository *DividendsOrderRepository) SaveMany(dividendsOrders []*domain.DividendsOrder) ([]*domain.DividendsOrder, error) {
  299 + tx := repository.transactionContext.PgTx
  300 + var dividendsOrderModels []*models.DividendsOrder
  301 + var orderGoodsModel []*models.OrderGood
  302 + for _, dividendsOrder := range dividendsOrders {
  303 + dividendsOrderModels = append(dividendsOrderModels, &models.DividendsOrder{
  304 + DividendsOrderNumber: dividendsOrder.DividendsOrderNumber,
  305 + DividendsOriginalOrderNum: dividendsOrder.DividendsOriginalOrderNum,
  306 + DividendsOrderAmount: dividendsOrder.DividendsOrderAmount,
  307 + OrderTime: dividendsOrder.OrderTime,
  308 + DividendTime: dividendsOrder.DividendTime,
  309 + DividendStatus: dividendsOrder.DividendStatus,
  310 + Region: dividendsOrder.Region,
  311 + CustomerName: dividendsOrder.CustomerName,
  312 + Org: dividendsOrder.Org,
  313 + Company: dividendsOrder.Company,
  314 + CreatedAt: time.Now(),
  315 + DeletedAt: dividendsOrder.DeletedAt,
  316 + UpdatedAt: dividendsOrder.UpdatedAt,
  317 + OperateTime: time.Now(),
  318 + Operator: dividendsOrder.Operator,
  319 + Remarks: dividendsOrder.Remarks,
  320 + })
  321 + for _, good := range dividendsOrder.Goods {
  322 + orderGoodsModel = append(orderGoodsModel, &models.OrderGood{
  323 + OrderGoodAmount: good.OrderGoodAmount,
  324 + OrderGoodName: good.OrderGoodName,
  325 + OrderGoodPrice: good.OrderGoodPrice,
  326 + OrderGoodQuantity: good.OrderGoodQuantity,
  327 + DividendsOrderNumber: dividendsOrder.DividendsOrderNumber,
  328 + DividendsReturnedOrderNumber: "",
  329 + CooperationContractNumber: good.CooperationContractNumber,
  330 + CompanyId: good.CompanyId,
  331 + OrgId: good.OrgId,
  332 + OrderGoodExpense: good.OrderGoodExpense,
  333 + OrderGoodDividendsStatus: good.OrderGoodDividendsStatus,
  334 + CreatedAt: time.Now(),
  335 + DeletedAt: time.Time{},
  336 + UpdatedAt: time.Time{},
  337 + })
  338 + }
  339 + }
  340 + if _, err := tx.Model(&dividendsOrderModels).Insert(); err != nil { // 新增分红订单
  341 + return nil, err
  342 + } else { // 新增分红订单产品
  343 + if _, err := tx.Model(&orderGoodsModel).Insert(); err != nil {
  344 + return nil, err
  345 + }
  346 + }
  347 + var dividendsOrdersSaved []*domain.DividendsOrder
  348 + for _, dividendsOrderModel := range dividendsOrderModels {
  349 + //获取订单产品
  350 + var orderGoodModels []*models.OrderGood
  351 + orderGoodModelQuery := tx.Model(&orderGoodModels)
  352 + if err := orderGoodModelQuery.
  353 + Where("company_id = ?", dividendsOrderModel.Company.CompanyId).
  354 + Where("org_id = ?", dividendsOrderModel.Org.OrgId).
  355 + Where("dividends_order_number = ?", dividendsOrderModel.DividendsOrderNumber).
  356 + Select(); err != nil {
  357 + log.Logger.Error("分红订单关联的产品不存在", map[string]interface{}{
  358 + "dividendsOrderModel": dividendsOrderModel,
  359 + })
  360 + return nil, fmt.Errorf("分红订单关联的产品不存在")
  361 + }
  362 + // 聚合分红订单
  363 + if dividendsOrder, err := transform.TransformToDividendsOrderDomainModelFromPgModels(dividendsOrderModel, orderGoodModels); err != nil {
  364 + return dividendsOrders, err
  365 + } else {
  366 + dividendsOrders = append(dividendsOrders, dividendsOrder)
  367 + }
  368 + }
  369 + return dividendsOrdersSaved, nil
  370 +}
  371 +
298 func (repository *DividendsOrderRepository) UpdateMany(dividendsOrders []*domain.DividendsOrder) ([]*domain.DividendsOrder, error) { 372 func (repository *DividendsOrderRepository) UpdateMany(dividendsOrders []*domain.DividendsOrder) ([]*domain.DividendsOrder, error) {
299 tx := repository.transactionContext.PgTx 373 tx := repository.transactionContext.PgTx
300 var dividendsOrderModels []*models.DividendsOrder 374 var dividendsOrderModels []*models.DividendsOrder