123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108 |
- 'use strict';
- const assert = require('assert');
- const _ = require('lodash');
- const { CrudService } = require('naf-framework-mongoose/lib/service');
- const { BusinessError, ErrorCode } = require('naf-core').Error;
- const moment = require('moment');
- class DoctorMoneyService extends CrudService {
- constructor(ctx) {
- super(ctx, 'doctormoney');
- this.model = this.ctx.model.Doctormoney;
- this.cDoctor = this.ctx.model.Doctor;
- this.cPatient = this.ctx.model.Patient;
- }
- async query({ doctorid, ...query }, { skip, limit }) {
- assert(doctorid, 'doctorid不能为空');
- const moneyall = await this.model.find({ doctorid, ...query });
- const moneys = await this.model.find({ doctorid, ...query }).sort({ sendtime: -1 }).limit(limit)
- .skip(skip);
- const newmoneys = [];
- for (const el of moneys) {
- let icon;
- const { id, doctorid, doctorname, patientid, patientname, sendtime, money, content, orderno } = el;
- const patient = await this.cPatient.findById(patientid);
- if (patient) {
- icon = patient.icon;
- }
- newmoneys.push({ id, doctorid, doctorname, patientid, patientname, sendtime, money, content, orderno, icon });
- }
- const result = { total: moneyall.length, data: newmoneys };
- return result;
- }
- // 创建打赏记录
- async create({ doctorid }, data) {
- const { patientid, money, content } = data;
- assert(patientid, '发送人不能为空');
- assert(money, '金额不能为空');
- assert(doctorid, 'doctorid不能为空');
- // 通过群id取得群内医生信息
- const doctor = await this.cDoctor.findById(doctorid);
- if (!doctor) {
- throw new BusinessError(ErrorCode.DATA_NOT_EXIST, '医生不存在');
- }
- const patient = await this.cPatient.findById(patientid);
- if (!patient) {
- throw new BusinessError(ErrorCode.DATA_NOT_EXIST, '患者信息不存在');
- }
- // 建立打赏信息
- const newdata = {
- doctorname: doctor.name,
- patientname: patient.name,
- sendtime: moment().format('YYYY-MM-DD HH:mm:ss'),
- ...data };
- const entity = await this.model.create(newdata);
- if (entity) {
- // 将打赏金额加入到医生信息中
- doctor.money = await this.plus(doctor.money, money);
- doctor.balance = await this.plus(doctor.balance, money);
- await doctor.save();
- }
- // 当患者给医生打赏时提醒医生
- await this.service.wechat.sendTemplateMsg(this.ctx.app.config.REVIEW_TEMPLATE_ID, doctor.openid, content, patient.name, new Date().toLocaleDateString(), '礼物', '请及时查看');
- return await this.fetch({ id: entity.id });
- }
- async num(a) {
- if (a !== null && a.toString() !== '') {
- const r = /^-?(0|[1-9]+\d*|[1-9]+\d*\.\d+|0\.\d+)$/;
- if (r.test(a.toString())) {
- return true;
- }
- }
- return false;
- }
- async plus(a, b) {
- if (!a) a = 0;
- if (!this.num(a) || !this.num(b)) {
- return null;
- }
- let c,
- d,
- m;
- try {
- c = a.toString().split('.')[1].length;
- } catch (e) {
- c = 0;
- }
- try {
- d = b.toString().split('.')[1].length;
- } catch (e) {
- d = 0;
- }
- m = Math.pow(10, Math.max(c, d));
- return (a * m + b * m) / m;
- }
- async delete({ id }) {
- await this.model.findByIdAndDelete(id);
- return 'deleted';
- }
- }
- module.exports = DoctorMoneyService;
|