123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159 |
- '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 { ObjectId } = require('mongoose').Types;
- const moment = require('moment');
- class ChatService extends CrudService {
- constructor(ctx) {
- super(ctx, 'chat');
- this.model = this.ctx.model.Chat;
- this.cDoctor = this.ctx.model.Doctor;
- this.cPatient = this.ctx.model.Patient;
- this.cRoom = this.ctx.model.Room;
- this.remark = this.ctx.model.Remark;
- }
- async query({ sendid, receiveid, type, sort = 1, userid }, { skip, limit }) {
- assert(sendid, 'sendid不能为空');
- assert(receiveid, 'receiveid不能为空');
- const chatsall = await this.model.find({ $or: [{ sendid, receiveid }, { sendid: receiveid, receiveid: sendid }] });
- const chats = await this.model.find({ $or: [{ sendid, receiveid }, { sendid: receiveid, receiveid: sendid }] }).sort({ sendtime: sort }).limit(limit)
- .skip(skip);
- const newchats = [];
- const remarks = await this.remark.find({ remarkid: userid });
- for (const el of chats) {
- let icon;
- if (el.type === '1') {
- const patient = await this.cPatient.findById(el.sendid);
- if (patient) icon = patient.icon;
- }
- if (el.type === '0') {
- const doctor = await this.cDoctor.findById(el.sendid);
- if (doctor) icon = doctor.icon;
- }
- let { id, type, sendid, sendname, receiveid, receivename, sendtime, contenttype, content, status, audiotime } = el;
- const s_r = remarks.find(f => ObjectId(f.benoteid).equals(sendid));
- if (s_r) sendname = s_r.name;
- const r_r = remarks.find(f => ObjectId(f.benoteid).equals(receiveid));
- if (r_r) receivename = r_r.name;
- newchats.push({ id, type, sendid, sendname, receiveid, receivename, sendtime, contenttype, content, status, icon, audiotime });
- }
- const result = { total: chatsall.length, data: newchats };
- return result;
- }
- // 创建群信息
- async create(data) {
- const { sendid, receiveid, type, content, audiotime } = data;
- assert(sendid, '发送人不能为空');
- assert(type, '类别不能为空');
- assert(content, '发送内容不能为空');
- assert(receiveid, '收件人不能为空');
- // 通过群id取得群内医生信息
- let sendname;
- let receivename;
- let receiveopenid;
- let patientid;
- let patientname;
- let doctorid;
- let doctorname;
- let icon;
- if (type === '0') {
- const doctor = await this.cDoctor.findById(sendid);
- if (!doctor) {
- throw new BusinessError(ErrorCode.DATA_NOT_EXIST, '医生不存在');
- }
- sendname = doctor.name;
- const patient = await this.cPatient.findById(receiveid);
- if (!patient) {
- throw new BusinessError(ErrorCode.DATA_NOT_EXIST, '患者信息不存在');
- }
- receivename = patient.name;
- receiveopenid = patient.openid;
- doctorid = sendid;
- patientid = receiveid;
- doctorname = sendname;
- patientname = receivename;
- icon = doctor.icon;
- } else {
- const patient = await this.cPatient.findById(sendid);
- if (!patient) {
- throw new BusinessError(ErrorCode.DATA_NOT_EXIST, '患者信息不存在');
- }
- sendname = patient.name;
- const doctor = await this.cDoctor.findById(receiveid);
- if (!doctor) {
- throw new BusinessError(ErrorCode.DATA_NOT_EXIST, '医生不存在');
- }
- receivename = doctor.name;
- receiveopenid = doctor.openid;
- patientid = sendid;
- doctorid = receiveid;
- doctorname = receivename;
- patientname = sendname;
- icon = patient.icon;
- }
- // TODO: 检查是否已经注册
- const newdata = { type, sendid, audiotime, sendname, receiveid, receivename, content, contenttype: data.contenttype, sendtime: moment().format('YYYY-MM-DD HH:mm:ss') };
- const entity = await this.model.create(newdata);
- // 发送成功的时候创建房间,首先判断是已经创建房间
- let room = await this.cRoom.findOne({ patientid, doctorid });
- if (!room) {
- const newroom = { patientid, patientname, doctorid, doctorname };
- room = await this.cRoom.create(newroom);
- }
- // 调用mq发送消息 只有在医生的情况下才会触发群消息
- const exchange = 'chat';
- const routeKey = room.id;
- const contentMq = sendname + '给' + receivename + '发布了一条消息请及时查收。';
- const parm = {
- durable: true,
- headers: {
- sendid,
- receiveid,
- type,
- icon,
- sendname,
- audiotime,
- receivename,
- createtime: new Date().toLocaleDateString(),
- content,
- contenttype: data.contenttype,
- remark: contentMq,
- },
- };
- const { mq } = this.ctx;
- if (mq) {
- try {
- await mq.topic(exchange, routeKey, content, parm);
- } catch (error) {
- this.ctx.logger.error(error);
- }
- } else {
- this.ctx.logger.error('!!!!!!没有配置MQ插件!!!!!!');
- }
- // 微信提醒
- try {
- await this.service.wechat.sendTemplateMsg(this.ctx.app.config.REVIEW_TEMPLATE_ID, receiveopenid, contentMq, sendname, new Date().toLocaleDateString(), '私聊', '请及时查看');
- } catch (e) {
- this.ctx.logger.error(e.toString());
- }
- const d = await this.fetch({ id: entity.id });
- return d;
- }
- async delete({ id }) {
- await this.model.findByIdAndDelete(id);
- return 'deleted';
- }
- }
- module.exports = ChatService;
|