lessonStudent.js 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234
  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. const Transaction = require('mongoose-transactions');
  8. const { find } = require('lodash');
  9. //
  10. class LessonStudentService extends CrudService {
  11. constructor(ctx) {
  12. super(ctx, 'lessonstudent');
  13. this.model = this.ctx.model.Business.LessonStudent;
  14. this.rscModel = this.ctx.model.Relation.RelationStudentCoach;
  15. this.lessonCoachModel = this.ctx.model.Business.LessonCoach;
  16. this.lessonModel = this.ctx.model.Business.Lesson;
  17. this.rcsModel = this.ctx.model.Relation.RelationCoachSchool;
  18. this.rssModel = this.ctx.model.Relation.RelationStudentSchool;
  19. this.billModel = this.ctx.model.Business.Bill;
  20. this.payOrderModel = this.ctx.model.Business.PayOrder;
  21. this.studentModel = this.ctx.model.User.Student;
  22. this.payService = this.ctx.service.wxpay;
  23. this.tran = new Transaction();
  24. }
  25. async checkCanUse({ school_id, student_id, lesson_id }) {
  26. const num = await this.model.count({ school_id, student_id, lesson_id, $or: [{ is_try: '0', is_pay: { $and: [{ $ne: '-3' }, { $ne: '-1' }] } }, { is_try: '1' }] });
  27. if (num > 0) throw new BusinessError(ErrorCode.DATA_EXISTED, '已存在有效的报名信息');
  28. return true;
  29. }
  30. async beforeCreate(body) {
  31. const { lesson_id, school_id, student_id } = body;
  32. // 检查是否已经有数据: 支付失败和已退款不算
  33. const data = await this.model.findOne({ lesson_id, school_id, student_id, $or: [{ is_try: '0', is_pay: { $nin: [ '-1', '-3' ] } }, { is_try: '1' }] });
  34. // 数据已存在
  35. if (data) throw new BusinessError(ErrorCode.DATA_EXISTED, '您已报名');
  36. // 数据不存在
  37. const { is_try } = body;
  38. // 检查是否使用试听名额
  39. if (is_try === '1') {
  40. const num = await this.model.count({ student_id, is_try: '1' });
  41. if (num > 0) throw new BusinessError(ErrorCode.SERVICE_FAULT, '您已使用过免费试听的机会');
  42. }
  43. return body;
  44. }
  45. async toRePay({ id }) {
  46. const data = await this.model.findById(id);
  47. if (!data) throw new BusinessError(ErrorCode.DATA_NOT_EXIST, '未找到报名信息');
  48. if (data.is_pay === '1') throw new BusinessError(ErrorCode.SERVICE_FAULT, '该课程已支付成功');
  49. const payOrder = await this.payOrderModel.findById(data.pay_id);
  50. if (!payOrder) throw new BusinessError(ErrorCode.DATA_NOT_EXIST, '未找到课程缴费信息');
  51. const { openid, money, school_id, payer_id: student_id, from_id: lesson_id, order_no } = payOrder;
  52. try {
  53. // 关闭订单
  54. await this.payService.close(order_no);
  55. // 删了重做
  56. this.tran.remove('LessonStudent', id);
  57. await this.tran.run();
  58. await this.create({ openid, money, school_id, student_id, lesson_id });
  59. } catch (error) {
  60. await this.tran.rollback();
  61. } finally {
  62. this.tran.clean();
  63. }
  64. }
  65. async create(data) {
  66. // 检查是否能报名上课
  67. data = await this.beforeCreate(data);
  68. // 能报名上课,就直接创建数据
  69. try {
  70. // 创建数据
  71. const id = this.tran.insert('LessonStudent', data);
  72. const { school_id, student_id: payer_id, money, openid } = data;
  73. if (!openid) throw new BusinessError(ErrorCode.DATA_NOT_EXIST, '未找到用户的微信信息');
  74. const payData = {
  75. openid,
  76. school_id,
  77. payer_id,
  78. payer_role: 'Student',
  79. pay_for: 'LessonStudent',
  80. from_id: id,
  81. time: moment().format('YYYY-MM-DD HH:mm:ss'),
  82. order_no: this.ctx.service.business.payOrder.getOrderNo(),
  83. desc: '课程费',
  84. money,
  85. };
  86. // 计算付款单支付金额
  87. // 有余额且余额够,就直接扣余额;余额不足,就直接去支付
  88. const relation = await this.rssModel.findOne({ student_id: payer_id, school_id });
  89. if (!relation) throw new BusinessError(ErrorCode.DATA_NOT_EXIST, '未找到在校记录');
  90. const { money: surplus } = relation;
  91. let billId;
  92. if (this.ctx.minus(surplus, money) >= 0) {
  93. // 余额够,生成账单
  94. const billData = _.pick(payData, [ 'school_id', 'payer_role', 'payer_id', 'pay_for', 'from_id' ]);
  95. billData.type = '-2';
  96. billData.is_pay = '1';
  97. billData.time = moment().format('YYYY-MM-DD HH:mm:ss');
  98. billId = this.tran.insert('Bill', billData);
  99. // 减少余额
  100. const rs = this.ctx.minus(surplus, data.money);
  101. this.tran.update('RelationStudentSchool', relation._id, { money: rs });
  102. // 生成付款单数据,但是不需要支付
  103. payData.status = '1';
  104. payData.config = {
  105. useSurplus: true,
  106. bill: billId,
  107. money,
  108. };
  109. }
  110. const pay_id = this.tran.insert('PayOrder', payData);
  111. const result = { pay_id };
  112. // 如果生成账单id,则说明是全余额付的.修改账单的数据,将pay_id改了
  113. if (billId) this.tran.update('Bill', billId, { pay_id });
  114. else {
  115. // 需要微信支付签名(填充回调函数,回调函数处理)
  116. const wxSign = await this.payService.create({ ...payData, notice_url: this.app.config.payReturn });
  117. result.wxSign = wxSign;
  118. }
  119. await this.tran.run();
  120. return result;
  121. } catch (error) {
  122. await this.tran.rollback();
  123. throw Error(error);
  124. } finally {
  125. this.tran.clean();
  126. }
  127. }
  128. // 计算价格
  129. async toComputed({ lesson_id, student_id }) {
  130. // 通过课程id找到该课的所有教练
  131. const lesson = await this.lessonModel.findById(lesson_id);
  132. if (!lesson) throw new BusinessError(ErrorCode.DATA_NOT_EXIST, '未找到数据');
  133. const data = JSON.parse(JSON.stringify(lesson));
  134. const lessonMoney = _.get(data, 'money');
  135. const lessonType = _.get(data, 'type');
  136. if (!lessonMoney) throw new BusinessError(ErrorCode.DATA_INVALID, '课程设置错误,缺少教练费');
  137. // lesson中的money: 是学生应缴费用;
  138. // lessonStudent中的money:是学生应缴费用(公开课不计算,私教课需要查下有没有教师的优惠);
  139. // lessonCoach中的money: 是教师公开课每人应收的费用
  140. // 学生应缴计算方式: 公开课:lesson中的money; 私教课 lesson中的money - 和该教练的优惠 (可以没有)
  141. // 教练应收计算方式: 公开课: lessonCoach中设置的单价*人数; 私教课: (lesson的money - 对该学生的优惠) 之和
  142. if (lessonType === '0') {
  143. // 公开课
  144. data.real_money = lessonMoney;
  145. } else {
  146. // 找教练和学生的设置
  147. // 虽然用find,但实际上只有一个教练,但凡有2个,就应该不是私教课了
  148. const lessonCoachs = await this.lessonCoachModel.find({ lesson_id });
  149. if (lessonCoachs.length <= 0) return data;
  150. const coach_ids = lessonCoachs.map(i => i.coach_id);
  151. const rsc = await this.rscModel.findOne({ coach_id: coach_ids, student_id });
  152. if (rsc) {
  153. const discountType = _.get(rsc, 'config.discount_type'); // fixed;subtract;discount
  154. const number = _.get(rsc, 'config.number');
  155. if (discountType && number) {
  156. // 优惠目前设置3种: 固定金额; ${num}(num>1):减num元; x折; 打x折(money * (x/100))
  157. if (discountType === 'fixed') {
  158. // 固定金额
  159. data.real_money = number >= 0 ? number : 0;
  160. } else if (discountType === 'subtract') {
  161. data.real_money = lessonMoney - number >= 0 ? lessonMoney - number : 0;
  162. } else if (discountType === 'discount') {
  163. data.real_money = _.round(_.multiply(lessonMoney, _.divide(number, 10)), 2);
  164. }
  165. data.config = _.get(rsc, 'config');
  166. }
  167. }
  168. }
  169. return data;
  170. }
  171. // 退课,将钱退至余额
  172. async toRefund({ lesson_id, student_id }) {
  173. const data = await this.model.findOne({ lesson_id, student_id });
  174. if (!data) throw new BusinessError(ErrorCode.DATA_NOT_EXIST, '未找到数据');
  175. const lesson = await this.lessonModel.findById(lesson_id);
  176. if (!lesson) throw new BusinessError(ErrorCode.DATA_NOT_EXIST, '未找到课程数据');
  177. if (!moment().isBefore(lesson.refund_hour)) throw new BusinessError(ErrorCode.DATA_INVALID, '已经超过退课时间,无法退课');
  178. if (data.is_pay === '-3') throw new BusinessError(ErrorCode.DATA_INVALID, '已经完成退课,无法再次退课');
  179. if (data.is_try === '1') {
  180. // 试课 修改退款状态, 而不需要退钱,同时也浪费了试课机会
  181. data.is_pay = '-3';
  182. await data.save();
  183. return;
  184. }
  185. try {
  186. // 正常交钱的课,将 pay_id 的status 修改为 -3, 同时触发修改这条数据,但是没有退款;
  187. const payOrder = await this.payOrderModel.findById(data.pay_id);
  188. // 再生成账单记录
  189. const obj = _.pick(payOrder, [ 'school_id', 'payer_role', 'payer_id', 'pay_for', 'from_id' ]);
  190. obj.money = data.money;
  191. obj.type = '2';
  192. obj.is_pay = '1';
  193. obj.time = moment().format('YYYY-MM-DD HH:mm:ss');
  194. // 再将钱返回给余额
  195. let relation,
  196. model;
  197. const { payer_role, payer_id, school_id } = payOrder;
  198. if (payer_role === 'Student') {
  199. relation = await this.rssModel.findOne({ student_id: payer_id, school_id });
  200. model = 'RelationStudentSchool';
  201. } else if (payer_role === 'Coach') {
  202. relation = await this.rcsModel.findOne({ coach_id: payer_id, school_id });
  203. model = 'RelationCoachSchool';
  204. }
  205. const newMoney = this.ctx.plus(relation.money, obj.money);
  206. // 退至余额
  207. this.tran.update(model, relation._id, { money: newMoney });
  208. // 生成退款账单
  209. this.tran.insert('Bill', obj);
  210. // 付款单状态修改
  211. this.tran.update('PayOrder', data.pay_id, { status: '-3' });
  212. // 课程-学生状态修改
  213. this.tran.update('LessonStudent', data._id, { is_pay: '-3' });
  214. await this.tran.run();
  215. return;
  216. } catch (error) {
  217. await this.tran.rollback();
  218. throw new Error(error);
  219. } finally {
  220. this.tran.clean();
  221. }
  222. }
  223. }
  224. module.exports = LessonStudentService;