groupchat.js 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125
  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 { ObjectId } = require('mongoose').Types;
  7. const moment = require('moment');
  8. class GroupChatService extends CrudService {
  9. constructor(ctx) {
  10. super(ctx, 'groupchat');
  11. this.model = this.ctx.model.Groupchat;
  12. this.cGroup = this.ctx.model.Group;
  13. this.cDoctor = this.ctx.model.Doctor;
  14. this.cPatient = this.ctx.model.Patient;
  15. this.remark = this.ctx.model.Remark;
  16. }
  17. async query({ groupid, sort = 1, userid }, { skip, limit }) {
  18. assert(groupid, 'groupid不能为空');
  19. const groupall = await this.model.find({ groupid });
  20. const groups = await this.model.find({ groupid }).sort({ sendtime: sort }).limit(limit)
  21. .skip(skip);
  22. const newgroups = [];
  23. const remarks = await this.remark.find({ remarkid: userid });
  24. for (const el of groups) {
  25. let icon;
  26. let { id, doctorid, doctorname, groupid, groupname, type, sendid, sendname, sendtime, contenttype, content, audiotime } = el;
  27. if (el.type === '1') {
  28. const patient = await this.cPatient.findById(el.sendid);
  29. if (patient) {
  30. icon = patient.icon;
  31. }
  32. }
  33. if (el.type === '0') {
  34. const doctor = await this.cDoctor.findById(el.sendid);
  35. if (doctor) icon = doctor.icon;
  36. }
  37. const d_r = remarks.find(f => ObjectId(f.benoteid).equals(doctorid));
  38. if (d_r) doctorname = d_r.name;
  39. const p_r = remarks.find(f => ObjectId(f.benoteid).equals(sendid));
  40. if (p_r) sendname = p_r.name;
  41. const group = { id, doctorid, doctorname, groupid, groupname, type, sendid, sendname, sendtime, contenttype, content, icon, audiotime };
  42. newgroups.push(group);
  43. }
  44. const result = { total: groupall.length, data: newgroups };
  45. return result;
  46. }
  47. // 创建群信息
  48. async create({ groupid }, data) {
  49. const { sendid, type, content, audiotime } = data;
  50. assert(sendid, '发送人不能为空');
  51. assert(type, '类别不能为空');
  52. assert(content, '发送内容不能为空');
  53. assert(groupid, '缺少群信息项');
  54. // 通过群id取得群内医生信息
  55. const group = await this.cGroup.findById(groupid, '+patients');
  56. if (!group) {
  57. throw new BusinessError(ErrorCode.DATA_NOT_EXIST, '群不存在');
  58. }
  59. const doctor = await this.cDoctor.findById(group.doctorid);
  60. if (!doctor) {
  61. throw new BusinessError(ErrorCode.DATA_NOT_EXIST, '医生不存在');
  62. }
  63. let sendname;
  64. let icon;
  65. if (type === '0') {
  66. sendname = doctor.name;
  67. icon = doctor.icon;
  68. } else {
  69. const patient = await this.cPatient.findById(sendid);
  70. if (!patient) {
  71. throw new BusinessError(ErrorCode.DATA_NOT_EXIST, '患者信息不存在');
  72. }
  73. sendname = patient.name;
  74. icon = patient.icon;
  75. }
  76. // TODO: 检查是否已经注册
  77. 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 };
  78. const entity = await this.model.create(newdata);
  79. // 调用mq发送消息 只有在医生的情况下才会触发群消息
  80. const exchange = 'group_chat';
  81. const routeKey = groupid;
  82. const contentMq = doctor.name + '医生的' + group.name + '群内发布了一条消息请及时查收。';
  83. const parm = {
  84. durable: true,
  85. headers: {
  86. sendid,
  87. groupid,
  88. icon,
  89. type,
  90. audiotime,
  91. sendname,
  92. createtime: new Date().toLocaleDateString(),
  93. content,
  94. contenttype: data.contenttype,
  95. remark: contentMq,
  96. },
  97. };
  98. const { mq } = this.ctx;
  99. if (mq) {
  100. await mq.fanout(exchange, routeKey, content, parm);
  101. } else {
  102. this.ctx.logger.error('!!!!!!没有配置MQ插件!!!!!!');
  103. }
  104. // 当医生发送群消息时微信提醒所有群内患者
  105. const self = this;
  106. if (type === '0') {
  107. for (const elem of group.patients) {
  108. await self.service.wechat.sendTemplateMsg(self.ctx.app.config.REVIEW_TEMPLATE_ID, elem.openid, contentMq, doctor.name, new Date().toLocaleDateString(), '群聊', '请及时查看');
  109. }
  110. }
  111. return await this.fetch({ id: entity.id });
  112. }
  113. async delete({ id }) {
  114. await this.model.findByIdAndDelete(id);
  115. return 'deleted';
  116. }
  117. }
  118. module.exports = GroupChatService;