lessonStudent.js 11 KB

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