lrf 2 سال پیش
والد
کامیت
71978642c2
3فایلهای تغییر یافته به همراه28 افزوده شده و 3 حذف شده
  1. 1 1
      app/extend/context.js
  2. 0 2
      app/service/trade/orderDetail.js
  3. 27 0
      app/service/user/userCoupon.js

+ 1 - 1
app/extend/context.js

@@ -1,6 +1,6 @@
 'use strict';
 const Decimal = require('decimal.js');
-Decimal.set({ precision: 2 });
+// Decimal.set({ precision: 2 });
 module.exports = {
   // 加法
   plus(n1 = 0, n2 = 0) {

+ 0 - 2
app/service/trade/orderDetail.js

@@ -104,10 +104,8 @@ class OrderDetailService extends CrudService {
         const values = Object.values(obj);
         if (!values.every(e => _.isNumber(e))) continue;
         const dm = values.reduce((p, n) => this.ctx.plus(p, n), 0);
-        // console.log(discount, dm);
         discount = this.ctx.plus(discount, dm);
       }
-      // console.log(_.get(i, 'total_detail.goods_total'), discount);
       i.real_pay = this.ctx.minus(_.get(i, 'total_detail.goods_total'), discount);
     }
     return data;

+ 27 - 0
app/service/user/userCoupon.js

@@ -140,6 +140,12 @@ class UserCouponService extends CrudService {
         uc.canUse = false;
         continue;
       }
+      // 3.检查是否满足优惠券是用条件
+      r = this.checkCanUse_discount(uc, goods);
+      if (!r) {
+        uc.canUse = false;
+        continue;
+      }
       uc.canUse = true;
     }
     return couponList;
@@ -211,6 +217,27 @@ class UserCouponService extends CrudService {
     return result;
   }
 
+  /**
+   * 检查该优惠券是否可以在这些商品中使用
+   * @param {Object} userCoupon 用户领取的优惠券
+   * @param {Array} shopGoods 按店铺分组的商品列表
+   */
+  checkCanUse_discount(userCoupon, shopGoods) {
+    const { coupon } = userCoupon;
+    const { discount_config = {} } = coupon;
+    const limit = _.get(discount_config, 'limit');
+    if (limit === 'nolimit') return true;
+    const goods_total = shopGoods.reduce((p, n) =>
+      this.ctx.plus(
+        p,
+        n.goods.reduce((gp, gn) =>
+          this.ctx.plus(gp, this.ctx.multiply(gn.sell_money, gn.buy_num)), 0)
+      )
+    , 0);
+    if (this.ctx.minus(goods_total, limit) > 0) return true;
+    return false;
+
+  }
   /**
    * 计算订单使用优惠券的明细
    * @param {Array} couponIds 订单使用的用户领取的优惠券数据id数组