lrf 2 years ago
parent
commit
861fefae9f
4 changed files with 44 additions and 40 deletions
  1. 11 13
      app/service/shop/goodsSet.js
  2. 26 23
      app/service/trade/order.js
  3. 3 3
      app/service/util/order.js
  4. 4 1
      app/service/util/trade.js

+ 11 - 13
app/service/shop/goodsSet.js

@@ -35,20 +35,18 @@ class GoodsSetService extends CrudService {
    */
   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;
+    const { set = [] } = data;
+    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
+    data.goods = set;
+    delete data.set;
+    delete data._id;
     return data;
   }
 }

+ 26 - 23
app/service/trade/order.js

@@ -56,7 +56,6 @@ class OrderService extends CrudService {
           const { result, msg } = await this.ctx.service.util.trade.checkCanBuy(i, false);
           if (!result) throw new BusinessError(ErrorCode.DATA_INVALID, msg);
         }
-
       }
       const orderData = {};
       // 数据做快照处理
@@ -104,7 +103,7 @@ class OrderService extends CrudService {
       if (coupon.length > 0) await this.ctx.service.user.userCoupon.useCoupon(coupon, this.tran);
       await this.tran.run();
       // 创建定时任务(mq死信机制任务)
-      await this.toMakeTask(order_id);
+      // await this.toMakeTask(order_id);
       return order_id;
     } catch (error) {
       await this.tran.rollback();
@@ -178,36 +177,40 @@ class OrderService extends CrudService {
    */
   async dealGoodsNum(list) {
     for (const i of list) {
-      for (const g of i.goods) {
-        const { _id, buy_num, cart_id, is_set = '1', set_id, goods } = g;
-        if (is_set === '1') {
+      const { is_set = '1' } = i;
+      if (is_set === '1') {
+        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 });
+          if (cart_id) {
+            const num = await this.cartModel.count({ _id: cart_id });
+            if (num > 0) { this.tran.remove('Cart', cart_id); }
+          }
           // 活动相关:
           // 买赠:不一定非要处理赠品,赠品可能不在库中;
           // 特价,满减/折:没关系
           // 加价购,作为另一个商品出现,一样会减少库存,不需要处理
           // 先不处理活动库存问题.先有逻辑可以做到
-
-        } 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 });
-            }
-
+        }
+      } else {
+        // 套装减库存
+        const { goods, buy_num, cart_id } = i;
+        for (const i of goods) {
+          const { spec, set_num } = i;
+          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 });
+        }
+        if (cart_id) {
+          const num = await this.cartModel.count({ _id: cart_id });
+          if (num > 0) {
+            this.tran.remove('Cart', cart_id);
           }
         }
-        if (cart_id) this.tran.remove('Cart', cart_id);
-
       }
+
     }
   }
 
@@ -433,13 +436,13 @@ class OrderService extends CrudService {
         const setData = await this.setModel.findById(set_id).lean();
         const { _id, set = [], sell_money, name, freight } = setData;
         const newSet = [];
-        const obj = { _id, name, sell_money, is_set: '0', num, cart_id, freight, shop };
+        const obj = { _id, set_id: _id, name, sell_money, is_set: '0', num, cart_id, freight, shop };
         for (const s of set) {
           const { goods, goods_name, spec, spec_name, set_num } = s;
           const goodsData = await this.goodsModel.findById(goods, { file: 1 }).lean();
           const specData = await this.goodsSpecModel.findById(spec, { file: 1 }).lean();
           const file = [ ..._.get(specData, 'file', []), ..._.get(goodsData, 'file', []) ];
-          const newSetData = { goods_name, spec_name, file, set_num };
+          const newSetData = { goods_name, goods_id: goods, spec_name, file, set_num };
           newSet.push(newSetData);
         }
         obj.goods = newSet;

+ 3 - 3
app/service/util/order.js

@@ -106,9 +106,9 @@ class OrderService extends CrudService {
         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);
+        const { sell_money, buy_num, freight } = i;
+        i.goods_total = this.ctx.multiply(buy_num, sell_money);
+        i.freight_total = this.ctx.multiply(buy_num, freight);
       }
     }
     return list;

+ 4 - 1
app/service/util/trade.js

@@ -85,7 +85,10 @@ class TradeService extends CrudService {
    * @param {Object} data 购物车/直接购买 数据对象
    */
   async checkSetCanBuy(data) {
-    const { set_id, num, shop } = data;
+    const { num, shop } = data;
+    // 取set_id是在购物车检查是否可以购买; data._id是下单时使用
+    // 已经将购物车那边的数据加上了set_id,不过先这么写,兼容
+    const set_id = _.get(data, 'set_id', data._id);
     let result = { result: true };
     if (!shop) {
       result.result = false;