123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960 |
- 'use strict';
- const assert = require('assert');
- const _ = require('lodash');
- const { ObjectId } = require('mongoose').Types;
- const { CrudService } = require('naf-framework-mongoose/lib/service');
- const { BusinessError, ErrorCode } = require('naf-core').Error;
- class RoomService extends CrudService {
- constructor(ctx) {
- super(ctx, 'room');
- this.model = this.ctx.model.Room;
- this.pmodel = this.ctx.model.Patient;
- this.chatModel = this.ctx.model.Chat;
- this.remark = this.ctx.model.Remark;
- }
- async query(data) {
- const { patientid, doctorid } = data;
- let res;
- if (patientid) {
- res = await this.model.find({ patientid });
- }
- if (doctorid) {
- const res_ = await this.model.find({ doctorid });
- const remarks = await this.remark.find({ remarkid: doctorid });
- const data = [];
- for (const elm of res_) {
- const patient = await this.pmodel.findById(elm.patientid);
- let icon = '';
- if (patient) {
- icon = patient.icon;
- }
- 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 } = elm;
- const s_r = remarks.find(f => ObjectId(f.benoteid).equals(patientid));
- if (s_r) patientname = s_r.name;
- const newdata = { patientid: elm.patientid, patientname, icon, contenttype, content, sendtime, doctorid: elm.doctorid, doctorname: elm.doctorname };
- data.push(newdata);
- }
- res = data;
- }
- return res;
- }
- async delete({ id }) {
- await this.model.findByIdAndDelete(id);
- return 'deleted';
- }
- }
- module.exports = RoomService;
|