room.js 1.7 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455
  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. }
  14. async query(data) {
  15. const { patientid, doctorid } = data;
  16. let res;
  17. if (patientid) {
  18. res = await this.model.find({ patientid });
  19. }
  20. if (doctorid) {
  21. const res_ = await this.model.find({ doctorid });
  22. const data = [];
  23. for (const elm of res_) {
  24. const patient = await this.pmodel.findById(elm.patientid);
  25. let icon = '';
  26. if (patient) {
  27. icon = patient.icon;
  28. }
  29. let contenttype = '';
  30. let content = '';
  31. let sendtime = '';
  32. const chats = await this.chatModel.find({ $or: [{ sendid: elm.doctorid, receiveid: elm.patientid }, { sendid: elm.patientid, receiveid: elm.doctorid }] }).sort({ sendtime: -1 }).limit(1)
  33. .skip(0);
  34. if (chats && chats.length > 0) {
  35. contenttype = chats[0].contenttype;
  36. content = chats[0].content;
  37. sendtime = chats[0].sendtime;
  38. }
  39. const newdata = { patientid: elm.patientid, patientname: elm.patientname, icon, contenttype, content, sendtime, doctorid: elm.doctorid, doctorname: elm.doctorname };
  40. data.push(newdata);
  41. }
  42. res = data;
  43. }
  44. return res;
  45. }
  46. async delete({ id }) {
  47. await this.model.findByIdAndDelete(id);
  48. return 'deleted';
  49. }
  50. }
  51. module.exports = RoomService;