room.js 2.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960
  1. 'use strict';
  2. const assert = require('assert');
  3. const _ = require('lodash');
  4. const { ObjectId } = require('mongoose').Types;
  5. const { CrudService } = require('naf-framework-mongoose/lib/service');
  6. const { BusinessError, ErrorCode } = require('naf-core').Error;
  7. class RoomService extends CrudService {
  8. constructor(ctx) {
  9. super(ctx, 'room');
  10. this.model = this.ctx.model.Room;
  11. this.pmodel = this.ctx.model.Patient;
  12. this.chatModel = this.ctx.model.Chat;
  13. this.remark = this.ctx.model.Remark;
  14. }
  15. async query(data) {
  16. const { patientid, doctorid } = data;
  17. let res;
  18. if (patientid) {
  19. res = await this.model.find({ patientid });
  20. }
  21. if (doctorid) {
  22. const res_ = await this.model.find({ doctorid });
  23. const remarks = await this.remark.find({ remarkid: doctorid });
  24. const data = [];
  25. for (const elm of res_) {
  26. const patient = await this.pmodel.findById(elm.patientid);
  27. let icon = '';
  28. if (patient) {
  29. icon = patient.icon;
  30. }
  31. let contenttype = '';
  32. let content = '';
  33. let sendtime = '';
  34. const chats = await this.chatModel.find({ $or: [{ sendid: elm.doctorid, receiveid: elm.patientid }, { sendid: elm.patientid, receiveid: elm.doctorid }] }).sort({ sendtime: -1 }).limit(1)
  35. .skip(0);
  36. if (chats && chats.length > 0) {
  37. contenttype = chats[0].contenttype;
  38. content = chats[0].content;
  39. sendtime = chats[0].sendtime;
  40. }
  41. let { patientid, patientname } = elm;
  42. const s_r = remarks.find(f => ObjectId(f.benoteid).equals(patientid));
  43. if (s_r) patientname = s_r.name;
  44. const newdata = { patientid: elm.patientid, patientname, icon, contenttype, content, sendtime, doctorid: elm.doctorid, doctorname: elm.doctorname };
  45. data.push(newdata);
  46. }
  47. res = data;
  48. }
  49. return res;
  50. }
  51. async delete({ id }) {
  52. await this.model.findByIdAndDelete(id);
  53. return 'deleted';
  54. }
  55. }
  56. module.exports = RoomService;