作者 陈志颖

Merge branch 'dev-chenzhiying' into dev

@@ -560,26 +560,6 @@ func (dividendsEstimateService *DividendsEstimateService) ConfirmDividendsIncent @@ -560,26 +560,6 @@ func (dividendsEstimateService *DividendsEstimateService) ConfirmDividendsIncent
560 operator = data 560 operator = data
561 } 561 }
562 562
563 - //// 分红订单仓储初始化  
564 - //var dividendsOrderRepository domain.DividendsOrderRepository  
565 - //if value, err := factory.CreateDividendsOrderRepository(map[string]interface{}{  
566 - // "transactionContext": transactionContext,  
567 - //}); err != nil {  
568 - // return nil, application.ThrowError(application.INTERNAL_SERVER_ERROR, err.Error())  
569 - //} else {  
570 - // dividendsOrderRepository = value  
571 - //}  
572 -  
573 - // 分红退货单仓储初始化  
574 - //var dividendsReturnedOrderRepository domain.DividendsReturnedOrderRepository  
575 - //if value, err := factory.CreateDividendsReturnedOrderRepository(map[string]interface{}{  
576 - // "transactionContext": transactionContext,  
577 - //}); err != nil {  
578 - // return nil, application.ThrowError(application.INTERNAL_SERVER_ERROR, err.Error())  
579 - //} else {  
580 - // dividendsReturnedOrderRepository = value  
581 - //}  
582 -  
583 // 订单产品仓储初始化 563 // 订单产品仓储初始化
584 var orderGoodRepository domain.OrderGoodRepository 564 var orderGoodRepository domain.OrderGoodRepository
585 if value, err := factory.CreateOrderGoodRepository(map[string]interface{}{ 565 if value, err := factory.CreateOrderGoodRepository(map[string]interface{}{
@@ -703,7 +683,7 @@ func (dividendsEstimateService *DividendsEstimateService) ConfirmDividendsIncent @@ -703,7 +683,7 @@ func (dividendsEstimateService *DividendsEstimateService) ConfirmDividendsIncent
703 683
704 dividendsEstimates = append(dividendsEstimates, dividendsEstimate) 684 dividendsEstimates = append(dividendsEstimates, dividendsEstimate)
705 } else { 685 } else {
706 - // 记录失败的订单号和原因s 686 + // 记录失败的订单号和原因
707 estimateFailedDividendsOrders[dividendsEstimateDetail.OrderOrReturnedOrderNumber] = dividendsEstimateDetail.Reason 687 estimateFailedDividendsOrders[dividendsEstimateDetail.OrderOrReturnedOrderNumber] = dividendsEstimateDetail.Reason
708 } 688 }
709 } 689 }
@@ -767,22 +747,157 @@ func (dividendsEstimateService *DividendsEstimateService) ConfirmDividendsIncent @@ -767,22 +747,157 @@ func (dividendsEstimateService *DividendsEstimateService) ConfirmDividendsIncent
767 if dividendsEstimatesSaved, err := dividendsEstimateRepository.SaveMany(dividendsEstimates); err != nil { 747 if dividendsEstimatesSaved, err := dividendsEstimateRepository.SaveMany(dividendsEstimates); err != nil {
768 return nil, application.ThrowError(application.INTERNAL_SERVER_ERROR, err.Error()) 748 return nil, application.ThrowError(application.INTERNAL_SERVER_ERROR, err.Error())
769 } else { 749 } else {
  750 + // 获取产品ID
  751 + orderGoodSavedIds := make([]int64, 0)
  752 + for _, dividendsEstimateSaved := range dividendsEstimatesSaved {
  753 + orderGoodSavedIds = append(orderGoodSavedIds, dividendsEstimateSaved.OrderGoodId)
  754 + }
  755 +
  756 + // 获取订单产品
  757 + if countOrderGood, orderGoodsFound, err := orderGoodRepository.Find(map[string]interface{}{
  758 + "orderGoodIds": orderGoodSavedIds,
  759 + }); err != nil {
  760 + return nil, application.ThrowError(application.INTERNAL_SERVER_ERROR, err.Error())
  761 + } else {
  762 + if countOrderGood > 0 {
  763 + for i, _ := range orderGoodsFound {
  764 + orderGoodsFound[i].OrderGoodDividendsStatus = int32(2) // 已分红
  765 + }
  766 + if _, err6 := orderGoodRepository.UpdateMany(orderGoodsFound); err6 != nil {
  767 + return nil, application.ThrowError(application.INTERNAL_SERVER_ERROR, err6.Error())
  768 + }
  769 + }
  770 + }
  771 +
770 if err := transactionContext.CommitTransaction(); err != nil { 772 if err := transactionContext.CommitTransaction(); err != nil {
771 return nil, application.ThrowError(application.TRANSACTION_ERROR, err.Error()) 773 return nil, application.ThrowError(application.TRANSACTION_ERROR, err.Error())
772 } 774 }
  775 +
773 // 分析成功和失败原因 776 // 分析成功和失败原因
774 successfullyCount := len(dividendsEstimatesSaved) 777 successfullyCount := len(dividendsEstimatesSaved)
  778 +
775 // 错误原因收集,返回第一个可能的原因 779 // 错误原因收集,返回第一个可能的原因
776 failedReason := make([]string, 0) 780 failedReason := make([]string, 0)
777 for _, v := range estimateFailedDividendsOrders { 781 for _, v := range estimateFailedDividendsOrders {
778 failedReason = append(failedReason, v) 782 failedReason = append(failedReason, v)
779 } 783 }
  784 +
780 var failedReasonStr string 785 var failedReasonStr string
781 if len(failedReason) > 0 { 786 if len(failedReason) > 0 {
782 failedReasonStr = failedReason[0] 787 failedReasonStr = failedReason[0]
783 } else { 788 } else {
784 failedReasonStr = "无" 789 failedReasonStr = "无"
785 } 790 }
  791 +
  792 + // 开启薪事务
  793 + newTransactionContext, err := factory.CreateTransactionContext(nil)
  794 + if err != nil {
  795 + return nil, application.ThrowError(application.TRANSACTION_ERROR, err.Error())
  796 + }
  797 + if err := newTransactionContext.StartTransaction(); err != nil {
  798 + return nil, application.ThrowError(application.TRANSACTION_ERROR, err.Error())
  799 + }
  800 + defer func() {
  801 + _ = newTransactionContext.RollbackTransaction()
  802 + }()
  803 +
  804 + // 分离分红订单和退货单
  805 + dividendsOrderNumbers := make([]string, 0)
  806 + dividendsReturnedOrderNumbers := make([]string, 0)
  807 + for k, _ := range estimateSuccessfullyDividendsOrders {
  808 + if k[:2] == "RE" { // 分红退货单
  809 + dividendsReturnedOrderNumbers = append(dividendsReturnedOrderNumbers, k)
  810 + } else if k[:2] == "SL" { // 分红订单
  811 + dividendsOrderNumbers = append(dividendsOrderNumbers, k)
  812 + }
  813 + }
  814 +
  815 + // 分红订单仓储初始化
  816 + var dividendsOrderRepository domain.DividendsOrderRepository
  817 + if value, err := factory.CreateDividendsOrderRepository(map[string]interface{}{
  818 + "transactionContext": newTransactionContext,
  819 + }); err != nil {
  820 + return nil, application.ThrowError(application.INTERNAL_SERVER_ERROR, err.Error())
  821 + } else {
  822 + dividendsOrderRepository = value
  823 + }
  824 +
  825 + // 查找分红订单
  826 + if countDividendsOrdersFound, dividendsOrdersFound, err := dividendsOrderRepository.Find(map[string]interface{}{
  827 + "dividendsOrderNumbers": dividendsOrderNumbers,
  828 + "companyId": confirmDividendsIncentivesEstimateCommand.CompanyId,
  829 + "orgId": confirmDividendsIncentivesEstimateCommand.OrgId,
  830 + }); err != nil {
  831 + return nil, application.ThrowError(application.INTERNAL_SERVER_ERROR, err.Error())
  832 + } else {
  833 + if countDividendsOrdersFound > 0 {
  834 + for i, dividendsOrderFound := range dividendsOrdersFound {
  835 + var dividendsGoodCount int
  836 + for _, orderGood := range dividendsOrderFound.Goods {
  837 + if orderGood.OrderGoodDividendsStatus == 2 {
  838 + dividendsGoodCount = dividendsGoodCount + 1
  839 + }
  840 + }
  841 + if dividendsGoodCount == len(dividendsOrderFound.Goods) { // 2已分红
  842 + dividendsOrdersFound[i].DividendStatus = 2
  843 + } else if dividendsGoodCount > 0 && dividendsGoodCount < len(dividendsOrderFound.Goods) { // 3部分分红
  844 + dividendsOrdersFound[i].DividendStatus = 3
  845 + } else if dividendsGoodCount == 0 { // 1未分红
  846 + dividendsOrdersFound[i].DividendStatus = 1
  847 + }
  848 + }
  849 + // 保存订单状态
  850 + if _, err6 := dividendsOrderRepository.UpdateMany(dividendsOrdersFound); err6 != nil {
  851 + return nil, application.ThrowError(application.INTERNAL_SERVER_ERROR, err.Error())
  852 + }
  853 + }
  854 + }
  855 +
  856 + // 分红退货单仓储初始化
  857 + var dividendsReturnedOrderRepository domain.DividendsReturnedOrderRepository
  858 + if value, err := factory.CreateDividendsReturnedOrderRepository(map[string]interface{}{
  859 + "transactionContext": newTransactionContext,
  860 + }); err != nil {
  861 + return nil, application.ThrowError(application.INTERNAL_SERVER_ERROR, err.Error())
  862 + } else {
  863 + dividendsReturnedOrderRepository = value
  864 + }
  865 +
  866 + // 查找分红退货单
  867 + if countDividendsReturnedOrdersFound, dividendsReturnedOrdersFound, err := dividendsReturnedOrderRepository.Find(map[string]interface{}{
  868 + "dividendsReturnedOrderNumbers": dividendsReturnedOrderNumbers,
  869 + "companyId": confirmDividendsIncentivesEstimateCommand.CompanyId,
  870 + "orgId": confirmDividendsIncentivesEstimateCommand.OrgId,
  871 + }); err != nil {
  872 + return nil, application.ThrowError(application.INTERNAL_SERVER_ERROR, err.Error())
  873 + } else {
  874 + if countDividendsReturnedOrdersFound > 0 {
  875 + for i, dividendsReturnedOrderFound := range dividendsReturnedOrdersFound {
  876 + var dividendsGoodCount int
  877 + for _, orderGood := range dividendsReturnedOrderFound.Goods {
  878 + if orderGood.OrderGoodDividendsStatus == 2 {
  879 + dividendsGoodCount = dividendsGoodCount + 1
  880 + }
  881 + }
  882 + if dividendsGoodCount == len(dividendsReturnedOrderFound.Goods) { // 2已分红
  883 + dividendsReturnedOrdersFound[i].DividendStatus = 2
  884 + } else if dividendsGoodCount > 0 && dividendsGoodCount < len(dividendsReturnedOrderFound.Goods) { // 3部分分红
  885 + dividendsReturnedOrdersFound[i].DividendStatus = 3
  886 + } else if dividendsGoodCount == 0 { // 1未分红
  887 + dividendsReturnedOrdersFound[i].DividendStatus = 1
  888 + }
  889 + }
  890 + // 保存退货单状态
  891 + if _, err6 := dividendsReturnedOrderRepository.UpdateMany(dividendsReturnedOrdersFound); err6 != nil {
  892 + return nil, application.ThrowError(application.INTERNAL_SERVER_ERROR, err.Error())
  893 + }
  894 + }
  895 + }
  896 +
  897 + if err := newTransactionContext.CommitTransaction(); err != nil {
  898 + return nil, application.ThrowError(application.TRANSACTION_ERROR, err.Error())
  899 + }
  900 +
786 return map[string]interface{}{ 901 return map[string]interface{}{
787 "report": fmt.Sprintf("已完成%d笔单订单分红预算,生成%d笔单分红预算,%d笔订单分红预算失败,失败原因:%s", len(estimateSuccessfullyDividendsOrders), successfullyCount, len(estimateFailedDividendsOrders), failedReasonStr), 902 "report": fmt.Sprintf("已完成%d笔单订单分红预算,生成%d笔单分红预算,%d笔订单分红预算失败,失败原因:%s", len(estimateSuccessfullyDividendsOrders), successfullyCount, len(estimateFailedDividendsOrders), failedReasonStr),
788 }, nil 903 }, nil
@@ -43,6 +43,7 @@ type OrderGood struct { @@ -43,6 +43,7 @@ type OrderGood struct {
43 43
44 type OrderGoodRepository interface { 44 type OrderGoodRepository interface {
45 Save(orderGood *OrderGood) (*OrderGood, error) 45 Save(orderGood *OrderGood) (*OrderGood, error)
  46 + UpdateMany(orderGoods []*OrderGood) ([]*OrderGood, error)
46 Remove(orderGood *OrderGood) (*OrderGood, error) 47 Remove(orderGood *OrderGood) (*OrderGood, error)
47 FindOne(queryOptions map[string]interface{}) (*OrderGood, error) 48 FindOne(queryOptions map[string]interface{}) (*OrderGood, error)
48 Find(queryOptions map[string]interface{}) (int64, []*OrderGood, error) 49 Find(queryOptions map[string]interface{}) (int64, []*OrderGood, error)
@@ -526,6 +526,9 @@ func (repository *DividendsReturnedOrderRepository) Find(queryOptions map[string @@ -526,6 +526,9 @@ func (repository *DividendsReturnedOrderRepository) Find(queryOptions map[string
526 if dividendsReturnedOrderNumber, ok := queryOptions["dividendsReturnedOrderNumber"]; ok && dividendsReturnedOrderNumber != "" { 526 if dividendsReturnedOrderNumber, ok := queryOptions["dividendsReturnedOrderNumber"]; ok && dividendsReturnedOrderNumber != "" {
527 query.Where("dividends_returned_order_number ilike ?", fmt.Sprintf("%%%s%%", dividendsReturnedOrderNumber)) 527 query.Where("dividends_returned_order_number ilike ?", fmt.Sprintf("%%%s%%", dividendsReturnedOrderNumber))
528 } 528 }
  529 + if dividendsReturnedOrderNumbers, ok := queryOptions["dividendsReturnedOrderNumbers"]; ok && len(dividendsReturnedOrderNumbers.([]string)) > 0 {
  530 + query.Where("dividends_returned_order_number IN (?)", pg.In(dividendsReturnedOrderNumbers.([]string)))
  531 + }
529 if dividendsReturnedCustomerName, ok := queryOptions["dividendsReturnedCustomerName"]; ok && dividendsReturnedCustomerName != "" { 532 if dividendsReturnedCustomerName, ok := queryOptions["dividendsReturnedCustomerName"]; ok && dividendsReturnedCustomerName != "" {
530 query.Where("dividends_returned_customer_name like ?", fmt.Sprintf("%%%s%%", dividendsReturnedCustomerName)) 533 query.Where("dividends_returned_customer_name like ?", fmt.Sprintf("%%%s%%", dividendsReturnedCustomerName))
531 } 534 }
@@ -5,6 +5,7 @@ import ( @@ -5,6 +5,7 @@ import (
5 "github.com/go-pg/pg/v10" 5 "github.com/go-pg/pg/v10"
6 "github.com/go-pg/pg/v10/orm" 6 "github.com/go-pg/pg/v10/orm"
7 "gitlab.fjmaimaimai.com/allied-creation/allied-creation-cooperation/pkg/infrastructure/utils" 7 "gitlab.fjmaimaimai.com/allied-creation/allied-creation-cooperation/pkg/infrastructure/utils"
  8 + "time"
8 9
9 "github.com/linmadan/egglib-go/persistent/pg/sqlbuilder" 10 "github.com/linmadan/egglib-go/persistent/pg/sqlbuilder"
10 pgTransaction "github.com/linmadan/egglib-go/transaction/pg" 11 pgTransaction "github.com/linmadan/egglib-go/transaction/pg"
@@ -130,6 +131,34 @@ func (repository *OrderGoodRepository) Save(orderGood *domain.OrderGood) (*domai @@ -130,6 +131,34 @@ func (repository *OrderGoodRepository) Save(orderGood *domain.OrderGood) (*domai
130 return orderGood, nil 131 return orderGood, nil
131 } 132 }
132 133
  134 +func (repository *OrderGoodRepository) UpdateMany(orderGoods []*domain.OrderGood) ([]*domain.OrderGood, error) {
  135 + tx := repository.transactionContext.PgTx
  136 + var orderGoodModels []*models.OrderGood
  137 + for _, orderGood := range orderGoods {
  138 + orderGoodModels = append(orderGoodModels, &models.OrderGood{
  139 + OrderGoodId: orderGood.OrderGoodId,
  140 + OrderGoodAmount: orderGood.OrderGoodAmount,
  141 + OrderGoodName: orderGood.OrderGoodName,
  142 + OrderGoodPrice: orderGood.OrderGoodPrice,
  143 + OrderGoodQuantity: orderGood.OrderGoodQuantity,
  144 + DividendsOrderNumber: orderGood.DividendsOrderNumber,
  145 + DividendsReturnedOrderNumber: orderGood.DividendsReturnedOrderNumber,
  146 + CooperationContractNumber: orderGood.CooperationContractNumber,
  147 + OrgId: orderGood.OrgId,
  148 + CompanyId: orderGood.CompanyId,
  149 + OrderGoodExpense: orderGood.OrderGoodExpense,
  150 + OrderGoodDividendsStatus: orderGood.OrderGoodDividendsStatus,
  151 + CreatedAt: orderGood.CreatedAt,
  152 + DeletedAt: orderGood.DeletedAt,
  153 + UpdatedAt: time.Now(),
  154 + })
  155 + }
  156 + if _, err := tx.Model(&orderGoodModels).WherePK().Update(); err != nil {
  157 + return nil, err
  158 + }
  159 + return orderGoods, nil
  160 +}
  161 +
133 func (repository *OrderGoodRepository) Remove(orderGood *domain.OrderGood) (*domain.OrderGood, error) { 162 func (repository *OrderGoodRepository) Remove(orderGood *domain.OrderGood) (*domain.OrderGood, error) {
134 tx := repository.transactionContext.PgTx 163 tx := repository.transactionContext.PgTx
135 orderGoodModel := new(models.OrderGood) 164 orderGoodModel := new(models.OrderGood)