作者 陈志颖

feat:分红订单仓储整合产品仓储

@@ -3,6 +3,7 @@ package repository @@ -3,6 +3,7 @@ package repository
3 import ( 3 import (
4 "fmt" 4 "fmt"
5 "github.com/go-pg/pg/v10" 5 "github.com/go-pg/pg/v10"
  6 + "gitlab.fjmaimaimai.com/allied-creation/allied-creation-cooperation/pkg/infrastructure/utils"
6 "time" 7 "time"
7 8
8 "github.com/linmadan/egglib-go/persistent/pg/sqlbuilder" 9 "github.com/linmadan/egglib-go/persistent/pg/sqlbuilder"
@@ -162,8 +163,87 @@ func (repository *DividendsOrderRepository) Save(dividendsOrder *domain.Dividend @@ -162,8 +163,87 @@ func (repository *DividendsOrderRepository) Save(dividendsOrder *domain.Dividend
162 ); err != nil { 163 ); err != nil {
163 return dividendsOrder, err 164 return dividendsOrder, err
164 } 165 }
165 - //TODO 新增、更新、删除分红订单产品  
166 166
  167 + // 更新分红订单产品
  168 + var orderGoodsFetched []*models.OrderGood
  169 + orderGoodsQuery := tx.Model(&orderGoodsFetched)
  170 + if err := orderGoodsQuery.Where("dividends_order_number = ?", dividendsOrder.DividendsOrderNumber).Select(); err != nil {
  171 + return nil, err
  172 + }
  173 +
  174 + // 提取分红订单产品id
  175 + var orderGoodIdsFetched []int64
  176 + for _, orderGood := range orderGoodsFetched {
  177 + orderGoodIdsFetched = append(orderGoodIdsFetched, orderGood.OrderGoodId)
  178 + }
  179 +
  180 + // 待更新分红产品
  181 + var orderGoodsToUpdate []*domain.OrderGood
  182 + // 待添加分红产品
  183 + var orderGoodsToAdd []*domain.OrderGood
  184 + for _, good := range dividendsOrder.Goods {
  185 + if good.OrderGoodId != 0 {
  186 + orderGoodsToUpdate = append(orderGoodsToUpdate, good)
  187 + } else {
  188 + orderGoodsToAdd = append(orderGoodsToAdd, good)
  189 + }
  190 + }
  191 +
  192 + // 将待添加的分红订单产品领域模型转换为数据模型
  193 + var orderGoodsToAddModels []*models.OrderGood
  194 + for _, goodDomain := range orderGoodsToAdd {
  195 + orderGoodsToAddModels = append(orderGoodsToAddModels, &models.OrderGood{
  196 + OrderGoodAmount: goodDomain.OrderGoodAmount,
  197 + OrderGoodName: goodDomain.OrderGoodName,
  198 + OrderGoodPrice: goodDomain.OrderGoodPrice,
  199 + OrderGoodQuantity: goodDomain.OrderGoodQuantity,
  200 + DividendsOrderNumber: goodDomain.DividendsOrderNumber,
  201 + DividendsReturnedOrderNumber: goodDomain.DividendsReturnedOrderNumber,
  202 + CooperationContractNumber: goodDomain.CooperationContractNumber,
  203 + OrderGoodExpense: goodDomain.OrderGoodExpense,
  204 + CreatedAt: time.Time{},
  205 + DeletedAt: time.Time{},
  206 + UpdatedAt: time.Now(),
  207 + })
  208 + }
  209 + // 添加分红订单产品
  210 + if _, err := tx.Model(&orderGoodsToAddModels).Insert(); err != nil {
  211 + return nil, err
  212 + }
  213 +
  214 + // 待更新或者删除的ids
  215 + var orderGoodIdsToUpdateOrDelete []int64
  216 + for _, orderGoodToUpdate := range orderGoodsToUpdate {
  217 + orderGoodIdsToUpdateOrDelete = append(orderGoodIdsToUpdateOrDelete, orderGoodToUpdate.OrderGoodId)
  218 + }
  219 +
  220 + // 待更新的分红订单产品id
  221 + orderGoodIdsToUpdate := utils.Intersect(orderGoodIdsFetched, orderGoodIdsToUpdateOrDelete)
  222 + var orderGoodModelsToUpdate []*models.OrderGood
  223 + for _, id := range orderGoodIdsToUpdate {
  224 + for _, orderGoodModel := range orderGoodsFetched {
  225 + if orderGoodModel.OrderGoodId == id {
  226 + orderGoodModelsToUpdate = append(orderGoodModelsToUpdate, orderGoodModel)
  227 + }
  228 + }
  229 + }
  230 + if _, err := tx.Model(&orderGoodModelsToUpdate).WherePK().Update(); err != nil {
  231 + return nil, err
  232 + }
  233 +
  234 + // 待删除的分红订单产品id
  235 + orderGoodIdsToDelete := utils.Difference(orderGoodIdsFetched, orderGoodIdsToUpdateOrDelete)
  236 + var orderGoodModelsToDelete []*models.OrderGood
  237 + for _, id := range orderGoodIdsToDelete {
  238 + for _, orderGoodModel := range orderGoodsFetched {
  239 + if orderGoodModel.OrderGoodId == id {
  240 + orderGoodModelsToDelete = append(orderGoodModelsToDelete, orderGoodModel)
  241 + }
  242 + }
  243 + }
  244 + if _, err := tx.Model(&orderGoodModelsToDelete).WherePK().Delete(); err != nil {
  245 + return nil, err
  246 + }
167 } 247 }
168 return dividendsOrder, nil 248 return dividendsOrder, nil
169 } 249 }