作者 陈志颖

合并分支 'dev' 到 'test'

Dev



查看合并请求 !3
... ... @@ -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
... ...
... ... @@ -43,6 +43,7 @@ type OrderGood struct {
type OrderGoodRepository interface {
Save(orderGood *OrderGood) (*OrderGood, error)
UpdateMany(orderGoods []*OrderGood) ([]*OrderGood, error)
Remove(orderGood *OrderGood) (*OrderGood, error)
FindOne(queryOptions map[string]interface{}) (*OrderGood, error)
Find(queryOptions map[string]interface{}) (int64, []*OrderGood, error)
... ...
... ... @@ -526,6 +526,9 @@ func (repository *DividendsReturnedOrderRepository) Find(queryOptions map[string
if dividendsReturnedOrderNumber, ok := queryOptions["dividendsReturnedOrderNumber"]; ok && dividendsReturnedOrderNumber != "" {
query.Where("dividends_returned_order_number ilike ?", fmt.Sprintf("%%%s%%", dividendsReturnedOrderNumber))
}
if dividendsReturnedOrderNumbers, ok := queryOptions["dividendsReturnedOrderNumbers"]; ok && len(dividendsReturnedOrderNumbers.([]string)) > 0 {
query.Where("dividends_returned_order_number IN (?)", pg.In(dividendsReturnedOrderNumbers.([]string)))
}
if dividendsReturnedCustomerName, ok := queryOptions["dividendsReturnedCustomerName"]; ok && dividendsReturnedCustomerName != "" {
query.Where("dividends_returned_customer_name like ?", fmt.Sprintf("%%%s%%", dividendsReturnedCustomerName))
}
... ...
... ... @@ -5,6 +5,7 @@ import (
"github.com/go-pg/pg/v10"
"github.com/go-pg/pg/v10/orm"
"gitlab.fjmaimaimai.com/allied-creation/allied-creation-cooperation/pkg/infrastructure/utils"
"time"
"github.com/linmadan/egglib-go/persistent/pg/sqlbuilder"
pgTransaction "github.com/linmadan/egglib-go/transaction/pg"
... ... @@ -130,6 +131,34 @@ func (repository *OrderGoodRepository) Save(orderGood *domain.OrderGood) (*domai
return orderGood, nil
}
func (repository *OrderGoodRepository) UpdateMany(orderGoods []*domain.OrderGood) ([]*domain.OrderGood, error) {
tx := repository.transactionContext.PgTx
var orderGoodModels []*models.OrderGood
for _, orderGood := range orderGoods {
orderGoodModels = append(orderGoodModels, &models.OrderGood{
OrderGoodId: orderGood.OrderGoodId,
OrderGoodAmount: orderGood.OrderGoodAmount,
OrderGoodName: orderGood.OrderGoodName,
OrderGoodPrice: orderGood.OrderGoodPrice,
OrderGoodQuantity: orderGood.OrderGoodQuantity,
DividendsOrderNumber: orderGood.DividendsOrderNumber,
DividendsReturnedOrderNumber: orderGood.DividendsReturnedOrderNumber,
CooperationContractNumber: orderGood.CooperationContractNumber,
OrgId: orderGood.OrgId,
CompanyId: orderGood.CompanyId,
OrderGoodExpense: orderGood.OrderGoodExpense,
OrderGoodDividendsStatus: orderGood.OrderGoodDividendsStatus,
CreatedAt: orderGood.CreatedAt,
DeletedAt: orderGood.DeletedAt,
UpdatedAt: time.Now(),
})
}
if _, err := tx.Model(&orderGoodModels).WherePK().Update(); err != nil {
return nil, err
}
return orderGoods, nil
}
func (repository *OrderGoodRepository) Remove(orderGood *domain.OrderGood) (*domain.OrderGood, error) {
tx := repository.transactionContext.PgTx
orderGoodModel := new(models.OrderGood)
... ...