lrf пре 2 година
родитељ
комит
de7089ed4a

+ 2 - 0
app/model/system/platformAct.js

@@ -26,6 +26,8 @@ const config = {
   time_end: { type: String, zh: '结束时间' }, // 死信机制,用来限时关闭活动
   // 满减部分, 只有满减和满折在平台活动设置,剩下的都是和商品有关的
   discount: { type: [ manJian ] || [ manZhe ], zh: '满减档位' },
+  // 加价购部分
+  plus_money: { type: Number, zh: '加价购下限金额' },
 };
 // 平台活动
 const platformAct = {

+ 0 - 6
app/model/trade/cart.js

@@ -1,12 +1,6 @@
 'use strict';
 const Schema = require('mongoose').Schema;
 const metaPlugin = require('naf-framework-mongoose-free/lib/model/meta-plugin');
-const act = {
-  // 套装: 下单时组合,购物车中不需要额外处理
-  platform_act: { type: String, zh: '活动id' },
-  set_id: { type: String, zh: '参加套装活动的商品组合数据id' },
-
-};
 // 购物车
 const cart = {
   customer: { type: String, required: false, zh: '顾客', ref: 'User.User' }, //

+ 1 - 0
app/service/system/platformAct.js

@@ -59,6 +59,7 @@ class PlatformActService extends CrudService {
       }
     } else if (type === '4') {
       tag = '加价购';
+
     } else if (type === '5') {
       tag = '满减';
       const discount = _.get(data, 'config.discount', []);

+ 78 - 77
app/service/trade/order.js

@@ -18,6 +18,8 @@ class OrderService extends CrudService {
     this.addressModel = this.ctx.model.User.Address;
     this.cartModel = this.ctx.model.Trade.Cart;
     this.userCouponModel = this.ctx.model.User.UserCoupon;
+    this.platformActModel = this.ctx.model.System.PlatformAct;
+    this.gjaModel = this.ctx.model.Shop.GoodsJoinAct;
     this.tran = new Transaction();
   }
 
@@ -202,13 +204,14 @@ class OrderService extends CrudService {
     if (!_.isArray(data)) throw new BusinessError(ErrorCode.DATA_INVALID, '数据不正确,请重新下单');
     const head = _.head(data);
     if (_.isString(head)) {
-      // 购物车来的
-    } else if (_.isObject(head)) {
-      // 直接购买来的
-      const { result, msg } = await this.ctx.service.util.trade.checkCanBuy(data, false);
-      if (!result) throw new BusinessError(ErrorCode.DATA_INVALID, msg);
-      specsData = await this.getPageData(data);
-    } else throw new BusinessError(ErrorCode.DATA_INVALID, '数据不正确,请重新下单');
+      // 购物车来的,将购物车中的数据拿出来转换下
+      const carts = await this.cartModel.find({ _id: data });
+      data = carts;
+    }
+    const { result, msg } = await this.ctx.service.util.trade.checkCanBuy(data, false);
+    if (!result) throw new BusinessError(ErrorCode.DATA_INVALID, msg);
+    // 正常整理商品的内容
+    specsData = await this.getPageData(data);
     // 组装页面的数据
     const user = this.ctx.user;
     const customer = _.get(user, '_id');
@@ -227,9 +230,74 @@ class OrderService extends CrudService {
     const couponList = await this.ctx.service.user.userCoupon.toMakeOrder_getList(specsData);
     pageData.couponList = couponList;
     // 返现部分:添加推荐人信息
-    pageData.inviter = _.get(data, 'inviter');
+    const inviter = data.find(f => ObjectId.isValid(f.inviter));
+    pageData.inviter = inviter;
+    // 活动部分
+    // 每个数据中都有act字段.这个字段表明商品参加了哪些活动
+    // await this.dealAct();
     return pageData;
   }
+  /**
+   * 处理该订单活动部分
+   * * 该商品可能会参加多个活动,活动之间有叠加问题
+   * * 买赠:没关系,只要去找赠品就行
+   * * 特价:需要找到特价,将价格更改为特价
+   * * 加价购: 满足下限金额时,组织数据,允许前端进行加价购
+   * * 满减/折: 针对整个订单而言. 满足就处理,不满足就是不处理
+   * * 套装:暂不处理
+   * * ps: 特价与满减/折叠加, 按特价计算总价再看满减/折; 加价购不在满减/折的金额下限计算范围内
+   * @param {Array} data 购物车数据(直接购买也会组织成购物车数据)
+   */
+  async dealAct(data) {
+    const actList = [];
+    for (const i of data) {
+      const { act = [], spec } = i;
+      if (act.length <= 0) continue;
+      for (const a of act) {
+        const platformAct = await this.platformActModel.findById(a);
+        // 没有找到活动,略过
+        if (!platformAct) continue;
+        // 活动未开启,略过
+        if (_.get(platformAct, 'is_use') !== '0') continue;
+        // 活动类型为 0&1不需要处理
+        const type = _.get(platformAct, 'type');
+        if (type === '1' || type === '0') continue;
+        // 先找下是否处于活动设置的程序时间
+        const start = _.get(platformAct, 'config.time_start');
+        const end = _.get(platformAct, 'config.time_end');
+        const r = moment().isBetween(start, end, null, '[]');
+        // 不在程序设定的活动时间内,下一个
+        if (!r) continue;
+        // 有关最后活动总结数据问题:
+        // 1.买赠:需要具体到规格
+        // 2.特价:需要具体到规格
+        // 3.加价购:需要和商品一起进行判断
+        // 4&5:满减/折:需要和商品一起判断
+        // 6.套装:先不考虑
+        if (type === '2') {
+          // 买赠,直接去到活动中找到赠品
+          const gja = await this.gjaModel.findOne({ platform_act: platformAct._id, 'spec._id': spec }, { config: 1 });
+          const gift = _.get(gja, 'config.gift', []);
+          actList.push({ platform_act_type: type, spec, gift });
+        } else if (type === '3') {
+          // 特价,找出特价
+          const gja = await this.gjaModel.findOne({ platform_act: platformAct._id, 'spec._id': spec }, { config: 1 });
+          const sp_price = _.get(gja, 'config.sp_price');
+        } else if (type === '4') {
+          // 加价购,找出加价购下限;如果判断下限够了,那就可以让前端去加价购
+          const plus_money = _.get(platformAct, 'config.plus_money', 0);
+        } else if (type === '5') {
+          // 满减
+          const discount = _.get(platformAct, 'discount', []);
+        } else if (type === '6') {
+          // 满折
+          const discount = _.get(platformAct, 'discount', []);
+        } else if (type === '7') {
+          // 套装先不考虑
+        }
+      }
+    }
+  }
 
   // 直接购买&购物车,这俩字段基本没差, 组织订单页商品数据
   async getPageData(data) {
@@ -238,6 +306,7 @@ class OrderService extends CrudService {
       const { goodsSpec, num } = i;
       const d = await this.goodsSpecModel.aggregate([
         { $match: { _id: ObjectId(goodsSpec) } },
+        // #region 处理店铺与商品部分
         { $addFields: { goods_id: { $toObjectId: '$goods' } } },
         {
           $lookup: {
@@ -261,6 +330,7 @@ class OrderService extends CrudService {
           },
         },
         { $unwind: '$goods' },
+        // #endregion
         {
           $project: {
             _id: 0,
@@ -294,75 +364,6 @@ class OrderService extends CrudService {
     return result;
   }
 
-  /**
-   * 单商品整理数据,剩下的可以简略
-   * @param {Array} list 规格数组
-   * @param num 购买数量
-   */
-  setGoodsToPageData(list, num) {
-    // 按店铺分组,精简字段
-    const arr = [];
-    for (const i of list) {
-      const obj = {};
-      obj.shop_name = _.get(i.goods, 'shop.name');
-      obj.shop = _.get(i.goods, 'shop._id');
-      const goods = {};
-      goods.goods_id = _.get(i.goods, '_id');
-      goods.goods_name = _.get(i.goods, 'name');
-      goods.goodsSpec_id = _.get(i, '_id');
-      goods.goodsSpec_name = _.get(i, 'name');
-      goods.freight = _.get(i, 'freight');
-      goods.money = _.get(i, 'sell_money');
-      goods.num = num;
-      goods.file = _.get(i.goods, 'file');
-      goods.tags = _.get(i.goods, 'tags');
-      // #region 团购部分
-      // 团购价
-      goods.group_sell_money = _.get(i, 'group_config.money');
-      // #endregion
-      obj.goods = [ goods ];
-      arr.push(obj);
-    }
-    return arr;
-  }
-  /**
-   *购物车数据整理
-   * @param {Array} list 规格数组
-   */
-  setCartsGoodsToPageData(list) {
-    const arr = [];
-    list = _.groupBy(list, 'shop._id');
-    for (const key in list) {
-      const data = list[key];
-      const shopData = _.get(_.head(data), 'shop');
-      const obj = {};
-      obj.shop = _.get(shopData, '_id');
-      obj.shop_name = _.get(shopData, 'name');
-      const goodsList = [];
-      for (const i of data) {
-        const goods = {};
-        goods.cart_id = _.get(i, '_id');
-        goods.goods_id = _.get(i.goods, '_id');
-        goods.goods_name = _.get(i.goods, 'name');
-        goods.goodsSpec_id = _.get(i.goodsSpec, '_id');
-        goods.goodsSpec_name = _.get(i.goodsSpec, 'name');
-        goods.freight = _.get(i.goodsSpec, 'freight');
-        goods.money = _.get(i.goodsSpec, 'sell_money');
-        goods.num = _.get(i, 'num');
-        goods.file = _.get(i.goods, 'file', []);
-        goods.tags = _.get(i.goods, 'tags');
-        // #region 团购部分
-        // 团购价
-        goods.group_sell_money = _.get(i.goodsSpec, 'group_config.money');
-        // #endregion
-        goodsList.push(goods);
-      }
-      obj.goods = goodsList;
-      arr.push(obj);
-    }
-    return arr;
-  }
-
   async afterQuery(filter, data) {
     data = JSON.parse(JSON.stringify(data));
     for (const i of data) {

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

@@ -15,7 +15,7 @@ class OrderService extends CrudService {
   /**
    * 计算每个店铺的价格
    * @param {Array} list 按店铺分组的商品列表
-   * list是经过 setCartsGoodsToPageData/setGoodsToPageData 处理过的数据
+   * list是经过 getPageData 处理过的数据
    * @param {String} type 订单类型码: 0常规单,1团购单
    */
   makeOrder_computedShopTotal(list, type = '0') {
@@ -23,7 +23,7 @@ class OrderService extends CrudService {
     if (type === '1') sell_money_key = 'group_sell_money';
     else sell_money_key = 'sell_money';
     for (const i of list) {
-      i.goods = i.goods.map(e => ({ ...e, price: _.get(e, sell_money_key) }));
+      i.goods = i.goods.map((e) => ({ ...e, price: _.get(e, sell_money_key) }));
       i.goods_total = i.goods.reduce((p, n) => this.ctx.plus(p, this.ctx.multiply(_.get(n, sell_money_key), n.num)), 0);
       i.freight_total = i.goods.reduce((p, n) => this.ctx.plus(p, this.ctx.multiply(n.freight, n.num)), 0);
     }
@@ -32,7 +32,7 @@ class OrderService extends CrudService {
   /**
    * 计算整个订单的价格
    * @param {Array} list 按店铺分组的商品列表
-   * list是经过 setCartsGoodsToPageData/setGoodsToPageData 处理过的数据
+   * list是经过 getPageData 处理过的数据
    */
   makerOrder_computedOrderTotal(list) {
     const obj = {
@@ -57,7 +57,7 @@ class OrderService extends CrudService {
     // 解除店铺层
     const sd = Object.values(detail);
     // 取出规格层
-    const sgd = sd.map(i => Object.values(i));
+    const sgd = sd.map((i) => Object.values(i));
     // 将规格明细降维至一维
     const oneLevel = _.flattenDeep(sgd);
     // 根据订单类型,计算应付(优惠券部分已经按订单类型计算并分配完了.这地方只是复现)