123456789101112131415161718192021222324252627282930313233343536373839404142434445464748 |
- 'use strict';
- const { CrudService } = require('naf-framework-mongoose-free/lib/service');
- const { BusinessError, ErrorCode } = require('naf-core').Error;
- const _ = require('lodash');
- const assert = require('assert');
- const moment = require('moment');
- //
- class TempLessonApplyService extends CrudService {
- constructor(ctx) {
- super(ctx, 'templessonapply');
- this.model = this.ctx.model.Apply.TempLessonApply;
- this.payOrderService = this.ctx.service.business.payOrder;
- }
- async beforeCreate(data) {
- const query = _.pick(data, [ 'school_id', 'lesson_id', 'student_id', 'coach_id' ]);
- const num = await this.model.count(query);
- if (num > 0) throw new BusinessError(ErrorCode.DATA_EXISTED, '学生已申请临时参加课程');
- return data;
- }
- // 去支付,生成支付单
- async toPay({ id, payer_id }) {
- const data = await this.model.findById(id);
- if (!data) throw new BusinessError(ErrorCode.DATA_NOT_EXIST, '未找到临时上课的申请,无法进行支付');
- const { student_id, coach_id, lesson_id, school_id, money } = data;
- const payer_role = payer_id === student_id ? 'Student' : payer_id === coach_id ? 'Coach' : null;
- if (!payer_role) throw new BusinessError(ErrorCode.DATA_INVALID, '该支付用户不是学生也不是教练');
- const obj = { payer_id, payer_role, pay_for: 'Lesson', from_id: lesson_id, school_id, money, time: moment().format('YYYY-MM-DD HH:mm:ss'), desc: '私教课' };
- // 找openid
- const user = await this.ctx.model.User[payer_role].findById(payer_id).populate('user_id');
- if (!user) throw new BusinessError(ErrorCode.DATA_NOT_EXIST, '未找到用户信息');
- const openid = _.get(user, 'user_id.openid');
- if (!openid) throw new BusinessError(ErrorCode.DATA_NOT_EXIST, '未找到用户微信信息');
- obj.openid = openid;
- const res = await this.payOrderService.create(obj);
- return res;
- }
- // 处理审核成功的情况
- async afterUpdate(filter, body, data) {
- return data;
- }
- }
- module.exports = TempLessonApplyService;
|