coupon.js 3.3 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091
  1. 'use strict';
  2. const { CrudService } = require('naf-framework-mongoose-free/lib/service');
  3. const { BusinessError, ErrorCode } = require('naf-core').Error;
  4. const _ = require('lodash');
  5. const assert = require('assert');
  6. const moment = require('moment');
  7. //
  8. class CouponService extends CrudService {
  9. constructor(ctx) {
  10. super(ctx, 'coupon');
  11. this.model = this.ctx.model.Trade.Coupon;
  12. this.userCouponModel = this.ctx.model.User.UserCoupon;
  13. }
  14. async userView(filter, { skip = 0, limit, sort, desc, projection } = {}) {
  15. const customer = _.get(this.ctx, 'user._id');
  16. if (!customer) throw new BusinessError(ErrorCode.NOT_LOGIN, '未找到用户登陆信息');
  17. let list = await this.query(filter, { skip, limit, sort, desc, projection });
  18. list = await this.makeShowData(list);
  19. // 还需要判断用户是否领取过了
  20. for (const coupon of list) {
  21. const { get_limit, get_limit_config } = coupon;
  22. if (get_limit === 'nolimit') coupon.canGet = true;
  23. else {
  24. const max = _.get(get_limit_config, 'max', 1);
  25. const num = await this.userCouponModel.count({ coupon: coupon._id, customer });
  26. if (this.ctx.minus(num, max) >= 0) coupon.canGet = false;
  27. else coupon.canGet = true;
  28. }
  29. }
  30. return list;
  31. }
  32. async makeShowData(couponList) {
  33. const dictDataModel = this.ctx.model.Dev.DictData;
  34. // 过期字典
  35. const expList = await dictDataModel.find({ code: 'coupon_expire_type' });
  36. // 减免字典
  37. const disList = await dictDataModel.find({ code: 'coupon_discount_type' });
  38. // 使用字典
  39. const useList = await dictDataModel.find({ code: 'coupon_use_limit' });
  40. // 领取字典
  41. const getList = await dictDataModel.find({ code: 'coupon_get_limit' });
  42. couponList = couponList.map(coupon => {
  43. const { _id } = coupon;
  44. let obj = { _id, name: _.get(coupon, 'name') };
  45. // 失效
  46. const expire_type = _.get(coupon, 'expire_type');
  47. const expire_type_label = _.get(
  48. expList.find(f => f.value === expire_type),
  49. 'label'
  50. );
  51. let expire_time = _.get(coupon, `expire_config.${expire_type}`);
  52. if (expire_time) {
  53. if (expire_type === 'fixed') expire_time = expire_time.join(' 至 ');
  54. }
  55. obj = { ...obj, expire_type, expire_type_label, expire_time };
  56. // 减免
  57. const discount_type = _.get(coupon, 'discount_type');
  58. const discount_type_label = _.get(
  59. disList.find(f => f.value === discount_type),
  60. 'label'
  61. );
  62. const discount_config = _.get(coupon, 'discount_config');
  63. obj = { ...obj, discount_type, discount_type_label, discount_config };
  64. // 使用
  65. const use_limit = _.get(coupon, 'use_limit');
  66. const use_limit_label = _.get(
  67. useList.find(f => f.value === use_limit),
  68. 'label'
  69. );
  70. const use_limit_config = _.get(coupon, 'use_limit_config');
  71. obj = { ...obj, use_limit, use_limit_label, use_limit_config };
  72. // 领取
  73. const get_limit = _.get(coupon, 'get_limit');
  74. const get_limit_label = _.get(
  75. getList.find(f => f.value === get_limit),
  76. 'label'
  77. );
  78. const get_limit_config = _.get(coupon, 'get_limit_config');
  79. obj = { ...obj, get_limit, get_limit_label, get_limit_config };
  80. return obj;
  81. });
  82. return couponList;
  83. }
  84. }
  85. module.exports = CouponService;