'use strict'; const { CrudService } = require('naf-framework-mongoose/lib/service'); const { BusinessError, ErrorCode } = require('naf-core').Error; const _ = require('lodash'); const assert = require('assert'); // 车奖 class Car_showService extends CrudService { constructor(ctx) { super(ctx); this.record = this.ctx.model.Record; this.model = this.ctx.model.Card; /** * @constant Number 车奖的积分 default:131419 */ this.car_point = 131419; /** * @constant Number 车奖的等级界限,前x级不是能获得车奖 default:4 */ this.car_show_limit_level = 4; /** * @constant Number 车奖的B梯队等级界限 default:4 */ this.car_show_b_limit_level = 4; /** * @constant Number 车奖的B梯队等级界限的人数 default:5 */ this.car_show_b_limit_person = 5; } /** * 检查用户是否符合车奖条件 * @param {String} {id} 用户id */ async checkUser({ id }) { assert(id, '缺少用户信息'); const user = await this.model.findById(id); if (!user) throw new BusinessError(ErrorCode.DATA_NOT_EXIST, '未找到用户信息'); const { car_show, mobile, level } = user; if (car_show) throw new BusinessError(ErrorCode.BUSINESS, '您已领取车奖,不能重复领取'); if (level < this.car_show_limit_level) throw new BusinessError(ErrorCode.BUSINESS, '未到达领取车奖等级'); const b = await this.model.count({ r_mobile: mobile, level: { $gte: this.car_show_b_limit_level } }); if (b < this.car_show_b_limit_person) throw new BusinessError(ErrorCode.BUSINESS, '该用户下满足条件的人数少于5人'); return true; } /** * 用户领取车奖 * @param {String} {id} 用户id */ async getCarShow({ id }) { assert(id, '缺少用户信息'); const r = await this.checkUser({ id }); if (!r) throw new BusinessError(ErrorCode.BUSINESS, '用户不满足车奖要求'); const user = await this.model.findById(id); user.car_show = true; user.points = user.points + this.car_point; await user.save(); // 添加积分记录 const record = _.pick(user, [ 'mobile', 'name' ]); record.points = this.car_point; record.opera = '2'; try { await this.record.create(record); } catch (error) { this.logger.error(`line-202-积分记录添加失败${JSON.stringify(record)}`); } } } module.exports = Car_showService;