car_show.js 2.4 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071
  1. 'use strict';
  2. const { CrudService } = require('naf-framework-mongoose/lib/service');
  3. const { BusinessError, ErrorCode } = require('naf-core').Error;
  4. const _ = require('lodash');
  5. const assert = require('assert');
  6. // 车奖
  7. class Car_showService extends CrudService {
  8. constructor(ctx) {
  9. super(ctx);
  10. this.record = this.ctx.model.Record;
  11. this.model = this.ctx.model.Card;
  12. /**
  13. * @constant Number 车奖的积分 default:131419
  14. */
  15. this.car_point = 131419;
  16. /**
  17. * @constant Number 车奖的等级界限,前x级不是能获得车奖 default:4
  18. */
  19. this.car_show_limit_level = 4;
  20. /**
  21. * @constant Number 车奖的B梯队等级界限 default:4
  22. */
  23. this.car_show_b_limit_level = 4;
  24. /**
  25. * @constant Number 车奖的B梯队等级界限的人数 default:5
  26. */
  27. this.car_show_b_limit_person = 5;
  28. }
  29. /**
  30. * 检查用户是否符合车奖条件
  31. * @param {String} {id} 用户id
  32. */
  33. async checkUser({ id }) {
  34. assert(id, '缺少用户信息');
  35. const user = await this.model.findById(id);
  36. if (!user) throw new BusinessError(ErrorCode.DATA_NOT_EXIST, '未找到用户信息');
  37. const { car_show, mobile, level } = user;
  38. if (car_show) throw new BusinessError(ErrorCode.BUSINESS, '您已领取车奖,不能重复领取');
  39. if (level < this.car_show_limit_level) throw new BusinessError(ErrorCode.BUSINESS, '未到达领取车奖等级');
  40. const b = await this.model.count({ r_mobile: mobile, level: { $gte: this.car_show_b_limit_level } });
  41. if (b < this.car_show_b_limit_person) throw new BusinessError(ErrorCode.BUSINESS, '该用户下满足条件的人数少于5人');
  42. return true;
  43. }
  44. /**
  45. * 用户领取车奖
  46. * @param {String} {id} 用户id
  47. */
  48. async getCarShow({ id }) {
  49. assert(id, '缺少用户信息');
  50. const r = await this.checkUser({ id });
  51. if (!r) throw new BusinessError(ErrorCode.BUSINESS, '用户不满足车奖要求');
  52. const user = await this.model.findById(id);
  53. user.car_show = true;
  54. user.points = user.points + this.car_point;
  55. await user.save();
  56. // 添加积分记录
  57. const record = _.pick(user, [ 'mobile', 'name' ]);
  58. record.points = this.car_point;
  59. record.opera = '2';
  60. try {
  61. await this.record.create(record);
  62. } catch (error) {
  63. this.logger.error(`line-202-积分记录添加失败${JSON.stringify(record)}`);
  64. }
  65. }
  66. }
  67. module.exports = Car_showService;