lrf 2 năm trước cách đây
mục cha
commit
0c50629390
1 tập tin đã thay đổi với 24 bổ sung31 xóa
  1. 24 31
      app/service/util/trade.js

+ 24 - 31
app/service/util/trade.js

@@ -85,18 +85,25 @@ class TradeService extends CrudService {
    * @param {Object} data 购物车/直接购买 数据对象
    */
   async checkSetCanBuy(data) {
-    const { set_id, num } = data;
-    const setData = await this.setModel.findById(set_id).lean();
+    const { set_id, num, shop } = data;
     let result = { result: true };
-    const { set, single_stock = '1', stock, is_use } = setData;
+    if (!shop) {
+      result.result = false;
+      result.msg = '缺少店铺信息';
+      return result;
+    }
+    // 1.检查商店是否正常运行
+    const shopData = await this.shopModel.findById(shop);
+    const shopRes = this.checkShop(shopData);
+    if (shopRes.result !== true) {
+      result = shopRes;
+      return result;
+    }
+    const setData = await this.setModel.findById(set_id).lean();
+    const { set, is_use } = setData;
     if (is_use === '1') return { result: false, msg: '套装已下架' };
     for (const sd of set) {
-      const { shop, goods, spec, set_num } = sd;
-      if (!shop) {
-        result.result = false;
-        result.msg = '缺少店铺信息';
-        break;
-      }
+      const { goods, spec, set_num } = sd;
       if (!goods) {
         result.result = false;
         result.msg = '缺少商品信息';
@@ -112,13 +119,6 @@ class TradeService extends CrudService {
         result.msg = '缺少购买数量';
         break;
       }
-      // 1.检查商店是否正常运行
-      const shopData = await this.shopModel.findById(shop);
-      const shopRes = this.checkShop(shopData);
-      if (shopRes.result !== true) {
-        result = shopRes;
-        break;
-      }
       // 2.检查商品是否可以购买
       const goodsData = await this.goodsModel.findById(goods);
       const goodsRes = this.checkGoods(goodsData);
@@ -126,24 +126,17 @@ class TradeService extends CrudService {
         result = goodsRes;
         break;
       }
-
       // 3.根据套装库存设置判断是走各个商品库存还是走套装库存
-      if (single_stock === '1') {
-        // 走各个商品库存
-        const goodsSpecData = await this.goodsSpecModel.findById(spec);
-        // 单独计算下商品数量 = 购买数量 * 组成套装的数量
-        const setGoodsNum = this.ctx.multiply(num, set_num);
-        const gsRes = this.checkGoodsSpec(goodsSpecData, setGoodsNum, '0');
-        if (gsRes.result !== true) {
-          result = gsRes;
-          break;
-        }
+      // 走各个商品库存
+      const goodsSpecData = await this.goodsSpecModel.findById(spec);
+      // 单独计算下商品数量 = 购买数量 * 组成套装的数量
+      const setGoodsNum = this.ctx.multiply(num, set_num);
+      const gsRes = this.checkGoodsSpec(goodsSpecData, setGoodsNum, '0');
+      if (gsRes.result !== true) {
+        result = gsRes;
+        break;
       }
     }
-    if (single_stock !== '1') {
-      // 走单独的库存
-      if (this.ctx.minus(stock, num) < 0) result = { result: false, msg: '套装库存不足' };
-    }
     return result;
   }