作者 陈志颖

refactor:优化订单导入

@@ -370,12 +370,14 @@ func (dividendsOrderService *DividendsOrderService) ImportDividendsOrder(importD @@ -370,12 +370,14 @@ func (dividendsOrderService *DividendsOrderService) ImportDividendsOrder(importD
370 "cooperationContractNumber": "项目合约编号", 370 "cooperationContractNumber": "项目合约编号",
371 } 371 }
372 372
373 - // 空文件校验 373 + /************************************************ 1.空文件校验 ******************************************************/
374 if len(importDividendsOrderCommand.DividendsOrderData) == 0 { 374 if len(importDividendsOrderCommand.DividendsOrderData) == 0 {
375 return nil, application.ThrowError(application.INTERNAL_SERVER_ERROR, "导入的Excel文件为空文件,请上传正确的文件") 375 return nil, application.ThrowError(application.INTERNAL_SERVER_ERROR, "导入的Excel文件为空文件,请上传正确的文件")
376 } 376 }
  377 + /******************************************************************************************************************/
377 378
378 - // 必填项校验 379 + /************************************************ 2.必填项校验 ******************************************************/
  380 + // 空单元格错误
379 nullCellError := make([]*domain.FailInfo, 0) 381 nullCellError := make([]*domain.FailInfo, 0)
380 382
381 // 数据行计数 383 // 数据行计数
@@ -419,15 +421,16 @@ func (dividendsOrderService *DividendsOrderService) ImportDividendsOrder(importD @@ -419,15 +421,16 @@ func (dividendsOrderService *DividendsOrderService) ImportDividendsOrder(importD
419 }, 421 },
420 }, nil 422 }, nil
421 } 423 }
  424 + /******************************************************************************************************************/
422 425
423 - // 单元格类型校验  
424 - typeError := make([]interface{}, 0)  
425 - for i, dividendsOrder := range importDividendsOrderCommand.DividendsOrderData { // 行 426 + /***************************************************** 3.单元格类型校验 *********************************************/
  427 + typeError := make([]*domain.FailInfo, 0)
  428 + for _, dividendsOrder := range importDividendsOrderCommand.DividendsOrderData { // 行
426 var myRow []*domain.FailInfo 429 var myRow []*domain.FailInfo
427 t := reflect.TypeOf(dividendsOrder) 430 t := reflect.TypeOf(dividendsOrder)
428 v := reflect.ValueOf(dividendsOrder) 431 v := reflect.ValueOf(dividendsOrder)
429 for k := 0; k < t.NumField(); k++ { // 列 432 for k := 0; k < t.NumField(); k++ { // 列
430 - r := strconv.Itoa(i + 1) 433 + //r := strconv.Itoa(i + 1)
431 switch k { 434 switch k {
432 case 3: // 订单日期校验 435 case 3: // 订单日期校验
433 { 436 {
@@ -439,6 +442,7 @@ func (dividendsOrderService *DividendsOrderService) ImportDividendsOrder(importD @@ -439,6 +442,7 @@ func (dividendsOrderService *DividendsOrderService) ImportDividendsOrder(importD
439 } 442 }
440 tmpRow.ImportDividendsOrderData = &dividendsOrder 443 tmpRow.ImportDividendsOrderData = &dividendsOrder
441 myRow = append(myRow, tmpRow) 444 myRow = append(myRow, tmpRow)
  445 + continue
442 } 446 }
443 } 447 }
444 case 5: // 产品数量校验 448 case 5: // 产品数量校验
@@ -451,6 +455,7 @@ func (dividendsOrderService *DividendsOrderService) ImportDividendsOrder(importD @@ -451,6 +455,7 @@ func (dividendsOrderService *DividendsOrderService) ImportDividendsOrder(importD
451 } 455 }
452 tmpRow.ImportDividendsOrderData = &dividendsOrder 456 tmpRow.ImportDividendsOrderData = &dividendsOrder
453 myRow = append(myRow, tmpRow) 457 myRow = append(myRow, tmpRow)
  458 + continue
454 } 459 }
455 // 正负判断 460 // 正负判断
456 if orderGoodQuantity < 0 { 461 if orderGoodQuantity < 0 {
@@ -459,6 +464,7 @@ func (dividendsOrderService *DividendsOrderService) ImportDividendsOrder(importD @@ -459,6 +464,7 @@ func (dividendsOrderService *DividendsOrderService) ImportDividendsOrder(importD
459 } 464 }
460 tmpRow.ImportDividendsOrderData = &dividendsOrder 465 tmpRow.ImportDividendsOrderData = &dividendsOrder
461 myRow = append(myRow, tmpRow) 466 myRow = append(myRow, tmpRow)
  467 + continue
462 } 468 }
463 } 469 }
464 case 6: // 产品价格校验 470 case 6: // 产品价格校验
@@ -471,16 +477,17 @@ func (dividendsOrderService *DividendsOrderService) ImportDividendsOrder(importD @@ -471,16 +477,17 @@ func (dividendsOrderService *DividendsOrderService) ImportDividendsOrder(importD
471 } 477 }
472 tmpRow.ImportDividendsOrderData = &dividendsOrder 478 tmpRow.ImportDividendsOrderData = &dividendsOrder
473 myRow = append(myRow, tmpRow) 479 myRow = append(myRow, tmpRow)
  480 + continue
474 } 481 }
475 482
476 // 长度校验 483 // 长度校验
477 if univalent >= 1e16 { 484 if univalent >= 1e16 {
478 tmpRow := &domain.FailInfo{ 485 tmpRow := &domain.FailInfo{
479 - FailReason: "第" + r + "行产品价格超过最大限制,产品价格小数点前面不能超过十六位数字,并保留两位小数", 486 + FailReason: "产品价格超过最大限制,产品价格小数点前面不能超过十六位数字,并保留两位小数",
480 } 487 }
481 tmpRow.ImportDividendsOrderData = &dividendsOrder 488 tmpRow.ImportDividendsOrderData = &dividendsOrder
482 - // 错误行数据  
483 myRow = append(myRow, tmpRow) 489 myRow = append(myRow, tmpRow)
  490 + continue
484 } 491 }
485 } 492 }
486 case 7: // 费用校验 493 case 7: // 费用校验
@@ -490,28 +497,30 @@ func (dividendsOrderService *DividendsOrderService) ImportDividendsOrder(importD @@ -490,28 +497,30 @@ func (dividendsOrderService *DividendsOrderService) ImportDividendsOrder(importD
490 univalent, typeErr := strconv.ParseFloat(fmt.Sprintf("%v", v.Field(k).Interface()), 64) 497 univalent, typeErr := strconv.ParseFloat(fmt.Sprintf("%v", v.Field(k).Interface()), 64)
491 if typeErr != nil { 498 if typeErr != nil {
492 tmpRow := &domain.FailInfo{ 499 tmpRow := &domain.FailInfo{
493 - FailReason: "第" + r + "行费用格式错误,费用必须为数字类型", 500 + FailReason: "费用格式错误,费用必须为数字类型",
494 } 501 }
495 tmpRow.ImportDividendsOrderData = &dividendsOrder 502 tmpRow.ImportDividendsOrderData = &dividendsOrder
496 myRow = append(myRow, tmpRow) 503 myRow = append(myRow, tmpRow)
497 log.Logger.Info("费用类型错误", map[string]interface{}{ 504 log.Logger.Info("费用类型错误", map[string]interface{}{
498 "tmpRow": tmpRow, 505 "tmpRow": tmpRow,
499 }) 506 })
  507 + continue
500 } 508 }
501 // 长度校验 509 // 长度校验
502 if univalent >= 1e16 { 510 if univalent >= 1e16 {
503 tmpRow := &domain.FailInfo{ 511 tmpRow := &domain.FailInfo{
504 - FailReason: "第" + r + "行费用超过最大限制,费用小数点前面不能超过十六位数字,并保留两位小数", 512 + FailReason: "费用超过最大限制,费用小数点前面不能超过十六位数字,并保留两位小数",
505 } 513 }
506 tmpRow.ImportDividendsOrderData = &dividendsOrder 514 tmpRow.ImportDividendsOrderData = &dividendsOrder
507 myRow = append(myRow, tmpRow) 515 myRow = append(myRow, tmpRow)
  516 + continue
508 } 517 }
509 } 518 }
510 } 519 }
511 } 520 }
512 } 521 }
513 - if myRow != nil {  
514 - typeError = append(typeError, myRow) 522 + if len(myRow) > 0 {
  523 + typeError = append(typeError, myRow...)
515 } 524 }
516 } 525 }
517 526
@@ -525,11 +534,15 @@ func (dividendsOrderService *DividendsOrderService) ImportDividendsOrder(importD @@ -525,11 +534,15 @@ func (dividendsOrderService *DividendsOrderService) ImportDividendsOrder(importD
525 }, 534 },
526 }, nil 535 }, nil
527 } 536 }
  537 + /******************************************************************************************************************/
528 538
529 - // 聚合订单并进行类型校验 539 + /******************************************** 4.聚合订单并进行类型校验 ************************************************/
  540 + // 订单聚合错误
530 aggregateErrorList := make([]interface{}, 0) 541 aggregateErrorList := make([]interface{}, 0)
531 542
  543 + // 新增订单命令集
532 var dividendsOrderCommands = make(map[string]*command.CreateDividendsOrderCommand) 544 var dividendsOrderCommands = make(map[string]*command.CreateDividendsOrderCommand)
  545 +
533 for i, dividendsOrder := range importDividendsOrderCommand.DividendsOrderData { 546 for i, dividendsOrder := range importDividendsOrderCommand.DividendsOrderData {
534 hashValue := md5.Sum([]byte(dividendsOrder.OriginalOrderNum)) 547 hashValue := md5.Sum([]byte(dividendsOrder.OriginalOrderNum))
535 hashString := hex.EncodeToString(hashValue[:]) 548 hashString := hex.EncodeToString(hashValue[:])
@@ -542,22 +555,37 @@ func (dividendsOrderService *DividendsOrderService) ImportDividendsOrder(importD @@ -542,22 +555,37 @@ func (dividendsOrderService *DividendsOrderService) ImportDividendsOrder(importD
542 } 555 }
543 tmpRow.ImportDividendsOrderData = &dividendsOrder 556 tmpRow.ImportDividendsOrderData = &dividendsOrder
544 aggregateErrorList = append(aggregateErrorList, tmpRow) 557 aggregateErrorList = append(aggregateErrorList, tmpRow)
545 - break 558 + continue
546 } 559 }
547 560
548 // 产品相关:产品名称,产品数量、产品价格、费用 561 // 产品相关:产品名称,产品数量、产品价格、费用
549 quantity, err := strconv.ParseFloat(dividendsOrder.OrderGoodQuantity, 64) 562 quantity, err := strconv.ParseFloat(dividendsOrder.OrderGoodQuantity, 64)
550 if err != nil { 563 if err != nil {
551 - return nil, application.ThrowError(application.ARG_ERROR, err.Error()) 564 + tmpRow := &domain.FailInfo{
  565 + FailReason: "产品数量类型错误",
  566 + }
  567 + tmpRow.ImportDividendsOrderData = &dividendsOrder
  568 + aggregateErrorList = append(aggregateErrorList, tmpRow)
  569 + continue
552 } 570 }
553 price, err := strconv.ParseFloat(dividendsOrder.OrderGoodPrice, 64) 571 price, err := strconv.ParseFloat(dividendsOrder.OrderGoodPrice, 64)
554 if err != nil { 572 if err != nil {
555 - return nil, application.ThrowError(application.ARG_ERROR, err.Error()) 573 + tmpRow := &domain.FailInfo{
  574 + FailReason: "产品价格类型错误",
  575 + }
  576 + tmpRow.ImportDividendsOrderData = &dividendsOrder
  577 + aggregateErrorList = append(aggregateErrorList, tmpRow)
  578 + continue
556 } 579 }
557 var expense float64 580 var expense float64
558 if dividendsOrder.Expense != "" { 581 if dividendsOrder.Expense != "" {
559 if expenseParse, err := strconv.ParseFloat(dividendsOrder.Expense, 64); err != nil { 582 if expenseParse, err := strconv.ParseFloat(dividendsOrder.Expense, 64); err != nil {
560 - return nil, application.ThrowError(application.ARG_ERROR, err.Error()) 583 + tmpRow := &domain.FailInfo{
  584 + FailReason: "费用类型错误",
  585 + }
  586 + tmpRow.ImportDividendsOrderData = &dividendsOrder
  587 + aggregateErrorList = append(aggregateErrorList, tmpRow)
  588 + continue
561 } else { 589 } else {
562 expense = expenseParse 590 expense = expenseParse
563 } 591 }
@@ -593,16 +621,31 @@ func (dividendsOrderService *DividendsOrderService) ImportDividendsOrder(importD @@ -593,16 +621,31 @@ func (dividendsOrderService *DividendsOrderService) ImportDividendsOrder(importD
593 // 产品相关:产品名称,产品数量、产品价格、费用 621 // 产品相关:产品名称,产品数量、产品价格、费用
594 quantity, err := strconv.ParseFloat(dividendsOrder.OrderGoodQuantity, 64) 622 quantity, err := strconv.ParseFloat(dividendsOrder.OrderGoodQuantity, 64)
595 if err != nil { 623 if err != nil {
596 - return nil, application.ThrowError(application.ARG_ERROR, err.Error()) 624 + tmpRow := &domain.FailInfo{
  625 + FailReason: "产品数量类型错误",
  626 + }
  627 + tmpRow.ImportDividendsOrderData = &dividendsOrder
  628 + aggregateErrorList = append(aggregateErrorList, tmpRow)
  629 + continue
597 } 630 }
598 price, err := strconv.ParseFloat(dividendsOrder.OrderGoodPrice, 64) 631 price, err := strconv.ParseFloat(dividendsOrder.OrderGoodPrice, 64)
599 if err != nil { 632 if err != nil {
600 - return nil, application.ThrowError(application.ARG_ERROR, err.Error()) 633 + tmpRow := &domain.FailInfo{
  634 + FailReason: "产品价格类型错误",
  635 + }
  636 + tmpRow.ImportDividendsOrderData = &dividendsOrder
  637 + aggregateErrorList = append(aggregateErrorList, tmpRow)
  638 + continue
601 } 639 }
602 var expense float64 640 var expense float64
603 if dividendsOrder.Expense != "" { 641 if dividendsOrder.Expense != "" {
604 if expenseParse, err := strconv.ParseFloat(dividendsOrder.Expense, 64); err != nil { 642 if expenseParse, err := strconv.ParseFloat(dividendsOrder.Expense, 64); err != nil {
605 - return nil, application.ThrowError(application.ARG_ERROR, err.Error()) 643 + tmpRow := &domain.FailInfo{
  644 + FailReason: "费用类型错误",
  645 + }
  646 + tmpRow.ImportDividendsOrderData = &dividendsOrder
  647 + aggregateErrorList = append(aggregateErrorList, tmpRow)
  648 + continue
606 } else { 649 } else {
607 expense = expenseParse 650 expense = expenseParse
608 } 651 }
@@ -634,22 +677,20 @@ func (dividendsOrderService *DividendsOrderService) ImportDividendsOrder(importD @@ -634,22 +677,20 @@ func (dividendsOrderService *DividendsOrderService) ImportDividendsOrder(importD
634 }, 677 },
635 }, nil 678 }, nil
636 } 679 }
  680 + /******************************************************************************************************************/
  681 +
  682 + // 错误数据返回
  683 + errorDataList := make([]*domain.ImportInfo, 0)
637 684
638 - // 批量创建分红订单命令集 685 + /******************************************** 批量创建分红订单命令集 **************************************************/
639 var createDividendsOrderCommands []*command.CreateDividendsOrderCommand 686 var createDividendsOrderCommands []*command.CreateDividendsOrderCommand
640 for _, dividendsOrderCommand := range dividendsOrderCommands { 687 for _, dividendsOrderCommand := range dividendsOrderCommands {
641 createDividendsOrderCommands = append(createDividendsOrderCommands, dividendsOrderCommand) 688 createDividendsOrderCommands = append(createDividendsOrderCommands, dividendsOrderCommand)
642 } 689 }
643 690
644 - // 新增失败记录  
645 - failureDataList := make([]interface{}, 0)  
646 -  
647 // 新增成功记录计数 691 // 新增成功记录计数
648 var successDataCount int64 692 var successDataCount int64
649 693
650 - // 错误数据返回  
651 - var errorDataList []*domain.ImportInfo  
652 -  
653 // 循环校验命令 694 // 循环校验命令
654 for _, cmd := range createDividendsOrderCommands { 695 for _, cmd := range createDividendsOrderCommands {
655 if err = cmd.ValidateCommand(); err != nil { 696 if err = cmd.ValidateCommand(); err != nil {
@@ -664,6 +705,9 @@ func (dividendsOrderService *DividendsOrderService) ImportDividendsOrder(importD @@ -664,6 +705,9 @@ func (dividendsOrderService *DividendsOrderService) ImportDividendsOrder(importD
664 } 705 }
665 } 706 }
666 707
  708 + // 新增失败记录
  709 + failureDataList := make([]interface{}, 0)
  710 +
667 // 循环校验命令失败返回 711 // 循环校验命令失败返回
668 if len(errorDataList) > 0 { 712 if len(errorDataList) > 0 {
669 successDataCount = 0 713 successDataCount = 0
@@ -695,8 +739,9 @@ func (dividendsOrderService *DividendsOrderService) ImportDividendsOrder(importD @@ -695,8 +739,9 @@ func (dividendsOrderService *DividendsOrderService) ImportDividendsOrder(importD
695 }, 739 },
696 }, nil 740 }, nil
697 } 741 }
  742 + /******************************************************************************************************************/
698 743
699 - // 创建分红订单领域模型 744 + /********************************************** 创建分红订单领域模型 *************************************************/
700 var creatDividendsOrder []*domain.DividendsOrder 745 var creatDividendsOrder []*domain.DividendsOrder
701 746
702 // 统计当前分红订单数 747 // 统计当前分红订单数
@@ -710,7 +755,13 @@ func (dividendsOrderService *DividendsOrderService) ImportDividendsOrder(importD @@ -710,7 +755,13 @@ func (dividendsOrderService *DividendsOrderService) ImportDividendsOrder(importD
710 // 生成分红订单号 755 // 生成分红订单号
711 dividendsOrderNumber, err := dividendsOrder.GenerateSpecificDividendsOrderNumber(int64(count), time.Time{}, int64(i)) 756 dividendsOrderNumber, err := dividendsOrder.GenerateSpecificDividendsOrderNumber(int64(count), time.Time{}, int64(i))
712 if err != nil { 757 if err != nil {
713 - return nil, application.ThrowError(application.TRANSACTION_ERROR, err.Error()) 758 + row := &domain.ImportInfo{
  759 + Error: application.ThrowError(application.INTERNAL_SERVER_ERROR, fmt.Sprintf("分红订单号生成错误:%s", err)),
  760 + LineNumbers: dividendsOrder.LineNumbers, // 错误影响的行
  761 + GoodLine: map[int]interface{}{},
  762 + }
  763 + errorDataList = append(errorDataList, row)
  764 + continue
714 } 765 }
715 766
716 // 订单时间转换 767 // 订单时间转换
@@ -1048,6 +1099,8 @@ func (dividendsOrderService *DividendsOrderService) BatchRemoveDividendsOrder(ba @@ -1048,6 +1099,8 @@ func (dividendsOrderService *DividendsOrderService) BatchRemoveDividendsOrder(ba
1048 } 1099 }
1049 } 1100 }
1050 1101
  1102 + // TODO 校验分红订单是否有分红预算
  1103 +
1051 dividendsOrdersRemoved, err := dividendsOrderRepository.BatchRemove(dividendsOrders) 1104 dividendsOrdersRemoved, err := dividendsOrderRepository.BatchRemove(dividendsOrders)
1052 if err != nil { 1105 if err != nil {
1053 return nil, application.ThrowError(application.TRANSACTION_ERROR, err.Error()) 1106 return nil, application.ThrowError(application.TRANSACTION_ERROR, err.Error())
@@ -31,7 +31,7 @@ type OrderGood struct { @@ -31,7 +31,7 @@ type OrderGood struct {
31 // 创建时间 31 // 创建时间
32 CreatedAt time.Time `comment:"创建时间"` 32 CreatedAt time.Time `comment:"创建时间"`
33 // 删除时间 33 // 删除时间
34 - DeletedAt time.Time `comment:"删除时间"` 34 + DeletedAt time.Time `comment:"删除时间" pg:",soft_delete"`
35 // 更新时间 35 // 更新时间
36 UpdatedAt time.Time `comment:"更新时间"` 36 UpdatedAt time.Time `comment:"更新时间"`
37 } 37 }