123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264 |
- 'use strict';
- const assert = require('assert');
- const _ = require('lodash');
- const { CrudService } = require('naf-framework-mongoose/lib/service');
- const { BusinessError, ErrorCode } = require('naf-core').Error;
- const { ObjectId } = require('mongoose').Types;
- 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;
- this.remark = this.ctx.model.Remark;
- this.gc = this.ctx.model.Groupchat;
- this.room = this.ctx.model.Room;
- }
- async query({ doctorid, ...query }, { skip, limit }) {
- assert(doctorid, 'doctorid不能为空');
- const pdocall = await this.pDocs.find({ doctorid, ...query });
- const pdoc = await this.pDocs.find({ doctorid, ...query }).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);
- // TODO: 检查是否已经注册
- let entity = await this.model.findOne({ openid });
- if (entity) {
- 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 {
- console.log(group.patients);
- console.log(entity._id);
- const groupPatient = group.patients.find(fil => ObjectId(fil.patientid).equals(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 = [];
- const remarks = await this.remark.find({ remarkid: id });
- 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;
- }
- let { patientid, patientname, doctorid, doctorname } = elm;
- const s_r = remarks.find(f => ObjectId(f.benoteid).equals(patientid));
- if (s_r) patientname = s_r.name;
- const r_r = remarks.find(f => ObjectId(f.benoteid).equals(doctorid));
- if (r_r) doctorname = r_r.name;
- const newdata = { patientid: elm.patientid, patientname, doctorid: elm.doctorid, doctorname, contenttype, content, sendtime };
- data.push(newdata);
- }
- return data;
- }
- // 查询患者所属医生列表信息
- async groups({ id }) {
- assert(id, '患者id不能为空');
- let groups = await this.pGroups.find({ 'patients.patientid': id });
- if (groups.length > 0) groups = JSON.parse(JSON.stringify(groups));
- for (const g of groups) {
- const chat = await this.gc.findOne({ groupid: g.id }).sort({ 'meta.createdAt': -1 });
- if (chat) {
- const { content, contenttype } = chat;
- g.content = content;
- g.contenttype = contenttype;
- }
- }
- return groups;
- }
- async delete({ id }) {
- // 删除病人和医生的关联
- await this.pDocs.deleteMany({ patientid: id });
- // 删除群组中的病人
- const res = await this.pGroups.find({ 'patients.patientid': id }, '+patients');
- for (const i of res) {
- i.patients = i.patients.filter(f => f.patientid !== id);
- await i.save();
- }
- // 删除该病人
- await this.model.findByIdAndDelete(id);
- return 'deleted';
- }
- async updateInfo({ id }, data) {
- // TODO: 检查数据是否存在
- 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, '标题不能为空');
- // TODO: 检查数据是否存在
- 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 };
- // TODO: 保存数据
- const newemr = patient.emrs.create(newData);
- patient.emrs.push(newemr);
- await patient.save();
- return newemr;
- }
- // 删除患者病历信息
- async deleteEmr({ patientid, id }) {
- assert(id, 'id不能为空');
- // TODO: 检查数据是否存在
- let patient;
- if (patientid) {
- patient = await this.model.findById(patientid, '+emrs');
- }
- if (!patient) {
- throw new BusinessError(ErrorCode.DATA_NOT_EXIST, '患者信息不存在');
- }
- // TODO: 保存数据
- patient.emrs.id(id).remove();
- return await patient.save();
- }
- // 修改患者病历信息
- async updateEmr({ patientid, id }, data) {
- assert(id, 'id不能为空');
- // TODO: 检查数据是否存在
- let patient;
- if (patientid) {
- patient = await this.model.findById(patientid, '+emrs');
- }
- if (!patient) {
- throw new BusinessError(ErrorCode.DATA_NOT_EXIST, '患者信息不存在');
- }
- // TODO: 保存数据
- const emr = patient.emrs.id(id);
- const { indate, outdate, title, content, doctorid, doctorname, img } = data;
- emr.indate = indate;
- emr.outdate = outdate;
- emr.title = title;
- emr.content = content;
- emr.doctorid = doctorid;
- emr.doctorname = doctorname;
- emr.img = img;
- return await patient.save();
- }
- // 取得患者病历信息
- async fetchEmr({ patientid, emrid }) {
- assert(patientid, '患者id不能为空');
- assert(emrid, '病历id不能为空');
- // TODO: 检查数据是否存在
- const patient = await this.model.findById(patientid, '+emrs');
- const emr = patient.emrs.id(emrid);
- return emr;
- }
- // 根据openid 取得医生信息
- async findByOpenid(data) {
- return await this.model.findOne({ openid: data.openid });
- }
- // 医生移除病人
- async fromDoctorDelete({ doctorid, patientid } = {}) {
- assert(doctorid, '缺少医生信息');
- assert(patientid, '缺少病人信息');
- // 切断医生和病人的关系
- // 删除医生群组下的这个病人
- const res = await this.pGroups.find({ 'patients.patientid': patientid, doctorid }, '+patients');
- for (const i of res) {
- i.patients = i.patients.filter(f => f.patientid !== patientid);
- await i.save();
- }
- // 删除医生和病人的关系,patient_doctor
- await this.pDocs.deleteMany({ patientid, doctorid });
- // 删除医生和病人的聊天房间
- await this.room.deleteMany({ patientid, doctorid });
- // 删除医生和病人的单聊,群聊的聊天记录
- await this.chatModel.deleteMany({ $or: [{ sendid: doctorid, receiveid: patientid }, { sendid: patientid, receiveid: doctorid }] });
- await this.gc.deleteMany({ doctorid, sendid: patientid });
- }
- }
- module.exports = PatientService;
|