cash.js 1.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142
  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. // 提现
  6. class CashService extends CrudService {
  7. constructor(ctx) {
  8. super(ctx, 'cash');
  9. this.model = this.ctx.model.Cash;
  10. this.record = this.ctx.model.Record;
  11. this.card = this.ctx.model.Card;
  12. this.charge = 0.06; // 6%
  13. }
  14. async create(data) {
  15. const { b_point, i_point, e_point, mobile } = data;
  16. const user = await this.card.findOne({ mobile });
  17. if (!user) throw new BusinessError(ErrorCode.DATA_NOT_EXIST, '为找到提现的用户信息');
  18. if (!b_point) data.b_point = user.points; // 手动赋值
  19. if (e_point || e_point === 0) {
  20. // 说明存在剩余积分,直接用
  21. user.points = e_point;
  22. } else {
  23. user.points = user.points - (i_point + _.ceil(i_point * 0.06));
  24. data.e_point = user.points;
  25. }
  26. await this.model.create(data);
  27. await user.save();
  28. const record = _.pick(user, [ 'mobile', 'name' ]);
  29. record.points = -i_point;
  30. record.opera = '提现';
  31. record.params = { charge: -_.ceil(i_point * 0.06) };
  32. try {
  33. await this.record.create(record);
  34. } catch (error) {
  35. this.logger.error(`积分记录添加失败${JSON.stringify(record)}`);
  36. }
  37. }
  38. }
  39. module.exports = CashService;