lrf 2 years ago
parent
commit
2c013bd616
3 changed files with 53 additions and 32 deletions
  1. 14 3
      app/service/trade/order.js
  2. 8 4
      app/service/user/userCoupon.js
  3. 31 25
      app/service/util/order.js

+ 14 - 3
app/service/trade/order.js

@@ -36,7 +36,7 @@ class OrderService extends CrudService {
       const user = this.ctx.user;
       const customer = _.get(user, '_id');
       if (!customer) throw new BusinessError(ErrorCode.NOT_LOGIN, '未找到用户信息');
-      const { address, goods, total_detail, coupon = [], plus_goods = [], type = '0', inviter } = body;
+      const { address, goods, coupon = [], plus_goods = [], type = '0', inviter } = body;
       if (coupon.length > 1) throw new BusinessError(ErrorCode.DATA_INVALID, '目前只允许使用1张优惠券');
       // 检测商品是否可以下单
       for (const i of goods) {
@@ -49,6 +49,7 @@ class OrderService extends CrudService {
       }
       const orderData = {};
       const goodsSpecs = [];
+      const actList = [];
       // 数据做快照处理
       // 1.地址快照
       const addressData = await this.addressModel.findById(address._id);
@@ -80,9 +81,19 @@ class OrderService extends CrudService {
             delete ogs._id;
           }
           goodsSpecs.push({ ...ogs, type });
+          // 将活动提取出来:只需要满减/折;买赠和特价跟着规格走,计算过程中已经处理;加价购是外面处理
+          const { act = [] } = goodsSpec;
+          let gAct = act.filter(f => f.platform_act_type === '5' || f.platform_act_type === '6');
+          gAct = gAct.map(i => ({ ...i, goodsSpec_id: goodsSpec._id }));
+          actList.push(...gAct);
         }
         goodsData.push({ ...others, goods: qp });
       }
+      const total_detail = {
+        goods_total: goodsData.reduce((p, n) => this.ctx.plus(p, n.goods_total), 0),
+        freight_total: goodsData.reduce((p, n) => this.ctx.plus(p, n.freight_total), 0),
+        act: actList,
+      };
       // 3.商品总计明细.
       // 计算优惠券的明细
       const discountDetail = await this.ctx.service.user.userCoupon.computedOrderCouponDiscount(coupon, goodsSpecs);
@@ -234,8 +245,8 @@ class OrderService extends CrudService {
     const inviter = data.find(f => ObjectId.isValid(f.inviter));
     pageData.inviter = inviter;
     // 活动部分
-    // 将加价购拿出来,给前端,需要特殊处理
-    pageData.actList = actList.filter(f => f.platform_act_type === '4');
+    // 将加价购拿出来,给前端,需要特殊处理;
+    pageData.actList = actList.filter(f => f.platform_act_type === '4' && f.activity);
     // 满减/折 直接放在orderTotal中,作为明细,反正后面也不用; 只有discount为数字(金额)的情况,说明满足该满减/折的优惠,数组是不满足的
     const daList = actList.filter(f => (f.platform_act_type === '5' || f.platform_act_type === '6') && _.isNumber(f.discount));
     for (const da of daList) {

+ 8 - 4
app/service/user/userCoupon.js

@@ -246,6 +246,7 @@ class UserCouponService extends CrudService {
     const goodsSpecIds = goodsSpecs.map(i => i.id);
     const { populate: gp } = this.ctx.service.shop.goodsSpec.getRefMods();
     let goodsSpecList = await this.goodsSpecModel.find({ _id: goodsSpecIds }).populate(gp);
+    // 新价格和新整理的数据结合
     goodsSpecList = this.resetGoodsSpecData(goodsSpecList, goodsSpecs);
     const { populate: cp } = this.getRefMods();
     let userCouponList = await this.model.find({ _id: couponIds }, { customer: 0 }).populate(cp);
@@ -396,10 +397,10 @@ class UserCouponService extends CrudService {
    * 整理商品规格列表数据
    * 将goods,shop和goodsSpec重组为3个属性在object同一级上
    * @param {Array} list 商品规格列表
-   * @param {Array} orderInfo 有关订单的信息 {id,buy_num}
+   * @param {Array} orderInfo 有关订单的信息 {id,buy_num,sell_money,price}
    */
   resetGoodsSpecData(list, orderInfo) {
-    const priceKey = 'sell_money';
+    const priceKey = 'price';
     const nouse = [ 'meta', '__v' ];
     const arr = list.map(i => {
       const obj = {};
@@ -410,11 +411,14 @@ class UserCouponService extends CrudService {
       obj.shop = shop;
       obj.goods = goods;
       const r = orderInfo.find(f => ObjectId(f.id).equals(goodsSpec._id));
-      if (r) obj.buy_num = r.buy_num;
+      // 将变化的价格赋上去:传来价格变动就用新的价格;没有就用设置的销售价
+      if (r) {
+        obj.buy_num = r.buy_num;
+        obj.goodsSpec.price = r.price;
+      } else obj.goodsSpec.price = obj.sell_money;
       if (obj.buy_num) {
         // 计算 常规/团购 商品总价
         obj.goods_total = this.ctx.multiply(obj.buy_num, _.get(obj.goodsSpec, priceKey));
-
       }
       return obj;
     });

+ 31 - 25
app/service/util/order.js

@@ -59,9 +59,7 @@ class OrderService extends CrudService {
    * @return {Number} 订单实付金额
    */
   payOrder_RealPay(order) {
-    let priceKey;
-    if (_.get(order, 'type', '0') === '1') priceKey = 'ggrp';
-    else priceKey = 'grp';
+    const priceKey = 'price';
     const detail = this.moneyDetail(order);
     // 解除店铺层
     const sd = Object.values(detail);
@@ -124,32 +122,38 @@ class OrderService extends CrudService {
    * 按店铺-商品 计算该订单的价格明细
    * @param {Object} data 支付订单数据
    * @return {Object} 返回:{
-   *  店铺id:{
-   *    规格id:{
-   *      sm: 规格正常销售价格(sell_money),
-   *      f: 规格运费(freight),
-   *      bn: 购买数量(buy_num),
-   *      st: 规格正常销售总价(sell_total: sm * bn)
-   *      ft: 规格运费总价(freight_total: f * bn)
-   *      gt: 商品支付原价(goods_total: st + ft)
-   *      dd: {
-   *        key:优惠券id,
-   *        value:优惠价格
-   *      },
-   *      dt: 优惠总价(d_detail的value之和)
-   *      grp:商品实际支付价格(goods_real_pay: gt - dt)
-   *      gsm:规格团购销售价格(group_sell_money)
-   *      gst: 规格团购销售总价(group_sell_total: gsm * bn)
-   *      ggt: 商品团购支付原价(goods_group_total: gst + ft)
-   *      ggrp: 商品团购实际支付价格(goods_group_real_pay: ggt - dt)
-   *    }
-   *  }
-   * }
+   **  店铺id:{
+   **    规格id:{
+   **      sm: 规格正常销售价格(sell_money),
+   **      f: 规格运费(freight),
+   **      bn: 购买数量(buy_num),
+   **      st: 规格正常销售总价(sell_total: sm * bn)
+   **      ft: 规格运费总价(freight_total: f * bn)
+   **      gt: 商品支付原价(goods_total: st + ft)
+   **      dd: {
+   **        key:优惠券id,
+   **        value:优惠价格
+   **      },
+   **      dt: 优惠总价(d_detail的value之和)
+   **      ad: [{
+   **         money:活动优惠金额(负数),
+   **         platform_act:
+   **      }]
+   **      grp:商品实际支付价格(goods_real_pay: gt - dt)
+   **      gsm:规格团购销售价格(group_sell_money)
+   **      gst: 规格团购销售总价(group_sell_total: gsm * bn)
+   **      ggt: 商品团购支付原价(goods_group_total: gst + ft)
+   **      ggrp: 商品团购实际支付价格(goods_group_real_pay: ggt - dt)
+   **    }
+   **  }
+   ** }
    */
   moneyDetail(data) {
     if (!data) throw new BusinessError(ErrorCode.DATA_NOT_EXIST, '未找到订单信息');
     // 优惠部分
     const ddt = _.get(data, 'total_detail.discount_detail', {});
+    // 活动部分
+    const act = _.get(data, 'total_detail.act', []);
     // 店铺规格商品数据
     const shopGoods = _.get(data, 'goods', []);
     const result = {};
@@ -157,7 +161,9 @@ class OrderService extends CrudService {
       const { goods, shop } = s;
       const shopResult = {};
       for (const g of goods) {
-        const { sell_money: sm, freight: f, buy_num: bn, group_config, _id } = g;
+        const { sell_money, freight: f, buy_num: bn, group_config, _id } = g;
+        // 优先获取price字段,没有再取sell_money
+        const sm = _.get(g, 'price', sell_money);
         const st = this.ctx.multiply(sm, bn);
         const ft = this.ctx.multiply(f, bn);
         const gt = this.ctx.plus(st, ft);