|  | @ -3,11 +3,15 @@ package com.yihu.jw.im.service;
 | 
	
		
			
				|  |  | import com.alibaba.fastjson.JSON;
 | 
	
		
			
				|  |  | import com.alibaba.fastjson.JSONArray;
 | 
	
		
			
				|  |  | import com.alibaba.fastjson.JSONObject;
 | 
	
		
			
				|  |  | import com.yihu.jw.entity.base.im.ConsultDo;
 | 
	
		
			
				|  |  | import com.yihu.jw.entity.base.im.ConsultTeamDo;
 | 
	
		
			
				|  |  | import com.yihu.jw.entity.base.im.ConsultTeamDoctorDo;
 | 
	
		
			
				|  |  | import com.yihu.jw.entity.base.im.ConsultTeamLogDo;
 | 
	
		
			
				|  |  | import com.yihu.jw.entity.base.patient.BasePatientDO;
 | 
	
		
			
				|  |  | import com.yihu.jw.im.dao.ConsultDao;
 | 
	
		
			
				|  |  | import com.yihu.jw.im.dao.ConsultTeamDao;
 | 
	
		
			
				|  |  | import com.yihu.jw.im.dao.ConsultTeamDoctorDao;
 | 
	
		
			
				|  |  | import com.yihu.jw.im.dao.ConsultTeamLogDao;
 | 
	
		
			
				|  |  | import com.yihu.jw.im.util.ImUtil;
 | 
	
		
			
				|  |  | import com.yihu.jw.im.util.ImageCompress;
 | 
	
		
			
				|  |  | import com.yihu.jw.patient.dao.BasePatientDao;
 | 
	
	
		
			
				|  | @ -47,6 +51,12 @@ public class ImService {
 | 
	
		
			
				|  |  | 	@Autowired
 | 
	
		
			
				|  |  | 	public ConsultTeamDao consultTeamDao;
 | 
	
		
			
				|  |  | 	
 | 
	
		
			
				|  |  | 	@Autowired
 | 
	
		
			
				|  |  | 	public ConsultTeamDoctorDao consultTeamDoctorDao;
 | 
	
		
			
				|  |  | 	
 | 
	
		
			
				|  |  | 	@Autowired
 | 
	
		
			
				|  |  | 	public ConsultTeamLogDao consultTeamLogDao;
 | 
	
		
			
				|  |  | 	
 | 
	
		
			
				|  |  | 	@Autowired
 | 
	
		
			
				|  |  | 	public BasePatientDao basePatientDao;
 | 
	
		
			
				|  |  | 	
 | 
	
	
		
			
				|  | @ -585,4 +595,182 @@ public class ImService {
 | 
	
		
			
				|  |  | 		return null;
 | 
	
		
			
				|  |  | 	}
 | 
	
		
			
				|  |  | 	
 | 
	
		
			
				|  |  | 	/**
 | 
	
		
			
				|  |  | 	 * 患者端
 | 
	
		
			
				|  |  | 	 * 发起专家咨询
 | 
	
		
			
				|  |  | 	 * @param ct      专家咨询对象
 | 
	
		
			
				|  |  | 	 * @param patient 患者标识
 | 
	
		
			
				|  |  | 	 * @return
 | 
	
		
			
				|  |  | 	 * @throws Exception
 | 
	
		
			
				|  |  | 	 */
 | 
	
		
			
				|  |  | 	public JSONObject addTeamConsult(ConsultTeamDo ct, String patient, String doctor) throws Exception {
 | 
	
		
			
				|  |  | 		JSONObject re = new JSONObject();
 | 
	
		
			
				|  |  | 		if (getUnfinishedConsult(patient, doctor) > 0) {//判断是否有未结束的咨询移到同步方法中
 | 
	
		
			
				|  |  | //			re.put("status", -3);
 | 
	
		
			
				|  |  | //			return re;
 | 
	
		
			
				|  |  | 			throw new RuntimeException("居民还有未结束的专家咨询!");
 | 
	
		
			
				|  |  | 		}
 | 
	
		
			
				|  |  | 		
 | 
	
		
			
				|  |  | 		JSONObject users = new JSONObject();
 | 
	
		
			
				|  |  | 		users.put(doctor,0);
 | 
	
		
			
				|  |  | 
 | 
	
		
			
				|  |  | 		
 | 
	
		
			
				|  |  | 		// 设置患者信息
 | 
	
		
			
				|  |  | 		ct.setPatient(patient);
 | 
	
		
			
				|  |  | 		// 查询患者信息
 | 
	
		
			
				|  |  | 		BasePatientDO tempPatient = basePatientDao.findById(patient);
 | 
	
		
			
				|  |  | 		// 设置患者姓名
 | 
	
		
			
				|  |  | 		ct.setName(tempPatient.getName());
 | 
	
		
			
				|  |  | 		// 设置患者生日
 | 
	
		
			
				|  |  | 		ct.setBirthday(tempPatient.getBirthday());
 | 
	
		
			
				|  |  | 		//新增性别
 | 
	
		
			
				|  |  | 		ct.setSex(tempPatient.getSex());
 | 
	
		
			
				|  |  | 		// 设置患者头像
 | 
	
		
			
				|  |  | 		ct.setPhoto(tempPatient.getPhoto());
 | 
	
		
			
				|  |  | 		// 设置操作日期
 | 
	
		
			
				|  |  | 		ct.setCzrq(new Date());
 | 
	
		
			
				|  |  | 		ct.setDel("1");
 | 
	
		
			
				|  |  | 		ct.setStatus(0);
 | 
	
		
			
				|  |  | 		ct.setEvaluate(0);
 | 
	
		
			
				|  |  | 		// 医生未读数量为1
 | 
	
		
			
				|  |  | 		ct.setDoctorRead(1);
 | 
	
		
			
				|  |  | 		// 添加咨询记录
 | 
	
		
			
				|  |  | 		ConsultDo consult = addConsult(ct.getPatient(), null, ct.getSymptoms(), ct.getImages(), ct.getType());
 | 
	
		
			
				|  |  | //		// 设置关联指导
 | 
	
		
			
				|  |  | //		consult.setGuidance(ct.getGuidance());
 | 
	
		
			
				|  |  | 		// 设置咨询标识
 | 
	
		
			
				|  |  | 		ct.setConsult(consult.getId());
 | 
	
		
			
				|  |  | 		
 | 
	
		
			
				|  |  | 		//推送给IM去创建议题,取得成员消息
 | 
	
		
			
				|  |  | 		JSONObject messages = imUtil.getCreateTopicMessage(patient, tempPatient.getName(), consult.getTitle(), consult.getSymptoms(), consult.getImages(), null);
 | 
	
		
			
				|  |  | 		users.put(patient, 0);//+ " "+(tempPatient.getSex()==1?"(男 ":"(女 ") + IdCardUtil.getAgeForIdcard(tempPatient.getIdcard())+")"
 | 
	
		
			
				|  |  | 		JSONObject obj = imUtil.createTopics(patient + "_" + ct.getTeam() + "_" + ct.getType(), consult.getId(), tempPatient.getName(), users, messages, imUtil.SESSION_TYPE_MUC);
 | 
	
		
			
				|  |  | 		if (obj == null) {
 | 
	
		
			
				|  |  | 			throw new RuntimeException("IM消息发送异常!");
 | 
	
		
			
				|  |  | 		}
 | 
	
		
			
				|  |  | 		if (obj.getInteger("status") == -1) {//im议题创建失败
 | 
	
		
			
				|  |  | 			throw new RuntimeException(obj.getString("message"));
 | 
	
		
			
				|  |  | 		}
 | 
	
		
			
				|  |  | 		ct.setStartMsgId(obj.get("start_msg_id").toString());
 | 
	
		
			
				|  |  | 		consultTeamDao.save(ct);
 | 
	
		
			
				|  |  | 		consultDao.save(consult);
 | 
	
		
			
				|  |  | 		
 | 
	
		
			
				|  |  | 		JSONArray doctors = new JSONArray();
 | 
	
		
			
				|  |  | 		for (String key : users.keySet()) {
 | 
	
		
			
				|  |  | 			if (patient.equals(key)) {
 | 
	
		
			
				|  |  | 				continue;
 | 
	
		
			
				|  |  | 			}
 | 
	
		
			
				|  |  | 			doctors.add(key);
 | 
	
		
			
				|  |  | 			//记录咨询的医生详情误删
 | 
	
		
			
				|  |  | 			ConsultTeamDoctorDo cd = new ConsultTeamDoctorDo();
 | 
	
		
			
				|  |  | 			cd.setConsult(consult.getId());
 | 
	
		
			
				|  |  | 			cd.setDel("1");
 | 
	
		
			
				|  |  | 			cd.setCzrq(new Date());
 | 
	
		
			
				|  |  | 			cd.setTo(key);
 | 
	
		
			
				|  |  | 			consultTeamDoctorDao.save(cd);
 | 
	
		
			
				|  |  | 		}
 | 
	
		
			
				|  |  | 		
 | 
	
		
			
				|  |  | 		// 保存医生咨询信息
 | 
	
		
			
				|  |  | 		// 添加咨询转发记录
 | 
	
		
			
				|  |  | 		// 添加医生咨询日志
 | 
	
		
			
				|  |  | 		String content = addLogs(ct);
 | 
	
		
			
				|  |  | 		re.put("doctor", doctor);
 | 
	
		
			
				|  |  | 		re.put("status", 1);
 | 
	
		
			
				|  |  | 		return re;
 | 
	
		
			
				|  |  | 		
 | 
	
		
			
				|  |  | 	}
 | 
	
		
			
				|  |  | 	
 | 
	
		
			
				|  |  | 	/**
 | 
	
		
			
				|  |  | 	 * 添加一条咨询记录
 | 
	
		
			
				|  |  | 	 * @param patient 患者信息
 | 
	
		
			
				|  |  | 	 * @param title 咨询标题
 | 
	
		
			
				|  |  | 	 * @param type 咨询类型:9在线复诊,13专家咨询
 | 
	
		
			
				|  |  | 	 * @return
 | 
	
		
			
				|  |  | 	 */
 | 
	
		
			
				|  |  | 	public ConsultDo addConsult(String patient, String title, String symptoms, String images, int type) {
 | 
	
		
			
				|  |  | 		ConsultDo consult = new ConsultDo();
 | 
	
		
			
				|  |  | 		consult.setId(UUID.randomUUID().toString().replaceAll("-", ""));
 | 
	
		
			
				|  |  | 		consult.setCzrq(new Date());
 | 
	
		
			
				|  |  | 		consult.setDel("1");
 | 
	
		
			
				|  |  | 		consult.setPatient(patient);
 | 
	
		
			
				|  |  | 		consult.setTitle(title);
 | 
	
		
			
				|  |  | 		consult.setSymptoms(symptoms);
 | 
	
		
			
				|  |  | 		consult.setImages(images);
 | 
	
		
			
				|  |  | 		consult.setType(type);
 | 
	
		
			
				|  |  | 		return consultDao.save(consult);
 | 
	
		
			
				|  |  | 	}
 | 
	
		
			
				|  |  | 	
 | 
	
		
			
				|  |  | 	/**
 | 
	
		
			
				|  |  | 	 * 添加咨询日志
 | 
	
		
			
				|  |  | 	 *
 | 
	
		
			
				|  |  | 	 * @param ct
 | 
	
		
			
				|  |  | 	 * @throws Exception
 | 
	
		
			
				|  |  | 	 */
 | 
	
		
			
				|  |  | 	private String addLogs(ConsultTeamDo ct) throws Exception {
 | 
	
		
			
				|  |  | 		List<ConsultTeamLogDo> logs = new ArrayList<ConsultTeamLogDo>();
 | 
	
		
			
				|  |  | 		// 添加问题咨询日志
 | 
	
		
			
				|  |  | 		String content = "";
 | 
	
		
			
				|  |  | 		
 | 
	
		
			
				|  |  | 		content += "咨询问题:" + (org.apache.commons.lang3.StringUtils.isEmpty(ct.getSymptoms()) ? "无" : ct.getSymptoms());
 | 
	
		
			
				|  |  | 		
 | 
	
		
			
				|  |  | 		// 生成提问日志,并推送相关消息
 | 
	
		
			
				|  |  | 		ConsultTeamLogDo infoLog = new ConsultTeamLogDo();
 | 
	
		
			
				|  |  | 		infoLog.setConsult(ct.getConsult());
 | 
	
		
			
				|  |  | 		if (content.length() > 2500) {
 | 
	
		
			
				|  |  | 			content = content.substring(0, 2500);
 | 
	
		
			
				|  |  | 		}
 | 
	
		
			
				|  |  | 		infoLog.setContent(content);
 | 
	
		
			
				|  |  | 		infoLog.setDel("1");
 | 
	
		
			
				|  |  | 		infoLog.setType(0);
 | 
	
		
			
				|  |  | 		infoLog.setChatType(1);
 | 
	
		
			
				|  |  | 		infoLog.setCzrq(new Date());
 | 
	
		
			
				|  |  | 		logs.add(infoLog);
 | 
	
		
			
				|  |  | 		// 图片日志
 | 
	
		
			
				|  |  | 		if (org.apache.commons.lang3.StringUtils.isNotEmpty(ct.getImages())) {
 | 
	
		
			
				|  |  | 			String[] images = ct.getImages().split(",");
 | 
	
		
			
				|  |  | 			for (String image : images) {
 | 
	
		
			
				|  |  | 				if (org.apache.commons.lang3.StringUtils.isNoneEmpty(image)) {
 | 
	
		
			
				|  |  | 					ConsultTeamLogDo imgLog = new ConsultTeamLogDo();
 | 
	
		
			
				|  |  | 					// 设置咨询标识
 | 
	
		
			
				|  |  | 					imgLog.setConsult(ct.getConsult());
 | 
	
		
			
				|  |  | 					// 设置图片URL
 | 
	
		
			
				|  |  | 					imgLog.setContent(image);
 | 
	
		
			
				|  |  | 					imgLog.setDel("1");
 | 
	
		
			
				|  |  | 					infoLog.setType(0);
 | 
	
		
			
				|  |  | 					imgLog.setCzrq(new Date());
 | 
	
		
			
				|  |  | 					// 图片类型
 | 
	
		
			
				|  |  | 					imgLog.setChatType(2);
 | 
	
		
			
				|  |  | 					// 添加到待保存队列
 | 
	
		
			
				|  |  | 					logs.add(imgLog);
 | 
	
		
			
				|  |  | 				}
 | 
	
		
			
				|  |  | 			}
 | 
	
		
			
				|  |  | 		}
 | 
	
		
			
				|  |  | 		// 语音日志
 | 
	
		
			
				|  |  | 		if (org.apache.commons.lang3.StringUtils.isNotEmpty(ct.getVoice())) {
 | 
	
		
			
				|  |  | 			ConsultTeamLogDo voiceLog = new ConsultTeamLogDo();
 | 
	
		
			
				|  |  | 			// 设置咨询标识
 | 
	
		
			
				|  |  | 			voiceLog.setConsult(ct.getConsult());
 | 
	
		
			
				|  |  | 			// 设置语音URL
 | 
	
		
			
				|  |  | 			voiceLog.setContent(ct.getVoice());
 | 
	
		
			
				|  |  | 			voiceLog.setDel("1");
 | 
	
		
			
				|  |  | 			infoLog.setType(0);
 | 
	
		
			
				|  |  | 			// 语音类型
 | 
	
		
			
				|  |  | 			voiceLog.setChatType(3);
 | 
	
		
			
				|  |  | 			voiceLog.setCzrq(new Date());
 | 
	
		
			
				|  |  | 			// 添加到待保存队列
 | 
	
		
			
				|  |  | 			logs.add(voiceLog);
 | 
	
		
			
				|  |  | 		}
 | 
	
		
			
				|  |  | 		if (!logs.isEmpty()) {
 | 
	
		
			
				|  |  | 			Iterable<ConsultTeamLogDo> iterable = consultTeamLogDao.save(logs);
 | 
	
		
			
				|  |  | 			if (iterable == null || iterable.iterator() == null || !iterable.iterator().hasNext()) {
 | 
	
		
			
				|  |  | 				// 日志保存失败
 | 
	
		
			
				|  |  | 				throw new Exception("consult team log save failed!");
 | 
	
		
			
				|  |  | 			}
 | 
	
		
			
				|  |  | 		}
 | 
	
		
			
				|  |  | 		// 患者提问或追问,给医生发消息
 | 
	
		
			
				|  |  | 		// sendMessage(ct.getDoctor(), ct.getPatient(), "三师咨询", "您有新的三师咨询消息", ct.getConsult(), 116, 1, 0, 0);
 | 
	
		
			
				|  |  | 		return content;
 | 
	
		
			
				|  |  | 	}
 | 
	
		
			
				|  |  | 
 | 
	
		
			
				|  |  | 	
 | 
	
		
			
				|  |  | }
 |