123456789101112131415161718192021222324252627282930313233343536373839404142 |
- 'use strict';
- const { CrudService } = require('naf-framework-mongoose/lib/service');
- const { BusinessError, ErrorCode } = require('naf-core').Error;
- const _ = require('lodash');
- // 提现
- class CashService extends CrudService {
- constructor(ctx) {
- super(ctx, 'cash');
- this.model = this.ctx.model.Cash;
- this.record = this.ctx.model.Record;
- this.card = this.ctx.model.Card;
- this.charge = 0.06; // 6%
- }
- async create(data) {
- const { b_point, i_point, e_point, mobile } = data;
- const user = await this.card.findOne({ mobile });
- if (!user) throw new BusinessError(ErrorCode.DATA_NOT_EXIST, '为找到提现的用户信息');
- if (!b_point) data.b_point = user.points; // 手动赋值
- if (e_point || e_point === 0) {
- // 说明存在剩余积分,直接用
- user.points = e_point;
- } else {
- user.points = user.points - (i_point + _.ceil(i_point * 0.06));
- data.e_point = user.points;
- }
- await this.model.create(data);
- await user.save();
- const record = _.pick(user, [ 'mobile', 'name' ]);
- record.points = -i_point;
- record.opera = '提现';
- record.params = { charge: -_.ceil(i_point * 0.06) };
- try {
- await this.record.create(record);
- } catch (error) {
- this.logger.error(`积分记录添加失败${JSON.stringify(record)}`);
- }
- }
- }
- module.exports = CashService;
|