patient.js 7.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232
  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 PatientService extends CrudService {
  8. constructor(ctx) {
  9. super(ctx, 'patient');
  10. this.model = this.ctx.model.Patient;
  11. this.pDocs = this.ctx.model.Patientdocs;
  12. this.pGroups = this.ctx.model.Group;
  13. this.pDoctor = this.ctx.model.Doctor;
  14. this.chatModel = this.ctx.model.Chat;
  15. this.remark = this.ctx.model.Remark;
  16. this.gc = this.ctx.model.Groupchat;
  17. }
  18. async query({ doctorid, ...query }, { skip, limit }) {
  19. assert(doctorid, 'doctorid不能为空');
  20. const pdocall = await this.pDocs.find({ doctorid, ...query });
  21. const pdoc = await this.pDocs.find({ doctorid, ...query }).limit(limit).skip(skip);
  22. const patients = [];
  23. for (const el of pdoc) {
  24. const patient = await this.model.findById(el.patientid);
  25. patients.push(patient);
  26. }
  27. const result = { total: pdocall.length, data: patients };
  28. return result;
  29. }
  30. // 创建患者信息
  31. async create(data) {
  32. const { name, tel, openid, groupid } = data;
  33. assert(name, '患者名称不能为空');
  34. assert(tel, '患者电话不能为空');
  35. assert(openid, '微信openid不能为空');
  36. assert(groupid, '缺少群信息项');
  37. const group = await this.pGroups.findById(groupid, '+patients');
  38. if (!group) {
  39. throw new BusinessError(ErrorCode.DATA_NOT_EXIST, '群不存在');
  40. }
  41. const doctor = await this.pDoctor.findById(group.doctorid);
  42. // TODO: 检查是否已经注册
  43. let entity = await this.model.findOne({ openid });
  44. if (entity) {
  45. if (group.patients.length === 0) {
  46. const patient = { patientid: entity.id, url: entity.url, name: entity.name, openid: entity.openid };
  47. group.patients.push(patient);
  48. await group.save();
  49. } else {
  50. const groupPatient = group.patients.filter(fil => fil.patientid === entity.id);
  51. if (groupPatient) {
  52. throw new BusinessError(ErrorCode.DATA_EXIST, '已经注册,无需重复注册');
  53. } else {
  54. const patient = { patientid: entity.id, url: entity.url, name: entity.name, openid: entity.openid };
  55. group.patients.push(patient);
  56. await group.save();
  57. }
  58. }
  59. const pdoc = await this.pDocs.findOne({ doctorid: doctor.id });
  60. if (!pdoc) {
  61. await this.pDocs.create({ patientid: entity.id, patientname: entity.name, doctorid: doctor.id, doctorname: doctor.name });
  62. }
  63. } else {
  64. entity = await this.model.create(data);
  65. await this.pDocs.create({ patientid: entity.id, patientname: name, doctorid: doctor.id, doctorname: doctor.name });
  66. const patient = { patientid: entity.id, url: entity.url, name: entity.name, openid: entity.openid };
  67. group.patients.push(patient);
  68. await group.save();
  69. }
  70. return await this.fetch({ id: entity.id });
  71. }
  72. // 查询患者所属医生列表信息
  73. async doctors({ id }) {
  74. assert(id, '患者id不能为空');
  75. const doctors = await this.pDocs.find({ patientid: id });
  76. const data = [];
  77. const remarks = await this.remark.find({ remarkid: id });
  78. for (const elm of doctors) {
  79. let contenttype = '';
  80. let content = '';
  81. let sendtime = '';
  82. const chats = await this.chatModel.find({ $or: [{ sendid: elm.doctorid, receiveid: elm.patientid }, { sendid: elm.patientid, receiveid: elm.doctorid }] }).sort({ sendtime: -1 }).limit(1)
  83. .skip(0);
  84. if (chats && chats.length > 0) {
  85. contenttype = chats[0].contenttype;
  86. content = chats[0].content;
  87. sendtime = chats[0].sendtime;
  88. }
  89. let { patientid, patientname, doctorid, doctorname } = elm;
  90. const s_r = remarks.find(f => ObjectId(f.benoteid).equals(patientid));
  91. if (s_r) patientname = s_r.name;
  92. const r_r = remarks.find(f => ObjectId(f.benoteid).equals(doctorid));
  93. if (r_r) doctorname = r_r.name;
  94. const newdata = { patientid: elm.patientid, patientname, doctorid: elm.doctorid, doctorname, contenttype, content, sendtime };
  95. data.push(newdata);
  96. }
  97. return data;
  98. }
  99. // 查询患者所属医生列表信息
  100. async groups({ id }) {
  101. assert(id, '患者id不能为空');
  102. let groups = await this.pGroups.find({ 'patients.patientid': id });
  103. if (groups.length > 0) groups = JSON.parse(JSON.stringify(groups));
  104. for (const g of groups) {
  105. const chat = await this.gc.findOne({ groupid: g.id }).sort({ 'meta.createdAt': -1 });
  106. if (chat) {
  107. const { content, contenttype } = chat;
  108. g.content = content;
  109. g.contenttype = contenttype;
  110. }
  111. }
  112. return groups;
  113. }
  114. async delete({ id }) {
  115. await this.model.findByIdAndDelete(id);
  116. await this.pDocs.deleteMany({ patientid: id });
  117. return 'deleted';
  118. }
  119. async updateInfo({ id }, data) {
  120. // TODO: 检查数据是否存在
  121. const entity = await this.model.findById(id);
  122. if (!entity) {
  123. throw new BusinessError(ErrorCode.DATA_NOT_EXIST, '患者信息不存在');
  124. }
  125. entity.name = data.name;
  126. entity.cardno = data.cardno;
  127. entity.gender = data.gender;
  128. entity.birthday = data.birthday;
  129. entity.address = data.address;
  130. entity.tel = data.tel;
  131. entity.urgentname = data.urgentname;
  132. entity.urgenttel = data.urgenttel;
  133. entity.content = data.content;
  134. entity.openid = data.openid;
  135. entity.icon = data.icon;
  136. return await entity.save();
  137. }
  138. // 用户创建患者病历信息
  139. async createEmr({ patientid }, data) {
  140. const { indate, outdate, title, content, doctorid, doctorname } = data;
  141. assert(patientid, '患者ID不能为空');
  142. assert(title, '标题不能为空');
  143. // TODO: 检查数据是否存在
  144. const patient = await this.model.findById(patientid, '+emrs');
  145. if (!patient) {
  146. throw new BusinessError(ErrorCode.DATA_NOT_EXIST, '患者信息不存在');
  147. }
  148. // 准备数据
  149. const newData = { indate, outdate, title, content, doctorid, doctorname };
  150. // TODO: 保存数据
  151. const newemr = patient.emrs.create(newData);
  152. patient.emrs.push(newemr);
  153. await patient.save();
  154. return newemr;
  155. }
  156. // 删除患者病历信息
  157. async deleteEmr({ patientid, id }) {
  158. assert(id, 'id不能为空');
  159. // TODO: 检查数据是否存在
  160. let patient;
  161. if (patientid) {
  162. patient = await this.model.findById(patientid, '+emrs');
  163. }
  164. if (!patient) {
  165. throw new BusinessError(ErrorCode.DATA_NOT_EXIST, '患者信息不存在');
  166. }
  167. // TODO: 保存数据
  168. patient.emrs.id(id).remove();
  169. return await patient.save();
  170. }
  171. // 修改患者病历信息
  172. async updateEmr({ patientid, id }, data) {
  173. assert(id, 'id不能为空');
  174. // TODO: 检查数据是否存在
  175. let patient;
  176. if (patientid) {
  177. patient = await this.model.findById(patientid, '+emrs');
  178. }
  179. if (!patient) {
  180. throw new BusinessError(ErrorCode.DATA_NOT_EXIST, '患者信息不存在');
  181. }
  182. // TODO: 保存数据
  183. const emr = patient.emrs.id(id);
  184. const { indate, outdate, title, content, doctorid, doctorname } = data;
  185. emr.indate = indate;
  186. emr.outdate = outdate;
  187. emr.title = title;
  188. emr.content = content;
  189. emr.doctorid = doctorid;
  190. emr.doctorname = doctorname;
  191. return await patient.save();
  192. }
  193. // 取得患者病历信息
  194. async fetchEmr({ patientid, emrid }) {
  195. assert(patientid, '患者id不能为空');
  196. assert(emrid, '病历id不能为空');
  197. // TODO: 检查数据是否存在
  198. const patient = await this.model.findById(patientid, '+emrs');
  199. const emr = patient.emrs.id(emrid);
  200. return emr;
  201. }
  202. // 根据openid 取得医生信息
  203. async findByOpenid(data) {
  204. return await this.model.findOne({ openid: data.openid });
  205. }
  206. }
  207. module.exports = PatientService;