|
@@ -17,12 +17,75 @@ class PayOrderService extends CrudService {
|
|
|
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;
|
|
|
}
|
|
|
|
|
|
async beforeCreate(data) {
|
|
|
if (!_.get(data, 'order_no')) data.order_no = this.getOrderNo();
|
|
|
+ const { data: nd, next } = await this.checkSurplus(data);
|
|
|
+ if (!next) throw new BusinessError(0, '扣除余额成功');
|
|
|
+ data = nd;
|
|
|
return data;
|
|
|
}
|
|
|
+
|
|
|
+ async checkSurplus(data) {
|
|
|
+ const pay_for = _.get(data, 'pay_for');
|
|
|
+
|
|
|
+ if (pay_for && pay_for === 'charge') return { data, next: true };
|
|
|
+ const payer_id = _.get(data, 'payer_id');
|
|
|
+ const payer_role = _.get(data, 'payer_role');
|
|
|
+ let costDetailData;
|
|
|
+ let relation;
|
|
|
+
|
|
|
+ if (!(payer_id && payer_role)) return { data, next: true };
|
|
|
+
|
|
|
+ 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 });
|
|
|
+
|
|
|
+ if (!relation) return { data, next: true };
|
|
|
+
|
|
|
+ const { money: surplus } = relation;
|
|
|
+ const { money } = data;
|
|
|
+
|
|
|
+ if (surplus && surplus <= 0) return { data, next: true };
|
|
|
+
|
|
|
+ if (surplus >= money) {
|
|
|
+ try {
|
|
|
+
|
|
|
+ const costDetail = _.pick(data, [ 'school_id', 'payer_id', 'payer_role', 'pay_for', 'from_id' ]);
|
|
|
+
|
|
|
+ costDetail.money = money;
|
|
|
+ costDetail.type = '1';
|
|
|
+ costDetailData = await this.costDetailModel.create(costDetail);
|
|
|
+
|
|
|
+ relation.money = surplus - money;
|
|
|
+ await relation.save();
|
|
|
+ return false;
|
|
|
+ } catch (error) {
|
|
|
+
|
|
|
+
|
|
|
+ if (costDetailData) {
|
|
|
+ await this.costDetailModel.deleteOne({ _id: costDetailData._id });
|
|
|
+ }
|
|
|
+
|
|
|
+ }
|
|
|
+ } else {
|
|
|
+
|
|
|
+ const needPay = surplus - money;
|
|
|
+ const costDetail = _.pick(data, [ 'school_id', 'payer_id', 'payer_role', 'pay_for', 'from_id' ]);
|
|
|
+ costDetail.money = surplus;
|
|
|
+ costDetail.type = '1';
|
|
|
+ if (!data.config) data.config = {};
|
|
|
+
|
|
|
+ data.config.useSurplus = true;
|
|
|
+ data.config.costDetail = costDetail;
|
|
|
+ data.money = needPay;
|
|
|
+ }
|
|
|
+
|
|
|
+ return { data, next: true };
|
|
|
+ }
|
|
|
|
|
|
async afterCreate(body, data) {
|
|
|
await this.syncData(data);
|
|
@@ -40,10 +103,8 @@ class PayOrderService extends CrudService {
|
|
|
*/
|
|
|
async syncData(data) {
|
|
|
const pay_for = _.get(data, 'pay_for');
|
|
|
- if (!pay_for) {
|
|
|
-
|
|
|
- return;
|
|
|
- }
|
|
|
+
|
|
|
+ if (!pay_for) return;
|
|
|
const { from_id: _id, status: is_pay, money, _id: pay_id, config } = data;
|
|
|
if (pay_for === 'lessonStudent') {
|
|
|
|
|
@@ -59,8 +120,16 @@ class PayOrderService extends CrudService {
|
|
|
const obj = { lesson_id, student_id, school_id, is_pay, pay_id, config, money };
|
|
|
await this.lessonStudentService.create(obj);
|
|
|
} else if (pay_for === 'charge') {
|
|
|
-
|
|
|
- await this.chargeModel.updateOne({ _id }, { is_pay });
|
|
|
+
|
|
|
+ const chargeData = await this.chargeModel.findOne({ pay_id });
|
|
|
+ if (chargeData) {
|
|
|
+
|
|
|
+ chargeData.is_pay = is_pay;
|
|
|
+ await chargeData.save();
|
|
|
+ } else {
|
|
|
+ const obj = _.pick(data, [ 'school_id', 'payer_id', 'payer_role', 'pay_id' ]);
|
|
|
+ await this.chargeModel.create({ ...obj, is_pay, time: moment().format('YYYY-MM-DD HH:mm:ss') });
|
|
|
+ }
|
|
|
}
|
|
|
}
|
|
|
|