فهرست منبع

购物车内商品自动检测活动

lrf 2 سال پیش
والد
کامیت
afc384a4de
2فایلهای تغییر یافته به همراه25 افزوده شده و 19 حذف شده
  1. 24 19
      app/service/trade/cart.js
  2. 1 0
      app/service/trade/order.js

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

@@ -10,6 +10,8 @@ class CartService extends CrudService {
     super(ctx, 'cart');
     this.model = this.ctx.model.Trade.Cart;
     this.goodsSpecModel = this.ctx.model.Shop.GoodsSpec;
+    this.platformActModel = this.ctx.model.System.PlatformAct;
+    this.gjaModel = this.ctx.model.Shop.GoodsJoinAct;
   }
 
   /**
@@ -29,31 +31,35 @@ class CartService extends CrudService {
       data = JSON.parse(JSON.stringify(data));
       data = data.map(i => ({ ...i, cart_id: i._id }));
     }
+    data = await this.getCartGoodsAct(data);
     const actList = await this.ctx.service.trade.order.getActList(data);
     const pageData = await this.ctx.service.trade.order.getPageData(data, actList);
     return pageData;
   }
-
   /**
-   * 重组购物车商品
-   * @param {Object} goods 商品
-   * @param {Object} goodsSpec 商品规格
-   * @param {Object} data 购物车数据
+   * 实时获取购物车内的商品参与的活动
+   * @param {Array} data 购物车商品
    */
-  setCartGoodsData(data) {
-    const { goods, goodsSpec, _id, num = 1 } = data;
-    const obj = {};
-    obj.cart_id = _id;
-    obj.goods_id = _.get(goods, '_id');
-    obj.goods_name = _.get(goods, 'name');
-    obj.goodsSpec_id = _.get(goodsSpec, '_id');
-    obj.goodsSpec_name = _.get(goodsSpec, 'name');
-    obj.money = _.get(goodsSpec, 'sell_money');
-    obj.num = num;
-    obj.file = _.get(goods, 'file');
-    return obj;
+  async getCartGoodsAct(data) {
+    // 实时匹配活动问题
+    const platformActList = await this.platformActModel.find({ is_use: '0' });
+    for (const cart of data) {
+      const { goodsSpec: spec } = cart;
+      const actList = [];
+      for (const act of platformActList) {
+        const { _id: platform_act } = act;
+        const gjaNum = await this.gjaModel.count({
+          platform_act,
+          'spec._id': spec,
+          $or: [{ platform_act_type: '4', 'config.goods_type': 'basic' }, { platform_act_type: { $ne: '4' } }],
+        });
+        if (gjaNum > 0) actList.push(platform_act);
+      }
+      cart.act = actList;
+    }
   }
 
+
   /**
    ** 创建购物车信息,若找到该店的该商品规格.进行库存校验
    ** 数量进行合并;
@@ -66,7 +72,7 @@ class CartService extends CrudService {
    * @param body.num 数量
    */
   async create({ num, ...body }) {
-    const { customer, shop, goods, goodsSpec, act } = body;
+    const { customer, shop, goods, goodsSpec } = body;
     assert(customer, '缺少顾客信息');
     assert(shop, '缺少店铺信息');
     assert(goods, '缺少商品信息');
@@ -78,7 +84,6 @@ class CartService extends CrudService {
       const { enough } = await this.checkGoodsNum({ goodsSpecId: goodsSpec, num: buyNum });
       if (!enough) throw new BusinessError(ErrorCode.SERVICE_FAULT, '库存不足,无法添加至购物车');
       data.num = buyNum;
-      if (act) data.act = act;
       await data.save();
     } else {
       const { enough } = await this.checkGoodsNum({ goodsSpecId: goodsSpec, num });

+ 1 - 0
app/service/trade/order.js

@@ -199,6 +199,7 @@ class OrderService extends CrudService {
         carts = JSON.parse(JSON.stringify(carts));
         carts = carts.map(i => ({ ...i, cart_id: i._id }));
       }
+      await this.ctx.service.trade.cart.getCartGoodsAct(carts);
       data = carts;
     }
     const { result, msg } = await this.ctx.service.util.trade.checkCanBuy(data, false);