patient.js 8.2 KB

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