lrf 2 år sedan
förälder
incheckning
db1975426b

+ 13 - 0
app/controller/business/bill.js

@@ -0,0 +1,13 @@
+'use strict';
+const meta = require('./config/.bill.js');
+const Controller = require('egg').Controller;
+const { CrudController } = require('naf-framework-mongoose-free/lib/controller');
+
+//
+class BillController extends Controller {
+  constructor(ctx) {
+    super(ctx);
+    this.service = this.ctx.service.business.bill;
+  }
+}
+module.exports = CrudController(BillController, meta);

+ 4 - 2
app/controller/business/config/.costDetail.js

@@ -1,6 +1,6 @@
 module.exports = {
   create: {
-    requestBody: ['school_id', 'payer_role', 'payer_id', 'pay_for', 'from_id', 'type', 'pay_id'],
+    requestBody: ['school_id', 'payer_role', 'payer_id', 'pay_for', 'from_id', 'type', 'pay_id', 'is_pay', 'time'],
   },
   destroy: {
     params: ['!id'],
@@ -8,7 +8,7 @@ module.exports = {
   },
   update: {
     params: ['!id'],
-    requestBody: ['school_id', 'payer_role', 'payer_id', 'pay_for', 'from_id', 'type', 'pay_id'],
+    requestBody: ['school_id', 'payer_role', 'payer_id', 'pay_for', 'from_id', 'type', 'pay_id', 'is_pay', 'time'],
   },
   show: {
     parameters: {
@@ -28,6 +28,8 @@ module.exports = {
         from_id: 'from_id',
         type: 'type',
         pay_id: 'pay_id',
+        is_pay: 'is_pay',
+        time: 'time',
       },
       // options: {
       //   "meta.state": 0 // 默认条件

+ 0 - 13
app/controller/business/costDetail.js

@@ -1,13 +0,0 @@
-'use strict';
-const meta = require('./config/.costDetail.js');
-const Controller = require('egg').Controller;
-const { CrudController } = require('naf-framework-mongoose-free/lib/controller');
-
-// 
-class CostDetailController extends Controller {
-  constructor(ctx) {
-    super(ctx);
-    this.service = this.ctx.service.costDetail;
-  }
-}
-module.exports = CrudController(CostDetailController, meta);

+ 41 - 0
app/model/business/bill.js

@@ -0,0 +1,41 @@
+'use strict';
+const Schema = require('mongoose').Schema;
+const metaPlugin = require('naf-framework-mongoose-free/lib/model/meta-plugin');
+const MoneyPlugin = require('naf-framework-mongoose-free/lib/model/type-money-plugin');
+
+// 账单
+// 1,充值:type为1, 缺少 from_id,pay_for为Bill; 因为没有来源;
+// 2,退款至余额:type为2, pay_for:tempLessonApply/lessonStudent,有from_id, 有pay_id和is_pay
+// 3,直接支付:type为-1 pay_for:tempLessonApply/lessonStudent,有from_id,有pay_id和is_pay
+// 4,余额支付:type为-2 pay_for:tempLessonApply/lessonStudent,有from_id,有pay_id和is_pay
+const bill = {
+  school_id: { type: String, required: false, zh: '学校id', ref: 'School', getProp: ['name'] }, //
+  payer_role: { type: String, required: false, zh: '用户角色' }, //
+  payer_id: { type: String, required: false, zh: '用户角色id', refPath: 'payer_role' }, //
+  pay_for: { type: String, required: false, zh: '原因' }, //
+  from_id: { type: String, required: false, zh: '原因id', refPath: 'pay_for' }, //
+  type: { type: String, required: false, zh: '来源' }, // 1:充值;2退款至余额;-1:直接支付;-2余额支付
+  pay_id: { type: String, required: false, zh: '支付id', ref: 'PayOrder' }, //
+  is_pay: { type: String, required: false, default: '0', zh: '是否支付' }, //  0:未支付;1:支付成功;-1:支付失败;-3:已退款
+  time: { type: String, required: false, zh: '时间' }, //
+};
+const schema = new Schema(bill, { toJSON: { getters: true, virtuals: true } });
+schema.index({ id: 1 });
+schema.index({ 'meta.createdAt': 1 });
+schema.index({ school_id: 1 });
+schema.index({ payer_role: 1 });
+schema.index({ payer_id: 1 });
+schema.index({ pay_for: 1 });
+schema.index({ from_id: 1 });
+schema.index({ type: 1 });
+schema.index({ pay_id: 1 });
+schema.index({ is_pay: 1 });
+schema.index({ time: 1 });
+
+schema.plugin(metaPlugin);
+schema.plugin(MoneyPlugin({ zh: '金额', required: false }));
+
+module.exports = app => {
+  const { mongoose } = app;
+  return mongoose.model('Bill', schema, 'bill');
+};

+ 1 - 0
app/model/business/charge.js

@@ -10,6 +10,7 @@ const charge = {
   payer_role: { type: String, required: false, zh: '充值用户角色' }, //
   pay_id: { type: String, required: false, zh: '支付id', ref: 'Payorder' }, //
   is_pay: { type: String, required: false, zh: '支付状态' }, // 0:未支付;1:支付成功;-1:支付失败;-3:已退款
+  type: { type: String, default: '0', zh: '类型' }, // 0:充值;1:退款至余额
   time: { type: String, required: false, zh: '时间' }, //
 };
 const schema = new Schema(charge, { toJSON: { getters: true, virtuals: true } });

+ 1 - 1
app/model/business/payOrder.js

@@ -4,7 +4,7 @@ const metaPlugin = require('naf-framework-mongoose-free/lib/model/meta-plugin');
 const moneyPlugin = require('naf-framework-mongoose-free/lib/model/type-money-plugin');
 const config = {
   useSurplus: true || false, // 是否使用余额(有该字段就是用了,没有默认)
-  costDetail: Object || String('id'), // 消费记录(余额对应的消费记录数据/id)
+  bill: Object || String('id'), // 账单记录(余额对应的消费记录数据/id)
   discoun_type: 'fixed' || 'subtract' || 'discount', // 打折类型, 学员与教练的设置
   number: Number, // 打折数值 学员与教练的设置
 };

+ 1 - 2
app/router.js

@@ -39,8 +39,7 @@ module.exports = app => {
   require('./z_router/business/lessonStudent')(app); // 课程-学员
   require('./z_router/business/coachInBill')(app); // 教练费明细
   require('./z_router/business/payOrder')(app); // 支付订单
-  require('./z_router/business/charge')(app); // 充值
-  require('./z_router/business/costDetail')(app); // 消费明细
+  require('./z_router/business/bill')(app); // 账单
 
   require('./z_router/apply/billApply')(app); // 教练费申请
   require('./z_router/apply/tryLessonApply')(app); // 试课申请

+ 15 - 0
app/service/business/bill.js

@@ -0,0 +1,15 @@
+'use strict';
+const { CrudService } = require('naf-framework-mongoose-free/lib/service');
+const { BusinessError, ErrorCode } = require('naf-core').Error;
+const _ = require('lodash');
+const assert = require('assert');
+
+//
+class BillService extends CrudService {
+  constructor(ctx) {
+    super(ctx, 'bill');
+    this.model = this.ctx.model.Business.Bill;
+  }
+}
+
+module.exports = BillService;

+ 0 - 1
app/service/business/lesson.js

@@ -47,7 +47,6 @@ class LessonService extends CrudService {
       obj.money = total;
       arr.push(obj);
     }
-
     // 创建,如果有该学校,这节课的这个教练已经有数据了,就不需要生成了
     for (const i of arr) {
       try {

+ 15 - 0
app/service/business/lessonStudent.js

@@ -80,6 +80,21 @@ class LessonStudentService extends CrudService {
 
     return data;
   }
+
+  // 退课,将钱退至余额
+  async toRefund({ lesson_id, student_id }) {
+    const data = await this.model.findOne({ lesson_id, student_id });
+    if (!data) throw new BusinessError(ErrorCode.DATA_NOT_EXIST, '未找到数据');
+    if (data.is_try === '1') {
+      // 试课 修改退款状态, 而不需要退钱,同时也浪费了试课机会
+      data.is_pay = '-3';
+      await data.save();
+      return;
+    }
+    // 正常交钱的课,将 pay_id 的status 修改为 -3, 同时触发修改这条数据,但是没有退款;
+    // 再将金额打到充值表里,
+
+  }
 }
 
 module.exports = LessonStudentService;

+ 27 - 25
app/service/business/payOrder.js

@@ -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);
       }
     }

+ 3 - 3
app/z_router/business/charge.js

@@ -3,9 +3,9 @@
 const path = require('path');
 const regPath = path.resolve('app', 'public', 'routerRegister');
 const routerRegister = require(regPath);
-const rkey = 'charge';
-const ckey = 'business.charge';
-const keyZh = '充值';
+const rkey = 'bill';
+const ckey = 'business.bill';
+const keyZh = '账单';
 const routes = [
   { method: 'get', path: `${rkey}`, controller: `${ckey}.index`, name: `${ckey}Query`, zh: `${keyZh}列表查询` },
   { method: 'get', path: `${rkey}/:id`, controller: `${ckey}.show`, name: `${ckey}Show`, zh: `${keyZh}查询` },

+ 0 - 19
app/z_router/business/costDetail.js

@@ -1,19 +0,0 @@
-'use strict';
-// 路由配置
-const path = require('path');
-const regPath = path.resolve('app', 'public', 'routerRegister');
-const routerRegister = require(regPath);
-const rkey = 'costDetail';
-const ckey = 'business.costDetail';
-const keyZh = '消费明细';
-const routes = [
-  { method: 'get', path: `${rkey}`, controller: `${ckey}.index`, name: `${ckey}Query`, zh: `${keyZh}列表查询` },
-  { method: 'get', path: `${rkey}/:id`, controller: `${ckey}.show`, name: `${ckey}Show`, zh: `${keyZh}查询` },
-  { method: 'post', path: `${rkey}`, controller: `${ckey}.create`, name: `${ckey}Create`, zh: `创建${keyZh}` },
-  { method: 'post', path: `${rkey}/:id`, controller: `${ckey}.update`, name: `${ckey}Update`, zh: `修改${keyZh}` },
-  { method: 'delete', path: `${rkey}/:id`, controller: `${ckey}.destroy`, name: `${ckey}Delete`, zh: `删除${keyZh}` },
-];
-
-module.exports = app => {
-  routerRegister(app, routes, keyZh, rkey, ckey);
-};