|
@@ -0,0 +1,219 @@
|
|
|
+'use strict';
|
|
|
+
|
|
|
+
|
|
|
+const assert = require('assert');
|
|
|
+const _ = require('lodash');
|
|
|
+const { CrudService } = require('naf-framework-mongoose/lib/service');
|
|
|
+const { BusinessError, ErrorCode } = require('naf-core').Error;
|
|
|
+
|
|
|
+class PatientService extends CrudService {
|
|
|
+ constructor(ctx) {
|
|
|
+ super(ctx, 'patient');
|
|
|
+ this.model = this.ctx.model.Patient;
|
|
|
+ this.pDocs = this.ctx.model.Patientdocs;
|
|
|
+ this.pGroups = this.ctx.model.Group;
|
|
|
+ this.pDoctor = this.ctx.model.Doctor;
|
|
|
+ this.chatModel = this.ctx.model.Chat;
|
|
|
+ }
|
|
|
+
|
|
|
+ async query({ doctorid }, { skip, limit }) {
|
|
|
+ assert(doctorid, 'doctorid不能为空');
|
|
|
+ const pdocall = await this.pDocs.find({ doctorid });
|
|
|
+ const pdoc = await this.pDocs.find({ doctorid }).limit(limit).skip(skip);
|
|
|
+ const patients = [];
|
|
|
+ for (const el of pdoc) {
|
|
|
+ const patient = await this.model.findById(el.patientid);
|
|
|
+ patients.push(patient);
|
|
|
+ }
|
|
|
+ const result = { total: pdocall.length, data: patients };
|
|
|
+ return result;
|
|
|
+ }
|
|
|
+
|
|
|
+
|
|
|
+ async create(data) {
|
|
|
+ const { name, tel, openid, groupid } = data;
|
|
|
+ assert(name, '患者名称不能为空');
|
|
|
+ assert(tel, '患者电话不能为空');
|
|
|
+ assert(openid, '微信openid不能为空');
|
|
|
+ assert(groupid, '缺少群信息项');
|
|
|
+
|
|
|
+ const group = await this.pGroups.findById(groupid, '+patients');
|
|
|
+ if (!group) {
|
|
|
+ throw new BusinessError(ErrorCode.DATA_NOT_EXIST, '群不存在');
|
|
|
+ }
|
|
|
+ const doctor = await this.pDoctor.findById(group.doctorid);
|
|
|
+
|
|
|
+ let entity = await this.model.findOne({ openid });
|
|
|
+ if (entity) {
|
|
|
+ console.log(group.patients);
|
|
|
+ if (group.patients.length === 0) {
|
|
|
+ const patient = { patientid: entity.id, url: entity.url, name: entity.name, openid: entity.openid };
|
|
|
+
|
|
|
+
|
|
|
+ group.patients.push(patient);
|
|
|
+ await group.save();
|
|
|
+ } else {
|
|
|
+ const groupPatient = group.patients.filter(fil => fil.patientid === entity.id);
|
|
|
+ if (groupPatient) {
|
|
|
+ throw new BusinessError(ErrorCode.DATA_EXIST, '已经注册,无需重复注册');
|
|
|
+ } else {
|
|
|
+ const patient = { patientid: entity.id, url: entity.url, name: entity.name, openid: entity.openid };
|
|
|
+
|
|
|
+
|
|
|
+ group.patients.push(patient);
|
|
|
+ await group.save();
|
|
|
+ }
|
|
|
+ }
|
|
|
+ const pdoc = await this.pDocs.findOne({ doctorid: doctor.id });
|
|
|
+ if (!pdoc) {
|
|
|
+ await this.pDocs.create({ patientid: entity.id, patientname: entity.name, doctorid: doctor.id, doctorname: doctor.name });
|
|
|
+ }
|
|
|
+ } else {
|
|
|
+ entity = await this.model.create(data);
|
|
|
+ await this.pDocs.create({ patientid: entity.id, patientname: name, doctorid: doctor.id, doctorname: doctor.name });
|
|
|
+ const patient = { patientid: entity.id, url: entity.url, name: entity.name, openid: entity.openid };
|
|
|
+ group.patients.push(patient);
|
|
|
+ await group.save();
|
|
|
+ }
|
|
|
+ return await this.fetch({ id: entity.id });
|
|
|
+ }
|
|
|
+
|
|
|
+
|
|
|
+ async doctors({ id }) {
|
|
|
+ assert(id, '患者id不能为空');
|
|
|
+ const doctors = await this.pDocs.find({ patientid: id });
|
|
|
+ const data = [];
|
|
|
+ for (const elm of doctors) {
|
|
|
+
|
|
|
+ let contenttype = '';
|
|
|
+ let content = '';
|
|
|
+ let sendtime = '';
|
|
|
+ const chats = await this.chatModel.find({ $or: [{ sendid: elm.doctorid, receiveid: elm.patientid }, { sendid: elm.patientid, receiveid: elm.doctorid }] }).sort({ sendtime: -1 }).limit(1)
|
|
|
+ .skip(0);
|
|
|
+ if (chats && chats.length > 0) {
|
|
|
+ contenttype = chats[0].contenttype;
|
|
|
+ content = chats[0].content;
|
|
|
+ sendtime = chats[0].sendtime;
|
|
|
+ }
|
|
|
+ const newdata = { patientid: elm.patientid, patientname: elm.patientname, doctorid: elm.doctorid, doctorname: elm.doctorname, contenttype, content, sendtime };
|
|
|
+ data.push(newdata);
|
|
|
+ }
|
|
|
+ return data;
|
|
|
+ }
|
|
|
+
|
|
|
+
|
|
|
+ async groups({ id }) {
|
|
|
+ assert(id, '患者id不能为空');
|
|
|
+ const groups = await this.pGroups.find({ 'patients.patientid': id }, '+patients');
|
|
|
+ return groups;
|
|
|
+ }
|
|
|
+
|
|
|
+ async delete({ id }) {
|
|
|
+ await this.model.findByIdAndDelete(id);
|
|
|
+ await this.pDocs.deleteMany({ patientid: id });
|
|
|
+ return 'deleted';
|
|
|
+ }
|
|
|
+
|
|
|
+ async updateInfo({ id }, data) {
|
|
|
+
|
|
|
+ const entity = await this.model.findById(id);
|
|
|
+ if (!entity) {
|
|
|
+ throw new BusinessError(ErrorCode.DATA_NOT_EXIST, '患者信息不存在');
|
|
|
+ }
|
|
|
+ entity.name = data.name;
|
|
|
+ entity.cardno = data.cardno;
|
|
|
+ entity.gender = data.gender;
|
|
|
+ entity.birthday = data.birthday;
|
|
|
+ entity.address = data.address;
|
|
|
+ entity.tel = data.tel;
|
|
|
+ entity.urgentname = data.urgentname;
|
|
|
+ entity.urgenttel = data.urgenttel;
|
|
|
+ entity.content = data.content;
|
|
|
+ entity.openid = data.openid;
|
|
|
+ entity.icon = data.icon;
|
|
|
+ return await entity.save();
|
|
|
+ }
|
|
|
+
|
|
|
+
|
|
|
+ async createEmr({ patientid }, data) {
|
|
|
+ const { indate, outdate, title, content, doctorid, doctorname } = data;
|
|
|
+ assert(patientid, '患者ID不能为空');
|
|
|
+ assert(title, '标题不能为空');
|
|
|
+
|
|
|
+
|
|
|
+ const patient = await this.model.findById(patientid, '+emrs');
|
|
|
+ if (!patient) {
|
|
|
+ throw new BusinessError(ErrorCode.DATA_NOT_EXIST, '患者信息不存在');
|
|
|
+ }
|
|
|
+
|
|
|
+
|
|
|
+ const newData = { indate, outdate, title, content, doctorid, doctorname };
|
|
|
+
|
|
|
+ const newemr = patient.emrs.create(newData);
|
|
|
+ console.log('newemr:', newemr);
|
|
|
+ patient.emrs.push(newemr);
|
|
|
+ await patient.save();
|
|
|
+ return newemr;
|
|
|
+ }
|
|
|
+
|
|
|
+
|
|
|
+ async deleteEmr({ patientid, id }) {
|
|
|
+ assert(id, 'id不能为空');
|
|
|
+
|
|
|
+
|
|
|
+ let patient;
|
|
|
+ if (patientid) {
|
|
|
+ patient = await this.model.findById(patientid, '+emrs');
|
|
|
+ }
|
|
|
+ if (!patient) {
|
|
|
+ throw new BusinessError(ErrorCode.DATA_NOT_EXIST, '患者信息不存在');
|
|
|
+ }
|
|
|
+
|
|
|
+ patient.emrs.id(id).remove();
|
|
|
+ return await patient.save();
|
|
|
+ }
|
|
|
+
|
|
|
+
|
|
|
+ async updateEmr({ patientid, id }, data) {
|
|
|
+ assert(id, 'id不能为空');
|
|
|
+
|
|
|
+
|
|
|
+ let patient;
|
|
|
+ if (patientid) {
|
|
|
+ patient = await this.model.findById(patientid, '+emrs');
|
|
|
+ }
|
|
|
+ if (!patient) {
|
|
|
+ throw new BusinessError(ErrorCode.DATA_NOT_EXIST, '患者信息不存在');
|
|
|
+ }
|
|
|
+
|
|
|
+
|
|
|
+ const emr = patient.emrs.id(id);
|
|
|
+ const { indate, outdate, title, content, doctorid, doctorname } = data;
|
|
|
+ emr.indate = indate;
|
|
|
+ emr.outdate = outdate;
|
|
|
+ emr.title = title;
|
|
|
+ emr.content = content;
|
|
|
+ emr.doctorid = doctorid;
|
|
|
+ emr.doctorname = doctorname;
|
|
|
+ return await patient.save();
|
|
|
+ }
|
|
|
+
|
|
|
+
|
|
|
+ async fetchEmr({ patientid, emrid }) {
|
|
|
+ assert(patientid, '患者id不能为空');
|
|
|
+ assert(emrid, '病历id不能为空');
|
|
|
+
|
|
|
+ const patient = await this.model.findById(patientid, '+emrs');
|
|
|
+ const emr = patient.emrs.id(emrid);
|
|
|
+ return emr;
|
|
|
+ }
|
|
|
+
|
|
|
+
|
|
|
+ async findByOpenid(data) {
|
|
|
+ return await this.model.findOne({ openid: data.openid });
|
|
|
+ }
|
|
|
+
|
|
|
+
|
|
|
+}
|
|
|
+
|
|
|
+module.exports = PatientService;
|