...
|
...
|
@@ -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"
|
...
|
...
|
@@ -162,8 +163,87 @@ func (repository *DividendsOrderRepository) Save(dividendsOrder *domain.Dividend |
|
|
); err != nil {
|
|
|
return dividendsOrder, err
|
|
|
}
|
|
|
//TODO 新增、更新、删除分红订单产品
|
|
|
|
|
|
// 更新分红订单产品
|
|
|
var orderGoodsFetched []*models.OrderGood
|
|
|
orderGoodsQuery := tx.Model(&orderGoodsFetched)
|
|
|
if err := orderGoodsQuery.Where("dividends_order_number = ?", dividendsOrder.DividendsOrderNumber).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 dividendsOrder.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 dividendsOrder, nil
|
|
|
}
|
...
|
...
|
|