|
@@ -10,9 +10,81 @@ class CouponService extends CrudService {
|
|
|
constructor(ctx) {
|
|
|
super(ctx, 'coupon');
|
|
|
this.model = this.ctx.model.Trade.Coupon;
|
|
|
+ this.userCouponModel = this.ctx.model.User.UserCoupon;
|
|
|
+ }
|
|
|
+
|
|
|
+ async userView(filter, { skip = 0, limit, sort, desc, projection } = {}) {
|
|
|
+ const customer = _.get(this.ctx, 'user._id');
|
|
|
+ if (customer) throw new BusinessError(ErrorCode.NOT_LOGIN, '未找到用户登陆信息');
|
|
|
+ let list = await this.query(filter, { skip, limit, sort, desc, projection });
|
|
|
+ list = await this.makeShowData(list);
|
|
|
+ // 还需要判断用户是否领取过了
|
|
|
+ for (const coupon of list) {
|
|
|
+ const { get_limit, get_limit_config } = coupon;
|
|
|
+ if (get_limit === 'nolimit') coupon.canGet = true;
|
|
|
+ else {
|
|
|
+ const max = _.get(get_limit_config, 'max', 1);
|
|
|
+ const num = await this.userCouponModel.count({ coupon: coupon._id, customer });
|
|
|
+ if (this.ctx.minus(num, max) > 0) coupon.canGet = false;
|
|
|
+ }
|
|
|
+ }
|
|
|
+ return list;
|
|
|
}
|
|
|
|
|
|
|
|
|
+ async makeShowData(couponList) {
|
|
|
+ const dictDataModel = this.ctx.model.Dev.DictData;
|
|
|
+ // 过期字典
|
|
|
+ const expList = await dictDataModel.find({ code: 'coupon_expire_type' });
|
|
|
+ // 减免字典
|
|
|
+ const disList = await dictDataModel.find({ code: 'coupon_discount_type' });
|
|
|
+ // 使用字典
|
|
|
+ const useList = await dictDataModel.find({ code: 'coupon_use_limit' });
|
|
|
+ // 领取字典
|
|
|
+ const getList = await dictDataModel.find({ code: 'coupon_get_limit' });
|
|
|
+ couponList = couponList.map(coupon => {
|
|
|
+ const { _id } = coupon;
|
|
|
+ let obj = { _id, name: _.get(coupon, 'name') };
|
|
|
+ // 失效
|
|
|
+ const expire_type = _.get(coupon, 'expire_type');
|
|
|
+ const expire_type_label = _.get(
|
|
|
+ expList.find(f => f.value === expire_type),
|
|
|
+ 'label'
|
|
|
+ );
|
|
|
+ let expire_time = _.get(coupon, `expire_config.${expire_type}`);
|
|
|
+ if (expire_time) {
|
|
|
+ if (expire_type === 'fixed') expire_time = expire_time.join(' 至 ');
|
|
|
+ }
|
|
|
+ obj = { ...obj, expire_type, expire_type_label, expire_time };
|
|
|
+ // 减免
|
|
|
+ const discount_type = _.get(coupon, 'discount_type');
|
|
|
+ const discount_type_label = _.get(
|
|
|
+ disList.find(f => f.value === discount_type),
|
|
|
+ 'label'
|
|
|
+ );
|
|
|
+ const discount_config = _.get(coupon, 'discount_config');
|
|
|
+ obj = { ...obj, discount_type, discount_type_label, discount_config };
|
|
|
+ // 使用
|
|
|
+ const use_limit = _.get(coupon, 'use_limit');
|
|
|
+ const use_limit_label = _.get(
|
|
|
+ useList.find(f => f.value === use_limit),
|
|
|
+ 'label'
|
|
|
+ );
|
|
|
+ const use_limit_config = _.get(coupon, 'use_limit_config');
|
|
|
+ obj = { ...obj, use_limit, use_limit_label, use_limit_config };
|
|
|
+
|
|
|
+ // 领取
|
|
|
+ const get_limit = _.get(coupon, 'get_limit');
|
|
|
+ const get_limit_label = _.get(
|
|
|
+ getList.find(f => f.value === get_limit),
|
|
|
+ 'label'
|
|
|
+ );
|
|
|
+ const get_limit_config = _.get(coupon, 'get_limit_config');
|
|
|
+ obj = { ...obj, get_limit, get_limit_label, get_limit_config };
|
|
|
+ return obj;
|
|
|
+ });
|
|
|
+ return couponList;
|
|
|
+ }
|
|
|
}
|
|
|
|
|
|
module.exports = CouponService;
|