doctormoney.js 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108
  1. 'use strict';
  2. const assert = require('assert');
  3. const _ = require('lodash');
  4. const { CrudService } = require('naf-framework-mongoose/lib/service');
  5. const { BusinessError, ErrorCode } = require('naf-core').Error;
  6. const moment = require('moment');
  7. class DoctorMoneyService extends CrudService {
  8. constructor(ctx) {
  9. super(ctx, 'doctormoney');
  10. this.model = this.ctx.model.Doctormoney;
  11. this.cDoctor = this.ctx.model.Doctor;
  12. this.cPatient = this.ctx.model.Patient;
  13. }
  14. async query({ doctorid, ...query }, { skip, limit }) {
  15. assert(doctorid, 'doctorid不能为空');
  16. const moneyall = await this.model.find({ doctorid, ...query });
  17. const moneys = await this.model.find({ doctorid, ...query }).sort({ sendtime: -1 }).limit(limit)
  18. .skip(skip);
  19. const newmoneys = [];
  20. for (const el of moneys) {
  21. let icon;
  22. const { id, doctorid, doctorname, patientid, patientname, sendtime, money, content, orderno } = el;
  23. const patient = await this.cPatient.findById(patientid);
  24. if (patient) {
  25. icon = patient.icon;
  26. }
  27. newmoneys.push({ id, doctorid, doctorname, patientid, patientname, sendtime, money, content, orderno, icon });
  28. }
  29. const result = { total: moneyall.length, data: newmoneys };
  30. return result;
  31. }
  32. // 创建打赏记录
  33. async create({ doctorid }, data) {
  34. const { patientid, money, content } = data;
  35. assert(patientid, '发送人不能为空');
  36. assert(money, '金额不能为空');
  37. assert(doctorid, 'doctorid不能为空');
  38. // 通过群id取得群内医生信息
  39. const doctor = await this.cDoctor.findById(doctorid);
  40. if (!doctor) {
  41. throw new BusinessError(ErrorCode.DATA_NOT_EXIST, '医生不存在');
  42. }
  43. const patient = await this.cPatient.findById(patientid);
  44. if (!patient) {
  45. throw new BusinessError(ErrorCode.DATA_NOT_EXIST, '患者信息不存在');
  46. }
  47. // 建立打赏信息
  48. const newdata = {
  49. doctorname: doctor.name,
  50. patientname: patient.name,
  51. sendtime: moment().format('YYYY-MM-DD HH:mm:ss'),
  52. ...data };
  53. const entity = await this.model.create(newdata);
  54. if (entity) {
  55. // 将打赏金额加入到医生信息中
  56. doctor.money = await this.plus(doctor.money, money);
  57. doctor.balance = await this.plus(doctor.balance, money);
  58. await doctor.save();
  59. }
  60. // 当患者给医生打赏时提醒医生
  61. await this.service.wechat.sendTemplateMsg(this.ctx.app.config.REVIEW_TEMPLATE_ID, doctor.openid, content, patient.name, new Date().toLocaleDateString(), '礼物', '请及时查看');
  62. return await this.fetch({ id: entity.id });
  63. }
  64. async num(a) {
  65. if (a !== null && a.toString() !== '') {
  66. const r = /^-?(0|[1-9]+\d*|[1-9]+\d*\.\d+|0\.\d+)$/;
  67. if (r.test(a.toString())) {
  68. return true;
  69. }
  70. }
  71. return false;
  72. }
  73. async plus(a, b) {
  74. if (!a) a = 0;
  75. if (!this.num(a) || !this.num(b)) {
  76. return null;
  77. }
  78. let c,
  79. d,
  80. m;
  81. try {
  82. c = a.toString().split('.')[1].length;
  83. } catch (e) {
  84. c = 0;
  85. }
  86. try {
  87. d = b.toString().split('.')[1].length;
  88. } catch (e) {
  89. d = 0;
  90. }
  91. m = Math.pow(10, Math.max(c, d));
  92. return (a * m + b * m) / m;
  93. }
  94. async delete({ id }) {
  95. await this.model.findByIdAndDelete(id);
  96. return 'deleted';
  97. }
  98. }
  99. module.exports = DoctorMoneyService;