lessonStudent.js 3.9 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485
  1. 'use strict';
  2. const { CrudService } = require('naf-framework-mongoose-free/lib/service');
  3. const { BusinessError, ErrorCode } = require('naf-core').Error;
  4. const _ = require('lodash');
  5. const assert = require('assert');
  6. //
  7. class LessonStudentService extends CrudService {
  8. constructor(ctx) {
  9. super(ctx, 'lessonstudent');
  10. this.model = this.ctx.model.Business.LessonStudent;
  11. this.rscModel = this.ctx.model.Relation.RelationStudentCoach;
  12. this.lessonCoachModel = this.ctx.model.Business.LessonCoach;
  13. this.lessonModel = this.ctx.model.Business.Lesson;
  14. }
  15. async checkCanUse({ school_id, student_id, lesson_id }) {
  16. const num = await this.model.count({ school_id, student_id, lesson_id, $or: [{ is_try: '0', is_pay: { $ne: '-3' } }, { is_try: '1' }] });
  17. if (num > 0) throw new BusinessError(ErrorCode.DATA_EXISTED, '已存在有效的报名信息');
  18. return true;
  19. }
  20. async beforeCreate(body) {
  21. const { lesson_id, school_id, student_id } = body;
  22. // 检查是否已经有数据
  23. const data = await this.model.findOne({ lesson_id, school_id, student_id, $or: [{ is_try: '0', is_pay: { $ne: '-3' } }, { is_try: '1' }] });
  24. // 数据已存在
  25. if (data) throw new BusinessError(ErrorCode.DATA_EXISTED, '您已报名');
  26. // 数据不存在
  27. const { is_try } = body;
  28. // 检查是否使用试听名额
  29. if (is_try === '1') {
  30. const num = await this.model.count({ student_id, is_try: '1' });
  31. if (num > 0) throw new BusinessError(ErrorCode.SERVICE_FAULT, '您已使用过免费试听的机会');
  32. }
  33. return body;
  34. }
  35. // 计算价格
  36. async toComputed({ lesson_id, student_id }) {
  37. // 通过课程id找到该课的所有教练
  38. const lesson = await this.lessonModel.findById(lesson_id);
  39. if (!lesson) throw new BusinessError(ErrorCode.DATA_NOT_EXIST, '未找到数据');
  40. const data = JSON.parse(JSON.stringify(lesson));
  41. const lessonMoney = _.get(data, 'money');
  42. const lessonType = _.get(data, 'type');
  43. if (!lessonMoney) throw new BusinessError(ErrorCode.DATA_INVALID, '课程设置错误,缺少教练费');
  44. // lesson中的money: 是学生应缴费用;
  45. // lessonStudent中的money:是学生应缴费用(公开课不计算,私教课需要查下有没有教师的优惠);
  46. // lessonCoach中的money: 是教师公开课每人应收的费用
  47. // 学生应缴计算方式: 公开课:lesson中的money; 私教课 lesson中的money - 和该教练的优惠 (可以没有)
  48. // 教练应收计算方式: 公开课: lessonCoach中设置的单价*人数; 私教课: (lesson的money - 对该学生的优惠) 之和
  49. if (lessonType === '0') {
  50. // 公开课
  51. data.real_money = lessonMoney;
  52. } else {
  53. // 找教练和学生的设置
  54. // 虽然用find,但实际上只有一个教练,但凡有2个,就应该不是私教课了
  55. const lessonCoachs = await this.lessonCoachModel.find({ lesson_id });
  56. if (lessonCoachs.length <= 0) return data;
  57. const coach_ids = lessonCoachs.map(i => i.coach_id);
  58. const rsc = await this.rscModel.findOne({ coach_id: coach_ids, student_id });
  59. if (rsc) {
  60. const discountType = _.get(rsc, 'config.discount_type'); // fixed;subtract;discount
  61. const number = _.get(rsc, 'config.number');
  62. if (discountType && number) {
  63. // 优惠目前设置3种: 固定金额; ${num}(num>1):减num元; x折; 打x折(money * (x/100))
  64. if (discountType === 'fixed') {
  65. // 固定金额
  66. data.real_money = number >= 0 ? number : 0;
  67. } else if (discountType === 'subtract') {
  68. data.real_money = lessonMoney - number >= 0 ? lessonMoney - number : 0;
  69. } else if (discountType === 'discount') {
  70. data.real_money = _.round(_.multiply(lessonMoney, _.divide(number, 10)), 2);
  71. }
  72. data.config = _.get(rsc, 'config');
  73. }
  74. }
  75. }
  76. return data;
  77. }
  78. }
  79. module.exports = LessonStudentService;