patient.js 9.2 KB

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