123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133 |
- '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 GroupService extends CrudService {
- constructor(ctx) {
- super(ctx, 'group');
- this.model = this.ctx.model.Group;
- this.gcModel = this.ctx.model.Groupchat;
- }
- async query({ doctorid, name }, { skip, limit }) {
- assert(doctorid, 'doctorid不能为空');
- let info = { doctorid };
- if (name) {
- info = { doctorid, name };
- }
- const groupall = await this.model.find(info);
- let groups = await this.model.find(info).limit(limit).skip(skip);
- if (groups.length > 0) groups = JSON.parse(JSON.stringify(groups));
- const newgroups = [];
- for (const el of groups) {
- const groupchat = await this.gcModel.findOne({ groupid: el.id }).sort({ sendtime: -1 });
- let group;
- if (groupchat) {
- el.content = groupchat.content;
- el.contenttype = groupchat.contenttype;
- el.sendtime = groupchat.sendtime;
- group = el;
- } else {
- group = el;
- }
- newgroups.push(group);
- }
- const result = { total: groupall.length, data: newgroups };
- return result;
- }
- // 创建群信息
- async create(data) {
- const { name, doctorid } = data;
- assert(name, '群名称不能为空');
- assert(doctorid, '缺少医生信息项');
- // TODO: 检查是否已经注册
- const newdata = { ...data };
- const entity = await this.model.create(newdata);
- return await this.fetch({ id: entity.id });
- }
- async delete({ id }) {
- 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.doctorid = data.doctorid;
- entity.name = data.name;
- entity.content = data.content;
- return await entity.save();
- }
- // 群删除患者信息
- async deletePatient({ groupid, id }) {
- assert(id, 'id不能为空');
- // TODO: 检查数据是否存在
- let group;
- if (groupid) {
- group = await this.model.findById(groupid, '+patients');
- }
- if (!group) {
- throw new BusinessError(ErrorCode.DATA_NOT_EXIST, '群信息不存在');
- }
- // TODO: 保存数据
- group.patients.id(id).remove();
- return await group.save();
- }
- // 修改患者病历信息
- async updatePatient({ groupid, id }, data) {
- assert(id, 'id不能为空');
- // TODO: 检查数据是否存在
- let group;
- if (groupid) {
- group = await this.model.findById(groupid, '+patients');
- }
- if (!group) {
- throw new BusinessError(ErrorCode.DATA_NOT_EXIST, '群信息不存在');
- }
- // TODO: 保存数据
- const patient = group.patients.id(id);
- const { patientid, url, name, openid } = data;
- patient.patientid = patientid;
- patient.url = url;
- patient.name = name;
- patient.openid = openid;
- return await group.save();
- }
- // 取得患者病历信息
- async fetchPatient({ groupid }) {
- assert(groupid, '群id不能为空');
- // TODO: 检查数据是否存在
- return await this.model.findById(groupid, '+patients');
- }
- /**
- * 退群
- * @param {Object} params 群组id,病人id
- */
- async exit({ groupid, patientid }) {
- const group = await this.model.findById(groupid, '+patients');
- if (!group) throw new BusinessError(ErrorCode.DATA_NOT_EXIST, '未找到指定群组');
- const patient = group.patients.find(f => ObjectId(f.patientid).equals(patientid));
- if (!patient) throw new BusinessError(ErrorCode.DATA_NOT_EXIST, '未找到指定病人');
- await group.patients.id(patient._id).remove();
- await group.save();
- }
- }
- module.exports = GroupService;
|