lrf 2 anni fa
parent
commit
2cecb708cc

+ 3 - 2
app/controller/business/config/.coachInBill.js

@@ -1,6 +1,6 @@
 module.exports = {
   create: {
-    requestBody: ['lesson_id', 'type', 'school_id', 'coach_id', 'money'],
+    requestBody: ['is_settle', 'lesson_id', 'type', 'school_id', 'coach_id', 'money'],
   },
   destroy: {
     params: ['!id'],
@@ -8,7 +8,7 @@ module.exports = {
   },
   update: {
     params: ['!id'],
-    requestBody: ['lesson_id', 'type', 'school_id', 'coach_id', 'money'],
+    requestBody: ['is_settle', 'lesson_id', 'type', 'school_id', 'coach_id', 'money'],
   },
   show: {
     parameters: {
@@ -23,6 +23,7 @@ module.exports = {
         'meta.createdAt@end': 'meta.createdAt@end',
         school_id: 'school_id',
         coach_id: 'coach_id',
+        is_settle: 'is_settle',
       },
       // options: {
       //   "meta.state": 0 // 默认条件

+ 3 - 1
app/model/business/coachInBill.js

@@ -6,15 +6,17 @@ const moneyPlugin = require('naf-framework-mongoose-free/lib/model/type-money-pl
 // 教练费明细
 const coachInBill = {
   lesson_id: { type: String, required: false, zh: '课程id' }, //
-  type: { type: String, required: false, zh: '类型' }, // 0:公开课收入;1私教课收入;2提现
+  type: { type: String, required: false, zh: '类型' }, // 0:公开课收入;1私教课收入
   school_id: { type: String, required: false, zh: '学校id', ref: 'School', getProp: [ 'name' ] }, //
   coach_id: { type: String, required: false, zh: '教练id', ref: 'Coach', getProp: [ 'name' ] }, //
+  is_settle: { type: String, required: false, default: '0', zh: '是否结算' }, // 0:未结算;1已结算
 };
 const schema = new Schema(coachInBill, { toJSON: { getters: true, virtuals: true } });
 schema.index({ id: 1 });
 schema.index({ 'meta.createdAt': 1 });
 schema.index({ school_id: 1 });
 schema.index({ coach_id: 1 });
+schema.index({ is_settle: 1 });
 
 schema.plugin(metaPlugin);
 schema.plugin(moneyPlugin({ zh: '金额' }));

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

@@ -2,7 +2,12 @@
 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');
-
+const config = {
+  useSurplus: true || false, // 是否使用余额(有该字段就是用了,没有默认)
+  costDetail: Object || String('id'), // 消费记录(余额对应的消费记录数据/id)
+  discoun_type: 'fixed' || 'subtract' || 'discount', // 打折类型, 学员与教练的设置
+  number: Number, // 打折数值 学员与教练的设置
+};
 // 付款订单表
 const payOrder = {
   openid: { type: String, required: false, zh: '微信id' }, // 其实和user_id有些重复,但是这里一定要留痕,因为有可能发生更换绑定

+ 14 - 14
app/service/business/coachInBill.js

@@ -18,20 +18,20 @@ class CoachInBillService extends CrudService {
     if (num > 0) throw new BusinessError(ErrorCode.DATA_EXISTED, '已经存在 该节课已经存在明细,请核对数据');
     return body;
   }
-  // 改工资
-  async afterCreate(body, data) {
-    const { money } = data;
-    const query = _.pick(data, [ 'school_id', 'coach_id' ]);
-    // 查学校与教练的关联数据
-    const rcsData = await this.rcsModel.findOne(query);
-    if (!rcsData) throw new BusinessError(ErrorCode.DATA_NOT_EXIST, '未找到教练在该学校下任教的信息');
-    // 累加工资
-    let salary = _.get(rcsData, 'salary', 0);
-    salary += money;
-    rcsData.salary = salary;
-    await rcsData.save();
-    return data;
-  }
+  // 改工资(结算变为周期性结算,而不是实时性结算)
+  // async afterCreate(body, data) {
+  //   const { money } = data;
+  //   const query = _.pick(data, [ 'school_id', 'coach_id' ]);
+  //   // 查学校与教练的关联数据
+  //   const rcsData = await this.rcsModel.findOne(query);
+  //   if (!rcsData) throw new BusinessError(ErrorCode.DATA_NOT_EXIST, '未找到教练在该学校下任教的信息');
+  //   // 累加工资
+  //   let salary = _.get(rcsData, 'salary', 0);
+  //   salary += money;
+  //   rcsData.salary = salary;
+  //   await rcsData.save();
+  //   return data;
+  // }
 }
 
 module.exports = CoachInBillService;

+ 36 - 3
app/service/business/lesson.js

@@ -9,6 +9,9 @@ class LessonService extends CrudService {
   constructor(ctx) {
     super(ctx, 'lesson');
     this.model = this.ctx.model.Business.Lesson;
+    this.lessonCoachModel = this.ctx.model.Business.LessonCoach;
+    this.lessonStudentModel = this.ctx.model.Business.LessonStudent;
+    this.coachInBillService = this.ctx.service.business.coachInBill;
   }
 
   /**
@@ -19,10 +22,40 @@ class LessonService extends CrudService {
    * @return {Object} 返回整理后的内容
    */
   async afterUpdate(filter, body, data) {
-    const { _id: lesson_id, type, status } = data;
+    const { _id: lesson_id, type, status, school_id } = data;
     if (status !== '4') return data;
-    // 公开课,每个教师都带着自己的金额. 按 人头数 * 单价 算钱
-    // 私教课, 按学生实际缴费
+    const arr = [];
+
+    if (type === '0') {
+      // 公开课,每个教师都带着自己的金额. 按 人头数 * 单价 算钱
+      const lessonCoachList = await this.lessonCoachModel.find({ lesson_id });
+      // 这节课付了款的学生总数
+      const studentNum = await this.lessonStudentModel.count({ lesson_id, is_pay: '1' });
+      const obj = { lesson_id, type, school_id };
+      for (const c of lessonCoachList) {
+        const { coach_id, money } = c;
+        const total = money * studentNum;
+        arr.push({ ...obj, coach_id, money: total });
+      }
+    } else {
+      // 私教课, 按学生实际缴费
+      const lessonCoach = await this.lessonCoachModel.findOne({ lesson_id });
+      const studentList = await this.lessonStudentModel.find({ lesson_id });
+      const obj = { lesson_id, type, school_id };
+      obj.coach_id = lessonCoach.coach_id;
+      const total = studentList.reduce((p, n) => p + (n.money || 0), 0);
+      obj.money = total;
+      arr.push(obj);
+    }
+
+    // 创建,如果有该学校,这节课的这个教练已经有数据了,就不需要生成了
+    for (const i of arr) {
+      try {
+        await this.coachInBillService.create(i);
+      } catch (error) {
+        // 不是数据有问题,就是有数据了,要不一般不会出错
+      }
+    }
 
     return data;
   }

+ 20 - 0
app/service/business/payOrder.js

@@ -4,6 +4,7 @@ const { BusinessError, ErrorCode } = require('naf-core').Error;
 const _ = require('lodash');
 const assert = require('assert');
 const moment = require('moment');
+const { ObjectId } = require('mongoose').Types;
 //
 class PayOrderService extends CrudService {
   constructor(ctx) {
@@ -109,6 +110,8 @@ class PayOrderService extends CrudService {
     if (pay_for === 'lessonStudent') {
       // 因为上课产生的支付,去找lessonStudent,修改指定学生的支付状态
       await this.lessonStudentModel.updateOne({ _id }, { is_pay });
+      // 检查下各种记录
+      await this.makeRecord(data);
     } else if (pay_for === 'tempLessonApply') {
       // 私教课临时上课,需要到临时申请那找到相关数据
       const tempApply = await this.tempLessonApplyModel.findById(_id);
@@ -119,6 +122,7 @@ class PayOrderService extends CrudService {
       const { lesson_id, student_id, school_id } = tempApply;
       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') {
       // 充值记录,找到充值记录,没有就生成
       const chargeData = await this.chargeModel.findOne({ pay_id });
@@ -132,6 +136,22 @@ class PayOrderService extends CrudService {
       }
     }
   }
+  // 检查 消费记录
+  async makeRecord(data) {
+    const { config, _id: pay_id } = data;
+    // 消费记录
+    if (_.get(config, 'useSurplus')) {
+      const { costDetail } = config;
+      // 使用了余额,但是余额记录不是直接生成的,需要检查下costDetail是否为ObjectId
+      // 如果是ObjectId,说明余额记录已经生成.无需操作
+      if (_.isObject(costDetail)) {
+        // 是数据,生成消费记录
+        const cdd = await this.costDetailModel.create({ ...costDetail, pay_id });
+        if (cdd) data.config.costDetail = ObjectId(cdd._id).toString();
+        await this.model.updateOne({ _id: data._id }, data);
+      }
+    }
+  }
 
   /**
    * 重新支付