作者 陈志颖

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"
@@ -167,8 +168,87 @@ func (repository *DividendsReturnedOrderRepository) Save(dividendsReturnedOrder @@ -167,8 +168,87 @@ func (repository *DividendsReturnedOrderRepository) Save(dividendsReturnedOrder
167 ); err != nil { 168 ); err != nil {
168 return dividendsReturnedOrder, err 169 return dividendsReturnedOrder, err
169 } 170 }
170 - //TODO 新增、更新、删除分红退货单产品  
171 171
  172 + // 获取分红退货订单产品
  173 + var orderGoodsFetched []*models.OrderGood
  174 + orderGoodsQuery := tx.Model(&orderGoodsFetched)
  175 + if err := orderGoodsQuery.Where("dividends_return_order_number = ?", dividendsReturnedOrder.DividendsReturnedOrderNumber).Select(); err != nil {
  176 + return nil, err
  177 + }
  178 +
  179 + // 提取分红退货订单产品id
  180 + var orderGoodIdsFetched []int64
  181 + for _, orderGood := range orderGoodsFetched {
  182 + orderGoodIdsFetched = append(orderGoodIdsFetched, orderGood.OrderGoodId)
  183 + }
  184 +
  185 + // 待更新分红退货单产品
  186 + var orderGoodsToUpdate []*domain.OrderGood
  187 + // 待添加分红退货单产品
  188 + var orderGoodsToAdd []*domain.OrderGood
  189 + for _, good := range dividendsReturnedOrder.Goods {
  190 + if good.OrderGoodId != 0 {
  191 + orderGoodsToUpdate = append(orderGoodsToUpdate, good)
  192 + } else {
  193 + orderGoodsToAdd = append(orderGoodsToAdd, good)
  194 + }
  195 + }
  196 +
  197 + // 将待添加的分红退户订单产品领域模型转换为数据模型
  198 + var orderGoodsToAddModels []*models.OrderGood
  199 + for _, goodDomain := range orderGoodsToAdd {
  200 + orderGoodsToAddModels = append(orderGoodsToAddModels, &models.OrderGood{
  201 + OrderGoodAmount: goodDomain.OrderGoodAmount,
  202 + OrderGoodName: goodDomain.OrderGoodName,
  203 + OrderGoodPrice: goodDomain.OrderGoodPrice,
  204 + OrderGoodQuantity: goodDomain.OrderGoodQuantity,
  205 + DividendsOrderNumber: goodDomain.DividendsOrderNumber,
  206 + DividendsReturnedOrderNumber: goodDomain.DividendsReturnedOrderNumber,
  207 + CooperationContractNumber: goodDomain.CooperationContractNumber,
  208 + OrderGoodExpense: goodDomain.OrderGoodExpense,
  209 + CreatedAt: time.Time{},
  210 + DeletedAt: time.Time{},
  211 + UpdatedAt: time.Now(),
  212 + })
  213 + }
  214 + // 添加分红退货订单产品
  215 + if _, err := tx.Model(&orderGoodsToAddModels).Insert(); err != nil {
  216 + return nil, err
  217 + }
  218 +
  219 + // 待更新或者删除的ids
  220 + var orderGoodIdsToUpdateOrDelete []int64
  221 + for _, orderGoodToUpdate := range orderGoodsToUpdate {
  222 + orderGoodIdsToUpdateOrDelete = append(orderGoodIdsToUpdateOrDelete, orderGoodToUpdate.OrderGoodId)
  223 + }
  224 +
  225 + // 待更新的分红退货订单产品id
  226 + orderGoodIdsToUpdate := utils.Intersect(orderGoodIdsFetched, orderGoodIdsToUpdateOrDelete)
  227 + var orderGoodModelsToUpdate []*models.OrderGood
  228 + for _, id := range orderGoodIdsToUpdate {
  229 + for _, orderGoodModel := range orderGoodsFetched {
  230 + if orderGoodModel.OrderGoodId == id {
  231 + orderGoodModelsToUpdate = append(orderGoodModelsToUpdate, orderGoodModel)
  232 + }
  233 + }
  234 + }
  235 + if _, err := tx.Model(&orderGoodModelsToUpdate).WherePK().Update(); err != nil {
  236 + return nil, err
  237 + }
  238 +
  239 + // 待删除的分红退货订单产品id
  240 + orderGoodIdsToDelete := utils.Difference(orderGoodIdsFetched, orderGoodIdsToUpdateOrDelete)
  241 + var orderGoodModelsToDelete []*models.OrderGood
  242 + for _, id := range orderGoodIdsToDelete {
  243 + for _, orderGoodModel := range orderGoodsFetched {
  244 + if orderGoodModel.OrderGoodId == id {
  245 + orderGoodModelsToDelete = append(orderGoodModelsToDelete, orderGoodModel)
  246 + }
  247 + }
  248 + }
  249 + if _, err := tx.Model(&orderGoodModelsToDelete).WherePK().Delete(); err != nil {
  250 + return nil, err
  251 + }
172 } 252 }
173 return dividendsReturnedOrder, nil 253 return dividendsReturnedOrder, nil
174 } 254 }