coupon.js 3.4 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192
  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 data = await this.query({ ...filter, status: '0' }, { skip, limit, sort, desc, projection });
  18. data = await this.makeShowData(data);
  19. // 还需要判断用户是否领取过了
  20. for (const coupon of data) {
  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. const total = await this.model.count({ ...filter, status: '0' });
  31. return { data, total };
  32. }
  33. async makeShowData(couponList) {
  34. const dictDataModel = this.ctx.model.Dev.DictData;
  35. // 过期字典
  36. const expList = await dictDataModel.find({ code: 'coupon_expire_type' });
  37. // 减免字典
  38. const disList = await dictDataModel.find({ code: 'coupon_discount_type' });
  39. // 使用字典
  40. const useList = await dictDataModel.find({ code: 'coupon_use_limit' });
  41. // 领取字典
  42. const getList = await dictDataModel.find({ code: 'coupon_get_limit' });
  43. couponList = couponList.map(coupon => {
  44. const { _id } = coupon;
  45. let obj = { _id, name: _.get(coupon, 'name') };
  46. // 失效
  47. const expire_type = _.get(coupon, 'expire_type');
  48. const expire_type_label = _.get(
  49. expList.find(f => f.value === expire_type),
  50. 'label'
  51. );
  52. let expire_time = _.get(coupon, `expire_config.${expire_type}`);
  53. if (expire_time) {
  54. if (expire_type === 'fixed') expire_time = expire_time.join(' 至 ');
  55. }
  56. obj = { ...obj, expire_type, expire_type_label, expire_time };
  57. // 减免
  58. const discount_type = _.get(coupon, 'discount_type');
  59. const discount_type_label = _.get(
  60. disList.find(f => f.value === discount_type),
  61. 'label'
  62. );
  63. const discount_config = _.get(coupon, 'discount_config');
  64. obj = { ...obj, discount_type, discount_type_label, discount_config };
  65. // 使用
  66. const use_limit = _.get(coupon, 'use_limit');
  67. const use_limit_label = _.get(
  68. useList.find(f => f.value === use_limit),
  69. 'label'
  70. );
  71. const use_limit_config = _.get(coupon, 'use_limit_config');
  72. obj = { ...obj, use_limit, use_limit_label, use_limit_config };
  73. // 领取
  74. const get_limit = _.get(coupon, 'get_limit');
  75. const get_limit_label = _.get(
  76. getList.find(f => f.value === get_limit),
  77. 'label'
  78. );
  79. const get_limit_config = _.get(coupon, 'get_limit_config');
  80. obj = { ...obj, get_limit, get_limit_label, get_limit_config };
  81. return obj;
  82. });
  83. return couponList;
  84. }
  85. }
  86. module.exports = CouponService;