lrf преди 2 години
родител
ревизия
d043954153
променени са 1 файла, в които са добавени 10 реда и са изтрити 4 реда
  1. 10 4
      app/service/trade/cart.js

+ 10 - 4
app/service/trade/cart.js

@@ -13,6 +13,8 @@ class CartService extends CrudService {
     this.platformActModel = this.ctx.model.System.PlatformAct;
     this.gjaModel = this.ctx.model.Shop.GoodsJoinAct;
     this.setModel = this.ctx.model.Shop.GoodsSet;
+    this.shopModel = this.ctx.model.Shop.Shop;
+    this.goodsModel = this.ctx.model.Shop.Goods;
   }
 
   /**
@@ -111,7 +113,7 @@ class CartService extends CrudService {
         data.num = buyNum;
         await data.save();
       } else {
-        const { enough, msg } = await this.checkSetGoodsNum(data, num);
+        const { enough, msg } = await this.checkSetGoodsNum(body, num);
         if (!enough) throw new BusinessError(ErrorCode.SERVICE_FAULT, msg);
         await this.model.create({ num, ...body });
       }
@@ -130,9 +132,11 @@ class CartService extends CrudService {
     const stockList = [];
     for (const sd of set) {
       const { shop, goods, spec, set_num } = sd;
-      const shopRes = this.ctx.service.util.trade.checkShop(shop);
+      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 goodsRes = this.ctx.service.util.trade.checkGoods(goods);
+      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);
@@ -140,7 +144,9 @@ class CartService extends CrudService {
         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;
-        stockList.push(gsnum);
+        // 应该用 库存/每套的件数 向下取整作为库存量
+        const stock_num = _.floor(this.ctx.divide(gsnum, set_num));
+        stockList.push(stock_num);
       }
     }
     if (single_stock !== '1') {