doctormoney.js 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109
  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, contenttype } = 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, contenttype });
  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. console.log(data);
  36. assert(patientid, '发送人不能为空');
  37. assert(money, '金额不能为空');
  38. assert(doctorid, 'doctorid不能为空');
  39. // 通过群id取得群内医生信息
  40. const doctor = await this.cDoctor.findById(doctorid);
  41. if (!doctor) {
  42. throw new BusinessError(ErrorCode.DATA_NOT_EXIST, '医生不存在');
  43. }
  44. const patient = await this.cPatient.findById(patientid);
  45. if (!patient) {
  46. throw new BusinessError(ErrorCode.DATA_NOT_EXIST, '患者信息不存在');
  47. }
  48. // 建立打赏信息
  49. const newdata = {
  50. doctorname: doctor.name,
  51. patientname: patient.name,
  52. sendtime: moment().format('YYYY-MM-DD HH:mm:ss'),
  53. ...data };
  54. const entity = await this.model.create(newdata);
  55. if (entity) {
  56. // 将打赏金额加入到医生信息中
  57. doctor.money = await this.plus(doctor.money, money);
  58. doctor.balance = await this.plus(doctor.balance, money);
  59. await doctor.save();
  60. }
  61. // 当患者给医生打赏时提醒医生
  62. await this.service.wechat.sendTemplateMsg(this.ctx.app.config.REVIEW_TEMPLATE_ID, doctor.openid, content, patient.name, new Date().toLocaleDateString(), '礼物', '请及时查看');
  63. return await this.fetch({ id: entity.id });
  64. }
  65. async num(a) {
  66. if (a !== null && a.toString() !== '') {
  67. const r = /^-?(0|[1-9]+\d*|[1-9]+\d*\.\d+|0\.\d+)$/;
  68. if (r.test(a.toString())) {
  69. return true;
  70. }
  71. }
  72. return false;
  73. }
  74. async plus(a, b) {
  75. if (!a) a = 0;
  76. if (!this.num(a) || !this.num(b)) {
  77. return null;
  78. }
  79. let c,
  80. d,
  81. m;
  82. try {
  83. c = a.toString().split('.')[1].length;
  84. } catch (e) {
  85. c = 0;
  86. }
  87. try {
  88. d = b.toString().split('.')[1].length;
  89. } catch (e) {
  90. d = 0;
  91. }
  92. m = Math.pow(10, Math.max(c, d));
  93. return (a * m + b * m) / m;
  94. }
  95. async delete({ id }) {
  96. await this.model.findByIdAndDelete(id);
  97. return 'deleted';
  98. }
  99. }
  100. module.exports = DoctorMoneyService;