lrf пре 2 година
родитељ
комит
56c37a4759
2 измењених фајлова са 33 додато и 15 уклоњено
  1. 29 13
      app/service/business/lesson.js
  2. 4 2
      app/service/wxpay.js

+ 29 - 13
app/service/business/lesson.js

@@ -14,7 +14,6 @@ class LessonService extends CrudService {
     this.lessonCoachModel = this.ctx.model.Business.LessonCoach;
     this.lessonStudentModel = this.ctx.model.Business.LessonStudent;
     this.coachInBillService = this.ctx.service.business.coachInBill;
-    this.lessonStudentModel = this.ctx.model.Business.LessonStudent;
     this.tempLessonApplyModel = this.ctx.model.Apply.TempLessonApply;
     this.rssModel = this.ctx.model.Relation.RelationStudentSchool;
     this.payOrderModel = this.ctx.model.Business.PayOrder;
@@ -31,11 +30,11 @@ class LessonService extends CrudService {
    */
   async afterUpdate(filter, body, data) {
     const { _id: lesson_id, type, status, school_id } = data;
-    if (status !== '4') return data;
     if (status === '-1') {
       await this.refundThisLesson(data);
       return data;
     }
+    if (status !== '4') return data;
     const arr = [];
 
     // if (type === '0') {
@@ -79,24 +78,29 @@ class LessonService extends CrudService {
     if (!lesson_id) return;
     try {
       // 取出学生id,支付单id,缴费金额
-      const studentList = await this.lessonStudentModel.find({ lesson_id, is_pay: '1', is_try: '0' }, 'student_id, pay_id, money, school_id');
+      const studentList = await this.lessonStudentModel.find({ lesson_id, is_pay: '1', is_try: '0' }, 'student_id pay_id money school_id');
       for (const s of studentList) {
         // 修改支付状态,返回金额
         const r = await this.refundByRrs(s, this.tran);
         // 没有关系,那就直接去用支付订单退款
-        if (!r) await this.refundByPayOrder(s, this.tran);
+        if (!r) {
+          await this.refundByPayOrder(s, this.tran);
+        }
       }
-      const tempLessonList = await this.tempLessonApplyModel.find({ lesson_id, is_pay: '1' }, 'student_id, school_id, money,pay_id,');
+      const tempLessonList = await this.tempLessonApplyModel.find({ lesson_id, is_pay: '1' }, 'student_id school_id money pay_id');
       for (const t of tempLessonList) {
         // 如果有学生id,说明是有账号的,退余额
         const r = await this.refundByTempLesson(t, this.tran);
         // 没有学生id,需要原路返回
         if (!r) await this.refundByPayOrder(t, this.tran);
       }
+
       await this.tran.run();
     } catch (error) {
+      console.log(error.data);
+      console.log(error.error);
+      console.log(error.executedTransactions);
       await this.tran.rollback();
-      console.error(error);
     } finally {
       this.tran.clean();
     }
@@ -108,15 +112,16 @@ class LessonService extends CrudService {
    */
   async refundByRrs(lessonStudent, tran) {
     const { school_id, student_id, pay_id, money, _id } = lessonStudent;
-    const rss = await this.rssModel.find({ school_id, student_id });
+    const rss = await this.rssModel.findOne({ school_id, student_id });
     if (!rss) return false;
     const surplus = _.get(rss, 'money');
     const newMoney = this.ctx.plus(surplus, money);
     // 退款
+    if (!rss._id)console.log(rss);
     tran.update('RelationStudentSchool', rss._id, { money: newMoney });
     // 改状态
     tran.update('LessonStudent', lessonStudent._id, { is_pay: '-3' });
-    tran.update('PayOrder', pay_id, { is_pay: '-3' });
+    tran.update('PayOrder', pay_id, { status: '-3' });
     // 生成账单数据
     const billData = {
       school_id,
@@ -132,6 +137,7 @@ class LessonService extends CrudService {
     };
     tran.insert('Bill', billData);
     return true;
+
   }
   /**
    * 退临时上课申请至余额
@@ -141,15 +147,16 @@ class LessonService extends CrudService {
   async refundByTempLesson(tempLessonApply, tran) {
     const { school_id, student_id, pay_id, money, _id } = tempLessonApply;
     if (!student_id) return false;
-    const rss = await this.rssModel.find({ school_id, student_id });
+    const rss = await this.rssModel.findOne({ school_id, student_id });
     if (!rss) return false;
     const surplus = _.get(rss, 'money');
     const newMoney = this.ctx.plus(surplus, money);
     // 退款
+    if (!rss._id) console.log(rss);
     tran.update('RelationStudentSchool', rss._id, { money: newMoney });
     // 改状态
     tran.update('TempLessonApply', _id, { is_pay: '-3' });
-    tran.update('PayOrder', pay_id, { is_pay: '-3' });
+    tran.update('PayOrder', pay_id, { status: '-3' });
     // 生成账单数据
     const billData = {
       school_id,
@@ -165,6 +172,7 @@ class LessonService extends CrudService {
     };
     tran.insert('Bill', billData);
     return true;
+
   }
 
   /**
@@ -176,13 +184,20 @@ class LessonService extends CrudService {
     const { pay_id, school_id } = data;
     const payOrder = await this.payOrderModel.findById(pay_id);
     if (!payOrder) throw new BusinessError(ErrorCode.DATA_NOT_EXIST, '未找到支付单信息');
-    const order_no = _.get(data, 'order_no');
+
+    const order_no = _.get(payOrder, 'order_no');
     const pay_for = _.get(payOrder, 'pay_for');
     const from_id = _.get(payOrder, 'from_id');
+    const money = _.get(payOrder, 'money');
     if (!order_no) throw new BusinessError(ErrorCode.DATA_NOT_EXIST, '未找到支付单号');
-    await this.payService.refund(order_no, '课程取消');
+    try {
+      await this.payService.refund(order_no, '课程取消', money);
+    } catch (error) {
+      console.error(error);
+      return false;
+    }
     // 修改状态
-    tran.update('PayOrder', pay_id, { is_pay: '-3' });
+    tran.update('PayOrder', pay_id, { status: '-3' });
     tran.update(pay_for, from_id, { is_pay: '-3' });
     // 生成账单
     const billData = {
@@ -193,6 +208,7 @@ class LessonService extends CrudService {
       pay_id,
       is_pay: '1',
       time: moment().format('YYYY-MM-DD HH:mm:ss'),
+      money,
     };
     tran.insert('Bill', billData);
   }

+ 4 - 2
app/service/wxpay.js

@@ -63,11 +63,13 @@ class WxpayService extends CrudService {
    * 退款
    * @param {String} order_no 订单号
    * @param {String} reason 原因
+   * @param {Number} money 退款金额
    */
-  async refund(order_no, reason) {
+  async refund(order_no, reason, money) {
     assert(order_no, '缺少订单号,无法查询订单信息');
     const url = `${this.wxDomain}/pay/refundOrder`;
-    const params = { config: this.appConfig, order_no, reason };
+    const out_refund_no = `${order_no}-r-${_.random(1, 999999)}`;
+    const params = { config: this.appConfig, order_no, out_refund_no, reason, money };
     const wxRefundReq = await this.httpUtil.cpost(url, params);
     return wxRefundReq || 'ok';
   }