pg_cancel_dividends_estimate_service.go 16.4 KB
package domain_service

import (
	"fmt"
	"github.com/linmadan/egglib-go/core/application"
	coreDomain "github.com/linmadan/egglib-go/core/domain"
	pgTransaction "github.com/linmadan/egglib-go/transaction/pg"
	"gitlab.fjmaimaimai.com/allied-creation/allied-creation-cooperation/pkg/domain"
	"gitlab.fjmaimaimai.com/allied-creation/allied-creation-cooperation/pkg/infrastructure/repository"
	"gitlab.fjmaimaimai.com/allied-creation/allied-creation-cooperation/pkg/infrastructure/utils"
)

type CancelDividendsEstimateService struct {
	coreDomain.BaseEventPublisher
	transactionContext *pgTransaction.TransactionContext
}

func (domainService *CancelDividendsEstimateService) CancelEstimate(dividendsEstimateIds []int64, operator *domain.User) ([]*domain.DividendsEstimate, error) {
	var dividendsEstimateRepository domain.DividendsEstimateRepository           // 分红预算单仓储
	var dividendsOrderRepository domain.DividendsOrderRepository                 // 分红订单仓储
	var dividendsReturnedOrderRepository domain.DividendsReturnedOrderRepository // 分红退货单仓储
	var orderGoodRepository domain.OrderGoodRepository                           // 订单产品仓储
	//var cooperationContactRepository domain.CooperationContractRepository        // 合约仓储

	// 分红预算单仓储初始化
	if repo, err := repository.NewDividendsEstimateRepository(domainService.transactionContext); err != nil {
		return nil, err
	} else {
		dividendsEstimateRepository = repo
	}

	// 分红订单仓储初始化
	if repo, err := repository.NewDividendsOrderRepository(domainService.transactionContext); err != nil {
		return nil, err
	} else {
		dividendsOrderRepository = repo
	}

	// 分红退货单仓储初始化
	if repo, err := repository.NewDividendsReturnedOrderRepository(domainService.transactionContext); err != nil {
		return nil, err
	} else {
		dividendsReturnedOrderRepository = repo
	}

	// 订单产品仓储初始化
	if repo, err := repository.NewOrderGoodRepository(domainService.transactionContext); err != nil {
		return nil, err
	} else {
		orderGoodRepository = repo
	}

	// 共创合约仓储初始化
	//if repo, err := repository.NewCooperationContractRepository(domainService.transactionContext); err != nil {
	//	return nil, err
	//} else {
	//	cooperationContactRepository = repo
	//}

	// 获取分红预算单
	if count, dividendsEstimates, err := dividendsEstimateRepository.Find(map[string]interface{}{
		"dividendsEstimateIds": dividendsEstimateIds,
	}); err != nil {
		return nil, err
	} else {
		if count > 0 {
			var dividendsEstimatesCanceled []*domain.DividendsEstimate
			var orderOrReturnOrderNums []string
			var orderGoodIds []int64
			for _, dividendsEstimate := range dividendsEstimates {
				if dividendsEstimate.DividendsType == 1 || dividendsEstimate.DividendsType == 2 { //  1.订单分红 2.退货冲销
					// 自定义查询

					//queryOptions := map[string]interface{}{
					//	"companyId":                 dividendsEstimate.Company.CompanyId,
					//	"orgId":                     dividendsEstimate.Org.OrgId,
					//	"orderOrReturnedOrderNum":   dividendsEstimate.OrderOrReturnedOrderNum,
					//	"dividendsParticipateTypes": []int32{1, 2, 3},
					//	"offsetLimit":               false,
					//	"dividendsStage":            dividendsEstimate.DividendsStage,
					//	"dividendsTypes":            []int32{1, 2},
					//}
					//参与分红类型,1承接人,2推荐人,3关联业务员
					//if dividendsEstimate.DividendsParticipateType == 1 { // 当前取消的是承接人的分红预算单
					//	queryOptions = map[string]interface{}{
					//		"companyId":                 dividendsEstimate.Company.CompanyId,
					//		"orgId":                     dividendsEstimate.Org.OrgId,
					//		"orderOrReturnedOrderNum":   dividendsEstimate.OrderOrReturnedOrderNum,
					//		"dividendsParticipateTypes": []int32{1, 2, 3},
					//		"offsetLimit":               false,
					//		"dividendsStage":            dividendsEstimate.DividendsStage,
					//		"dividendsTypes":            []int32{1, 2},
					//	}
					//} else if dividendsEstimate.DividendsParticipateType == 2 { // 当前取消的是推荐人的分红预算单
					//	queryOptions = map[string]interface{}{
					//		"companyId":                 dividendsEstimate.Company.CompanyId,
					//		"orgId":                     dividendsEstimate.Org.OrgId,
					//		"orderOrReturnedOrderNum":   dividendsEstimate.OrderOrReturnedOrderNum,
					//		"dividendsParticipateTypes": []int32{1, 3},
					//		"offsetLimit":               false,
					//		"dividendsStage":            dividendsEstimate.DividendsStage,
					//		"dividendsTypes":            []int32{1, 2},
					//	}
					//} else if dividendsEstimate.DividendsParticipateType == 3 { // 当前取消的是关联业务员的分红预算单
					//	queryOptions = map[string]interface{}{
					//		"companyId":                 dividendsEstimate.Company.CompanyId,
					//		"orgId":                     dividendsEstimate.Org.OrgId,
					//		"orderOrReturnedOrderNum":   dividendsEstimate.OrderOrReturnedOrderNum,
					//		"dividendsParticipateTypes": []int32{1, 2},
					//		"offsetLimit":               false,
					//		"dividendsStage":            dividendsEstimate.DividendsStage,
					//		"dividendsTypes":            []int32{1, 2},
					//	}
					//}

					// 根据当前分红预算单的分红订单号或退货单号获取相关的分红预算单
					if countRelative, dividendsEstimatesRelative, err2 := dividendsEstimateRepository.Find(map[string]interface{}{
						"companyId":                 dividendsEstimate.Company.CompanyId,
						"orgId":                     dividendsEstimate.Org.OrgId,
						"orderOrReturnedOrderNum":   dividendsEstimate.OrderOrReturnedOrderNum,
						"dividendsParticipateTypes": []int32{1, 2, 3},
						"cooperationContractNumber": dividendsEstimate.CooperationContractNumber,
						"offsetLimit":               false,
						"dividendsStage":            dividendsEstimate.DividendsStage,
						"dividendsTypes":            []int32{1, 2},
						"orderGoodId":               dividendsEstimate.OrderGoodId,
					}); err2 != nil {
						return nil, err2
					} else {
						if countRelative > 0 {
							for i, _ := range dividendsEstimatesRelative {
								dividendsEstimatesRelative[i].IsCanceled = true
								dividendsEstimatesRelative[i].Operator = operator
								//	获取相关订单/退货单号
								orderOrReturnOrderNums = append(orderOrReturnOrderNums, dividendsEstimatesRelative[i].OrderOrReturnedOrderNum)
								orderGoodIds = append(orderGoodIds, dividendsEstimatesRelative[i].OrderGoodId)
							}
							dividendsEstimatesRelativeCanceled, err3 := dividendsEstimateRepository.UpdateMany(dividendsEstimatesRelative)
							if err3 != nil {
								return nil, err3
							}
							dividendsEstimatesCanceled = append(dividendsEstimatesCanceled, dividendsEstimatesRelativeCanceled...)
						}
					}
				} else if dividendsEstimate.DividendsType == 3 { // 3.金额激励

					// 查询合约
					//cooperationContract, err3 := cooperationContactRepository.FindOne(map[string]interface{}{
					//	"companyId":                 dividendsEstimate.Company.CompanyId,
					//	"orgId":                     dividendsEstimate.Org.OrgId,
					//	"cooperationContractNumber": dividendsEstimate.CooperationContractNumber,
					//})
					//if err3 != nil {
					//	return nil, err3
					//}

					// 自定义查询
					//queryOptions := map[string]interface{}{
					//	"companyId":                 dividendsEstimate.Company.CompanyId,
					//	"orgId":                     dividendsEstimate.Org.OrgId,
					//	"cooperationContractNumber": dividendsEstimate.CooperationContractNumber,
					//	"dividendsParticipateTypes": []int32{1, 2, 3},
					//	"offsetLimit":               false,
					//	"dividendsStage":            dividendsEstimate.DividendsStage,
					//	"dividendsType":             int32(3),
					//}
					//参与分红类型,1承接人,2推荐人,3关联业务员
					//if dividendsEstimate.DividendsParticipateType == 1 { // 当前取消的是承接人的分红预算单
					//	queryOptions = map[string]interface{}{
					//		"companyId":                 dividendsEstimate.Company.CompanyId,
					//		"orgId":                     dividendsEstimate.Org.OrgId,
					//		"cooperationContractNumber": dividendsEstimate.CooperationContractNumber,
					//		"dividendsParticipateTypes": []int32{2, 3},
					//		"offsetLimit":               false,
					//		"dividendsStage":            dividendsEstimate.DividendsStage,
					//		"dividendsType":             int32(3),
					//	}
					//} else if dividendsEstimate.DividendsParticipateType == 2 { // 当前取消的是推荐人的分红预算单
					//	queryOptions = map[string]interface{}{
					//		"companyId":                 dividendsEstimate.Company.CompanyId,
					//		"orgId":                     dividendsEstimate.Org.OrgId,
					//		"cooperationContractNumber": dividendsEstimate.CooperationContractNumber,
					//		"dividendsParticipateTypes": []int32{1, 3},
					//		"offsetLimit":               false,
					//		"dividendsStage":            dividendsEstimate.DividendsStage,
					//		"dividendsType":             int32(3),
					//	}
					//} else if dividendsEstimate.DividendsParticipateType == 3 { // 当前取消的是关联业务员的分红预算单
					//	queryOptions = map[string]interface{}{
					//		"companyId":                 dividendsEstimate.Company.CompanyId,
					//		"orgId":                     dividendsEstimate.Org.OrgId,
					//		"cooperationContractNumber": dividendsEstimate.CooperationContractNumber,
					//		"dividendsParticipateTypes": []int32{1, 2},
					//		"offsetLimit":               false,
					//		"dividendsStage":            dividendsEstimate.DividendsStage,
					//		"dividendsType":             int32(3),
					//	}
					//}

					// 根据当前分红预算单的分红订单号或退货单号获取相关的分红预算单
					if countRelative, dividendsEstimatesRelative, err2 := dividendsEstimateRepository.Find(map[string]interface{}{
						"companyId":                       dividendsEstimate.Company.CompanyId,
						"orgId":                           dividendsEstimate.Org.OrgId,
						"cooperationContractNumber":       dividendsEstimate.CooperationContractNumber,
						"cooperationContractUndertakerId": dividendsEstimate.CooperationContractUndertakerId,
						"dividendsParticipateTypes":       []int32{1, 2, 3},
						"offsetLimit":                     false,
						"dividendsStage":                  dividendsEstimate.DividendsStage,
						"dividendsType":                   int32(3),
					}); err2 != nil {
						return nil, err2
					} else {
						if countRelative > 0 {
							for i, _ := range dividendsEstimatesRelative {
								dividendsEstimatesRelative[i].IsCanceled = true
								dividendsEstimatesRelative[i].Operator = operator
							}
							dividendsEstimatesRelativeCanceled, err4 := dividendsEstimateRepository.UpdateMany(dividendsEstimatesRelative)
							if err4 != nil {
								return nil, err4
							}
							dividendsEstimatesCanceled = append(dividendsEstimatesCanceled, dividendsEstimatesRelativeCanceled...)
						}
					}
				}
			}

			// 分红订单
			var orderNums []string

			// 分红退货单
			var returnedOrderNums []string

			if len(orderOrReturnOrderNums) > 0 { // 业绩分红
				// 订单/退货单号数组去重
				orderOrReturnOrderNumsWithoutDup := utils.RemoveDuplication(orderOrReturnOrderNums)

				// 分离订单和退货单
				for _, orderNullNum := range orderOrReturnOrderNumsWithoutDup {
					if len(orderNullNum) > 0 {
						if orderNullNum[:2] == "RE" { // 分红退后单
							returnedOrderNums = append(returnedOrderNums, orderNullNum)
						} else if orderNullNum[:2] == "SL" { // 分红订单
							orderNums = append(orderNums, orderNullNum)
						}
					}
				}

				// 获取分红订单并更新分红订单产品的状态
				if len(orderNums) > 0 {
					if countDividendsOrder, orders, err4 := dividendsOrderRepository.Find(map[string]interface{}{
						"companyId":             dividendsEstimates[0].Company.CompanyId,
						"orgId":                 dividendsEstimates[0].Org.OrgId,
						"dividendsOrderNumbers": orderNums,
					}); err4 != nil {
						return nil, err4
					} else {
						if countDividendsOrder > 0 {
							for i, order := range orders {
								if len(order.Goods) == len(orderGoodIds) { // 1.全部产品取消分红预算
									orders[i].DividendStatus = 1
								} else if len(order.Goods) > len(orderGoodIds) { // 3.部分产品被取消分红预算
									orders[i].DividendStatus = 3
								}
								orders[i].DividendStatus = order.DividendStatus
							}

							//	保存订单状态
							if _, err6 := dividendsOrderRepository.UpdateMany(orders); err6 != nil {
								return nil, err6
							}

							// 获取订单编号
							ordersNumbers := make([]string, 0)
							for _, order := range orders {
								ordersNumbers = append(ordersNumbers, order.DividendsOrderNumber)
							}

							// 获取分红订单产品并更新产品状态
							if len(ordersNumbers) > 0 {
								if countOrderGood, orderGoodsFound, err := orderGoodRepository.Find(map[string]interface{}{
									//"orderNumbers": ordersNumbers,
									"orderGoodIds": orderGoodIds,
									"companyId":    dividendsEstimates[0].Company.CompanyId,
									"orgId":        dividendsEstimates[0].Org.OrgId,
								}); err != nil {
									return nil, application.ThrowError(application.INTERNAL_SERVER_ERROR, err.Error())
								} else {
									if countOrderGood > 0 {
										for i, _ := range orderGoodsFound {
											orderGoodsFound[i].OrderGoodDividendsStatus = int32(1) // 待分红
										}
										if _, err6 := orderGoodRepository.UpdateMany(orderGoodsFound); err6 != nil {
											return nil, application.ThrowError(application.INTERNAL_SERVER_ERROR, err6.Error())
										}
									}
								}
							}
						}
					}
				}

				// 获取分红退货单并更新订单产品的状态
				if len(returnedOrderNums) > 0 {
					if countDividendsReturnedOrder, returnedOrders, err5 := dividendsReturnedOrderRepository.Find(map[string]interface{}{
						"companyId":                     dividendsEstimates[0].Company.CompanyId,
						"orgId":                         dividendsEstimates[0].Org.OrgId,
						"dividendsReturnedOrderNumbers": returnedOrderNums,
					}); err5 != nil {
						return nil, err5
					} else {
						if countDividendsReturnedOrder > 0 {
							for i, returnedOrder := range returnedOrders {
								if len(returnedOrder.Goods) == len(orderGoodIds) { // 1.全部产品取消分红预算
									returnedOrders[i].DividendStatus = 1
								} else if len(returnedOrder.Goods) > len(orderGoodIds) { // 3.部分产品被取消分红预算
									returnedOrders[i].DividendStatus = 3
								}
								returnedOrders[i].DividendStatus = returnedOrder.DividendStatus
							}

							//	保存退货单状态
							if _, err7 := dividendsReturnedOrderRepository.UpdateMany(returnedOrders); err7 != nil {
								return nil, err7
							}

							// 获取订单编号
							returnedOrdersNumbers := make([]string, 0)
							for _, returnedOrder := range returnedOrders {
								returnedOrdersNumbers = append(returnedOrdersNumbers, returnedOrder.DividendsReturnedOrderNumber)
							}

							// 获取退货单产品并更新产品状态
							if len(returnedOrdersNumbers) > 0 {
								if countOrderGood, orderGoodsFound, err := orderGoodRepository.Find(map[string]interface{}{
									//"returnedOrderNumbers": returnedOrdersNumbers,
									"orderGoodIds": orderGoodIds,
									"companyId":    dividendsEstimates[0].Company.CompanyId,
									"orgId":        dividendsEstimates[0].Org.OrgId,
								}); err != nil {
									return nil, application.ThrowError(application.INTERNAL_SERVER_ERROR, err.Error())
								} else {
									if countOrderGood > 0 {
										for i, _ := range orderGoodsFound {
											orderGoodsFound[i].OrderGoodDividendsStatus = int32(1) // 待分红
										}
										if _, err6 := orderGoodRepository.UpdateMany(orderGoodsFound); err6 != nil {
											return nil, application.ThrowError(application.INTERNAL_SERVER_ERROR, err6.Error())
										}
									}
								}
							}
						}
					}
				}
			}
			return dividendsEstimatesCanceled, nil
		}
	}
	return nil, nil
}

func NewCancelDividendsEstimateService(transactionContext *pgTransaction.TransactionContext) (*CancelDividendsEstimateService, error) {
	if transactionContext == nil {
		return nil, fmt.Errorf("transactionContext参数不能为nil")
	} else {
		return &CancelDividendsEstimateService{
			transactionContext: transactionContext,
		}, nil
	}
}