作者 陈志颖

refactor:分红预算

... ... @@ -521,6 +521,7 @@ func (dividendsEstimateService *DividendsEstimateService) CreateDividendsEstimat
// ConfirmDividendsIncentivesEstimate 确定业绩激励分红预算
func (dividendsEstimateService *DividendsEstimateService) ConfirmDividendsIncentivesEstimate(confirmDividendsIncentivesEstimateCommand *command.ConfirmDividendsIncentivesEstimateCommand) (interface{}, error) {
start := time.Now()
if err := confirmDividendsIncentivesEstimateCommand.ValidateCommand(); err != nil {
return nil, application.ThrowError(application.ARG_ERROR, err.Error())
}
... ... @@ -550,12 +551,14 @@ func (dividendsEstimateService *DividendsEstimateService) ConfirmDividendsIncent
}
// 获取公司信息
startGetCompany := time.Now()
var company *domain.Company
if data, err := companyService.CompanyFrom(confirmDividendsIncentivesEstimateCommand.CompanyId); err != nil {
return nil, application.ThrowError(application.INTERNAL_SERVER_ERROR, err.Error())
} else {
company = data
}
fmt.Println("获取公司信息耗时:", time.Since(startGetCompany))
// 组织机构REST服务初始化
var organizationService service.OrgService
... ... @@ -566,12 +569,14 @@ func (dividendsEstimateService *DividendsEstimateService) ConfirmDividendsIncent
}
// 获取组织机构信息
startGetOrg := time.Now()
var organization *domain.Org
if data, err := organizationService.OrgFrom(confirmDividendsIncentivesEstimateCommand.CompanyId, confirmDividendsIncentivesEstimateCommand.OrgId); err != nil {
return nil, application.ThrowError(application.INTERNAL_SERVER_ERROR, err.Error())
} else {
organization = data
}
fmt.Println("获取组织机构数据耗时:", time.Since(startGetOrg))
// 用户REST服务初始化
var userService service.UserService
... ... @@ -582,12 +587,14 @@ func (dividendsEstimateService *DividendsEstimateService) ConfirmDividendsIncent
}
// 获取操作人
startGetOperator := time.Now()
var operator *domain.User
if data, err := userService.OperatorFrom(confirmDividendsIncentivesEstimateCommand.CompanyId, confirmDividendsIncentivesEstimateCommand.OrgId, confirmDividendsIncentivesEstimateCommand.UserId); err != nil {
return nil, application.ThrowError(application.INTERNAL_SERVER_ERROR, err.Error())
} else {
operator = data
}
fmt.Println("获取操作人耗时:", time.Since(startGetOperator))
// 订单产品仓储初始化
var orderGoodRepository domain.OrderGoodRepository
... ... @@ -669,12 +676,15 @@ func (dividendsEstimateService *DividendsEstimateService) ConfirmDividendsIncent
estimateFailedDividendsOrders := make(map[string]string)
// 获取订单产品
startGetOrderGoods := time.Now()
if _, orderGoods, err := orderGoodRepository.Find(map[string]interface{}{
"orderGoodIds": orderGoodIds,
"offsetLimit": false,
}); err != nil {
return nil, application.ThrowError(application.INTERNAL_SERVER_ERROR, err.Error())
} else {
fmt.Println("获取订单产品耗时:", time.Since(startGetOrderGoods))
dividendsEstimates := make([]*domain.DividendsEstimate, 0)
var cooperationContractNumbers []string
... ... @@ -689,6 +699,7 @@ func (dividendsEstimateService *DividendsEstimateService) ConfirmDividendsIncent
cooperationContractsMap := make(map[string]*domain.CooperationContract, 0)
// 查询共创合约
startQueryContracts := time.Now()
if len(newCooperationContractNumbers) > 0 {
if count, cooperationContractsFound, err := cooperationContractRepository.Find(map[string]interface{}{
"cooperationContractNumbers": newCooperationContractNumbers,
... ... @@ -709,6 +720,7 @@ func (dividendsEstimateService *DividendsEstimateService) ConfirmDividendsIncent
"report": fmt.Sprintf("已完成0单订单分红预算,生成0单分红预算,失败原因:产品未关联合约"),
}, nil
}
fmt.Println("查询共创合约耗时:", time.Since(startQueryContracts))
// 统计当前企业分红预算单数
count, err := dividendsEstimateDao.CountDividendsEstimate(map[string]interface{}{
... ... @@ -731,6 +743,7 @@ func (dividendsEstimateService *DividendsEstimateService) ConfirmDividendsIncent
}
}
startConfirm := time.Now()
if len(orderGoodsToConfirm) > 0 { // 分红订单产品预算
if dividendsEstimateDetails, err := confirmDividendsIncentivesEstimateService.Confirm(orderGoodsToConfirm, confirmDividendsIncentivesEstimateCommand.CompanyId, confirmDividendsIncentivesEstimateCommand.OrgId, cooperationContractsMap); err != nil {
return nil, application.ThrowError(application.INTERNAL_SERVER_ERROR, err.Error())
... ... @@ -838,127 +851,13 @@ func (dividendsEstimateService *DividendsEstimateService) ConfirmDividendsIncent
}
}
}
fmt.Println("订单分红耗时:", time.Since(startConfirm))
//for _, orderGood := range orderGoods {
// dividendsEstimate := &domain.DividendsEstimate{}
// if orderGood.DividendsOrderNumber != "" { // 查询分红订单
// orderGoodsToConfirm := make([]*domain.OrderGood, 0)
// orderGoodsToConfirm = append(orderGoodsToConfirm, orderGood)
// // 分红订单产品预算
// if dividendsEstimateDetails, err := confirmDividendsIncentivesEstimateService.Confirm(orderGoodsToConfirm, confirmDividendsIncentivesEstimateCommand.CompanyId, confirmDividendsIncentivesEstimateCommand.OrgId, cooperationContractsMap); err != nil {
// return nil, application.ThrowError(application.INTERNAL_SERVER_ERROR, err.Error())
// } else {
// for _, dividendsEstimateDetail := range dividendsEstimateDetails {
// if dividendsEstimateDetail.IsSuccessfully {
// // 生成分红预算单号
// dividendsEstimateOrderNumber, err := dividendsEstimateDetail.GenerateSpecificDividendsEstimateNumber(int64(count), countDividendsEstimate)
// if err != nil {
// return nil, application.ThrowError(application.TRANSACTION_ERROR, err.Error())
// }
//
// // 记录成功的订单号
// estimateSuccessfullyDividendsOrders[dividendsEstimateDetail.OrderOrReturnedOrderNumber] = dividendsEstimateDetail.OrderOrReturnedOrderNumber
//
// // 统计分红预算单数目
// countDividendsEstimate = countDividendsEstimate + 1
//
// dividendsEstimate = &domain.DividendsEstimate{
// DividendsEstimateId: 0,
// DividendsAccountStatus: domain.TO_BE_ACCOUNT,
// DividendsAmount: dividendsEstimateDetail.DividendsAmount,
// DividendsEstimateOrderNumber: dividendsEstimateOrderNumber,
// DividendsEstimateTime: time.Now(),
// DividendsParticipateType: dividendsEstimateDetail.DividendsParticipateType,
// DividendsType: domain.ORDER_DIVIDENDS,
// DividendsTypeName: "订单分红",
// OrderOrReturnedOrderNum: orderGood.DividendsOrderNumber,
// CooperationContractNumber: orderGood.CooperationContractNumber,
// DividendsUser: dividendsEstimateDetail.DividendsUser,
// DividendsPercentage: dividendsEstimateDetail.DividendsPercentage,
// DividendsStage: dividendsEstimateDetail.DividendsStage,
// Org: organization,
// Company: company,
// Operator: operator,
// OperateTime: time.Time{},
// IsCanceled: false,
// CreatedAt: time.Now(),
// DeletedAt: time.Time{},
// UpdatedAt: time.Time{},
// OrderGoodId: orderGood.OrderGoodId,
// OrderGoodAmount: orderGood.OrderGoodAmount,
// PaymentStatus: int32(1),
// CooperationContractUndertakerId: dividendsEstimateDetail.CooperationContractUndertakerId,
// }
//
// dividendsEstimates = append(dividendsEstimates, dividendsEstimate)
// } else {
// // 记录失败的订单号和原因
// estimateFailedDividendsOrders[dividendsEstimateDetail.OrderOrReturnedOrderNumber] = dividendsEstimateDetail.Reason
// }
// }
// }
// } else if orderGood.DividendsReturnedOrderNumber != "" { // 查询分红退货单
// orderGoodsToConfirm := make([]*domain.OrderGood, 0)
// orderGoodsToConfirm = append(orderGoodsToConfirm, orderGood)
// // 分红退货单产品预算
// if dividendsReturnedEstimateDetails, err := confirmDividendsIncentivesEstimateService.Confirm(orderGoodsToConfirm, confirmDividendsIncentivesEstimateCommand.CompanyId, confirmDividendsIncentivesEstimateCommand.OrgId, cooperationContractsMap); err != nil {
// return nil, application.ThrowError(application.INTERNAL_SERVER_ERROR, err.Error())
// } else {
// for _, dividendsReturnedEstimateDetail := range dividendsReturnedEstimateDetails {
// if dividendsReturnedEstimateDetail.IsSuccessfully {
// // 生成分红预算单号
// dividendsEstimateOrderNumber, err := dividendsReturnedEstimateDetail.GenerateSpecificDividendsEstimateNumber(int64(count), countDividendsEstimate)
// if err != nil {
// return nil, application.ThrowError(application.TRANSACTION_ERROR, err.Error())
// }
//
// // 记录成功的退货单号
// estimateSuccessfullyDividendsOrders[dividendsReturnedEstimateDetail.OrderOrReturnedOrderNumber] = dividendsReturnedEstimateDetail.OrderOrReturnedOrderNumber
//
// // 统计生成的预算单数量
// countDividendsEstimate = countDividendsEstimate + 1
//
// dividendsEstimate = &domain.DividendsEstimate{
// DividendsEstimateId: 0,
// DividendsAccountStatus: domain.TO_BE_ACCOUNT,
// DividendsAmount: dividendsReturnedEstimateDetail.DividendsAmount,
// DividendsEstimateOrderNumber: dividendsEstimateOrderNumber,
// DividendsEstimateTime: time.Now(),
// DividendsParticipateType: dividendsReturnedEstimateDetail.DividendsParticipateType,
// DividendsType: domain.RETURN_WRITE_OFF,
// DividendsTypeName: "退货冲销",
// OrderOrReturnedOrderNum: orderGood.DividendsReturnedOrderNumber,
// CooperationContractNumber: orderGood.CooperationContractNumber,
// DividendsUser: dividendsReturnedEstimateDetail.DividendsUser,
// DividendsPercentage: dividendsReturnedEstimateDetail.DividendsPercentage,
// DividendsStage: dividendsReturnedEstimateDetail.DividendsStage,
// Org: organization,
// Company: company,
// Operator: operator,
// OperateTime: time.Time{},
// IsCanceled: false,
// CreatedAt: time.Now(),
// DeletedAt: time.Time{},
// UpdatedAt: time.Time{},
// OrderGoodId: orderGood.OrderGoodId,
// OrderGoodAmount: orderGood.OrderGoodAmount,
// PaymentStatus: int32(1),
// CooperationContractUndertakerId: dividendsReturnedEstimateDetail.CooperationContractUndertakerId,
// }
//
// dividendsEstimates = append(dividendsEstimates, dividendsEstimate)
// } else {
// // 记录失败的退货单号和原因
// estimateFailedDividendsOrders[dividendsReturnedEstimateDetail.OrderOrReturnedOrderNumber] = dividendsReturnedEstimateDetail.Reason
// }
// }
// }
// }
//}
startInsert := time.Now()
if dividendsEstimatesSaved, err := dividendsEstimateRepository.SaveMany(dividendsEstimates); err != nil {
return nil, application.ThrowError(application.INTERNAL_SERVER_ERROR, err.Error())
} else {
fmt.Println("插入预算单耗时:", time.Since(startInsert))
// 获取产品ID
orderGoodSavedIds := make([]int64, 0)
for _, dividendsEstimate := range dividendsEstimates {
... ... @@ -966,6 +865,7 @@ func (dividendsEstimateService *DividendsEstimateService) ConfirmDividendsIncent
}
// 更新订单产品分红状态
startUpdateGoodStatus := time.Now()
var orderGoodsSaved []*domain.OrderGood
if len(orderGoodSavedIds) > 0 {
// 订单产品ID去重
... ... @@ -987,6 +887,7 @@ func (dividendsEstimateService *DividendsEstimateService) ConfirmDividendsIncent
}
}
}
fmt.Println("更新订单产品分红状态耗时:", time.Since(startUpdateGoodStatus))
// 分离分红订单和退货单
dividendsOrderNumbers := make([]string, 0)
... ... @@ -1004,6 +905,7 @@ func (dividendsEstimateService *DividendsEstimateService) ConfirmDividendsIncent
}
// 查找分红订单
startUpdateOrder := time.Now()
if len(dividendsOrderNumbers) > 0 {
if countDividendsOrdersFound, dividendsOrdersFound, err := dividendsOrderRepository.Find(map[string]interface{}{
"dividendsOrderNumbers": dividendsOrderNumbers,
... ... @@ -1046,8 +948,10 @@ func (dividendsEstimateService *DividendsEstimateService) ConfirmDividendsIncent
}
}
}
fmt.Println("更新订单耗时:", time.Since(startUpdateOrder))
// 查找分红退货单
startUpdateReturnedOrder := time.Now()
if len(dividendsReturnedOrderNumbers) > 0 {
if countDividendsReturnedOrdersFound, dividendsReturnedOrdersFound, err := dividendsReturnedOrderRepository.Find(map[string]interface{}{
"dividendsReturnedOrderNumbers": dividendsReturnedOrderNumbers,
... ... @@ -1090,9 +994,13 @@ func (dividendsEstimateService *DividendsEstimateService) ConfirmDividendsIncent
}
}
fmt.Println("更新退货单耗时:", time.Since(startUpdateReturnedOrder))
startCommit := time.Now()
if err := transactionContext.CommitTransaction(); err != nil {
return nil, application.ThrowError(application.TRANSACTION_ERROR, err.Error())
}
fmt.Println("提交事务耗时:", time.Since(startCommit))
// 统计成功的分红预算
successfullyCount := len(dividendsEstimatesSaved)
... ... @@ -1109,6 +1017,8 @@ func (dividendsEstimateService *DividendsEstimateService) ConfirmDividendsIncent
failedReasonStr = "无"
}
fmt.Println("函数执行完毕:", time.Since(start))
return map[string]interface{}{
"report": fmt.Sprintf("已完成%d单订单分红预算,生成%d单分红预算,%d笔订单分红预算失败,失败原因:%s", len(estimateSuccessfullyDividendsOrders), successfullyCount, len(estimateFailedDividendsOrders), failedReasonStr),
}, nil
... ...
... ... @@ -26,7 +26,7 @@ func (dao *DividendsEstimateDao) GenerateDividendsEstimateNumber(queryOptions ma
if companyId, ok := queryOptions["companyId"]; ok && companyId.(int64) != 0 {
query = query.Where(`dividends_estimate.company @> '{"companyId":"?"}'`, companyId)
}
if count, err := query.AllWithDeleted().SelectAndCount(); err != nil {
if count, err := query.AllWithDeleted().Count(); err != nil {
return "", err
} else {
if count < 1000 {
... ... @@ -118,7 +118,7 @@ func (dao *DividendsEstimateDao) CountDividendsEstimate(queryOptions map[string]
if orgId, ok := queryOptions["orgId"]; ok && orgId.(int64) != 0 {
query = query.Where(`dividends_estimate.org @> '{"orgId":"?"}'`, orgId)
}
if count, err := query.AllWithDeleted().SelectAndCount(); err != nil {
if count, err := query.AllWithDeleted().Count(); err != nil {
return 0, err
} else {
return count, nil
... ...