|
@@ -119,9 +119,11 @@ class PayOrderService extends CrudService {
|
|
|
tempApply.pay_id = pay_id;
|
|
|
await tempApply.save();
|
|
|
// 修改完申请,再创建 lessonStudent
|
|
|
- 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);
|
|
|
+ if (is_pay === '1') {
|
|
|
+ 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 === 'Bill' && is_pay !== '0') {
|
|
|
// 充值记录,找到充值记录,没有就生成
|
|
@@ -136,19 +138,32 @@ class PayOrderService extends CrudService {
|
|
|
}
|
|
|
}
|
|
|
}
|
|
|
- // 检查 消费记录
|
|
|
+ // 检查记录
|
|
|
async makeRecord(data) {
|
|
|
- const { config, _id: pay_id } = data;
|
|
|
- // 消费记录
|
|
|
+ const { config, _id: pay_id, status: is_pay } = data;
|
|
|
+ let billData;
|
|
|
+ // 使用余额的处理
|
|
|
if (_.get(config, 'useSurplus')) {
|
|
|
const { bill } = config;
|
|
|
- // 使用了余额,但是余额记录不是直接生成的,需要检查下costDetail是否为ObjectId
|
|
|
+ // 使用了余额,但是余额记录不是直接生成的,需要检查下bill是否为ObjectId
|
|
|
// 如果是ObjectId,说明余额记录已经生成.无需操作
|
|
|
if (_.isObject(bill)) {
|
|
|
- // 是数据,生成消费记录
|
|
|
- const cdd = await this.billModel.create({ ...bill, pay_id, time: moment().format('YYYY-MM-DD HH:mm:ss') });
|
|
|
- if (cdd) data.config.bill = ObjectId(cdd._id).toString();
|
|
|
+ // 是数据,生成账单记录
|
|
|
+ billData = await this.billModel.create({ ...bill, pay_id, is_pay, time: moment().format('YYYY-MM-DD HH:mm:ss') });
|
|
|
+ if (billData) data.config.bill = ObjectId(billData._id).toString();
|
|
|
await this.model.updateOne({ _id: data._id }, data);
|
|
|
+ } else {
|
|
|
+ // 已经有数据了, 修改账单.并根据is_pay去做相应的处理
|
|
|
+ await this.billModel.updateOne({ _id: data.config.bill }, { is_pay });
|
|
|
+ billData = await this.billModel.findById(data.config.bill);
|
|
|
+ }
|
|
|
+ const { type, payer_role, payer_id, money } = billData;
|
|
|
+ if (type === '-2') {
|
|
|
+ let relation;
|
|
|
+ if (payer_role === 'Student') relation = await this.rssModel.findOne({ student_id: payer_id });
|
|
|
+ else if (payer_role === 'Coach') relation = await this.rcsModel.findOne({ coach_id: payer_id });
|
|
|
+ relation.money = relation.money - money;
|
|
|
+ await relation.save();
|
|
|
}
|
|
|
}
|
|
|
}
|