lrf 2 年之前
父节点
当前提交
2b266d9dd4
共有 3 个文件被更改,包括 29 次插入0 次删除
  1. 3 0
      app/controller/user/config/.userCoupon.js
  2. 25 0
      app/service/user/userCoupon.js
  3. 1 0
      app/z_router/user/userCoupon.js

+ 3 - 0
app/controller/user/config/.userCoupon.js

@@ -41,4 +41,7 @@ module.exports = {
   getCoupon: {
     params: ['!coupon_id'],
   },
+  giveCoupon: {
+    requestBody: ['users', 'coupon', 'num'],
+  },
 };

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

@@ -512,6 +512,31 @@ class UserCouponService extends CrudService {
     data = this.makeShowData(data);
     return data;
   }
+  /**
+   * 批量发送优惠券
+   * @param {Object} body 参数体
+   * @param {Array} body.users 用户列表
+   * @param {string} body.coupon 要发送的优惠券
+   * @param {Number} body.num 要发送的优惠券数量
+   */
+  async giveCoupon({ users, coupon, num = 1 }) {
+    const couponData = await this.couponModel.findById(coupon).lean();
+    if (!couponData) throw new BusinessError(ErrorCode.DATA_NOT_EXIST, '未找到优惠券');
+    if (couponData.status !== '0') throw new BusinessError(ErrorCode.DATA_INVALID, '该优惠券处于禁用状态');
+    let loop = 0;
+    if (_.get(couponData, 'get_limit') === 'no_limit') loop = num;
+    else {
+      const limit = _.get(couponData, 'get_limit_config.max');
+      if (this.ctx.minus(num, limit) > 0) loop = limit;
+      else loop = num;
+    }
+    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);
+      }
+    }
+  }
 }
 
 module.exports = UserCouponService;

+ 1 - 0
app/z_router/user/userCoupon.js

@@ -7,6 +7,7 @@ const rkey = 'userCoupon';
 const ckey = 'user.userCoupon';
 const keyZh = '用户优惠券';
 const routes = [
+  { method: 'post', path: `${rkey}/giveCoupon`, controller: `${ckey}.giveCoupon`, name: `${ckey}giveCoupon`, zh: `${keyZh}-发送` },
   { method: 'post', path: `${rkey}/getCoupon/:coupon_id`, controller: `${ckey}.getCoupon`, name: `${ckey}getCoupon`, zh: `${keyZh}-领取` },
   { method: 'get', path: `${rkey}`, controller: `${ckey}.index`, name: `${ckey}Query`, zh: `${keyZh}列表查询` },
   { method: 'get', path: `${rkey}/:id`, controller: `${ckey}.show`, name: `${ckey}Show`, zh: `${keyZh}查询` },