lrf 2 jaren geleden
bovenliggende
commit
1e478007b6
1 gewijzigde bestanden met toevoegingen van 19 en 27 verwijderingen
  1. 19 27
      app/service/trade/cart.js

+ 19 - 27
app/service/trade/cart.js

@@ -32,7 +32,7 @@ class CartService extends CrudService {
     let data = await this.model.find({ customer });
     if (data.length > 0) {
       data = JSON.parse(JSON.stringify(data));
-      data = data.map(i => ({ ...i, cart_id: i._id }));
+      data = data.map((i) => ({ ...i, cart_id: i._id }));
     }
     data = await this.getCartGoodsAct(data);
     const actList = await this.ctx.service.trade.order.getActList(data);
@@ -44,10 +44,10 @@ class CartService extends CrudService {
    * @param {Array} data 购物车商品
    */
   async getCartGoodsAct(data) {
-    data = data.map(i => ({ ...i, act: [] }));
+    data = data.map((i) => ({ ...i, act: [] }));
     // 实时匹配活动问题
     const platformActList = await this.platformActModel.find({ is_use: '0' });
-    const platform_act = platformActList.map(i => ObjectId(i._id).toString());
+    const platform_act = platformActList.map((i) => ObjectId(i._id).toString());
     for (const cart of data) {
       const { goodsSpec: spec_id, goods: goods_id, act = [], is_set = '1', set_id } = cart;
       if (is_set === '1' || !set_id) {
@@ -88,7 +88,7 @@ class CartService extends CrudService {
     } else assert(set_id, '缺少套装id');
     if (is_set === '1') {
       // 非套装 添加购物车
-      const query = _.pick(body, [ 'customer', 'shop', 'goods', 'goodsSpec' ]);
+      const query = _.pick(body, ['customer', 'shop', 'goods', 'goodsSpec']);
       const data = await this.model.findOne(query);
       if (data) {
         const buyNum = this.ctx.plus(data.num, num);
@@ -103,7 +103,7 @@ class CartService extends CrudService {
       }
     } else {
       // 套装添加购物车
-      const query = _.pick(body, [ 'customer', 'set_id' ]);
+      const query = _.pick(body, ['customer', 'set_id']);
       const data = await this.model.findOne(query);
       if (data) {
         // 计算套装购买数量
@@ -128,31 +128,25 @@ class CartService extends CrudService {
   async checkSetGoodsNum(data, buyNum) {
     const { set_id } = data;
     const setData = await this.setModel.findById(set_id).lean();
-    const { set, single_stock = '1', stock } = setData;
+    const { shop } = setData;
+    const shopData = await this.shopModel.findById(shop);
+    const shopRes = this.ctx.service.util.trade.checkShop(shopData);
+    if (shopRes.result !== true) throw new BusinessError(ErrorCode.DATA_INVALID, shopRes.msg);
+    const { set } = setData;
     const stockList = [];
     for (const sd of set) {
-      const { shop, goods, spec, set_num } = sd;
-      const shopData = await this.shopModel.findById(shop);
-      const shopRes = this.ctx.service.util.trade.checkShop(shopData);
-      if (shopRes.result !== true) throw new BusinessError(ErrorCode.DATA_INVALID, shopRes.msg);
+      const { goods, spec, set_num } = sd;
       const goodsData = await this.goodsModel.findById(goods);
       const goodsRes = this.ctx.service.util.trade.checkGoods(goodsData);
       if (goodsRes.result !== true) throw new BusinessError(ErrorCode.DATA_INVALID, goodsRes.msg);
-      if (single_stock === '1') {
-        const goodsSpecData = await this.goodsSpecModel.findById(spec);
-        const setGoodsNum = this.ctx.multiply(buyNum, set_num);
-        const gsRes = this.ctx.service.util.trade.checkGoodsSpec(goodsSpecData, setGoodsNum, '0');
-        if (gsRes.result !== true) throw new BusinessError(ErrorCode.DATA_INVALID, gsRes.msg);
-        const { num: gsnum = 0 } = goodsSpecData;
-        // 应该用 库存/每套的件数 向下取整作为库存量
-        const stock_num = _.floor(this.ctx.divide(gsnum, set_num));
-        stockList.push(stock_num);
-      }
-    }
-    if (single_stock !== '1') {
-      // 走单独的库存
-      if (this.ctx.minus(stock, buyNum) < 0) throw new BusinessError(ErrorCode.DATA_INVALID, '套装库存不足');
-      stockList.push(stock);
+      const goodsSpecData = await this.goodsSpecModel.findById(spec);
+      const setGoodsNum = this.ctx.multiply(buyNum, set_num);
+      const gsRes = this.ctx.service.util.trade.checkGoodsSpec(goodsSpecData, setGoodsNum, '0');
+      if (gsRes.result !== true) throw new BusinessError(ErrorCode.DATA_INVALID, gsRes.msg);
+      const { num: gsnum = 0 } = goodsSpecData;
+      // 应该用 库存/每套的件数 向下取整作为库存量
+      const stock_num = _.floor(this.ctx.divide(gsnum, set_num));
+      stockList.push(stock_num);
     }
     const total = _.min(stockList);
     return { enough: true, total };
@@ -195,8 +189,6 @@ class CartService extends CrudService {
       await this.model.updateOne({ _id: cartId }, { num });
     }
     return { enough: true, total: gsnum };
-
-
   }
 }