lrf 2 vuotta sitten
vanhempi
commit
442a3a5972
2 muutettua tiedostoa jossa 51 lisäystä ja 55 poistoa
  1. 46 39
      app/service/trade/order.js
  2. 5 16
      app/service/user/userCoupon.js

+ 46 - 39
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 = [], type = '0', inviter } = body;
+      const { address, goods, total_detail, coupon = [], plus_goods = [], type = '0', inviter } = body;
       if (coupon.length > 1) throw new BusinessError(ErrorCode.DATA_INVALID, '目前只允许使用1张优惠券');
       // 检测商品是否可以下单
       for (const i of goods) {
@@ -56,23 +56,30 @@ class OrderService extends CrudService {
       // 2.商品快照
       const goodsData = [];
       // 商店不做快照,但是商品和商品对应的规格做快照
-      const { populate } = this.ctx.service.shop.goodsSpec.getRefMods();
       for (const i of goods) {
         const { goods: goodsList, ...others } = i;
         const qp = [];
         for (const g of goodsList) {
-          const { goodsSpec_id, num, cart_id } = g;
-          let goodsSpec = await this.goodsSpecModel.findById(goodsSpec_id).populate(populate);
+          const { goodsSpec_id, goods_id } = g;
+          // 需要的订单数据
+          const orderNeedData = _.pick(g, [ 'act', 'price', 'sp_price', 'num', 'cart_id' ]);
+          if (orderNeedData.num) {
+            orderNeedData.buy_num = orderNeedData.num;
+            delete orderNeedData.num;
+          }
+          let goodsSpec = await this.goodsSpecModel.findById(goodsSpec_id);
           if (!goodsSpec) continue;
           goodsSpec = JSON.parse(JSON.stringify(goodsSpec));
-          // 将商店内容剔除
-          const { goods: gd, ...dOthers } = goodsSpec;
-          const gdOthers = _.omit(gd, [ 'shop' ]);
-          const obj = { ...dOthers, goods: gdOthers, buy_num: num, tags: _.get(gdOthers, 'tags') };
-          if (cart_id) obj.cart_id = cart_id;
-          qp.push(obj);
-
-          goodsSpecs.push({ id: goodsSpec_id, buy_num: num, sell_money: obj.sell_money, group_sell_money: _.get(obj, 'group_config.money'), type });
+          const goods = await this.goodsModel.findById(goods_id);
+          if (goods) goodsSpec.goods = JSON.parse(JSON.stringify(goods));
+          goodsSpec = { ...goodsSpec, ...orderNeedData };
+          qp.push(goodsSpec);
+          const ogs = _.pick(goodsSpec, [ '_id', 'buy_num', 'sell_money', 'price' ]);
+          if (ogs._id) {
+            ogs.id = ogs._id;
+            delete ogs._id;
+          }
+          goodsSpecs.push({ ...ogs, type });
         }
         goodsData.push({ ...others, goods: qp });
       }
@@ -80,31 +87,31 @@ class OrderService extends CrudService {
       // 计算优惠券的明细
       const discountDetail = await this.ctx.service.user.userCoupon.computedOrderCouponDiscount(coupon, goodsSpecs);
       const totalDetailData = { ...total_detail, discount_detail: JSON.parse(JSON.stringify(discountDetail)) };
-      // 接下来组织订单数据
-      orderData.address = addressData;
-      orderData.goods = goodsData;
-      orderData.total_detail = totalDetailData;
-      // 1.用户数据
-      orderData.customer = customer;
-      // 2.下单时间
-      orderData.buy_time = moment().format('YYYY-MM-DD HH:mm:ss');
-      // 3.订单号
-      const str = this.ctx.service.util.trade.createNonceStr();
-      orderData.no = `${moment().format('YYYYMMDDHHmmss')}-${str}`;
-      // 4.状态
-      orderData.status = '0';
-      // 5.返现部分:邀请人: 自己发链接自己买不行
-      if (customer !== inviter && ObjectId.isValid(inviter)) orderData.inviter = inviter;
-      // 生成数据
-      const order_id = this.tran.insert('Order', orderData);
-      // 处理库存,删除购物车
-      await this.dealGoodsNum(goodsData);
-      // 处理优惠券,改为使用过
-      if (coupon.length > 0) await this.ctx.service.user.userCoupon.useCoupon(coupon, this.tran);
-      await this.tran.run();
-      // 创建定时任务(mq死信机制任务)
-      await this.toMakeTask(order_id);
-      return order_id;
+      // // 接下来组织订单数据
+      // orderData.address = addressData;
+      // orderData.goods = goodsData;
+      // orderData.total_detail = totalDetailData;
+      // // 1.用户数据
+      // orderData.customer = customer;
+      // // 2.下单时间
+      // orderData.buy_time = moment().format('YYYY-MM-DD HH:mm:ss');
+      // // 3.订单号
+      // const str = this.ctx.service.util.trade.createNonceStr();
+      // orderData.no = `${moment().format('YYYYMMDDHHmmss')}-${str}`;
+      // // 4.状态
+      // orderData.status = '0';
+      // // 5.返现部分:邀请人: 自己发链接自己买不行
+      // if (customer !== inviter && ObjectId.isValid(inviter)) orderData.inviter = inviter;
+      // // 生成数据
+      // const order_id = this.tran.insert('Order', orderData);
+      // // 处理库存,删除购物车
+      // await this.dealGoodsNum(goodsData);
+      // // 处理优惠券,改为使用过
+      // if (coupon.length > 0) await this.ctx.service.user.userCoupon.useCoupon(coupon, this.tran);
+      // await this.tran.run();
+      // // 创建定时任务(mq死信机制任务)
+      // await this.toMakeTask(order_id);
+      // return order_id;
     } catch (error) {
       await this.tran.rollback();
       console.error(error);
@@ -229,8 +236,8 @@ class OrderService extends CrudService {
     // 活动部分
     // 将加价购拿出来,给前端,需要特殊处理
     pageData.actList = actList.filter(f => f.platform_act_type === '4');
-    // 满减/折 直接放在orderTotal中,作为明细,反正后面也不用
-    const daList = actList.filter(f => f.platform_act_type === '5' || f.platform_act_type === '6');
+    // 满减/折 直接放在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) {
       const obj = { zh: da.title, key: da.platform_act, money: da.discount };
       shopTotalDetail.push(obj);

+ 5 - 16
app/service/user/userCoupon.js

@@ -239,15 +239,14 @@ class UserCouponService extends CrudService {
   /**
    * 计算订单使用优惠券的明细
    * @param {Array} couponIds 订单使用的用户领取的优惠券数据id数组
-   * @param {Array} goodsSpecs 订单所有商品规格的数据:{id,buy_num,sell_money,group_sell_money}
-   * @param {String} type 订单类型:0:常规单;1:团购单; 常规单,用销售金额算,团购单用团购金额算
+   * @param {Array} goodsSpecs 订单所有商品规格的数据:{id,buy_num,sell_money,price}
    */
-  async computedOrderCouponDiscount(couponIds, goodsSpecs, type = '0') {
+  async computedOrderCouponDiscount(couponIds, goodsSpecs) {
     const result = {}; // 最终结果: 以 couponId: { goodsSpecId: ${money} } 为明细格式
     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, type);
+    goodsSpecList = this.resetGoodsSpecData(goodsSpecList, goodsSpecs);
     const { populate: cp } = this.getRefMods();
     let userCouponList = await this.model.find({ _id: couponIds }, { customer: 0 }).populate(cp);
     // 先查下有没有不能用的,有不能用的需要返回
@@ -398,12 +397,9 @@ class UserCouponService extends CrudService {
    * 将goods,shop和goodsSpec重组为3个属性在object同一级上
    * @param {Array} list 商品规格列表
    * @param {Array} orderInfo 有关订单的信息 {id,buy_num}
-   * @param {String} type 订单类型:0:常规单-sell_money;1:团购单,group_config.money
    */
-  resetGoodsSpecData(list, orderInfo, type) {
-    let priceKey;
-    if (type === '1') priceKey = 'group_config.money';
-    else priceKey = 'sell_money';
+  resetGoodsSpecData(list, orderInfo) {
+    const priceKey = 'sell_money';
     const nouse = [ 'meta', '__v' ];
     const arr = list.map(i => {
       const obj = {};
@@ -416,13 +412,6 @@ class UserCouponService extends CrudService {
       const r = orderInfo.find(f => ObjectId(f.id).equals(goodsSpec._id));
       if (r) obj.buy_num = r.buy_num;
       if (obj.buy_num) {
-        // #region 团购部分
-        const group_sell_money = _.get(obj.goodsSpec, 'group_config.money');
-        if (group_sell_money) {
-          obj.group_sell_money = group_sell_money;
-          obj.group_goods_total = this.ctx.multiply(obj.buy_num, group_sell_money);
-        }
-        // #endregion
         // 计算 常规/团购 商品总价
         obj.goods_total = this.ctx.multiply(obj.buy_num, _.get(obj.goodsSpec, priceKey));