lrf 2 years ago
parent
commit
d4c4405b92
2 changed files with 32 additions and 6 deletions
  1. 14 5
      app/service/matchSign.js
  2. 18 1
      app/service/payOrder.js

+ 14 - 5
app/service/matchSign.js

@@ -3,7 +3,7 @@ const { CrudService } = require('naf-framework-mongoose-free/lib/service');
 const { BusinessError, ErrorCode } = require('naf-core').Error;
 const _ = require('lodash');
 const assert = require('assert');
-
+const moment = require('moment');
 //
 class MatchSignService extends CrudService {
   constructor(ctx) {
@@ -12,6 +12,7 @@ class MatchSignService extends CrudService {
     this.matchModel = this.ctx.model.Race.Match;
     this.matchGroupModel = this.ctx.model.Race.MatchGroup;
     this.matchProjectModel = this.ctx.model.Race.MatchProject;
+    this.billModel = this.ctx.model.Race.Bill;
   }
   /**
    * 检查是否已经有该赛事,该组别,该项目的报名
@@ -21,6 +22,18 @@ class MatchSignService extends CrudService {
     await this.checkHas(body);
     return body;
   }
+  // 退赛
+  async afterUpdate(filter, body, data) {
+    const { pay_status } = data;
+    if (pay_status !== '-3') return data;
+    // 线下管理人员允许退款,生成账单
+    const obj = _.pick(data, [ 'match_id', 'group_id', 'project_id' ]);
+    obj.payer_id = data.user_id;
+    obj.time = moment().format('YYYY-MM-DD HH:mm:ss');
+    obj.type = '-1';
+    await this.billModel.create(obj);
+    return data;
+  }
 
   /**
    * 检查有没有报过名
@@ -59,7 +72,6 @@ class MatchSignService extends CrudService {
       if (gender !== cardGender) throw new BusinessError(ErrorCode.DATA_INVALID, '性别不符合组别条件');
     }
     return true;
-
   }
 
   checkAgeInRange(age, cardAge) {
@@ -172,16 +184,13 @@ class MatchSignService extends CrudService {
         return '1';
       }
       return '0';
-
     } else if (idCard.length === 18) {
       if (idCard.substring(14, 17) % 2 === 0) {
         return '1';
       }
       return '0';
-
     }
     return null;
-
   }
   // 年龄
   getAge(idCard) {

+ 18 - 1
app/service/payOrder.js

@@ -11,6 +11,8 @@ class PayOrderService extends CrudService {
     super(ctx, 'payorder');
     this.model = this.ctx.model.Race.PayOrder;
     this.payService = this.ctx.service.wxpay;
+    this.matchSignModel = this.ctx.model.Race.MatchSign;
+    this.billModel = this.ctx.model.Race.Bill;
   }
   async beforeCreate(data) {
     if (!_.get(data, 'order_no')) data.order_no = this.getOrderNo();
@@ -33,7 +35,22 @@ class PayOrderService extends CrudService {
     if (status === '0') return data;
     // 支付失败也不做处理
     if (status === '-1') return data;
-    // 只有支付成功和退款才进行操作
+    // 只有支付成功和退款才进行操作;不过这个退款是走线下,其实也没有
+    if (status === '-3') throw new BusinessError(ErrorCode.DATA_INVALID, '退款应该是线下退款');
+    const obj = _.pick(data, [ 'match_id', 'group_id', 'project_id' ]);
+    const { _id: pay_id, status: pay_status, payer_id: user_id } = data;
+    // 报名数据
+    const signData = { ...obj, pay_id, pay_status, user_id };
+    // 账单数据
+    const billData = { ...obj, pay_id, payer_id: data.payer_id, type: '1', time: moment().format('YYYY-MM-DD HH:mm:ss'), is_pay: pay_status };
+    // 生成报名数据,与账单数据. 如果失败,则删除报名数据并提示联系管理员
+    const sign = await this.matchSignModel.create(signData);
+    try {
+      await this.billModel.create(billData);
+    } catch (error) {
+      await this.matchSignModel.deleteOne({ _id: sign._id });
+      throw new BusinessError(ErrorCode.SERVICE_FAULT, '支付失败,请联系管理员处理该报名情况');
+    }
   }
 
   getOrderNo() {