|
@@ -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;
|