...
|
...
|
@@ -560,26 +560,6 @@ func (dividendsEstimateService *DividendsEstimateService) ConfirmDividendsIncent |
|
|
operator = data
|
|
|
}
|
|
|
|
|
|
//// 分红订单仓储初始化
|
|
|
//var dividendsOrderRepository domain.DividendsOrderRepository
|
|
|
//if value, err := factory.CreateDividendsOrderRepository(map[string]interface{}{
|
|
|
// "transactionContext": transactionContext,
|
|
|
//}); err != nil {
|
|
|
// return nil, application.ThrowError(application.INTERNAL_SERVER_ERROR, err.Error())
|
|
|
//} else {
|
|
|
// dividendsOrderRepository = value
|
|
|
//}
|
|
|
|
|
|
// 分红退货单仓储初始化
|
|
|
//var dividendsReturnedOrderRepository domain.DividendsReturnedOrderRepository
|
|
|
//if value, err := factory.CreateDividendsReturnedOrderRepository(map[string]interface{}{
|
|
|
// "transactionContext": transactionContext,
|
|
|
//}); err != nil {
|
|
|
// return nil, application.ThrowError(application.INTERNAL_SERVER_ERROR, err.Error())
|
|
|
//} else {
|
|
|
// dividendsReturnedOrderRepository = value
|
|
|
//}
|
|
|
|
|
|
// 订单产品仓储初始化
|
|
|
var orderGoodRepository domain.OrderGoodRepository
|
|
|
if value, err := factory.CreateOrderGoodRepository(map[string]interface{}{
|
...
|
...
|
@@ -703,7 +683,7 @@ func (dividendsEstimateService *DividendsEstimateService) ConfirmDividendsIncent |
|
|
|
|
|
dividendsEstimates = append(dividendsEstimates, dividendsEstimate)
|
|
|
} else {
|
|
|
// 记录失败的订单号和原因s
|
|
|
// 记录失败的订单号和原因
|
|
|
estimateFailedDividendsOrders[dividendsEstimateDetail.OrderOrReturnedOrderNumber] = dividendsEstimateDetail.Reason
|
|
|
}
|
|
|
}
|
...
|
...
|
@@ -767,22 +747,157 @@ func (dividendsEstimateService *DividendsEstimateService) ConfirmDividendsIncent |
|
|
if dividendsEstimatesSaved, err := dividendsEstimateRepository.SaveMany(dividendsEstimates); err != nil {
|
|
|
return nil, application.ThrowError(application.INTERNAL_SERVER_ERROR, err.Error())
|
|
|
} else {
|
|
|
// 获取产品ID
|
|
|
orderGoodSavedIds := make([]int64, 0)
|
|
|
for _, dividendsEstimateSaved := range dividendsEstimatesSaved {
|
|
|
orderGoodSavedIds = append(orderGoodSavedIds, dividendsEstimateSaved.OrderGoodId)
|
|
|
}
|
|
|
|
|
|
// 获取订单产品
|
|
|
if countOrderGood, orderGoodsFound, err := orderGoodRepository.Find(map[string]interface{}{
|
|
|
"orderGoodIds": orderGoodSavedIds,
|
|
|
}); err != nil {
|
|
|
return nil, application.ThrowError(application.INTERNAL_SERVER_ERROR, err.Error())
|
|
|
} else {
|
|
|
if countOrderGood > 0 {
|
|
|
for i, _ := range orderGoodsFound {
|
|
|
orderGoodsFound[i].OrderGoodDividendsStatus = int32(2) // 已分红
|
|
|
}
|
|
|
if _, err6 := orderGoodRepository.UpdateMany(orderGoodsFound); err6 != nil {
|
|
|
return nil, application.ThrowError(application.INTERNAL_SERVER_ERROR, err6.Error())
|
|
|
}
|
|
|
}
|
|
|
}
|
|
|
|
|
|
if err := transactionContext.CommitTransaction(); err != nil {
|
|
|
return nil, application.ThrowError(application.TRANSACTION_ERROR, err.Error())
|
|
|
}
|
|
|
|
|
|
// 分析成功和失败原因
|
|
|
successfullyCount := len(dividendsEstimatesSaved)
|
|
|
|
|
|
// 错误原因收集,返回第一个可能的原因
|
|
|
failedReason := make([]string, 0)
|
|
|
for _, v := range estimateFailedDividendsOrders {
|
|
|
failedReason = append(failedReason, v)
|
|
|
}
|
|
|
|
|
|
var failedReasonStr string
|
|
|
if len(failedReason) > 0 {
|
|
|
failedReasonStr = failedReason[0]
|
|
|
} else {
|
|
|
failedReasonStr = "无"
|
|
|
}
|
|
|
|
|
|
// 开启薪事务
|
|
|
newTransactionContext, err := factory.CreateTransactionContext(nil)
|
|
|
if err != nil {
|
|
|
return nil, application.ThrowError(application.TRANSACTION_ERROR, err.Error())
|
|
|
}
|
|
|
if err := newTransactionContext.StartTransaction(); err != nil {
|
|
|
return nil, application.ThrowError(application.TRANSACTION_ERROR, err.Error())
|
|
|
}
|
|
|
defer func() {
|
|
|
_ = newTransactionContext.RollbackTransaction()
|
|
|
}()
|
|
|
|
|
|
// 分离分红订单和退货单
|
|
|
dividendsOrderNumbers := make([]string, 0)
|
|
|
dividendsReturnedOrderNumbers := make([]string, 0)
|
|
|
for k, _ := range estimateSuccessfullyDividendsOrders {
|
|
|
if k[:2] == "RE" { // 分红退货单
|
|
|
dividendsReturnedOrderNumbers = append(dividendsReturnedOrderNumbers, k)
|
|
|
} else if k[:2] == "SL" { // 分红订单
|
|
|
dividendsOrderNumbers = append(dividendsOrderNumbers, k)
|
|
|
}
|
|
|
}
|
|
|
|
|
|
// 分红订单仓储初始化
|
|
|
var dividendsOrderRepository domain.DividendsOrderRepository
|
|
|
if value, err := factory.CreateDividendsOrderRepository(map[string]interface{}{
|
|
|
"transactionContext": newTransactionContext,
|
|
|
}); err != nil {
|
|
|
return nil, application.ThrowError(application.INTERNAL_SERVER_ERROR, err.Error())
|
|
|
} else {
|
|
|
dividendsOrderRepository = value
|
|
|
}
|
|
|
|
|
|
// 查找分红订单
|
|
|
if countDividendsOrdersFound, dividendsOrdersFound, err := dividendsOrderRepository.Find(map[string]interface{}{
|
|
|
"dividendsOrderNumbers": dividendsOrderNumbers,
|
|
|
"companyId": confirmDividendsIncentivesEstimateCommand.CompanyId,
|
|
|
"orgId": confirmDividendsIncentivesEstimateCommand.OrgId,
|
|
|
}); err != nil {
|
|
|
return nil, application.ThrowError(application.INTERNAL_SERVER_ERROR, err.Error())
|
|
|
} else {
|
|
|
if countDividendsOrdersFound > 0 {
|
|
|
for i, dividendsOrderFound := range dividendsOrdersFound {
|
|
|
var dividendsGoodCount int
|
|
|
for _, orderGood := range dividendsOrderFound.Goods {
|
|
|
if orderGood.OrderGoodDividendsStatus == 2 {
|
|
|
dividendsGoodCount = dividendsGoodCount + 1
|
|
|
}
|
|
|
}
|
|
|
if dividendsGoodCount == len(dividendsOrderFound.Goods) { // 2已分红
|
|
|
dividendsOrdersFound[i].DividendStatus = 2
|
|
|
} else if dividendsGoodCount > 0 && dividendsGoodCount < len(dividendsOrderFound.Goods) { // 3部分分红
|
|
|
dividendsOrdersFound[i].DividendStatus = 3
|
|
|
} else if dividendsGoodCount == 0 { // 1未分红
|
|
|
dividendsOrdersFound[i].DividendStatus = 1
|
|
|
}
|
|
|
}
|
|
|
// 保存订单状态
|
|
|
if _, err6 := dividendsOrderRepository.UpdateMany(dividendsOrdersFound); err6 != nil {
|
|
|
return nil, application.ThrowError(application.INTERNAL_SERVER_ERROR, err.Error())
|
|
|
}
|
|
|
}
|
|
|
}
|
|
|
|
|
|
// 分红退货单仓储初始化
|
|
|
var dividendsReturnedOrderRepository domain.DividendsReturnedOrderRepository
|
|
|
if value, err := factory.CreateDividendsReturnedOrderRepository(map[string]interface{}{
|
|
|
"transactionContext": newTransactionContext,
|
|
|
}); err != nil {
|
|
|
return nil, application.ThrowError(application.INTERNAL_SERVER_ERROR, err.Error())
|
|
|
} else {
|
|
|
dividendsReturnedOrderRepository = value
|
|
|
}
|
|
|
|
|
|
// 查找分红退货单
|
|
|
if countDividendsReturnedOrdersFound, dividendsReturnedOrdersFound, err := dividendsReturnedOrderRepository.Find(map[string]interface{}{
|
|
|
"dividendsReturnedOrderNumbers": dividendsReturnedOrderNumbers,
|
|
|
"companyId": confirmDividendsIncentivesEstimateCommand.CompanyId,
|
|
|
"orgId": confirmDividendsIncentivesEstimateCommand.OrgId,
|
|
|
}); err != nil {
|
|
|
return nil, application.ThrowError(application.INTERNAL_SERVER_ERROR, err.Error())
|
|
|
} else {
|
|
|
if countDividendsReturnedOrdersFound > 0 {
|
|
|
for i, dividendsReturnedOrderFound := range dividendsReturnedOrdersFound {
|
|
|
var dividendsGoodCount int
|
|
|
for _, orderGood := range dividendsReturnedOrderFound.Goods {
|
|
|
if orderGood.OrderGoodDividendsStatus == 2 {
|
|
|
dividendsGoodCount = dividendsGoodCount + 1
|
|
|
}
|
|
|
}
|
|
|
if dividendsGoodCount == len(dividendsReturnedOrderFound.Goods) { // 2已分红
|
|
|
dividendsReturnedOrdersFound[i].DividendStatus = 2
|
|
|
} else if dividendsGoodCount > 0 && dividendsGoodCount < len(dividendsReturnedOrderFound.Goods) { // 3部分分红
|
|
|
dividendsReturnedOrdersFound[i].DividendStatus = 3
|
|
|
} else if dividendsGoodCount == 0 { // 1未分红
|
|
|
dividendsReturnedOrdersFound[i].DividendStatus = 1
|
|
|
}
|
|
|
}
|
|
|
// 保存退货单状态
|
|
|
if _, err6 := dividendsReturnedOrderRepository.UpdateMany(dividendsReturnedOrdersFound); err6 != nil {
|
|
|
return nil, application.ThrowError(application.INTERNAL_SERVER_ERROR, err.Error())
|
|
|
}
|
|
|
}
|
|
|
}
|
|
|
|
|
|
if err := newTransactionContext.CommitTransaction(); err != nil {
|
|
|
return nil, application.ThrowError(application.TRANSACTION_ERROR, err.Error())
|
|
|
}
|
|
|
|
|
|
return map[string]interface{}{
|
|
|
"report": fmt.Sprintf("已完成%d笔单订单分红预算,生成%d笔单分红预算,%d笔订单分红预算失败,失败原因:%s", len(estimateSuccessfullyDividendsOrders), successfullyCount, len(estimateFailedDividendsOrders), failedReasonStr),
|
|
|
}, nil
|
...
|
...
|
|