|
@@ -4,6 +4,7 @@ const { BusinessError, ErrorCode } = require('naf-core').Error;
|
|
|
const _ = require('lodash');
|
|
|
const assert = require('assert');
|
|
|
const moment = require('moment');
|
|
|
+const { ObjectId } = require('mongoose').Types;
|
|
|
//
|
|
|
class PayOrderService extends CrudService {
|
|
|
constructor(ctx) {
|
|
@@ -109,6 +110,8 @@ class PayOrderService extends CrudService {
|
|
|
if (pay_for === 'lessonStudent') {
|
|
|
// 因为上课产生的支付,去找lessonStudent,修改指定学生的支付状态
|
|
|
await this.lessonStudentModel.updateOne({ _id }, { is_pay });
|
|
|
+ // 检查下各种记录
|
|
|
+ await this.makeRecord(data);
|
|
|
} else if (pay_for === 'tempLessonApply') {
|
|
|
// 私教课临时上课,需要到临时申请那找到相关数据
|
|
|
const tempApply = await this.tempLessonApplyModel.findById(_id);
|
|
@@ -119,6 +122,7 @@ class PayOrderService extends CrudService {
|
|
|
const { lesson_id, student_id, school_id } = tempApply;
|
|
|
const obj = { lesson_id, student_id, school_id, is_pay, pay_id, config, money };
|
|
|
await this.lessonStudentService.create(obj);
|
|
|
+ await this.makeRecord(data);
|
|
|
} else if (pay_for === 'charge' && is_pay !== '0') {
|
|
|
// 充值记录,找到充值记录,没有就生成
|
|
|
const chargeData = await this.chargeModel.findOne({ pay_id });
|
|
@@ -132,6 +136,22 @@ class PayOrderService extends CrudService {
|
|
|
}
|
|
|
}
|
|
|
}
|
|
|
+ // 检查 消费记录
|
|
|
+ async makeRecord(data) {
|
|
|
+ const { config, _id: pay_id } = data;
|
|
|
+ // 消费记录
|
|
|
+ if (_.get(config, 'useSurplus')) {
|
|
|
+ const { costDetail } = config;
|
|
|
+ // 使用了余额,但是余额记录不是直接生成的,需要检查下costDetail是否为ObjectId
|
|
|
+ // 如果是ObjectId,说明余额记录已经生成.无需操作
|
|
|
+ if (_.isObject(costDetail)) {
|
|
|
+ // 是数据,生成消费记录
|
|
|
+ const cdd = await this.costDetailModel.create({ ...costDetail, pay_id });
|
|
|
+ if (cdd) data.config.costDetail = ObjectId(cdd._id).toString();
|
|
|
+ await this.model.updateOne({ _id: data._id }, data);
|
|
|
+ }
|
|
|
+ }
|
|
|
+ }
|
|
|
|
|
|
/**
|
|
|
* 重新支付
|