patient.js 7.3 KB

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