作者 陈志颖

refactor:优化取消分红预算服务,整合领域服务,增加校验

... ... @@ -177,6 +177,17 @@ func (dividendsEstimateService *DividendsEstimateService) CancelDividendsEstimat
defer func() {
_ = transactionContext.RollbackTransaction()
}()
// 取消分红预算领域服务初始化
var cancelDividendsEstimateService service.CancelDividendsEstimate
if value, err := factory.CreateCancelDividendsEstimateService(map[string]interface{}{
"transactionContext": transactionContext,
}); err != nil {
return nil, application.ThrowError(application.INTERNAL_SERVER_ERROR, err.Error())
} else {
cancelDividendsEstimateService = value
}
// 分红预算仓储初始化
var dividendsEstimateRepository domain.DividendsEstimateRepository
if value, err := factory.CreateDividendsEstimateRepository(map[string]interface{}{
... ... @@ -186,6 +197,8 @@ func (dividendsEstimateService *DividendsEstimateService) CancelDividendsEstimat
} else {
dividendsEstimateRepository = value
}
// 获取当前被取消的分红预算单
dividendsEstimate, err := dividendsEstimateRepository.FindOne(map[string]interface{}{"dividendsEstimateId": cancelDividendsEstimateCommand.DividendsEstimateId})
if err != nil {
return nil, application.ThrowError(application.INTERNAL_SERVER_ERROR, err.Error())
... ... @@ -193,20 +206,42 @@ func (dividendsEstimateService *DividendsEstimateService) CancelDividendsEstimat
if dividendsEstimate == nil {
return nil, application.ThrowError(application.RES_NO_FIND_ERROR, fmt.Sprintf("%s", strconv.FormatInt(cancelDividendsEstimateCommand.DividendsEstimateId, 10)))
}
// TODO 校验是否能够取消
// 设置取消状态
cancelDividendsEstimateCommand.IsCanceled = true
if err := dividendsEstimate.Update(tool_funs.SimpleStructToMap(cancelDividendsEstimateCommand)); err != nil {
return nil, application.ThrowError(application.BUSINESS_ERROR, err.Error())
// 校验是否能够取消
if dividendsEstimate.DividendsType == domain.ORDER_DIVIDENDS || dividendsEstimate.DividendsType == domain.RETURN_WRITE_OFF { // 订单分红、退货冲销
if count, dividendsEstimatesRelative, err2 := dividendsEstimateRepository.Find(map[string]interface{}{"orderOrReturnedOrderNum": dividendsEstimate.OrderOrReturnedOrderNum}); err2 != nil {
return nil, application.ThrowError(application.INTERNAL_SERVER_ERROR, err2.Error())
} else {
if count > 0 {
for _, dividendsEstimateRelative := range dividendsEstimatesRelative {
if dividendsEstimateRelative.DividendsAccountStatus == domain.ACCOUNTED {
return nil, application.ThrowError(application.INTERNAL_SERVER_ERROR, "已有相关分红预算单已结算,无法取消")
}
if dividendsEstimate, err := dividendsEstimateRepository.Save(dividendsEstimate); err != nil {
}
}
}
} else if dividendsEstimate.DividendsType == domain.MONEY_INCENTIVES { // 金额激励分红
if count, dividendsEstimatesRelative, err3 := dividendsEstimateRepository.Find(map[string]interface{}{"cooperationContractNumber": dividendsEstimate.CooperationContractNumber}); err3 != nil {
return nil, application.ThrowError(application.INTERNAL_SERVER_ERROR, err3.Error())
} else {
if count > 0 {
for _, dividendsEstimateRelative := range dividendsEstimatesRelative {
if dividendsEstimateRelative.DividendsAccountStatus == domain.ACCOUNTED {
return nil, application.ThrowError(application.INTERNAL_SERVER_ERROR, "已有相关分红预算单已结算,无法取消")
}
}
}
}
}
// 取消分红预算
if dividendsEstimatesCanceled, err := cancelDividendsEstimateService.CancelEstimate([]int64{cancelDividendsEstimateCommand.DividendsEstimateId}, cancelDividendsEstimateCommand.UserId); err != nil {
return nil, application.ThrowError(application.INTERNAL_SERVER_ERROR, err.Error())
} else {
if err := transactionContext.CommitTransaction(); err != nil {
return nil, application.ThrowError(application.TRANSACTION_ERROR, err.Error())
}
return dividendsEstimate, nil
return dividendsEstimatesCanceled[1], nil
}
}
... ... @@ -225,6 +260,17 @@ func (dividendsEstimateService *DividendsEstimateService) BatchCancelDividendsEs
defer func() {
_ = transactionContext.RollbackTransaction()
}()
// 取消分红预算领域服务初始化
var cancelDividendsEstimateService service.CancelDividendsEstimate
if value, err := factory.CreateCancelDividendsEstimateService(map[string]interface{}{
"transactionContext": transactionContext,
}); err != nil {
return nil, application.ThrowError(application.INTERNAL_SERVER_ERROR, err.Error())
} else {
cancelDividendsEstimateService = value
}
// 分红预算仓储初始化
var dividendsEstimateRepository domain.DividendsEstimateRepository
if value, err := factory.CreateDividendsEstimateRepository(map[string]interface{}{
... ... @@ -234,24 +280,58 @@ func (dividendsEstimateService *DividendsEstimateService) BatchCancelDividendsEs
} else {
dividendsEstimateRepository = value
}
dividendsEstimateIds, _ := utils.SliceAtoi(batchCancelEstimateCommand.DividendsEstimateIds)
// 分红预算ID类型转换
dividendsEstimateIds, err := utils.SliceAtoi(batchCancelEstimateCommand.DividendsEstimateIds)
if err != nil {
return nil, application.ThrowError(application.INTERNAL_SERVER_ERROR, "分红预算单参数转换错误")
}
// 获取分红预算
if count, dividendsEstimates, err := dividendsEstimateRepository.Find(map[string]interface{}{
"dividendsEstimateIds": dividendsEstimateIds,
}); err != nil {
return nil, application.ThrowError(application.INTERNAL_SERVER_ERROR, err.Error())
} else {
if count > 0 {
for i, _ := range dividendsEstimates {
dividendsEstimates[i].IsCanceled = true
// 校验分红预算是否可以取消,已有相关分红预算单已结算,无法取消
for _, dividendsEstimate := range dividendsEstimates {
// 校验是否能够取消
if dividendsEstimate.DividendsType == domain.ORDER_DIVIDENDS || dividendsEstimate.DividendsType == domain.RETURN_WRITE_OFF { // 订单分红、退货冲销
if count, dividendsEstimatesRelative, err2 := dividendsEstimateRepository.Find(map[string]interface{}{"orderOrReturnedOrderNum": dividendsEstimate.OrderOrReturnedOrderNum}); err2 != nil {
return nil, application.ThrowError(application.INTERNAL_SERVER_ERROR, err2.Error())
} else {
if count > 0 {
for _, dividendsEstimateRelative := range dividendsEstimatesRelative {
if dividendsEstimateRelative.DividendsAccountStatus == domain.ACCOUNTED {
return nil, application.ThrowError(application.INTERNAL_SERVER_ERROR, "已有相关分红预算单已结算,无法取消")
}
dividendsEstimatesCanceled, err := dividendsEstimateRepository.UpdateMany(dividendsEstimates)
if err != nil {
return nil, application.ThrowError(application.TRANSACTION_ERROR, err.Error())
}
}
}
} else if dividendsEstimate.DividendsType == domain.MONEY_INCENTIVES { // 金额激励分红
if count, dividendsEstimatesRelative, err3 := dividendsEstimateRepository.Find(map[string]interface{}{"cooperationContractNumber": dividendsEstimate.CooperationContractNumber}); err3 != nil {
return nil, application.ThrowError(application.INTERNAL_SERVER_ERROR, err3.Error())
} else {
if count > 0 {
for _, dividendsEstimateRelative := range dividendsEstimatesRelative {
if dividendsEstimateRelative.DividendsAccountStatus == domain.ACCOUNTED {
return nil, application.ThrowError(application.INTERNAL_SERVER_ERROR, "已有相关分红预算单已结算,无法取消")
}
}
}
}
}
}
// 批量取消分红预算单
if dividendsEstimatesCanceled, err := cancelDividendsEstimateService.CancelEstimate(dividendsEstimateIds, batchCancelEstimateCommand.UserId); err != nil {
return nil, application.ThrowError(application.INTERNAL_SERVER_ERROR, err.Error())
} else {
if err := transactionContext.CommitTransaction(); err != nil {
return nil, application.ThrowError(application.TRANSACTION_ERROR, err.Error())
}
return dividendsEstimatesCanceled, nil
}
} else {
return map[string]interface{}{}, nil
}
... ...
... ... @@ -92,9 +92,9 @@ func (domainService *CancelDividendsEstimateService) CancelEstimate(dividendsEst
// 订单/退货单号数组去重
orderOrReturnOrderNumsWithoutDup := utils.RemoveDuplication(orderOrReturnOrderNums)
for _, orderNullNum := range orderOrReturnOrderNumsWithoutDup {
if orderNullNum[0:2] == "RE" { // 分红退后单
if orderNullNum[:2] == "RE" { // 分红退后单
returnedOrderNums = append(returnedOrderNums, orderNullNum)
} else if orderNullNum[0:2] == "FH" { // 分红订单
} else if orderNullNum[:2] == "SL" { // 分红订单
orderNums = append(orderNums, orderNullNum)
}
}
... ...
... ... @@ -258,6 +258,9 @@ func (repository *DividendsEstimateRepository) FindOne(queryOptions map[string]i
dividendsEstimateModel := new(models.DividendsEstimate)
query := sqlbuilder.BuildQuery(tx.Model(dividendsEstimateModel), queryOptions)
query.SetWhereByQueryOption("dividends_estimate.dividends_estimate_id = ?", "dividendsEstimateId")
if orderOrReturnedOrderNum, ok := queryOptions["orderOrReturnedOrderNum"]; ok && orderOrReturnedOrderNum != "" {
query.Where("order_or_returned_order_num = ?", orderOrReturnedOrderNum)
}
if err := query.First(); err != nil {
if err.Error() == "pg: no rows in result set" {
return nil, fmt.Errorf("没有此资源")
... ... @@ -286,6 +289,9 @@ func (repository *DividendsEstimateRepository) Find(queryOptions map[string]inte
if dividendsEstimateIds, ok := queryOptions["dividendsEstimateIds"]; ok && len(dividendsEstimateIds.([]int64)) > 0 {
query.Where("dividends_estimate_id IN (?)", pg.In(dividendsEstimateIds))
}
if orderOrReturnedOrderNum, ok := queryOptions["orderOrReturnedOrderNum"]; ok && orderOrReturnedOrderNum != "" {
query.Where("order_or_returned_order_num = ?", orderOrReturnedOrderNum)
}
if companyId, ok := queryOptions["companyId"]; ok && companyId.(int64) != 0 {
query.Where("company->>'companyId' = '?'", companyId)
}
... ...