tempLessonApply.js 3.7 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697
  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 TempLessonApplyService extends CrudService {
  10. constructor(ctx) {
  11. super(ctx, 'templessonapply');
  12. this.model = this.ctx.model.Apply.TempLessonApply;
  13. this.payOrderModel = this.ctx.model.Business.PayOrder;
  14. this.rssModel = this.ctx.model.Relation.RelationStudentSchool;
  15. this.tran = new Transaction();
  16. }
  17. async create(data) {
  18. try {
  19. // 创建数据
  20. const id = await this.tran.insert('TempLessonApply', data);
  21. // 组织payOrder的数据,使用native支付
  22. const payData = {
  23. school_id: _.get(data, 'school_id'),
  24. pay_for: 'TempLessonApply',
  25. from_id: id,
  26. time: moment().format('YYYY-MM-DD HH:mm:ss'),
  27. desc: '临时上课缴费',
  28. money: _.get(data, 'money'),
  29. order_no: this.ctx.service.business.payOrder.getOrderNo(),
  30. };
  31. // 有学生信息就把学生信息补上
  32. // 再查询学生是否有余额
  33. let needPay = true;
  34. let result = 'ok';
  35. let billId;
  36. if (data.student_id) {
  37. payData.payer_id = data.student_id;
  38. payData.payer_role = 'Student';
  39. const rss = await this.rssModel.findOne({ school_id: data.school_id, student_id: data.student_id });
  40. if (rss) {
  41. // 有学生信息,将信息填充,因为传student_id可能不会传name和phone
  42. const student = await this.studentModel.findById(data.student_id);
  43. const name = _.get(student, 'name');
  44. const phone = _.get(student, 'phone');
  45. if (name && phone) this.tran.update('TempLessonApply', id, { name, phone });
  46. const surplus = _.get(rss, 'money');
  47. const newMoney = this.ctx.minus(surplus, data.money);
  48. if (newMoney >= 0) {
  49. needPay = false;
  50. this.tran.update('RelationStudentSchool', data.student_id, { money: newMoney });
  51. // 生成账单
  52. const billData = {
  53. school_id: _.get(data, 'school_id'),
  54. payer_role: 'Student',
  55. payer_id: data.student_id,
  56. pay_for: 'TempLessonApply',
  57. from_id: id,
  58. type: '-2',
  59. is_pay: '1',
  60. time: moment().format('YYYY-MM-DD HH:mm:ss'),
  61. };
  62. billId = this.tran.insert('Bill', billData);
  63. // 生成付款单数据,但是不需要支付
  64. payData.status = '1';
  65. payData.config = {
  66. useSurplus: true,
  67. bill: billId,
  68. money: _.get(data, 'money'),
  69. };
  70. }
  71. }
  72. }
  73. // 需要支付,不会生成账单,native支付处生成payOrder数据
  74. if (needPay) {
  75. // 获取支付二维码和生成的支付信息,再修改进去
  76. const { pay_id, qrcode } = await this.ctx.service.business.payOrder.toPayNative(payData, this.tran);
  77. this.tran.update('TempLessonApply', id, { pay_id });
  78. result = qrcode;
  79. } else {
  80. // 不需要支付的情况,直接将支付单生成,然后将临时课程和账单的pay_id&is_pay都修改了
  81. const pay_id = this.tran.insert('PayOrder', payData);
  82. this.tran.update('TempLessonApply', id, { pay_id, is_pay: '1' });
  83. this.tran.update('Bill', billId, { pay_id, is_pay: '1' });
  84. }
  85. await this.tran.run();
  86. // 返回结果
  87. return result;
  88. } catch (error) {
  89. await this.tran.rollback();
  90. } finally {
  91. this.tran.clean();
  92. }
  93. }
  94. }
  95. module.exports = TempLessonApplyService;