lrf 2 年之前
父节点
当前提交
6647d245b0

+ 6 - 2
app/controller/business/config/.lessonStudent.js

@@ -1,6 +1,6 @@
 module.exports = {
   create: {
-    requestBody: ['is_pay', 'lesson_id', 'school_id', 'student_id', 'money', 'is_try', 'try_status'],
+    requestBody: ['pay_id', 'is_pay', 'lesson_id', 'school_id', 'student_id', 'money', 'is_try', 'try_status'],
   },
   destroy: {
     params: ['!id'],
@@ -8,7 +8,7 @@ module.exports = {
   },
   update: {
     params: ['!id'],
-    requestBody: ['is_pay', 'lesson_id', 'school_id', 'student_id', 'money', 'is_try', 'try_status'],
+    requestBody: ['pay_id', 'is_pay', 'lesson_id', 'school_id', 'student_id', 'money', 'is_try', 'try_status'],
   },
   show: {
     parameters: {
@@ -28,6 +28,7 @@ module.exports = {
         is_try: 'is_try',
         try_status: 'try_status',
         is_pay: 'is_pay',
+        pay_id: 'pay_id',
       },
       // options: {
       //   "meta.state": 0 // 默认条件
@@ -41,4 +42,7 @@ module.exports = {
       count: true,
     },
   },
+  reComputed: {
+    params: ['!id'],
+  },
 };

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

@@ -11,6 +11,8 @@ const lessonStudent = {
   is_try: { type: String, required: false, default: '0', zh: '是否试课' }, // 0:非试课;1:试课
   try_status: { type: String, default: '0', zh: '试课审核状态' }, // 0:未审核;1:审核通过;2审核拒绝
   is_pay: { type: String, default: '0', zh: '是否已支付' }, // 0:未支付;1:已支付:-1:支付失败:-2已退款
+  pay_id: { type: Array, required: false, zh: '支付id', ref: 'PayOrder' }, //
+  config: { type: Array, required: false, zh: '学生与教练关系表的设置快照' }, //
 };
 const schema = new Schema(lessonStudent, { toJSON: { virtuals: true } });
 schema.index({ id: 1 });
@@ -18,7 +20,7 @@ schema.index({ 'meta.createdAt': 1 });
 schema.index({ lesson_id: 1 });
 schema.index({ school_id: 1 });
 schema.index({ student_id: 1 });
-schema.index({ money: 1 });
+schema.index({ pay_id: 1 });
 schema.index({ is_try: 1 });
 schema.index({ is_pay: 1 });
 

+ 7 - 2
app/service/apply/tempLessonApply.js

@@ -10,6 +10,7 @@ class TempLessonApplyService extends CrudService {
     super(ctx, 'templessonapply');
     this.model = this.ctx.model.Apply.TempLessonApply;
     this.payOrderService = this.ctx.service.business.payOrder;
+    this.lessonStudentService = this.ctx.service.business.lessonStudent;
   }
 
   async beforeCreate(data) {
@@ -39,8 +40,12 @@ class TempLessonApplyService extends CrudService {
 
   // 处理审核成功的情况
   async afterUpdate(filter, body, data) {
-
-
+    const { pay_id } = data;
+    if (pay_id.status !== '1') return data;
+    // 付款成功,去添加课程与学生关联
+    const obj = _.pick(data, [ 'school_id', 'lesson_id', 'student_id', 'money', 'is_pay', 'pay_id' ]);
+    obj.is_pay = '1';
+    await this.lessonStudentService.create(obj);
     return data;
   }
 }

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

@@ -9,6 +9,9 @@ class LessonStudentService extends CrudService {
   constructor(ctx) {
     super(ctx, 'lessonstudent');
     this.model = this.ctx.model.Business.LessonStudent;
+    this.rscModel = this.ctx.model.Relation.RelationStudentCoach;
+    this.lessonCoachModel = this.ctx.model.Business.LessonCoach;
+    this.lessonModel = this.ctx.model.Business.Lesson;
   }
 
   async beforeCreate(body) {
@@ -27,8 +30,66 @@ class LessonStudentService extends CrudService {
       const num = await this.model.count({ student_id, is_try: '1' });
       if (num > 0) throw new BusinessError(ErrorCode.SERVICE_FAULT, '您已使用过免费试听的机会');
     }
+
+    body = await this.toComputedMoney(body);
     return body;
   }
+
+
+  async reComputed({ id }) {
+    let data = await this.model.findById(id);
+    if (!data) throw new BusinessError(ErrorCode.DATA_NOT_EXIST, '未找到数据');
+    data = JSON.parse(JSON.stringify(data));
+    data = await this.toComputedMoney(data);
+    await this.model.updateOne({ _id: id }, data);
+    return await this.model.findById(id);
+  }
+
+  // 计算价格
+  async toComputedMoney(data) {
+    const { lesson_id, student_id } = data;
+    // 通过课程id找到该课的所有教练
+    const lesson = await this.lessonModel.findById(lesson_id);
+    if (!lesson) return data;
+    const lessonMoney = _.get(lesson, 'money');
+    const lessonType = _.get(lesson, 'type');
+    if (!lessonMoney) throw new BusinessError(ErrorCode.DATA_INVALID, '课程设置错误,缺少教练费');
+    // lesson中的money: 是学生应缴费用;
+    // lessonStudent中的money:是学生应缴费用(公开课不计算,私教课需要查下有没有教师的优惠);
+    // lessonCoach中的money: 是教师公开课每人应收的费用
+    // 学生应缴计算方式: 公开课:lesson中的money; 私教课 lesson中的money - 和该教练的优惠 (可以没有)
+    // 教练应收计算方式: 公开课: lessonCoach中设置的单价*人数; 私教课: (lesson的money - 对该学生的优惠) 之和
+    if (lessonType === '0') {
+      // 公开课
+      data.money = lessonMoney;
+    } else {
+      // 找教练和学生的设置
+      // 虽然用find,但实际上只有一个教练,但凡有2个,就应该不是私教课了
+      const lessonCoachs = await this.lessonCoachModel.find({ lesson_id });
+      if (lessonCoachs.length <= 0) return data;
+      const coach_ids = lessonCoachs.map(i => i.coach_id);
+      const rsc = await this.rscModel.findOne({ coach_id: coach_ids, student_id });
+      if (rsc) {
+        const discountType = _.get(rsc, 'config.discount_type'); // fixed;subtract;discount
+        const number = _.get(rsc, 'config.number');
+        if (discountType && number) {
+          // 优惠目前设置3种: 固定金额; ${num}(num>1):减num元; x折; 打x折(money * (x/100))
+          if (discountType === 'fixed') {
+            // 固定金额
+            data.money = number >= 0 ? number : 0;
+          } else if (discountType === 'subtract') {
+            data.money = lessonMoney - number >= 0 ? lessonMoney - number : 0;
+          } else if (discountType === 'discount') {
+            console.log(_.divide(number, 100));
+            data.money = _.round(_.multiply(lessonMoney, _.divide(number, 10)), 2);
+          }
+          data.config = _.get(rsc, 'config');
+        }
+      }
+    }
+
+    return data;
+  }
 }
 
 module.exports = LessonStudentService;

+ 2 - 1
app/z_router/business/lessonStudent.js

@@ -7,9 +7,10 @@ const rkey = 'lessonStudent';
 const ckey = 'business.lessonStudent';
 const keyZh = '课程-学员';
 const routes = [
+  { method: 'post', path: `${rkey}/reComputed/:id`, controller: `${ckey}.reComputed`, name: `${ckey}ReComputed`, zh: `重新计算价格${keyZh}` },
   { 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`, middleware: [ 'password' ], name: `${ckey}Create`, zh: `创建${keyZh}` },
+  { method: 'post', path: `${rkey}`, controller: `${ckey}.create`, middleware: ['password'], 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}` },
 ];