123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899 |
- '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');
- const Transaction = require('mongoose-transactions');
- //
- class TempLessonApplyService extends CrudService {
- constructor(ctx) {
- super(ctx, 'templessonapply');
- this.model = this.ctx.model.Apply.TempLessonApply;
- this.studentModel = this.ctx.model.User.Student;
- this.payOrderModel = this.ctx.model.Business.PayOrder;
- this.rssModel = this.ctx.model.Relation.RelationStudentSchool;
- this.tran = new Transaction();
- }
- async create(data) {
- try {
- // 创建数据
- const id = await this.tran.insert('TempLessonApply', data);
- // 组织payOrder的数据,使用native支付
- const payData = {
- school_id: _.get(data, 'school_id'),
- pay_for: 'TempLessonApply',
- from_id: id,
- time: moment().format('YYYY-MM-DD HH:mm:ss'),
- desc: '临时上课缴费',
- money: _.get(data, 'money'),
- order_no: this.ctx.service.business.payOrder.getOrderNo(),
- };
- // 有学生信息就把学生信息补上
- // 再查询学生是否有余额
- let needPay = true;
- let result = 'ok';
- let billId;
- if (data.student_id) {
- payData.payer_id = data.student_id;
- payData.payer_role = 'Student';
- const rss = await this.rssModel.findOne({ school_id: data.school_id, student_id: data.student_id });
- if (rss) {
- // 有学生信息,将信息填充,因为传student_id可能不会传name和phone
- const student = await this.studentModel.findById(data.student_id);
- const name = _.get(student, 'name');
- const phone = _.get(student, 'phone');
- if (name && phone) this.tran.update('TempLessonApply', id, { name, phone });
- const surplus = _.get(rss, 'money');
- const newMoney = this.ctx.minus(surplus, data.money);
- if (newMoney >= 0) {
- needPay = false;
- this.tran.update('RelationStudentSchool', rss._id, { money: newMoney });
- // 生成账单
- const billData = {
- school_id: _.get(data, 'school_id'),
- payer_role: 'Student',
- payer_id: data.student_id,
- pay_for: 'TempLessonApply',
- from_id: id,
- type: '-2',
- is_pay: '1',
- time: moment().format('YYYY-MM-DD HH:mm:ss'),
- };
- billId = this.tran.insert('Bill', billData);
- // 生成付款单数据,但是不需要支付
- payData.status = '1';
- payData.config = {
- useSurplus: true,
- bill: billId,
- money: _.get(data, 'money'),
- };
- }
- }
- }
- // 需要支付,不会生成账单,native支付处生成payOrder数据
- if (needPay) {
- // 获取支付二维码和生成的支付信息,再修改进去
- const { pay_id, qrcode } = await this.ctx.service.business.payOrder.toPayNative(payData, this.tran);
- this.tran.update('TempLessonApply', id, { pay_id });
- result = qrcode;
- } else {
- // 不需要支付的情况,直接将支付单生成,然后将临时课程和账单的pay_id&is_pay都修改了
- const pay_id = this.tran.insert('PayOrder', payData);
- this.tran.update('TempLessonApply', id, { pay_id, is_pay: '1' });
- this.tran.update('Bill', billId, { pay_id, is_pay: '1' });
- }
- await this.tran.run();
- // 返回结果
- return result;
- } catch (error) {
- await this.tran.rollback();
- console.log(error);
- } finally {
- this.tran.clean();
- }
- }
- }
- module.exports = TempLessonApplyService;
|