作者 陈志颖

refactor:分红退货单导入调整、优化

@@ -824,6 +824,7 @@ func (dividendsOrderService *DividendsOrderService) ImportDividendsOrder(importD @@ -824,6 +824,7 @@ func (dividendsOrderService *DividendsOrderService) ImportDividendsOrder(importD
824 log.Logger.Info("订单产品匹配的合约", map[string]interface{}{ 824 log.Logger.Info("订单产品匹配的合约", map[string]interface{}{
825 "contractMatched": cooperationContractsMap[orderGood.CooperationContractNumber], 825 "contractMatched": cooperationContractsMap[orderGood.CooperationContractNumber],
826 }) 826 })
  827 +
827 if cooperationContractsMap[orderGood.CooperationContractNumber] != nil && cooperationContractsMap[orderGood.CooperationContractNumber].IncentivesType != 1 { 828 if cooperationContractsMap[orderGood.CooperationContractNumber] != nil && cooperationContractsMap[orderGood.CooperationContractNumber].IncentivesType != 1 {
828 orderGoodErrMap[dividendsOrder.OrderGoods[j].LineNumber] = "分红订单不能关联金额激励规则" 829 orderGoodErrMap[dividendsOrder.OrderGoods[j].LineNumber] = "分红订单不能关联金额激励规则"
829 continue 830 continue
@@ -5,6 +5,7 @@ import ( @@ -5,6 +5,7 @@ import (
5 "github.com/beego/beego/v2/core/validation" 5 "github.com/beego/beego/v2/core/validation"
6 "reflect" 6 "reflect"
7 "strings" 7 "strings"
  8 + "time"
8 ) 9 )
9 10
10 type OrderGoods struct { 11 type OrderGoods struct {
@@ -61,6 +62,28 @@ type CreateDividendsReturnedOrderCommand struct { @@ -61,6 +62,28 @@ type CreateDividendsReturnedOrderCommand struct {
61 LineNumbers []int `cname:"记录行号" json:"lineNumbers"` 62 LineNumbers []int `cname:"记录行号" json:"lineNumbers"`
62 } 63 }
63 64
  65 +// GenerateSpecificDividendsReturnedOrderNumber 生成自定义退货单号
  66 +func (createDividendsReturnedOrderCommand *CreateDividendsReturnedOrderCommand) GenerateSpecificDividendsReturnedOrderNumber(count int64, orderTime time.Time, index int64) (string, error) {
  67 + currentTime := time.Now()
  68 + if count+index < 1000 {
  69 + countStr := fmt.Sprintf("%03d", count+index+1)
  70 + timestamp := currentTime.Unix()
  71 + timeNow := time.Unix(timestamp, 0)
  72 + timeString := timeNow.Format("20060102")
  73 + timeString = timeString[2:len(timeString)]
  74 + dividendsOrderNumber := "RE" + timeString + "#" + countStr
  75 + return dividendsOrderNumber, nil
  76 + } else {
  77 + countStr := fmt.Sprintf("%d", count+index+1)
  78 + timestamp := currentTime.Unix()
  79 + timeNow := time.Unix(timestamp, 0)
  80 + timeString := timeNow.Format("20060102")
  81 + timeString = timeString[2:len(timeString)]
  82 + dividendsOrderNumber := "RE" + timeString + "#" + countStr
  83 + return dividendsOrderNumber, nil
  84 + }
  85 +}
  86 +
64 func (createDividendsReturnedOrderCommand *CreateDividendsReturnedOrderCommand) Valid(validation *validation.Validation) { 87 func (createDividendsReturnedOrderCommand *CreateDividendsReturnedOrderCommand) Valid(validation *validation.Validation) {
65 } 88 }
66 89
@@ -14,6 +14,7 @@ import ( @@ -14,6 +14,7 @@ import (
14 "gitlab.fjmaimaimai.com/allied-creation/allied-creation-cooperation/pkg/domain/service" 14 "gitlab.fjmaimaimai.com/allied-creation/allied-creation-cooperation/pkg/domain/service"
15 "gitlab.fjmaimaimai.com/allied-creation/allied-creation-cooperation/pkg/infrastructure/dao" 15 "gitlab.fjmaimaimai.com/allied-creation/allied-creation-cooperation/pkg/infrastructure/dao"
16 "gitlab.fjmaimaimai.com/allied-creation/allied-creation-cooperation/pkg/infrastructure/utils" 16 "gitlab.fjmaimaimai.com/allied-creation/allied-creation-cooperation/pkg/infrastructure/utils"
  17 + "gitlab.fjmaimaimai.com/allied-creation/allied-creation-cooperation/pkg/log"
17 "reflect" 18 "reflect"
18 "regexp" 19 "regexp"
19 "strconv" 20 "strconv"
@@ -422,13 +423,15 @@ func (dividendsReturnedOrderService *DividendsReturnedOrderService) ImportDivide @@ -422,13 +423,15 @@ func (dividendsReturnedOrderService *DividendsReturnedOrderService) ImportDivide
422 "orderTime": "项目合约编号", 423 "orderTime": "项目合约编号",
423 } 424 }
424 425
425 - // 空文件校验 426 + /***************************************************** 空文件校验 ***************************************************/
426 if len(importDividendsReturnedOrderCommand.DividendsReturnedOrderData) == 0 { 427 if len(importDividendsReturnedOrderCommand.DividendsReturnedOrderData) == 0 {
427 return nil, application.ThrowError(application.INTERNAL_SERVER_ERROR, "导入的Excel文件为空文件,请上传正确的文件") 428 return nil, application.ThrowError(application.INTERNAL_SERVER_ERROR, "导入的Excel文件为空文件,请上传正确的文件")
428 } 429 }
  430 + /******************************************************************************************************************/
429 431
430 - // 必填项校验  
431 - nullCellError := make([]interface{}, 0) 432 + /************************************************ 2.必填项校验 ******************************************************/
  433 + // 空单元格错误
  434 + nullCellError := make([]*domain.FailReturnedInfo, 0)
432 435
433 // 数据行计数 436 // 数据行计数
434 rowCnt := 0 437 rowCnt := 0
@@ -439,11 +442,9 @@ func (dividendsReturnedOrderService *DividendsReturnedOrderService) ImportDivide @@ -439,11 +442,9 @@ func (dividendsReturnedOrderService *DividendsReturnedOrderService) ImportDivide
439 for i, dividendsReturnedOrder := range importDividendsReturnedOrderCommand.DividendsReturnedOrderData { 442 for i, dividendsReturnedOrder := range importDividendsReturnedOrderCommand.DividendsReturnedOrderData {
440 rowCnt++ 443 rowCnt++
441 nullCell := make([]interface{}, 0) 444 nullCell := make([]interface{}, 0)
442 - var myRow *domain.FailReturnedInfo  
443 t := reflect.TypeOf(dividendsReturnedOrder) 445 t := reflect.TypeOf(dividendsReturnedOrder)
444 v := reflect.ValueOf(dividendsReturnedOrder) 446 v := reflect.ValueOf(dividendsReturnedOrder)
445 for k := 0; k < t.NumField(); k++ { 447 for k := 0; k < t.NumField(); k++ {
446 - //fmt.Println("name:", fmt.Sprintf("%+v", t.Field(k).Name), ", value:", fmt.Sprintf("%v", v.Field(k).Interface()), ", yaml:", t.Field(k).Tag.Get("yaml"))  
447 if k != 5 && k != 8 { 448 if k != 5 && k != 8 {
448 if v.Field(k).Interface() == "" { 449 if v.Field(k).Interface() == "" {
449 col := strconv.Itoa(k + 1) 450 col := strconv.Itoa(k + 1)
@@ -453,9 +454,7 @@ func (dividendsReturnedOrderService *DividendsReturnedOrderService) ImportDivide @@ -453,9 +454,7 @@ func (dividendsReturnedOrderService *DividendsReturnedOrderService) ImportDivide
453 } 454 }
454 } 455 }
455 if nullFlag { 456 if nullFlag {
456 - //s := strconv.Itoa(i + 1)  
457 - //b := strings.Replace(strings.Trim(fmt.Sprint(nullCell), "[]"), " ", ",", -1)  
458 - myRow = &domain.FailReturnedInfo{ 457 + myRow := &domain.FailReturnedInfo{
459 FailReason: "必填项为空", 458 FailReason: "必填项为空",
460 ImportDividendsReturnedOrderData: &importDividendsReturnedOrderCommand.DividendsReturnedOrderData[i], 459 ImportDividendsReturnedOrderData: &importDividendsReturnedOrderCommand.DividendsReturnedOrderData[i],
461 } 460 }
@@ -474,11 +473,12 @@ func (dividendsReturnedOrderService *DividendsReturnedOrderService) ImportDivide @@ -474,11 +473,12 @@ func (dividendsReturnedOrderService *DividendsReturnedOrderService) ImportDivide
474 }, 473 },
475 }, nil 474 }, nil
476 } 475 }
  476 + /******************************************************************************************************************/
477 477
478 - // 单元格类型校验  
479 - typeError := make([]interface{}, 0) 478 + /***************************************************** 3.单元格类型校验 *********************************************/
  479 + typeError := make([]*domain.FailReturnedInfo, 0)
480 for _, dividendsReturnedOrder := range importDividendsReturnedOrderCommand.DividendsReturnedOrderData { // 行 480 for _, dividendsReturnedOrder := range importDividendsReturnedOrderCommand.DividendsReturnedOrderData { // 行
481 - var myRow *domain.FailReturnedInfo 481 + var myRow []*domain.FailReturnedInfo
482 t := reflect.TypeOf(dividendsReturnedOrder) 482 t := reflect.TypeOf(dividendsReturnedOrder)
483 v := reflect.ValueOf(dividendsReturnedOrder) 483 v := reflect.ValueOf(dividendsReturnedOrder)
484 for k := 0; k < t.NumField(); k++ { // 列 484 for k := 0; k < t.NumField(); k++ { // 列
@@ -488,12 +488,12 @@ func (dividendsReturnedOrderService *DividendsReturnedOrderService) ImportDivide @@ -488,12 +488,12 @@ func (dividendsReturnedOrderService *DividendsReturnedOrderService) ImportDivide
488 regexpStr := `(\d{4})/(\d{2})/(\d{2})` 488 regexpStr := `(\d{4})/(\d{2})/(\d{2})`
489 ok := regexp.MustCompile(regexpStr).MatchString(fmt.Sprintf("%v", v.Field(k).Interface())) 489 ok := regexp.MustCompile(regexpStr).MatchString(fmt.Sprintf("%v", v.Field(k).Interface()))
490 if !ok { 490 if !ok {
491 - var tmpRow *domain.FailReturnedInfo  
492 - tmpRow = &domain.FailReturnedInfo{ 491 + tmpRow := &domain.FailReturnedInfo{
493 FailReason: "退货日期格式错误,请输入正确的退货日期", 492 FailReason: "退货日期格式错误,请输入正确的退货日期",
494 ImportDividendsReturnedOrderData: &dividendsReturnedOrder, 493 ImportDividendsReturnedOrderData: &dividendsReturnedOrder,
495 } 494 }
496 - myRow = tmpRow 495 + myRow = append(myRow, tmpRow)
  496 + continue
497 } 497 }
498 } 498 }
499 case 4: // 订单日期校验 499 case 4: // 订单日期校验
@@ -501,12 +501,12 @@ func (dividendsReturnedOrderService *DividendsReturnedOrderService) ImportDivide @@ -501,12 +501,12 @@ func (dividendsReturnedOrderService *DividendsReturnedOrderService) ImportDivide
501 regexpStr := `(\d{4})/(\d{2})/(\d{2})` 501 regexpStr := `(\d{4})/(\d{2})/(\d{2})`
502 ok := regexp.MustCompile(regexpStr).MatchString(fmt.Sprintf("%v", v.Field(k).Interface())) 502 ok := regexp.MustCompile(regexpStr).MatchString(fmt.Sprintf("%v", v.Field(k).Interface()))
503 if !ok { 503 if !ok {
504 - var tmpRow *domain.FailReturnedInfo  
505 - tmpRow = &domain.FailReturnedInfo{ 504 + tmpRow := &domain.FailReturnedInfo{
506 FailReason: "订单日期格式错误,请输入正确的订单日期", 505 FailReason: "订单日期格式错误,请输入正确的订单日期",
507 ImportDividendsReturnedOrderData: &dividendsReturnedOrder, 506 ImportDividendsReturnedOrderData: &dividendsReturnedOrder,
508 } 507 }
509 - myRow = tmpRow 508 + myRow = append(myRow, tmpRow)
  509 + continue
510 } 510 }
511 } 511 }
512 case 6: // 退货数量校验 512 case 6: // 退货数量校验
@@ -514,21 +514,21 @@ func (dividendsReturnedOrderService *DividendsReturnedOrderService) ImportDivide @@ -514,21 +514,21 @@ func (dividendsReturnedOrderService *DividendsReturnedOrderService) ImportDivide
514 //参数类型转换 514 //参数类型转换
515 orderGoodQuantity, err := strconv.ParseInt(fmt.Sprintf("%v", v.Field(k).Interface()), 10, 64) 515 orderGoodQuantity, err := strconv.ParseInt(fmt.Sprintf("%v", v.Field(k).Interface()), 10, 64)
516 if err != nil { 516 if err != nil {
517 - var tmpRow *domain.FailReturnedInfo  
518 - tmpRow = &domain.FailReturnedInfo{ 517 + tmpRow := &domain.FailReturnedInfo{
519 FailReason: "退货数量格式错误,退货数量必须整数", 518 FailReason: "退货数量格式错误,退货数量必须整数",
520 ImportDividendsReturnedOrderData: &dividendsReturnedOrder, 519 ImportDividendsReturnedOrderData: &dividendsReturnedOrder,
521 } 520 }
522 - myRow = tmpRow 521 + myRow = append(myRow, tmpRow)
  522 + continue
523 } 523 }
524 // 正负判断 524 // 正负判断
525 if orderGoodQuantity < 0 { 525 if orderGoodQuantity < 0 {
526 - var tmpRow *domain.FailReturnedInfo  
527 - tmpRow = &domain.FailReturnedInfo{ 526 + tmpRow := &domain.FailReturnedInfo{
528 FailReason: "退货数量必须大于0,请重新填写", 527 FailReason: "退货数量必须大于0,请重新填写",
529 ImportDividendsReturnedOrderData: &dividendsReturnedOrder, 528 ImportDividendsReturnedOrderData: &dividendsReturnedOrder,
530 } 529 }
531 - myRow = tmpRow 530 + myRow = append(myRow, tmpRow)
  531 + continue
532 } 532 }
533 } 533 }
534 case 7: // 退货价格校验 534 case 7: // 退货价格校验
@@ -536,28 +536,28 @@ func (dividendsReturnedOrderService *DividendsReturnedOrderService) ImportDivide @@ -536,28 +536,28 @@ func (dividendsReturnedOrderService *DividendsReturnedOrderService) ImportDivide
536 // 参数类型转换 536 // 参数类型转换
537 univalent, typeErr := strconv.ParseFloat(fmt.Sprintf("%v", v.Field(k).Interface()), 64) 537 univalent, typeErr := strconv.ParseFloat(fmt.Sprintf("%v", v.Field(k).Interface()), 64)
538 if typeErr != nil { 538 if typeErr != nil {
539 - var tmpRow *domain.FailReturnedInfo  
540 - tmpRow = &domain.FailReturnedInfo{ 539 + tmpRow := &domain.FailReturnedInfo{
541 FailReason: "退货价格格式错误,退货价格必须为数字类型", 540 FailReason: "退货价格格式错误,退货价格必须为数字类型",
542 ImportDividendsReturnedOrderData: &dividendsReturnedOrder, 541 ImportDividendsReturnedOrderData: &dividendsReturnedOrder,
543 } 542 }
544 - myRow = tmpRow 543 + myRow = append(myRow, tmpRow)
  544 + continue
545 } 545 }
546 546
547 // 长度校验 547 // 长度校验
548 if univalent >= 1e16 { 548 if univalent >= 1e16 {
549 - var tmpRow *domain.FailReturnedInfo  
550 - tmpRow = &domain.FailReturnedInfo{ 549 + tmpRow := &domain.FailReturnedInfo{
551 FailReason: "退货价格超过最大限制,退货价格小数点前面不能超过十六位数字,并保留两位小数", 550 FailReason: "退货价格超过最大限制,退货价格小数点前面不能超过十六位数字,并保留两位小数",
552 ImportDividendsReturnedOrderData: &dividendsReturnedOrder, 551 ImportDividendsReturnedOrderData: &dividendsReturnedOrder,
553 } 552 }
554 - myRow = tmpRow 553 + myRow = append(myRow, tmpRow)
  554 + continue
555 } 555 }
556 } 556 }
557 } 557 }
558 } 558 }
559 - if myRow != nil {  
560 - typeError = append(typeError, myRow) 559 + if len(myRow) > 0 {
  560 + typeError = append(typeError, myRow...)
561 } 561 }
562 } 562 }
563 563
@@ -571,53 +571,59 @@ func (dividendsReturnedOrderService *DividendsReturnedOrderService) ImportDivide @@ -571,53 +571,59 @@ func (dividendsReturnedOrderService *DividendsReturnedOrderService) ImportDivide
571 }, 571 },
572 }, nil 572 }, nil
573 } 573 }
  574 + /******************************************************************************************************************/
574 575
  576 + /******************************************** 4.聚合订单并进行类型校验 ************************************************/
575 // 聚合订单并进行类型校验 577 // 聚合订单并进行类型校验
576 aggregateErrorList := make([]interface{}, 0) 578 aggregateErrorList := make([]interface{}, 0)
577 579
578 var dividendsReturnedOrderCommands = make(map[string]*command.CreateDividendsReturnedOrderCommand) 580 var dividendsReturnedOrderCommands = make(map[string]*command.CreateDividendsReturnedOrderCommand)
  581 +
579 for i, dividendsReturnedOrder := range importDividendsReturnedOrderCommand.DividendsReturnedOrderData { 582 for i, dividendsReturnedOrder := range importDividendsReturnedOrderCommand.DividendsReturnedOrderData {
580 hashValue := md5.Sum([]byte(dividendsReturnedOrder.OriginalOrderNum)) 583 hashValue := md5.Sum([]byte(dividendsReturnedOrder.OriginalOrderNum))
581 hashString := hex.EncodeToString(hashValue[:]) 584 hashString := hex.EncodeToString(hashValue[:])
582 if _, ok := dividendsReturnedOrderCommands[hashString]; !ok { 585 if _, ok := dividendsReturnedOrderCommands[hashString]; !ok {
583 - // 来源单号  
584 - dividendsReturnedOrderCommands[hashString].OriginalOrderNum = dividendsReturnedOrder.OriginalOrderNum  
585 -  
586 - // 客户名称  
587 - dividendsReturnedOrderCommands[hashString].DividendsReturnedCustomerName = dividendsReturnedOrder.DividendsReturnedCustomerName  
588 586
589 // 订单日期时间格式转换 587 // 订单日期时间格式转换
590 timeValue, err := time.ParseInLocation("2006/01/02", dividendsReturnedOrder.OrderTime, time.Local) 588 timeValue, err := time.ParseInLocation("2006/01/02", dividendsReturnedOrder.OrderTime, time.Local)
591 if err != nil { 589 if err != nil {
592 - var tmpRow []string  
593 - tmpRow = append(tmpRow, "无效的订单日期") // 错误信息  
594 - s := strconv.Itoa(i + 1)  
595 - tmpRow = append(tmpRow, s) // 行号  
596 - //tmpRow = append(tmpRow, dividendsReturnedOrder.OriginalOrderNum) // TODO 错误行数据 590 + tmpRow := &domain.FailReturnedInfo{
  591 + FailReason: "无效的订单日期",
  592 + ImportDividendsReturnedOrderData: &dividendsReturnedOrder,
  593 + }
597 aggregateErrorList = append(aggregateErrorList, tmpRow) 594 aggregateErrorList = append(aggregateErrorList, tmpRow)
598 - break 595 + continue
599 } 596 }
600 597
601 // 退货日期格式转换 598 // 退货日期格式转换
602 timeValue2, err := time.ParseInLocation("2006/01/02", dividendsReturnedOrder.DividendsReturnedDate, time.Local) 599 timeValue2, err := time.ParseInLocation("2006/01/02", dividendsReturnedOrder.DividendsReturnedDate, time.Local)
603 if err != nil { 600 if err != nil {
604 - var tmpRow []string  
605 - tmpRow = append(tmpRow, "无效的订单日期") // 错误信息  
606 - s := strconv.Itoa(i + 1)  
607 - tmpRow = append(tmpRow, s) // 行号  
608 - //tmpRow = append(tmpRow, dividendsReturnedOrder.OriginalOrderNum) // TODO 错误行数据 601 + tmpRow := &domain.FailReturnedInfo{
  602 + FailReason: "无效的订单日期",
  603 + ImportDividendsReturnedOrderData: &dividendsReturnedOrder,
  604 + }
609 aggregateErrorList = append(aggregateErrorList, tmpRow) 605 aggregateErrorList = append(aggregateErrorList, tmpRow)
610 - break 606 + continue
611 } 607 }
612 608
613 // 产品相关:产品名称,退货数量、退货价格 609 // 产品相关:产品名称,退货数量、退货价格
614 quantity, err := strconv.ParseFloat(dividendsReturnedOrder.OrderGoodQuantity, 64) 610 quantity, err := strconv.ParseFloat(dividendsReturnedOrder.OrderGoodQuantity, 64)
615 if err != nil { 611 if err != nil {
616 - return nil, application.ThrowError(application.ARG_ERROR, err.Error()) 612 + tmpRow := &domain.FailReturnedInfo{
  613 + FailReason: "产品数量类型错误",
  614 + ImportDividendsReturnedOrderData: &dividendsReturnedOrder,
  615 + }
  616 + aggregateErrorList = append(aggregateErrorList, tmpRow)
  617 + continue
617 } 618 }
618 price, err := strconv.ParseFloat(dividendsReturnedOrder.OrderGoodPrice, 64) 619 price, err := strconv.ParseFloat(dividendsReturnedOrder.OrderGoodPrice, 64)
619 if err != nil { 620 if err != nil {
620 - return nil, application.ThrowError(application.ARG_ERROR, err.Error()) 621 + tmpRow := &domain.FailReturnedInfo{
  622 + FailReason: "产品价格类型错误",
  623 + ImportDividendsReturnedOrderData: &dividendsReturnedOrder,
  624 + }
  625 + aggregateErrorList = append(aggregateErrorList, tmpRow)
  626 + continue
621 } 627 }
622 628
623 // 初始化新建分红订单命令集 629 // 初始化新建分红订单命令集
@@ -639,6 +645,7 @@ func (dividendsReturnedOrderService *DividendsReturnedOrderService) ImportDivide @@ -639,6 +645,7 @@ func (dividendsReturnedOrderService *DividendsReturnedOrderService) ImportDivide
639 DividendsOrderNumber: "", 645 DividendsOrderNumber: "",
640 CooperationContractNumber: dividendsReturnedOrder.CooperationContractNumber, 646 CooperationContractNumber: dividendsReturnedOrder.CooperationContractNumber,
641 OrderGoodExpense: 0, 647 OrderGoodExpense: 0,
  648 + LineNumber: i,
642 }, 649 },
643 }, 650 },
644 CompanyId: importDividendsReturnedOrderCommand.CompanyId, 651 CompanyId: importDividendsReturnedOrderCommand.CompanyId,
@@ -651,11 +658,21 @@ func (dividendsReturnedOrderService *DividendsReturnedOrderService) ImportDivide @@ -651,11 +658,21 @@ func (dividendsReturnedOrderService *DividendsReturnedOrderService) ImportDivide
651 // 产品相关:产品名称,退货数量、退货价格 658 // 产品相关:产品名称,退货数量、退货价格
652 quantity, err := strconv.ParseFloat(dividendsReturnedOrder.OrderGoodQuantity, 64) 659 quantity, err := strconv.ParseFloat(dividendsReturnedOrder.OrderGoodQuantity, 64)
653 if err != nil { 660 if err != nil {
654 - return nil, application.ThrowError(application.ARG_ERROR, err.Error()) 661 + tmpRow := &domain.FailReturnedInfo{
  662 + FailReason: "产品数量类型错误",
  663 + ImportDividendsReturnedOrderData: &dividendsReturnedOrder,
  664 + }
  665 + aggregateErrorList = append(aggregateErrorList, tmpRow)
  666 + continue
655 } 667 }
656 price, err := strconv.ParseFloat(dividendsReturnedOrder.OrderGoodPrice, 64) 668 price, err := strconv.ParseFloat(dividendsReturnedOrder.OrderGoodPrice, 64)
657 if err != nil { 669 if err != nil {
658 - return nil, application.ThrowError(application.ARG_ERROR, err.Error()) 670 + tmpRow := &domain.FailReturnedInfo{
  671 + FailReason: "产品价格类型错误",
  672 + ImportDividendsReturnedOrderData: &dividendsReturnedOrder,
  673 + }
  674 + aggregateErrorList = append(aggregateErrorList, tmpRow)
  675 + continue
659 } 676 }
660 677
661 dividendsReturnedOrderCommands[hashString].OrderGoods = append(dividendsReturnedOrderCommands[hashString].OrderGoods, command.OrderGoods{ 678 dividendsReturnedOrderCommands[hashString].OrderGoods = append(dividendsReturnedOrderCommands[hashString].OrderGoods, command.OrderGoods{
@@ -667,6 +684,7 @@ func (dividendsReturnedOrderService *DividendsReturnedOrderService) ImportDivide @@ -667,6 +684,7 @@ func (dividendsReturnedOrderService *DividendsReturnedOrderService) ImportDivide
667 DividendsOrderNumber: "", 684 DividendsOrderNumber: "",
668 CooperationContractNumber: dividendsReturnedOrder.CooperationContractNumber, 685 CooperationContractNumber: dividendsReturnedOrder.CooperationContractNumber,
669 OrderGoodExpense: 0, 686 OrderGoodExpense: 0,
  687 + LineNumber: i,
670 }) 688 })
671 689
672 // 记录聚合行号 690 // 记录聚合行号
@@ -674,6 +692,10 @@ func (dividendsReturnedOrderService *DividendsReturnedOrderService) ImportDivide @@ -674,6 +692,10 @@ func (dividendsReturnedOrderService *DividendsReturnedOrderService) ImportDivide
674 } 692 }
675 } 693 }
676 694
  695 + log.Logger.Info("聚合退货单", map[string]interface{}{
  696 + "dividendsReturnedOrderCommands": dividendsReturnedOrderCommands,
  697 + })
  698 +
677 // 聚合订单错误返回 699 // 聚合订单错误返回
678 if len(aggregateErrorList) > 0 { 700 if len(aggregateErrorList) > 0 {
679 return map[string]interface{}{ 701 return map[string]interface{}{
@@ -684,22 +706,20 @@ func (dividendsReturnedOrderService *DividendsReturnedOrderService) ImportDivide @@ -684,22 +706,20 @@ func (dividendsReturnedOrderService *DividendsReturnedOrderService) ImportDivide
684 }, 706 },
685 }, nil 707 }, nil
686 } 708 }
  709 + /******************************************************************************************************************/
  710 +
  711 + // 错误数据返回
  712 + var errorDataList []*domain.ImportInfo
687 713
688 - // 批量创建分红订单命令集 714 + /******************************************** 批量创建分红订单命令集 **************************************************/
689 var createDividendsReturnedOrderCommands []*command.CreateDividendsReturnedOrderCommand 715 var createDividendsReturnedOrderCommands []*command.CreateDividendsReturnedOrderCommand
690 for _, dividendsReturnedOrderCommand := range dividendsReturnedOrderCommands { 716 for _, dividendsReturnedOrderCommand := range dividendsReturnedOrderCommands {
691 createDividendsReturnedOrderCommands = append(createDividendsReturnedOrderCommands, dividendsReturnedOrderCommand) 717 createDividendsReturnedOrderCommands = append(createDividendsReturnedOrderCommands, dividendsReturnedOrderCommand)
692 } 718 }
693 719
694 - // 新增失败记录  
695 - failureDataList := make([]interface{}, 0)  
696 -  
697 // 新增成功记录计数 720 // 新增成功记录计数
698 var successDataCount int64 721 var successDataCount int64
699 722
700 - // 错误数据返回  
701 - var errorDataList []*domain.ImportInfo  
702 -  
703 // 循环校验命令 723 // 循环校验命令
704 for _, cmd := range createDividendsReturnedOrderCommands { 724 for _, cmd := range createDividendsReturnedOrderCommands {
705 if err = cmd.ValidateCommand(); err != nil { 725 if err = cmd.ValidateCommand(); err != nil {
@@ -714,6 +734,9 @@ func (dividendsReturnedOrderService *DividendsReturnedOrderService) ImportDivide @@ -714,6 +734,9 @@ func (dividendsReturnedOrderService *DividendsReturnedOrderService) ImportDivide
714 } 734 }
715 } 735 }
716 736
  737 + // 新增失败记录
  738 + failureDataList := make([]interface{}, 0)
  739 +
717 // 循环校验命令失败返回 740 // 循环校验命令失败返回
718 if len(errorDataList) > 0 { 741 if len(errorDataList) > 0 {
719 successDataCount = 0 742 successDataCount = 0
@@ -721,20 +744,18 @@ func (dividendsReturnedOrderService *DividendsReturnedOrderService) ImportDivide @@ -721,20 +744,18 @@ func (dividendsReturnedOrderService *DividendsReturnedOrderService) ImportDivide
721 for _, errorData := range errorDataList { 744 for _, errorData := range errorDataList {
722 if len(errorData.GoodLine) == 0 { // 订单错误 745 if len(errorData.GoodLine) == 0 { // 订单错误
723 for _, line := range errorData.LineNumbers { 746 for _, line := range errorData.LineNumbers {
724 - var tmpRow []string  
725 - tmpRow = append(tmpRow, errorData.Error.Error()) // 错误信息  
726 - s := strconv.Itoa(line + 1)  
727 - tmpRow = append(tmpRow, s) // 行号  
728 - //tmpRow = append(tmpRow, importDividendsOrderCommand.DividendsOrderData[line]...) // 错误行数据 747 + tmpRow := &domain.FailReturnedInfo{
  748 + FailReason: "数据校验错误:" + errorData.Error.Error(),
  749 + ImportDividendsReturnedOrderData: &importDividendsReturnedOrderCommand.DividendsReturnedOrderData[line],
  750 + }
729 failureDataList = append(failureDataList, tmpRow) 751 failureDataList = append(failureDataList, tmpRow)
730 } 752 }
731 } else if len(errorData.GoodLine) > 0 { // 订单产品错误 753 } else if len(errorData.GoodLine) > 0 { // 订单产品错误
732 for line := range errorData.GoodLine { 754 for line := range errorData.GoodLine {
733 - var tmpRow []string  
734 - tmpRow = append(tmpRow, errorData.Error.Error()) // 错误信息  
735 - s := strconv.Itoa(line + 1)  
736 - tmpRow = append(tmpRow, s) // 行号  
737 - //tmpRow = append(tmpRow, importDividendsOrderCommand.DividendsOrderData[line]...) // 错误行数据 755 + tmpRow := &domain.FailReturnedInfo{
  756 + FailReason: "退货单产品信息错误:" + errorData.Error.Error(),
  757 + ImportDividendsReturnedOrderData: &importDividendsReturnedOrderCommand.DividendsReturnedOrderData[line],
  758 + }
738 failureDataList = append(failureDataList, tmpRow) 759 failureDataList = append(failureDataList, tmpRow)
739 } 760 }
740 } 761 }
@@ -747,17 +768,67 @@ func (dividendsReturnedOrderService *DividendsReturnedOrderService) ImportDivide @@ -747,17 +768,67 @@ func (dividendsReturnedOrderService *DividendsReturnedOrderService) ImportDivide
747 }, 768 },
748 }, nil 769 }, nil
749 } 770 }
  771 + /******************************************************************************************************************/
  772 +
  773 + /********************************************** 创建分红退货单领域模型 *************************************************/
  774 + var createDividendsReturnedOrders []*domain.DividendsReturnedOrder
  775 +
  776 + // 统计当前分红退货单数
  777 + count, err := dividendsReturnedOrderDao.CountDividendsReturnedOrder(map[string]interface{}{})
  778 + if err != nil {
  779 + return nil, application.ThrowError(application.TRANSACTION_ERROR, err.Error())
  780 + }
750 781
751 // 批量导入创建退货单 782 // 批量导入创建退货单
752 - for _, dividendsReturnedOrder := range createDividendsReturnedOrderCommands { 783 + for i, dividendsReturnedOrder := range createDividendsReturnedOrderCommands {
753 // 生成退货订单号 784 // 生成退货订单号
754 - dividendsReturnedOrderNumber, err := dividendsReturnedOrderDao.GenerateDividendsReturnedOrderNumber(map[string]interface{}{  
755 - "companyId": importDividendsReturnedOrderCommand.CompanyId,  
756 - }) 785 + //dividendsReturnedOrderNumber, err := dividendsReturnedOrderDao.GenerateDividendsReturnedOrderNumber(map[string]interface{}{
  786 + // "companyId": importDividendsReturnedOrderCommand.CompanyId,
  787 + //})
  788 + //if err != nil {
  789 + // return nil, application.ThrowError(application.TRANSACTION_ERROR, err.Error())
  790 + //}
  791 +
  792 + // 生成分红订单号
  793 + dividendsReturnedOrderNumber, err := dividendsReturnedOrder.GenerateSpecificDividendsReturnedOrderNumber(int64(count), time.Time{}, int64(i))
757 if err != nil { 794 if err != nil {
758 - return nil, application.ThrowError(application.TRANSACTION_ERROR, err.Error()) 795 + row := &domain.ImportInfo{
  796 + Error: application.ThrowError(application.INTERNAL_SERVER_ERROR, fmt.Sprintf("分红退货单号生成错误:%s", err)),
  797 + LineNumbers: dividendsReturnedOrder.LineNumbers, // 错误影响的行
  798 + GoodLine: map[int]string{},
  799 + }
  800 + errorDataList = append(errorDataList, row)
  801 + continue
  802 + }
  803 +
  804 + // 订单时间转换
  805 + orderTimeInt, err := strconv.ParseInt(dividendsReturnedOrder.OrderTime, 10, 64)
  806 + if err != nil {
  807 + row := &domain.ImportInfo{
  808 + Error: application.ThrowError(application.INTERNAL_SERVER_ERROR, fmt.Sprintf("订单日期转换错误:%s", err)),
  809 + LineNumbers: dividendsReturnedOrder.LineNumbers, // 错误影响的行
  810 + GoodLine: map[int]string{},
  811 + }
  812 + errorDataList = append(errorDataList, row)
  813 + continue
  814 + }
  815 +
  816 + orderTime := utils.TransformTimestampToTime(orderTimeInt)
  817 +
  818 + // 退货时间转换
  819 + returnedTimeInt, err := strconv.ParseInt(dividendsReturnedOrder.DividendsReturnedDate, 10, 64)
  820 + if err != nil {
  821 + row := &domain.ImportInfo{
  822 + Error: application.ThrowError(application.INTERNAL_SERVER_ERROR, fmt.Sprintf("退货时间错误:%s", err)),
  823 + LineNumbers: dividendsReturnedOrder.LineNumbers, // 错误影响的行
  824 + GoodLine: map[int]string{},
  825 + }
  826 + errorDataList = append(errorDataList, row)
  827 + continue
759 } 828 }
760 829
  830 + returnedTime := utils.TransformTimestampToTime(returnedTimeInt)
  831 +
761 // 校验退货订单编号是否唯一 832 // 校验退货订单编号是否唯一
762 numberAvailable, err3 := dividendsReturnedOrderDao.CheckDividendsReturnedOrderNumberAvailable(map[string]interface{}{ 833 numberAvailable, err3 := dividendsReturnedOrderDao.CheckDividendsReturnedOrderNumberAvailable(map[string]interface{}{
763 "companyId": importDividendsReturnedOrderCommand.CompanyId, 834 "companyId": importDividendsReturnedOrderCommand.CompanyId,
@@ -773,8 +844,8 @@ func (dividendsReturnedOrderService *DividendsReturnedOrderService) ImportDivide @@ -773,8 +844,8 @@ func (dividendsReturnedOrderService *DividendsReturnedOrderService) ImportDivide
773 // 新增订单产品 844 // 新增订单产品
774 var orderGoods []*domain.OrderGood 845 var orderGoods []*domain.OrderGood
775 var dividendsReturnedOrderAmount float64 846 var dividendsReturnedOrderAmount float64
776 - orderGoodErrMap := make(map[int]interface{}, 0)  
777 - for i, orderGood := range dividendsReturnedOrder.OrderGoods { 847 + orderGoodErrMap := make(map[int]string, 0)
  848 + for j, orderGood := range dividendsReturnedOrder.OrderGoods {
778 // 校验共创合约是否合法 849 // 校验共创合约是否合法
779 contractNumberExist := false 850 contractNumberExist := false
780 for _, cooperationContract := range cooperationContracts { 851 for _, cooperationContract := range cooperationContracts {
@@ -784,16 +855,39 @@ func (dividendsReturnedOrderService *DividendsReturnedOrderService) ImportDivide @@ -784,16 +855,39 @@ func (dividendsReturnedOrderService *DividendsReturnedOrderService) ImportDivide
784 } 855 }
785 } 856 }
786 if !contractNumberExist { 857 if !contractNumberExist {
787 - orderGoodErrMap[dividendsReturnedOrder.OrderGoods[i].LineNumber] = application.ThrowError(application.INTERNAL_SERVER_ERROR, fmt.Sprintf("共创合约编号不存在:%s", err)) 858 + orderGoodErrMap[dividendsReturnedOrder.OrderGoods[j].LineNumber] = "订单产品关联的共创合约不存在"
  859 + continue
788 } 860 }
789 861
790 // 计算产品金额 862 // 计算产品金额
791 orderGoodAmount, _ := decimal.NewFromFloat(orderGood.OrderGoodPrice).Mul(decimal.NewFromFloat(orderGood.OrderGoodQuantity)).Float64() 863 orderGoodAmount, _ := decimal.NewFromFloat(orderGood.OrderGoodPrice).Mul(decimal.NewFromFloat(orderGood.OrderGoodQuantity)).Float64()
792 864
793 // 校验合约激励类型是否正确 865 // 校验合约激励类型是否正确
  866 + ruleMatchedFlag := false
794 if orderGood.CooperationContractNumber != "" { 867 if orderGood.CooperationContractNumber != "" {
  868 + // 校验共创合约激励类型是否正确
  869 + log.Logger.Info("退货单产品匹配的合约", map[string]interface{}{
  870 + "contractMatched": cooperationContractsMap[orderGood.CooperationContractNumber],
  871 + })
  872 +
795 if cooperationContractsMap[orderGood.CooperationContractNumber] != nil && cooperationContractsMap[orderGood.CooperationContractNumber].IncentivesType != 1 { 873 if cooperationContractsMap[orderGood.CooperationContractNumber] != nil && cooperationContractsMap[orderGood.CooperationContractNumber].IncentivesType != 1 {
796 - orderGoodErrMap[dividendsReturnedOrder.OrderGoods[i].LineNumber] = application.ThrowError(application.INTERNAL_SERVER_ERROR, "退货单产品不能关联金额激励规则") 874 + orderGoodErrMap[dividendsReturnedOrder.OrderGoods[j].LineNumber] = "退货单产品不能关联金额激励规则"
  875 + continue
  876 + }
  877 +
  878 + // 检验产品关联合约的激励规则是否匹配订单时间
  879 + if cooperationContractsMap[orderGood.CooperationContractNumber] != nil {
  880 + for _, incentivesRule := range cooperationContractsMap[orderGood.CooperationContractNumber].DividendsIncentivesRules {
  881 + if (orderTime.After(incentivesRule.DividendsIncentivesStageStart) || orderTime.Equal(incentivesRule.DividendsIncentivesStageStart)) &&
  882 + (orderTime.Before(incentivesRule.DividendsIncentivesStageEnd) || orderTime.Equal(incentivesRule.DividendsIncentivesStageEnd)) {
  883 + ruleMatchedFlag = true
  884 + break
  885 + }
  886 + }
  887 + }
  888 + if !ruleMatchedFlag {
  889 + orderGoodErrMap[dividendsReturnedOrder.OrderGoods[j].LineNumber] = "订单时间无法匹配分红激励规则"
  890 + continue
797 } 891 }
798 } 892 }
799 893
@@ -813,23 +907,23 @@ func (dividendsReturnedOrderService *DividendsReturnedOrderService) ImportDivide @@ -813,23 +907,23 @@ func (dividendsReturnedOrderService *DividendsReturnedOrderService) ImportDivide
813 DeletedAt: time.Time{}, 907 DeletedAt: time.Time{},
814 UpdatedAt: time.Time{}, 908 UpdatedAt: time.Time{},
815 }) 909 })
  910 +
816 // 计算分红订单金额 911 // 计算分红订单金额
817 dividendsReturnedOrderAmount = dividendsReturnedOrderAmount + orderGood.OrderGoodAmount 912 dividendsReturnedOrderAmount = dividendsReturnedOrderAmount + orderGood.OrderGoodAmount
818 } 913 }
819 914
820 - // 订单时间转换  
821 - orderTimeInt, err := strconv.ParseInt(dividendsReturnedOrder.OrderTime, 10, 64)  
822 - if err != nil {  
823 - return nil, application.ThrowError(application.INTERNAL_SERVER_ERROR, "订单时间错误")  
824 - }  
825 - orderTime := utils.TransformTimestampToTime(orderTimeInt)  
826 -  
827 - // 退货时间转换  
828 - returnedTimeInt, err := strconv.ParseInt(dividendsReturnedOrder.DividendsReturnedDate, 10, 64)  
829 - if err != nil {  
830 - return nil, application.ThrowError(application.INTERNAL_SERVER_ERROR, "退货时间错误") 915 + if len(orderGoodErrMap) > 0 {
  916 + log.Logger.Info("校验退货单产品", map[string]interface{}{
  917 + "orderGoodErrMap": orderGoodErrMap,
  918 + })
  919 + row := &domain.ImportInfo{
  920 + Error: nil,
  921 + LineNumbers: dividendsReturnedOrder.LineNumbers, // 错误影响的行
  922 + GoodLine: orderGoodErrMap, // 错误产品行号记录
  923 + }
  924 + errorDataList = append(errorDataList, row)
  925 + continue
831 } 926 }
832 - returnedTime := utils.TransformTimestampToTime(returnedTimeInt)  
833 927
834 // 新增退货单 928 // 新增退货单
835 newDividendsReturnedOrder := &domain.DividendsReturnedOrder{ 929 newDividendsReturnedOrder := &domain.DividendsReturnedOrder{
@@ -858,17 +952,15 @@ func (dividendsReturnedOrderService *DividendsReturnedOrderService) ImportDivide @@ -858,17 +952,15 @@ func (dividendsReturnedOrderService *DividendsReturnedOrderService) ImportDivide
858 OperateTime: time.Now(), 952 OperateTime: time.Now(),
859 } 953 }
860 954
861 - if _, err2 := dividendsReturnedOrderRepository.Save(newDividendsReturnedOrder); err2 != nil {  
862 - row := &domain.ImportInfo{  
863 - Error: application.ThrowError(application.INTERNAL_SERVER_ERROR, fmt.Sprintf("保存退货单数据失败:%s", err)),  
864 - LineNumbers: dividendsReturnedOrder.LineNumbers, // 错误影响的行  
865 - GoodLine: map[int]string{},  
866 - }  
867 - errorDataList = append(errorDataList, row)  
868 - continue  
869 - } 955 + createDividendsReturnedOrders = append(createDividendsReturnedOrders, newDividendsReturnedOrder)
870 } 956 }
  957 + /******************************************************************************************************************/
  958 +
  959 + /*********************************************** 错误报告处理 *******************************************************/
871 if len(errorDataList) <= 0 { 960 if len(errorDataList) <= 0 {
  961 + if _, err4 := dividendsReturnedOrderRepository.SaveMany(createDividendsReturnedOrders); err4 != nil {
  962 + return nil, application.ThrowError(application.INTERNAL_SERVER_ERROR, err4.Error())
  963 + }
872 if err3 := transactionContext.CommitTransaction(); err3 != nil { 964 if err3 := transactionContext.CommitTransaction(); err3 != nil {
873 return nil, application.ThrowError(application.TRANSACTION_ERROR, err3.Error()) 965 return nil, application.ThrowError(application.TRANSACTION_ERROR, err3.Error())
874 } 966 }
@@ -876,7 +968,7 @@ func (dividendsReturnedOrderService *DividendsReturnedOrderService) ImportDivide @@ -876,7 +968,7 @@ func (dividendsReturnedOrderService *DividendsReturnedOrderService) ImportDivide
876 if successDataCount == int64(rowCnt) { 968 if successDataCount == int64(rowCnt) {
877 return map[string]interface{}{ 969 return map[string]interface{}{
878 "successCount": successDataCount, 970 "successCount": successDataCount,
879 - "fail": nil, 971 + "fail": make(map[string]interface{}),
880 }, nil 972 }, nil
881 } 973 }
882 } else { 974 } else {
@@ -885,20 +977,18 @@ func (dividendsReturnedOrderService *DividendsReturnedOrderService) ImportDivide @@ -885,20 +977,18 @@ func (dividendsReturnedOrderService *DividendsReturnedOrderService) ImportDivide
885 for _, errorData := range errorDataList { 977 for _, errorData := range errorDataList {
886 if len(errorData.GoodLine) == 0 { // 订单错误 978 if len(errorData.GoodLine) == 0 { // 订单错误
887 for _, line := range errorData.LineNumbers { 979 for _, line := range errorData.LineNumbers {
888 - var tmpRow []string  
889 - tmpRow = append(tmpRow, errorData.Error.Error()) // 错误信息  
890 - s := strconv.Itoa(line + 1)  
891 - tmpRow = append(tmpRow, s) // 行号  
892 - //tmpRow = append(tmpRow, importDividendsOrderCommand.DividendsOrderData[line]...) // 错误行数据 980 + tmpRow := &domain.FailReturnedInfo{
  981 + FailReason: errorData.Error.Error(),
  982 + ImportDividendsReturnedOrderData: &importDividendsReturnedOrderCommand.DividendsReturnedOrderData[line],
  983 + }
893 failureDataList = append(failureDataList, tmpRow) 984 failureDataList = append(failureDataList, tmpRow)
894 } 985 }
895 } else if len(errorData.GoodLine) > 0 { // 订单产品错误 986 } else if len(errorData.GoodLine) > 0 { // 订单产品错误
896 for line := range errorData.GoodLine { 987 for line := range errorData.GoodLine {
897 - var tmpRow []string  
898 - tmpRow = append(tmpRow, errorData.Error.Error()) // 错误信息  
899 - s := strconv.Itoa(line + 1)  
900 - tmpRow = append(tmpRow, s) // 行号  
901 - //tmpRow = append(tmpRow, importDividendsOrderCommand.DividendsOrderData[line]...) // 错误行数据 988 + tmpRow := &domain.FailReturnedInfo{
  989 + FailReason: errorData.GoodLine[line],
  990 + ImportDividendsReturnedOrderData: &importDividendsReturnedOrderCommand.DividendsReturnedOrderData[line],
  991 + }
902 failureDataList = append(failureDataList, tmpRow) 992 failureDataList = append(failureDataList, tmpRow)
903 } 993 }
904 } 994 }
@@ -81,6 +81,23 @@ func (dao *DividendsReturnedOrderDao) CalculateDividendsReturnedOrderAmount(quer @@ -81,6 +81,23 @@ func (dao *DividendsReturnedOrderDao) CalculateDividendsReturnedOrderAmount(quer
81 return dividendsOrderModel.DividendsReturnedOrderRefund, nil 81 return dividendsOrderModel.DividendsReturnedOrderRefund, nil
82 } 82 }
83 83
  84 +// CountDividendsReturnedOrder 统计当前退货单总数
  85 +func (dao *DividendsReturnedOrderDao) CountDividendsReturnedOrder(queryOptions map[string]interface{}) (int, error) {
  86 + tx := dao.transactionContext.PgTx
  87 + var dividendsReturnedOrderModels []*models.DividendsReturnedOrder
  88 + query := tx.Model(&dividendsReturnedOrderModels)
  89 + currentTime := time.Now()
  90 + todayZeroTime := utils.GetZeroTime(currentTime)
  91 + nextDayZeroTime := utils.GetNextDayZeroTime(currentTime)
  92 + query.Where("dividends_order.created_at >= ?", todayZeroTime)
  93 + query.Where("dividends_order.created_at < ?", nextDayZeroTime)
  94 + if count, err := query.AllWithDeleted().SelectAndCount(); err != nil {
  95 + return 0, err
  96 + } else {
  97 + return count, nil
  98 + }
  99 +}
  100 +
84 func NewDividendsReturnedOrderDao(transactionContext *pgTransaction.TransactionContext) (*DividendsReturnedOrderDao, error) { 101 func NewDividendsReturnedOrderDao(transactionContext *pgTransaction.TransactionContext) (*DividendsReturnedOrderDao, error) {
85 if transactionContext == nil { 102 if transactionContext == nil {
86 return nil, fmt.Errorf("transactionContext参数不能未") 103 return nil, fmt.Errorf("transactionContext参数不能未")