lessonStudent.js 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119
  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. const moment = require('moment');
  7. //
  8. class LessonStudentService extends CrudService {
  9. constructor(ctx) {
  10. super(ctx, 'lessonstudent');
  11. this.model = this.ctx.model.Business.LessonStudent;
  12. this.rscModel = this.ctx.model.Relation.RelationStudentCoach;
  13. this.lessonCoachModel = this.ctx.model.Business.LessonCoach;
  14. this.lessonModel = this.ctx.model.Business.Lesson;
  15. }
  16. async checkCanUse({ school_id, student_id, lesson_id }) {
  17. const num = await this.model.count({ school_id, student_id, lesson_id, $or: [{ is_try: '0', is_pay: { $ne: '-3' } }, { is_try: '1' }] });
  18. if (num > 0) throw new BusinessError(ErrorCode.DATA_EXISTED, '已存在有效的报名信息');
  19. return true;
  20. }
  21. async beforeCreate(body) {
  22. const { lesson_id, school_id, student_id } = body;
  23. // 检查是否已经有数据
  24. const data = await this.model.findOne({ lesson_id, school_id, student_id, $or: [{ is_try: '0', is_pay: { $ne: '-3' } }, { is_try: '1' }] });
  25. // 数据已存在
  26. if (data) throw new BusinessError(ErrorCode.DATA_EXISTED, '您已报名');
  27. // 数据不存在
  28. const { is_try } = body;
  29. // 检查是否使用试听名额
  30. if (is_try === '1') {
  31. const num = await this.model.count({ student_id, is_try: '1' });
  32. if (num > 0) throw new BusinessError(ErrorCode.SERVICE_FAULT, '您已使用过免费试听的机会');
  33. }
  34. return body;
  35. }
  36. // 计算价格
  37. async toComputed({ lesson_id, student_id }) {
  38. // 通过课程id找到该课的所有教练
  39. const lesson = await this.lessonModel.findById(lesson_id);
  40. if (!lesson) throw new BusinessError(ErrorCode.DATA_NOT_EXIST, '未找到数据');
  41. const data = JSON.parse(JSON.stringify(lesson));
  42. const lessonMoney = _.get(data, 'money');
  43. const lessonType = _.get(data, 'type');
  44. if (!lessonMoney) throw new BusinessError(ErrorCode.DATA_INVALID, '课程设置错误,缺少教练费');
  45. // lesson中的money: 是学生应缴费用;
  46. // lessonStudent中的money:是学生应缴费用(公开课不计算,私教课需要查下有没有教师的优惠);
  47. // lessonCoach中的money: 是教师公开课每人应收的费用
  48. // 学生应缴计算方式: 公开课:lesson中的money; 私教课 lesson中的money - 和该教练的优惠 (可以没有)
  49. // 教练应收计算方式: 公开课: lessonCoach中设置的单价*人数; 私教课: (lesson的money - 对该学生的优惠) 之和
  50. if (lessonType === '0') {
  51. // 公开课
  52. data.real_money = lessonMoney;
  53. } else {
  54. // 找教练和学生的设置
  55. // 虽然用find,但实际上只有一个教练,但凡有2个,就应该不是私教课了
  56. const lessonCoachs = await this.lessonCoachModel.find({ lesson_id });
  57. if (lessonCoachs.length <= 0) return data;
  58. const coach_ids = lessonCoachs.map(i => i.coach_id);
  59. const rsc = await this.rscModel.findOne({ coach_id: coach_ids, student_id });
  60. if (rsc) {
  61. const discountType = _.get(rsc, 'config.discount_type'); // fixed;subtract;discount
  62. const number = _.get(rsc, 'config.number');
  63. if (discountType && number) {
  64. // 优惠目前设置3种: 固定金额; ${num}(num>1):减num元; x折; 打x折(money * (x/100))
  65. if (discountType === 'fixed') {
  66. // 固定金额
  67. data.real_money = number >= 0 ? number : 0;
  68. } else if (discountType === 'subtract') {
  69. data.real_money = lessonMoney - number >= 0 ? lessonMoney - number : 0;
  70. } else if (discountType === 'discount') {
  71. data.real_money = _.round(_.multiply(lessonMoney, _.divide(number, 10)), 2);
  72. }
  73. data.config = _.get(rsc, 'config');
  74. }
  75. }
  76. }
  77. return data;
  78. }
  79. // 退课,将钱退至余额
  80. async toRefund({ lesson_id, student_id }) {
  81. const payOrderService = this.ctx.service.business.payOrder;
  82. const payOrderModel = this.ctx.model.Business.PayOrder;
  83. const billModel = this.ctx.model.Business.Bill;
  84. const data = await this.model.findOne({ lesson_id, student_id });
  85. if (!data) throw new BusinessError(ErrorCode.DATA_NOT_EXIST, '未找到数据');
  86. if (data.is_pay === '-3') throw new BusinessError(ErrorCode.DATA_INVALID, '已经完成退课,无法再次退课');
  87. if (data.is_try === '1') {
  88. // 试课 修改退款状态, 而不需要退钱,同时也浪费了试课机会
  89. data.is_pay = '-3';
  90. await data.save();
  91. return;
  92. }
  93. // 正常交钱的课,将 pay_id 的status 修改为 -3, 同时触发修改这条数据,但是没有退款;
  94. await payOrderService.update({ _id: data.pay_id }, { status: '-3' });
  95. const payOrder = await payOrderModel.findById(data.pay_id);
  96. // 再生成账单记录
  97. const obj = _.pick(payOrder, [ 'school_id', 'payer_role', 'payer_id', 'pay_for', 'from_id', 'money', 'pay_id' ]);
  98. obj.type = '2';
  99. obj.is_pay = '1';
  100. obj.time = moment().format('YYYY-MM-DD HH:mm:ss');
  101. await billModel.create(obj);
  102. // 再将钱返回给余额
  103. let relation;
  104. const { payer_role, payer_id, school_id } = payOrder;
  105. if (payer_role === 'Student') relation = await this.rssModel.findOne({ student_id: payer_id, school_id });
  106. else if (payer_role === 'Coach') relation = await this.rcsModel.findOne({ coach_id: payer_id, school_id });
  107. relation.money = relation.money + obj.money;
  108. await relation.save();
  109. }
  110. }
  111. module.exports = LessonStudentService;