lrf 2 年之前
父節點
當前提交
aa2cc1d716
共有 3 個文件被更改,包括 135 次插入61 次删除
  1. 24 0
      app/service/shop/goodsSet.js
  2. 56 24
      app/service/trade/order.js
  3. 55 37
      app/service/util/order.js

+ 24 - 0
app/service/shop/goodsSet.js

@@ -10,6 +10,8 @@ class GoodsSetService extends CrudService {
   constructor(ctx) {
     super(ctx, 'goodsset');
     this.model = this.ctx.model.Shop.GoodsSet;
+    this.goodsModel = this.ctx.model.Shop.Goods;
+    this.goodsSpecModel = this.ctx.model.Shop.GoodsSpec;
   }
   async update(filter, update, { projection } = {}) {
     assert(filter);
@@ -27,6 +29,28 @@ class GoodsSetService extends CrudService {
     reSearchData = await this.afterUpdate(filter, update, reSearchData);
     return reSearchData;
   }
+  /**
+   * 获取套装快照
+   * @param id 套装id
+   */
+  async getSnapshot(id) {
+    const data = await this.model.findById(id).lean();
+    for (const i of data) {
+      const { set = [] } = i;
+      for (const s of set) {
+        const { goods, spec } = s;
+        const goodsData = await this.goodsModel.findById(goods).lean();
+        const goodsSpecData = await this.goodsSpecModel.findById(spec).lean();
+        s.spec = goodsSpecData;
+        s.goods = goodsData;
+      }
+      // 换成goods
+      i.goods = set;
+      delete i.set;
+      delete i._id;
+    }
+    return data;
+  }
 }
 
 module.exports = GoodsSetService;

+ 56 - 24
app/service/trade/order.js

@@ -38,16 +38,25 @@ class OrderService extends CrudService {
       const user = this.ctx.user;
       const customer = _.get(user, '_id');
       if (!customer) throw new BusinessError(ErrorCode.NOT_LOGIN, '未找到用户信息');
-      const { address, goods, coupon = [], plus_goods = [], type = '0', inviter } = body;
+      const { address, goods, coupon = [], plus_goods = [], inviter } = body;
       if (coupon.length > 1) throw new BusinessError(ErrorCode.DATA_INVALID, '目前只允许使用1张优惠券');
       // 检测商品是否可以下单
+      const resetGoodsList = [];
       for (const i of goods) {
-        const { shop } = i;
-        for (const g of i.goods) {
-          const { goods_id: goods, goodsSpec_id: goodsSpec, num } = g;
-          const { result, msg } = await this.ctx.service.util.trade.checkCanBuy({ shop, goods, goodsSpec, num }, false);
+        const { shop, is_set = '1' } = i;
+        if (is_set === '1') {
+          // 非套装
+          for (const g of i.goods) {
+            const { goods_id: goods, goodsSpec_id: goodsSpec, num } = g;
+            const { result, msg } = await this.ctx.service.util.trade.checkCanBuy({ shop, goods, goodsSpec, num }, false);
+            if (!result) throw new BusinessError(ErrorCode.DATA_INVALID, msg);
+          }
+          resetGoodsList.push(i);
+        } else {
+          const { result, msg } = await this.ctx.service.util.trade.checkCanBuy(i, false);
           if (!result) throw new BusinessError(ErrorCode.DATA_INVALID, msg);
         }
+
       }
       const orderData = {};
       // 数据做快照处理
@@ -125,18 +134,23 @@ class OrderService extends CrudService {
       }
       // 退单分为 2 部分, 涉及价格不需要管.因为这是交钱之前的的操作,不涉及退款
       // 1.货物的库存
-      const { goods: shopGoods, total_detail } = order;
-      // 需要归还库存的商品规格列表:{_id:商品规格id,buy_num:购买的数量}
-      for (const sg of shopGoods) {
-        const { goods: goodsList } = sg;
-        const list = goodsList.map(i => _.pick(i, [ '_id', 'buy_num' ]));
-        for (const i of list) {
-          const goodsSpec = await this.goodsSpecModel.findById(i._id, { num: 1 });
-          if (!goodsSpec) continue;
-          const newNum = this.ctx.plus(goodsSpec.num, i.buy_num);
-          this.tran.update('GoodsSpec', i._id, { num: newNum });
+      const { goods: shopGoods, total_detail, is_set = '1' } = order;
+      if (is_set === '1') {
+        // 需要归还库存的商品规格列表:{_id:商品规格id,buy_num:购买的数量}
+        for (const sg of shopGoods) {
+          const { goods: goodsList } = sg;
+          const list = goodsList.map(i => _.pick(i, [ '_id', 'buy_num' ]));
+          for (const i of list) {
+            const goodsSpec = await this.goodsSpecModel.findById(i._id, { num: 1 });
+            if (!goodsSpec) continue;
+            const newNum = this.ctx.plus(goodsSpec.num, i.buy_num);
+            this.tran.update('GoodsSpec', i._id, { num: newNum });
+          }
         }
+      } else {
+        // 套装退库存
       }
+
       // 2.优惠券
       const { discount_detail } = total_detail;
       if (discount_detail) {
@@ -165,16 +179,34 @@ class OrderService extends CrudService {
   async dealGoodsNum(list) {
     for (const i of list) {
       for (const g of i.goods) {
-        const { _id, buy_num, cart_id } = g;
-        const goodsSpec = await this.goodsSpecModel.findById(_id);
-        const newNum = this.ctx.minus(goodsSpec.num, buy_num);
-        this.tran.update('GoodsSpec', _id, { num: newNum });
-        // 活动相关:
-        // 买赠:不一定非要处理赠品,赠品可能不在库中;
-        // 特价,满减/折:没关系
-        // 加价购,作为另一个商品出现,一样会减少库存,不需要处理
-        // 先不处理活动库存问题.先有逻辑可以做到
+        const { _id, buy_num, cart_id, is_set = '1', set_id, goods } = g;
+        if (is_set === '1') {
+          const goodsSpec = await this.goodsSpecModel.findById(_id);
+          const newNum = this.ctx.minus(goodsSpec.num, buy_num);
+          this.tran.update('GoodsSpec', _id, { num: newNum });
+          // 活动相关:
+          // 买赠:不一定非要处理赠品,赠品可能不在库中;
+          // 特价,满减/折:没关系
+          // 加价购,作为另一个商品出现,一样会减少库存,不需要处理
+          // 先不处理活动库存问题.先有逻辑可以做到
+
+        } else {
+          // 套装减库存
+          for (const i of goods) {
+            const { spec, set_num, single_stock, stock } = i;
+            if (single_stock === '1') {
+              const specData = await this.goodsSpecModel.findById(spec._id, { num: 1 }).lean();
+              const newNum = this.ctx.minus(specData.num, this.ctx.multiply(set_num, buy_num));
+              this.tran.update('GoodsSpec', spec._id, { num: newNum });
+            } else {
+              const newNum = this.ctx.minus(stock, buy_num);
+              this.tran.update('GoodsSet', set_id, { stock: newNum });
+            }
+
+          }
+        }
         if (cart_id) this.tran.remove('Cart', cart_id);
+
       }
     }
   }

+ 55 - 37
app/service/util/order.js

@@ -13,6 +13,7 @@ class OrderService extends CrudService {
     this.gjaModel = this.ctx.model.Shop.GoodsJoinAct;
     this.goodsModel = this.ctx.model.Shop.Goods;
     this.goodsSpecModel = this.ctx.model.Shop.GoodsSpec;
+    this.goodsSetService = this.ctx.service.shop.goodsSet;
   }
 
   // #region 下单前计算函数
@@ -90,18 +91,25 @@ class OrderService extends CrudService {
       let goods_total = 0,
         freight_total = 0,
         discount = 0;
-      for (const g of i.goods) {
-        // 如果有特价,那就使用特价,没有特价就是用正常销售价
-        goods_total = this.ctx.plus(goods_total, this.ctx.multiply(_.get(g, 'price'), _.get(g, 'buy_num')));
-        freight_total = this.ctx.plus(freight_total, this.ctx.multiply(_.get(g, 'freight'), _.get(g, 'num')));
-        if (_.isArray(g.act)) {
-          const actDiscount = g.act.reduce((p, n) => this.ctx.plus(p, n.discount), 0);
-          discount = this.ctx.plus(discount, actDiscount);
+      const { is_set = '1' } = i;
+      if (is_set === '1') {
+        for (const g of i.goods) {
+          // 如果有特价,那就使用特价,没有特价就是用正常销售价
+          goods_total = this.ctx.plus(goods_total, this.ctx.multiply(_.get(g, 'price'), _.get(g, 'buy_num')));
+          freight_total = this.ctx.plus(freight_total, this.ctx.multiply(_.get(g, 'freight'), _.get(g, 'num')));
+          if (_.isArray(g.act)) {
+            const actDiscount = g.act.reduce((p, n) => this.ctx.plus(p, n.discount), 0);
+            discount = this.ctx.plus(discount, actDiscount);
+          }
         }
+        i.goods_total = goods_total;
+        i.freight_total = freight_total;
+        i.discount = this.ctx.minus(0, discount);
+      } else {
+        const { sell_money, num, freight } = i;
+        i.goods_total = this.ctx.multiply(num, sell_money);
+        i.freight_total = this.ctx.multiply(num, freight);
       }
-      i.goods_total = goods_total;
-      i.freight_total = freight_total;
-      i.discount = this.ctx.minus(0, discount);
     }
     return list;
   }
@@ -209,36 +217,46 @@ class OrderService extends CrudService {
       goodsSpecs = [],
       goodsData = [];
     for (const i of goods) {
-      const { goods: goodsList, ...others } = i;
+      const { goods: goodsList, is_set = '1', set_id, ...others } = i;
       const qp = [];
-      for (const g of goodsList) {
-        const { goodsSpec_id, goods_id } = g;
-        // 需要的订单数据
-        const orderNeedData = _.pick(g, [ 'act', 'price', 'sp_price', 'num', 'cart_id', 'gift' ]);
-        if (orderNeedData.num) {
-          orderNeedData.buy_num = orderNeedData.num;
-          delete orderNeedData.num;
-        }
-        let goodsSpec = await this.goodsSpecModel.findById(goodsSpec_id);
-        if (!goodsSpec) continue;
-        goodsSpec = JSON.parse(JSON.stringify(goodsSpec));
-        const goods = await this.goodsModel.findById(goods_id);
-        if (goods) goodsSpec.goods = JSON.parse(JSON.stringify(goods));
-        goodsSpec = { ...goodsSpec, ...orderNeedData };
-        qp.push(goodsSpec);
-        const ogs = _.pick(goodsSpec, [ '_id', 'buy_num', 'sell_money', 'price' ]);
-        if (ogs._id) {
-          ogs.id = ogs._id;
-          delete ogs._id;
+      if (is_set === '1') {
+        for (const g of goodsList) {
+          const { goodsSpec_id, goods_id } = g;
+          // 需要的订单数据
+          const orderNeedData = _.pick(g, [ 'act', 'price', 'sp_price', 'num', 'cart_id', 'gift' ]);
+          if (orderNeedData.num) {
+            orderNeedData.buy_num = orderNeedData.num;
+            delete orderNeedData.num;
+          }
+          let goodsSpec = await this.goodsSpecModel.findById(goodsSpec_id);
+          if (!goodsSpec) continue;
+          goodsSpec = JSON.parse(JSON.stringify(goodsSpec));
+          const goods = await this.goodsModel.findById(goods_id);
+          if (goods) goodsSpec.goods = JSON.parse(JSON.stringify(goods));
+          goodsSpec = { ...goodsSpec, ...orderNeedData };
+          qp.push(goodsSpec);
+          const ogs = _.pick(goodsSpec, [ '_id', 'buy_num', 'sell_money', 'price' ]);
+          if (ogs._id) {
+            ogs.id = ogs._id;
+            delete ogs._id;
+          }
+          goodsSpecs.push({ ...ogs });
+          // 将活动提取出来:只需要满减/折;买赠和特价跟着规格走,计算过程中已经处理;加价购是外面处理
+          const { act = [] } = goodsSpec;
+          let gAct = act.filter(f => f.platform_act_type === '5' || f.platform_act_type === '6');
+          gAct = gAct.map(i => ({ ...i, goodsSpec_id: goodsSpec._id }));
+          actList.push(...gAct);
         }
-        goodsSpecs.push({ ...ogs });
-        // 将活动提取出来:只需要满减/折;买赠和特价跟着规格走,计算过程中已经处理;加价购是外面处理
-        const { act = [] } = goodsSpec;
-        let gAct = act.filter(f => f.platform_act_type === '5' || f.platform_act_type === '6');
-        gAct = gAct.map(i => ({ ...i, goodsSpec_id: goodsSpec._id }));
-        actList.push(...gAct);
+        goodsData.push({ ...others, goods: qp });
+      } else {
+        const data = await this.goodsSetService.getSnapshot(set_id);
+        data.cart_id = i.cart_id;
+        data.buy_num = i.num;
+        data.is_set = '0';
+        data.set_id = i.set_id;
+        goodsData.push(data);
       }
-      goodsData.push({ ...others, goods: qp });
+
     }
     return { actList, goodsSpecs, goodsData };
   }