作者 陈志颖

feat:分红退货单仓储整合退货单产品仓储

... ... @@ -3,6 +3,7 @@ package repository
import (
"fmt"
"github.com/go-pg/pg/v10"
"gitlab.fjmaimaimai.com/allied-creation/allied-creation-cooperation/pkg/infrastructure/utils"
"time"
"github.com/linmadan/egglib-go/persistent/pg/sqlbuilder"
... ... @@ -167,8 +168,87 @@ func (repository *DividendsReturnedOrderRepository) Save(dividendsReturnedOrder
); err != nil {
return dividendsReturnedOrder, err
}
//TODO 新增、更新、删除分红退货单产品
// 获取分红退货订单产品
var orderGoodsFetched []*models.OrderGood
orderGoodsQuery := tx.Model(&orderGoodsFetched)
if err := orderGoodsQuery.Where("dividends_return_order_number = ?", dividendsReturnedOrder.DividendsReturnedOrderNumber).Select(); err != nil {
return nil, err
}
// 提取分红退货订单产品id
var orderGoodIdsFetched []int64
for _, orderGood := range orderGoodsFetched {
orderGoodIdsFetched = append(orderGoodIdsFetched, orderGood.OrderGoodId)
}
// 待更新分红退货单产品
var orderGoodsToUpdate []*domain.OrderGood
// 待添加分红退货单产品
var orderGoodsToAdd []*domain.OrderGood
for _, good := range dividendsReturnedOrder.Goods {
if good.OrderGoodId != 0 {
orderGoodsToUpdate = append(orderGoodsToUpdate, good)
} else {
orderGoodsToAdd = append(orderGoodsToAdd, good)
}
}
// 将待添加的分红退户订单产品领域模型转换为数据模型
var orderGoodsToAddModels []*models.OrderGood
for _, goodDomain := range orderGoodsToAdd {
orderGoodsToAddModels = append(orderGoodsToAddModels, &models.OrderGood{
OrderGoodAmount: goodDomain.OrderGoodAmount,
OrderGoodName: goodDomain.OrderGoodName,
OrderGoodPrice: goodDomain.OrderGoodPrice,
OrderGoodQuantity: goodDomain.OrderGoodQuantity,
DividendsOrderNumber: goodDomain.DividendsOrderNumber,
DividendsReturnedOrderNumber: goodDomain.DividendsReturnedOrderNumber,
CooperationContractNumber: goodDomain.CooperationContractNumber,
OrderGoodExpense: goodDomain.OrderGoodExpense,
CreatedAt: time.Time{},
DeletedAt: time.Time{},
UpdatedAt: time.Now(),
})
}
// 添加分红退货订单产品
if _, err := tx.Model(&orderGoodsToAddModels).Insert(); err != nil {
return nil, err
}
// 待更新或者删除的ids
var orderGoodIdsToUpdateOrDelete []int64
for _, orderGoodToUpdate := range orderGoodsToUpdate {
orderGoodIdsToUpdateOrDelete = append(orderGoodIdsToUpdateOrDelete, orderGoodToUpdate.OrderGoodId)
}
// 待更新的分红退货订单产品id
orderGoodIdsToUpdate := utils.Intersect(orderGoodIdsFetched, orderGoodIdsToUpdateOrDelete)
var orderGoodModelsToUpdate []*models.OrderGood
for _, id := range orderGoodIdsToUpdate {
for _, orderGoodModel := range orderGoodsFetched {
if orderGoodModel.OrderGoodId == id {
orderGoodModelsToUpdate = append(orderGoodModelsToUpdate, orderGoodModel)
}
}
}
if _, err := tx.Model(&orderGoodModelsToUpdate).WherePK().Update(); err != nil {
return nil, err
}
// 待删除的分红退货订单产品id
orderGoodIdsToDelete := utils.Difference(orderGoodIdsFetched, orderGoodIdsToUpdateOrDelete)
var orderGoodModelsToDelete []*models.OrderGood
for _, id := range orderGoodIdsToDelete {
for _, orderGoodModel := range orderGoodsFetched {
if orderGoodModel.OrderGoodId == id {
orderGoodModelsToDelete = append(orderGoodModelsToDelete, orderGoodModel)
}
}
}
if _, err := tx.Model(&orderGoodModelsToDelete).WherePK().Delete(); err != nil {
return nil, err
}
}
return dividendsReturnedOrder, nil
}
... ...