|
@@ -13,14 +13,16 @@ class PayOrderService extends CrudService {
|
|
|
this.payService = this.ctx.service.wxpay;
|
|
|
this.lessonStudentModel = this.ctx.model.Business.LessonStudent;
|
|
|
this.lessonStudentService = this.ctx.service.business.lessonStudent;
|
|
|
- this.chargeModel = this.ctx.model.Business.Charge;
|
|
|
this.tempLessonApplyModel = this.ctx.model.Apply.TempLessonApply;
|
|
|
this.userModel = this.ctx.model.User.User;
|
|
|
this.studentModel = this.ctx.model.User.Student;
|
|
|
this.coachModel = this.ctx.model.User.Coach;
|
|
|
this.rcsModel = this.ctx.model.Relation.RelationCoachSchool;
|
|
|
this.rssModel = this.ctx.model.Relation.RelationStudentSchool;
|
|
|
- this.costDetailModel = this.ctx.model.Business.CostDetail;
|
|
|
+
|
|
|
+ this.billModel = this.ctx.model.Business.Bill;
|
|
|
+ // this.costDetailModel = this.ctx.model.Business.CostDetail;
|
|
|
+ // this.chargeModel = this.ctx.model.Business.Charge;
|
|
|
}
|
|
|
|
|
|
async beforeCreate(data) {
|
|
@@ -34,10 +36,10 @@ class PayOrderService extends CrudService {
|
|
|
async checkSurplus(data) {
|
|
|
const pay_for = _.get(data, 'pay_for');
|
|
|
// 充值不检查余额
|
|
|
- if (pay_for && pay_for === 'charge') return { data, next: true };
|
|
|
+ if (pay_for && pay_for === 'Bill') return { data, next: true };
|
|
|
const payer_id = _.get(data, 'payer_id');
|
|
|
const payer_role = _.get(data, 'payer_role');
|
|
|
- let costDetailData;
|
|
|
+ let billData;
|
|
|
let relation;
|
|
|
// 没有要支付的人及其角色,那就找不到余额,返回下单
|
|
|
if (!(payer_id && payer_role)) return { data, next: true };
|
|
@@ -55,33 +57,33 @@ class PayOrderService extends CrudService {
|
|
|
if (surplus >= money) {
|
|
|
try {
|
|
|
// 余额够, 无需生成订单,直接去使用余额缴费
|
|
|
- const costDetail = _.pick(data, [ 'school_id', 'payer_id', 'payer_role', 'pay_for', 'from_id' ]);
|
|
|
+ const bill = _.pick(data, [ 'school_id', 'payer_id', 'payer_role', 'pay_for', 'from_id' ]);
|
|
|
// 本次扣除余额的金额就是本次需要支付的金额
|
|
|
- costDetail.money = money;
|
|
|
- costDetail.type = '1';
|
|
|
- costDetailData = await this.costDetailModel.create(costDetail);
|
|
|
+ bill.money = money;
|
|
|
+ bill.type = '-2';
|
|
|
+ billData = await this.billModel.create({ ...bill, time: moment().format('YYYY-MM-DD HH:mm:ss') });
|
|
|
// 余额需要扣除本次支付的费用
|
|
|
relation.money = surplus - money;
|
|
|
await relation.save();
|
|
|
return false;
|
|
|
} catch (error) {
|
|
|
// 抓取数据库创建和修改的异常,
|
|
|
- // 如果 costDetailData没有值,说明是创建消费记录时发生的错误,不需要做回滚处理;如果有值,那就删除
|
|
|
- if (costDetailData) {
|
|
|
- await this.costDetailModel.deleteOne({ _id: costDetailData._id });
|
|
|
+ // 如果 billData没有值,说明是创建消费记录时发生的错误,不需要做回滚处理;如果有值,那就删除
|
|
|
+ if (billData) {
|
|
|
+ await this.billModel.deleteOne({ _id: billData._id });
|
|
|
}
|
|
|
// 如果是余额修改错误,上面的回滚删除就足够了.因为保存新余额发生错误,余额不会保存,只要把消费记录删了,就回到原样了
|
|
|
}
|
|
|
} else {
|
|
|
// 余额不够,在config里记录部分使用了余额,再进行下单,下单完后,生成余额的扣款
|
|
|
const needPay = surplus - money;
|
|
|
- const costDetail = _.pick(data, [ 'school_id', 'payer_id', 'payer_role', 'pay_for', 'from_id' ]);
|
|
|
- costDetail.money = surplus;
|
|
|
- costDetail.type = '1';
|
|
|
+ const bill = _.pick(data, [ 'school_id', 'payer_id', 'payer_role', 'pay_for', 'from_id' ]);
|
|
|
+ bill.money = surplus;
|
|
|
+ bill.type = '-2';
|
|
|
if (!data.config) data.config = {};
|
|
|
// 添加余额的设置, 等支付成功后,再生成余额的消费记录
|
|
|
data.config.useSurplus = true;
|
|
|
- data.config.costDetail = costDetail;
|
|
|
+ data.config.bill = bill;
|
|
|
data.money = needPay;
|
|
|
}
|
|
|
|
|
@@ -123,16 +125,16 @@ class PayOrderService extends CrudService {
|
|
|
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') {
|
|
|
+ } else if (pay_for === 'Bill' && is_pay !== '0') {
|
|
|
// 充值记录,找到充值记录,没有就生成
|
|
|
- const chargeData = await this.chargeModel.findOne({ pay_id });
|
|
|
- if (chargeData) {
|
|
|
+ const data = await this.billModel.findOne({ pay_id });
|
|
|
+ if (data) {
|
|
|
// 有就修改
|
|
|
- chargeData.is_pay = is_pay;
|
|
|
- await chargeData.save();
|
|
|
+ data.is_pay = is_pay;
|
|
|
+ await data.save();
|
|
|
} else {
|
|
|
const obj = _.pick(data, [ 'school_id', 'payer_id', 'payer_role' ]);
|
|
|
- await this.chargeModel.create({ ...obj, pay_id, is_pay, time: moment().format('YYYY-MM-DD HH:mm:ss') });
|
|
|
+ await this.billModel.create({ ...obj, pay_for: 'Bill', pay_id, is_pay, time: moment().format('YYYY-MM-DD HH:mm:ss'), type: '1' });
|
|
|
}
|
|
|
}
|
|
|
}
|
|
@@ -141,13 +143,13 @@ class PayOrderService extends CrudService {
|
|
|
const { config, _id: pay_id } = data;
|
|
|
// 消费记录
|
|
|
if (_.get(config, 'useSurplus')) {
|
|
|
- const { costDetail } = config;
|
|
|
+ const { bill } = config;
|
|
|
// 使用了余额,但是余额记录不是直接生成的,需要检查下costDetail是否为ObjectId
|
|
|
// 如果是ObjectId,说明余额记录已经生成.无需操作
|
|
|
- if (_.isObject(costDetail)) {
|
|
|
+ if (_.isObject(bill)) {
|
|
|
// 是数据,生成消费记录
|
|
|
- const cdd = await this.costDetailModel.create({ ...costDetail, pay_id });
|
|
|
- if (cdd) data.config.costDetail = ObjectId(cdd._id).toString();
|
|
|
+ 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();
|
|
|
await this.model.updateOne({ _id: data._id }, data);
|
|
|
}
|
|
|
}
|