group.js 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133
  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. class GroupService extends CrudService {
  8. constructor(ctx) {
  9. super(ctx, 'group');
  10. this.model = this.ctx.model.Group;
  11. this.gcModel = this.ctx.model.Groupchat;
  12. }
  13. async query({ doctorid, name }, { skip, limit }) {
  14. assert(doctorid, 'doctorid不能为空');
  15. let info = { doctorid };
  16. if (name) {
  17. info = { doctorid, name };
  18. }
  19. const groupall = await this.model.find(info);
  20. let groups = await this.model.find(info).limit(limit).skip(skip);
  21. if (groups.length > 0) groups = JSON.parse(JSON.stringify(groups));
  22. const newgroups = [];
  23. for (const el of groups) {
  24. const groupchat = await this.gcModel.findOne({ groupid: el.id }).sort({ sendtime: -1 });
  25. let group;
  26. if (groupchat) {
  27. el.content = groupchat.content;
  28. el.contenttype = groupchat.contenttype;
  29. el.sendtime = groupchat.sendtime;
  30. group = el;
  31. } else {
  32. group = el;
  33. }
  34. newgroups.push(group);
  35. }
  36. const result = { total: groupall.length, data: newgroups };
  37. return result;
  38. }
  39. // 创建群信息
  40. async create(data) {
  41. const { name, doctorid } = data;
  42. assert(name, '群名称不能为空');
  43. assert(doctorid, '缺少医生信息项');
  44. // TODO: 检查是否已经注册
  45. const newdata = { ...data };
  46. const entity = await this.model.create(newdata);
  47. return await this.fetch({ id: entity.id });
  48. }
  49. async delete({ id }) {
  50. await this.model.findByIdAndDelete(id);
  51. return 'deleted';
  52. }
  53. async updateInfo({ id }, data) {
  54. // TODO: 检查数据是否存在
  55. const entity = await this.model.findById(id);
  56. if (!entity) {
  57. throw new BusinessError(ErrorCode.DATA_NOT_EXIST, '群信息不存在');
  58. }
  59. entity.doctorid = data.doctorid;
  60. entity.name = data.name;
  61. entity.content = data.content;
  62. return await entity.save();
  63. }
  64. // 群删除患者信息
  65. async deletePatient({ groupid, id }) {
  66. assert(id, 'id不能为空');
  67. // TODO: 检查数据是否存在
  68. let group;
  69. if (groupid) {
  70. group = await this.model.findById(groupid, '+patients');
  71. }
  72. if (!group) {
  73. throw new BusinessError(ErrorCode.DATA_NOT_EXIST, '群信息不存在');
  74. }
  75. // TODO: 保存数据
  76. group.patients.id(id).remove();
  77. return await group.save();
  78. }
  79. // 修改患者病历信息
  80. async updatePatient({ groupid, id }, data) {
  81. assert(id, 'id不能为空');
  82. // TODO: 检查数据是否存在
  83. let group;
  84. if (groupid) {
  85. group = await this.model.findById(groupid, '+patients');
  86. }
  87. if (!group) {
  88. throw new BusinessError(ErrorCode.DATA_NOT_EXIST, '群信息不存在');
  89. }
  90. // TODO: 保存数据
  91. const patient = group.patients.id(id);
  92. const { patientid, url, name, openid } = data;
  93. patient.patientid = patientid;
  94. patient.url = url;
  95. patient.name = name;
  96. patient.openid = openid;
  97. return await group.save();
  98. }
  99. // 取得患者病历信息
  100. async fetchPatient({ groupid }) {
  101. assert(groupid, '群id不能为空');
  102. // TODO: 检查数据是否存在
  103. return await this.model.findById(groupid, '+patients');
  104. }
  105. /**
  106. * 退群
  107. * @param {Object} params 群组id,病人id
  108. */
  109. async exit({ groupid, patientid }) {
  110. const group = await this.model.findById(groupid, '+patients');
  111. if (!group) throw new BusinessError(ErrorCode.DATA_NOT_EXIST, '未找到指定群组');
  112. const patient = group.patients.find(f => ObjectId(f.patientid).equals(patientid));
  113. if (!patient) throw new BusinessError(ErrorCode.DATA_NOT_EXIST, '未找到指定病人');
  114. await group.patients.id(patient._id).remove();
  115. await group.save();
  116. }
  117. }
  118. module.exports = GroupService;