lrf 2 years ago
parent
commit
edb996db43
1 changed files with 31 additions and 23 deletions
  1. 31 23
      app/service/user/userCoupon.js

+ 31 - 23
app/service/user/userCoupon.js

@@ -150,7 +150,7 @@ class UserCouponService extends CrudService {
       }
       uc.canUse = true;
     }
-    couponList = couponList.filter(f => f.canUse);
+    couponList = couponList.filter((f) => f.canUse);
     if (couponList.length <= 0) return [];
     couponList = await this.makeShowData(couponList);
     return couponList;
@@ -213,7 +213,7 @@ class UserCouponService extends CrudService {
         const r = arr.findIndex((f) => arr1.find((ff) => _.isEqual(f, ff)));
         console.log(r);
        */
-      const r = s.goods.some(g => g.tags && g.tags.find(f => tags && tags.find(ff => _.isEqual(f, ff))));
+      const r = s.goods.some((g) => g.tags && g.tags.find((f) => tags && tags.find((ff) => _.isEqual(f, ff))));
       if (r) {
         result = true;
         break;
@@ -243,7 +243,7 @@ class UserCouponService extends CrudService {
    */
   async computedOrderCouponDiscount(couponIds, goodsSpecs) {
     const result = {}; // 最终结果: 以 couponId: { goodsSpecId: ${money} } 为明细格式
-    const goodsSpecIds = goodsSpecs.map(i => i.id);
+    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);
     // 新价格和新整理的数据结合
@@ -251,9 +251,9 @@ class UserCouponService extends CrudService {
     const { populate: cp } = this.getRefMods();
     let userCouponList = await this.model.find({ _id: couponIds }, { customer: 0 }).populate(cp);
     // 先查下有没有不能用的,有不能用的需要返回
-    let cantUse = userCouponList.find(f => f.status !== '0');
+    let cantUse = userCouponList.find((f) => f.status !== '0');
     if (cantUse) throw new BusinessError(ErrorCode.DATA_INVALID, `优惠券:${_.get(cantUse, 'coupon.name')}已使用`);
-    cantUse = userCouponList.find(f => !this.checkCanUse_expire(f));
+    cantUse = userCouponList.find((f) => !this.checkCanUse_expire(f));
     if (cantUse) throw new BusinessError(ErrorCode.DATA_INVALID, `优惠券:${_.get(cantUse, 'coupon.name')}不能使用`);
     userCouponList = this.resetUserCouponData(userCouponList);
     // 上面整理完数据之后,下面开始用 均分优惠金额
@@ -265,7 +265,7 @@ class UserCouponService extends CrudService {
       // 平台券,都可以
       if (issue === '0') goodsList = goodsSpecList;
       // 店铺券,需要过滤店铺
-      else goodsList = goodsSpecList.filter(f => ObjectId(shop).equals(_.get(f, 'shop._id')));
+      else goodsList = goodsSpecList.filter((f) => ObjectId(shop).equals(_.get(f, 'shop._id')));
       // 2.商品已确定,再确定是否是指定分类
       if (use_limit !== 'all') {
         // all的情况不需要再次过滤出不符合的条件: all为全部商品类型
@@ -383,11 +383,11 @@ class UserCouponService extends CrudService {
    * @param {Array} tags 商品标签
    */
   getGoodsInTags(goodsSpecList, tags) {
-    const arr = goodsSpecList.filter(f => {
+    const arr = goodsSpecList.filter((f) => {
       const gtags = _.get(f, 'goods.tags');
       // 没打标签或者标签里没有内容的直接过滤出去
       if (!gtags || gtags.length <= 0) return false;
-      const r = gtags.find(f => tags.find(ff => _.isEqual(ff, f)));
+      const r = gtags.find((f) => tags.find((ff) => _.isEqual(ff, f)));
       return r;
     });
     return arr;
@@ -401,16 +401,16 @@ class UserCouponService extends CrudService {
    */
   resetGoodsSpecData(list, orderInfo) {
     const priceKey = 'price';
-    const nouse = [ 'meta', '__v' ];
-    const arr = list.map(i => {
+    const nouse = ['meta', '__v'];
+    const arr = list.map((i) => {
       const obj = {};
-      const goodsSpec = _.omit(i, [ ...nouse, 'goods' ]);
-      const shop = _.omit(_.get(i, 'goods.shop'), [ ...nouse ]);
-      const goods = _.omit(_.get(i, 'goods'), [ ...nouse, 'shop' ]);
+      const goodsSpec = _.omit(i, [...nouse, 'goods']);
+      const shop = _.omit(_.get(i, 'goods.shop'), [...nouse]);
+      const goods = _.omit(_.get(i, 'goods'), [...nouse, 'shop']);
       obj.goodsSpec = goodsSpec;
       obj.shop = shop;
       obj.goods = goods;
-      const r = orderInfo.find(f => ObjectId(f.id).equals(goodsSpec._id));
+      const r = orderInfo.find((f) => ObjectId(f.id).equals(goodsSpec._id));
       // 将变化的价格赋上去:传来价格变动就用新的价格;没有就用设置的销售价
       if (r) {
         obj.buy_num = r.buy_num;
@@ -429,7 +429,7 @@ class UserCouponService extends CrudService {
    * @param {Array} list 用户优惠券列表
    */
   resetUserCouponData(list) {
-    const arr = list.map(i => {
+    const arr = list.map((i) => {
       const { _id, meta, coupon } = i;
       const cd = _.pick(coupon, [
         'issue',
@@ -461,13 +461,13 @@ class UserCouponService extends CrudService {
     const useList = await dictDataModel.find({ code: 'coupon_use_limit' });
     // 领取字典
     const getList = await dictDataModel.find({ code: 'coupon_get_limit' });
-    couponList = couponList.map(i => {
+    couponList = couponList.map((i) => {
       const { _id, canUse, coupon, meta, status } = i;
       let obj = { _id, canUse, name: _.get(coupon, 'name'), status };
       // 失效
       const expire_type = _.get(coupon, 'expire_type');
       const expire_type_label = _.get(
-        expList.find(f => f.value === expire_type),
+        expList.find((f) => f.value === expire_type),
         'label'
       );
       let expire_time = _.get(coupon, `expire_config.${expire_type}`);
@@ -481,7 +481,7 @@ class UserCouponService extends CrudService {
       // 减免
       const discount_type = _.get(coupon, 'discount_type');
       const discount_type_label = _.get(
-        disList.find(f => f.value === discount_type),
+        disList.find((f) => f.value === discount_type),
         'label'
       );
       const discount_config = _.get(coupon, 'discount_config');
@@ -489,7 +489,7 @@ class UserCouponService extends CrudService {
       // 使用
       const use_limit = _.get(coupon, 'use_limit');
       const use_limit_label = _.get(
-        useList.find(f => f.value === use_limit),
+        useList.find((f) => f.value === use_limit),
         'label'
       );
       const use_limit_config = _.get(coupon, 'use_limit_config');
@@ -498,7 +498,7 @@ class UserCouponService extends CrudService {
       // 领取
       const get_limit = _.get(coupon, 'get_limit');
       const get_limit_label = _.get(
-        getList.find(f => f.value === get_limit),
+        getList.find((f) => f.value === get_limit),
         'label'
       );
       const get_limit_config = _.get(coupon, 'get_limit_config');
@@ -530,12 +530,20 @@ class UserCouponService extends CrudService {
       if (this.ctx.minus(num, limit) > 0) loop = limit;
       else loop = num;
     }
+    let giveNum = 0;
     for (const u of users) {
-      const obj = { customer: u, shop: _.get(couponData, 'shop'), coupon: couponData };
-      for (let i = 0; i < loop; i++) {
-        await this.model.create(obj);
+      try {
+        const obj = { customer: u, shop: _.get(couponData, 'shop'), coupon: couponData };
+        for (let i = 0; i < loop; i++) {
+          await this.model.create(obj);
+          giveNum++;
+        }
+      } catch (error) {
+        console.log(error);
       }
     }
+    const newNum = this.ctx.minus(couponData.num, giveNum);
+    await this.couponModel.updateOne({ _id: couponData._id }, { num: newNum });
   }
 }