tempLessonApply.js 3.8 KB

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