123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125 |
- '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 GroupChatService extends CrudService {
- constructor(ctx) {
- super(ctx, 'groupchat');
- this.model = this.ctx.model.Groupchat;
- this.cGroup = this.ctx.model.Group;
- this.cDoctor = this.ctx.model.Doctor;
- this.cPatient = this.ctx.model.Patient;
- this.remark = this.ctx.model.Remark;
- }
- async query({ groupid, sort = 1, userid }, { skip, limit }) {
- assert(groupid, 'groupid不能为空');
- const groupall = await this.model.find({ groupid });
- const groups = await this.model.find({ groupid }).sort({ sendtime: sort }).limit(limit)
- .skip(skip);
- const newgroups = [];
- const remarks = await this.remark.find({ remarkid: userid });
- for (const el of groups) {
- let icon;
- let { id, doctorid, doctorname, groupid, groupname, type, sendid, sendname, sendtime, contenttype, content, audiotime } = el;
- 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;
- }
- const d_r = remarks.find(f => ObjectId(f.benoteid).equals(doctorid));
- if (d_r) doctorname = d_r.name;
- const p_r = remarks.find(f => ObjectId(f.benoteid).equals(sendid));
- if (p_r) sendname = p_r.name;
- const group = { id, doctorid, doctorname, groupid, groupname, type, sendid, sendname, sendtime, contenttype, content, icon, audiotime };
- newgroups.push(group);
- }
- const result = { total: groupall.length, data: newgroups };
- return result;
- }
- // 创建群信息
- async create({ groupid }, data) {
- const { sendid, type, content, audiotime } = data;
- assert(sendid, '发送人不能为空');
- assert(type, '类别不能为空');
- assert(content, '发送内容不能为空');
- assert(groupid, '缺少群信息项');
- // 通过群id取得群内医生信息
- const group = await this.cGroup.findById(groupid, '+patients');
- if (!group) {
- throw new BusinessError(ErrorCode.DATA_NOT_EXIST, '群不存在');
- }
- const doctor = await this.cDoctor.findById(group.doctorid);
- if (!doctor) {
- throw new BusinessError(ErrorCode.DATA_NOT_EXIST, '医生不存在');
- }
- let sendname;
- let icon;
- if (type === '0') {
- sendname = doctor.name;
- icon = doctor.icon;
- } else {
- const patient = await this.cPatient.findById(sendid);
- if (!patient) {
- throw new BusinessError(ErrorCode.DATA_NOT_EXIST, '患者信息不存在');
- }
- sendname = patient.name;
- icon = patient.icon;
- }
- // TODO: 检查是否已经注册
- const newdata = { doctorid: group.doctorid, audiotime, doctorname: doctor.name, groupid, groupname: group.name, type, sendid, sendname, sendtime: moment().format('YYYY-MM-DD HH:mm:ss'), content, contenttype: data.contenttype };
- const entity = await this.model.create(newdata);
- // 调用mq发送消息 只有在医生的情况下才会触发群消息
- const exchange = 'group_chat';
- const routeKey = groupid;
- const contentMq = doctor.name + '医生的' + group.name + '群内发布了一条消息请及时查收。';
- const parm = {
- durable: true,
- headers: {
- sendid,
- groupid,
- icon,
- type,
- audiotime,
- sendname,
- createtime: new Date().toLocaleDateString(),
- content,
- contenttype: data.contenttype,
- remark: contentMq,
- },
- };
- const { mq } = this.ctx;
- if (mq) {
- await mq.fanout(exchange, routeKey, content, parm);
- } else {
- this.ctx.logger.error('!!!!!!没有配置MQ插件!!!!!!');
- }
- // 当医生发送群消息时微信提醒所有群内患者
- const self = this;
- if (type === '0') {
- for (const elem of group.patients) {
- await self.service.wechat.sendTemplateMsg(self.ctx.app.config.REVIEW_TEMPLATE_ID, elem.openid, contentMq, doctor.name, new Date().toLocaleDateString(), '群聊', '请及时查看');
- }
- }
- return await this.fetch({ id: entity.id });
- }
- async delete({ id }) {
- await this.model.findByIdAndDelete(id);
- return 'deleted';
- }
- }
- module.exports = GroupChatService;
|