|  | @ -3,15 +3,21 @@ 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.doctor.dao.BaseDoctorDao;
 | 
	
		
			
				|  |  | import com.yihu.jw.entity.base.doctor.BaseDoctorDO;
 | 
	
		
			
				|  |  | 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.entity.hospital.prescription.WlyyOutpatientDO;
 | 
	
		
			
				|  |  | import com.yihu.jw.hospital.prescription.dao.OutpatientDao;
 | 
	
		
			
				|  |  | 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;
 | 
	
	
		
			
				|  | @ -20,6 +26,8 @@ import com.yihu.jw.restmodel.im.ConsultVO;
 | 
	
		
			
				|  |  | import com.yihu.jw.util.common.FileUtil;
 | 
	
		
			
				|  |  | import com.yihu.jw.util.date.DateUtil;
 | 
	
		
			
				|  |  | import com.yihu.jw.wechat.service.WxAccessTokenService;
 | 
	
		
			
				|  |  | import org.apache.http.NameValuePair;
 | 
	
		
			
				|  |  | import org.apache.http.message.BasicNameValuePair;
 | 
	
		
			
				|  |  | import org.springframework.beans.factory.annotation.Autowired;
 | 
	
		
			
				|  |  | import org.springframework.beans.factory.annotation.Value;
 | 
	
		
			
				|  |  | import org.springframework.data.domain.Page;
 | 
	
	
		
			
				|  | @ -60,6 +68,7 @@ public class ImService {
 | 
	
		
			
				|  |  | 	@Autowired
 | 
	
		
			
				|  |  | 	public BasePatientDao basePatientDao;
 | 
	
		
			
				|  |  | 	
 | 
	
		
			
				|  |  | 	@Autowired
 | 
	
		
			
				|  |  | 	public ImUtil imUtil;
 | 
	
		
			
				|  |  | 	
 | 
	
		
			
				|  |  | 	public FileUtil fileUtil;
 | 
	
	
		
			
				|  | @ -91,6 +100,12 @@ public class ImService {
 | 
	
		
			
				|  |  | 	@Autowired
 | 
	
		
			
				|  |  | 	private JdbcTemplate jdbcTemplate;
 | 
	
		
			
				|  |  | 	
 | 
	
		
			
				|  |  | 	@Autowired
 | 
	
		
			
				|  |  | 	private BaseDoctorDao baseDoctorDao;
 | 
	
		
			
				|  |  | 	
 | 
	
		
			
				|  |  | 	@Autowired
 | 
	
		
			
				|  |  | 	private OutpatientDao outpatientDao;
 | 
	
		
			
				|  |  | 	
 | 
	
		
			
				|  |  | 	
 | 
	
		
			
				|  |  | 	
 | 
	
		
			
				|  |  | 	/**
 | 
	
	
		
			
				|  | @ -635,16 +650,16 @@ public class ImService {
 | 
	
		
			
				|  |  | 		// 医生未读数量为1
 | 
	
		
			
				|  |  | 		ct.setDoctorRead(1);
 | 
	
		
			
				|  |  | 		// 添加咨询记录
 | 
	
		
			
				|  |  | 		ConsultDo consult = addConsult(ct.getPatient(), null, ct.getSymptoms(), ct.getImages(), ct.getType());
 | 
	
		
			
				|  |  | 		ConsultDo consult = addConsult(ct.getPatient(), "专家咨询", 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);
 | 
	
		
			
				|  |  | 		JSONObject messages = imUtil.getCreateTopicMessage(patient, tempPatient.getName(), consult.getTitle(), consult.getSymptoms(), consult.getImages(), doctor);
 | 
	
		
			
				|  |  | 		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);
 | 
	
		
			
				|  |  | 		JSONObject obj = imUtil.createTopics(patient + "_" + doctor + "_" + ct.getType(), consult.getId(), tempPatient.getName(), users, messages, imUtil.SESSION_TYPE_MUC);
 | 
	
		
			
				|  |  | 		if (obj == null) {
 | 
	
		
			
				|  |  | 			throw new RuntimeException("IM消息发送异常!");
 | 
	
		
			
				|  |  | 		}
 | 
	
	
		
			
				|  | @ -771,6 +786,212 @@ public class ImService {
 | 
	
		
			
				|  |  | 		// sendMessage(ct.getDoctor(), ct.getPatient(), "三师咨询", "您有新的三师咨询消息", ct.getConsult(), 116, 1, 0, 0);
 | 
	
		
			
				|  |  | 		return content;
 | 
	
		
			
				|  |  | 	}
 | 
	
		
			
				|  |  | 
 | 
	
		
			
				|  |  | 	
 | 
	
		
			
				|  |  | 	/**
 | 
	
		
			
				|  |  | 	 * 添加续方咨询
 | 
	
		
			
				|  |  | 	 *
 | 
	
		
			
				|  |  | 	 * @param outpatientCode
 | 
	
		
			
				|  |  | 	 * @param patient
 | 
	
		
			
				|  |  | 	 * @param agent
 | 
	
		
			
				|  |  | 	 * @param doctorCode
 | 
	
		
			
				|  |  | 	 * @param reason
 | 
	
		
			
				|  |  | 	 * @return
 | 
	
		
			
				|  |  | 	 */
 | 
	
		
			
				|  |  | 	public Integer addPrescriptionConsult(String outpatientCode, String patient, String agent, String doctorCode, ConsultTeamDo ct, String reason, Integer type) throws Exception {
 | 
	
		
			
				|  |  | 		
 | 
	
		
			
				|  |  | 		synchronized (outpatientCode.intern()) {
 | 
	
		
			
				|  |  | 			/**
 | 
	
		
			
				|  |  | 			 * toto 判断是否存在未审核的复诊
 | 
	
		
			
				|  |  | 			 */
 | 
	
		
			
				|  |  | //			String check = prescriptionInfoService.presCheckState(jwCode);
 | 
	
		
			
				|  |  | //			if ("0".equals(check)) {//存在未审核的续方
 | 
	
		
			
				|  |  | //				return -1;
 | 
	
		
			
				|  |  | //			}
 | 
	
		
			
				|  |  | 			
 | 
	
		
			
				|  |  | 			
 | 
	
		
			
				|  |  | 			BaseDoctorDO doctor = baseDoctorDao.findById(doctorCode);
 | 
	
		
			
				|  |  | //			doctorCode = doctor.getCode();
 | 
	
		
			
				|  |  | 			// 查询患者信息
 | 
	
		
			
				|  |  | 			BasePatientDO p = basePatientDao.findById(patient);
 | 
	
		
			
				|  |  | 			
 | 
	
		
			
				|  |  | 			//1、获取his就诊记录详情(保存续方表、药品续方信息表、续方疾病类型表)
 | 
	
		
			
				|  |  | 			WlyyOutpatientDO wlyyOutpatientDO = outpatientDao.findOne(outpatientCode);
 | 
	
		
			
				|  |  | 			
 | 
	
		
			
				|  |  | 			JSONObject jsonObject = new JSONObject();
 | 
	
		
			
				|  |  | 			jsonObject.put("title", p.getName() + "申请复诊");
 | 
	
		
			
				|  |  | 			
 | 
	
		
			
				|  |  | 			
 | 
	
		
			
				|  |  | 			String content = jsonObject.toString();
 | 
	
		
			
				|  |  | 			
 | 
	
		
			
				|  |  | 			//4、创建咨询
 | 
	
		
			
				|  |  | 			JSONObject users = new JSONObject();//咨询参与者
 | 
	
		
			
				|  |  | 			users.put(patient, 0);
 | 
	
		
			
				|  |  | 			users.put(doctorCode, 0);
 | 
	
		
			
				|  |  | 			//关联业务code
 | 
	
		
			
				|  |  | 			ct.setRelationCode(outpatientCode);
 | 
	
		
			
				|  |  | 			//医生信息
 | 
	
		
			
				|  |  | 			ct.setDoctor(doctorCode);
 | 
	
		
			
				|  |  | 			ct.setDoctorName(doctor.getName());
 | 
	
		
			
				|  |  | 			// 设置患者信息
 | 
	
		
			
				|  |  | 			ct.setPatient(patient);
 | 
	
		
			
				|  |  | //			if (1 == type) {
 | 
	
		
			
				|  |  | //				ct.setSymptoms("高血压");
 | 
	
		
			
				|  |  | //			} else if (2 == type) {
 | 
	
		
			
				|  |  | //				ct.setSymptoms("糖尿病");
 | 
	
		
			
				|  |  | //			} else if (3 == type) {
 | 
	
		
			
				|  |  | //				ct.setSymptoms("高血压,糖尿病");
 | 
	
		
			
				|  |  | //			} else {
 | 
	
		
			
				|  |  | //			}
 | 
	
		
			
				|  |  | 			
 | 
	
		
			
				|  |  | 			// 设置患者姓名
 | 
	
		
			
				|  |  | 			ct.setName(p.getName());
 | 
	
		
			
				|  |  | 			// 设置患者生日
 | 
	
		
			
				|  |  | 			ct.setBirthday(p.getBirthday());
 | 
	
		
			
				|  |  | 			//新增性别
 | 
	
		
			
				|  |  | 			ct.setSex(p.getSex());
 | 
	
		
			
				|  |  | 			// 设置患者头像
 | 
	
		
			
				|  |  | 			ct.setPhoto(p.getPhoto());
 | 
	
		
			
				|  |  | 			// 设置操作日期
 | 
	
		
			
				|  |  | 			ct.setCzrq(new Date());
 | 
	
		
			
				|  |  | 			ct.setDel("1");
 | 
	
		
			
				|  |  | 			ct.setStatus(0);
 | 
	
		
			
				|  |  | 			ct.setEvaluate(0);
 | 
	
		
			
				|  |  | 			// 医生未读数量为1
 | 
	
		
			
				|  |  | 			ct.setDoctorRead(1);
 | 
	
		
			
				|  |  | 			// 添加咨询记录
 | 
	
		
			
				|  |  | 			ConsultDo consult = addConsult(ct.getPatient(), "申请复诊", ct.getSymptoms(), ct.getImages(), ct.getType());
 | 
	
		
			
				|  |  | 			// 设置关联指导
 | 
	
		
			
				|  |  | 			consult.setGuidance(ct.getGuidance());
 | 
	
		
			
				|  |  | 			//关联业务code
 | 
	
		
			
				|  |  | 			consult.setRelationCode(outpatientCode);
 | 
	
		
			
				|  |  | 			// 设置咨询标识
 | 
	
		
			
				|  |  | 			ct.setConsult(consult.getId());
 | 
	
		
			
				|  |  | 			
 | 
	
		
			
				|  |  | 			//推送给IM去创建议题,取得成员消息
 | 
	
		
			
				|  |  | 			JSONObject messages = imUtil.getCreateTopicMessage(patient, p.getName(), consult.getTitle(), content, consult.getImages(), agent);
 | 
	
		
			
				|  |  | 			
 | 
	
		
			
				|  |  | 			//5、(im创建咨询) 续方咨询的sessionid为居民code+就诊code+咨询类型(图文,视频,图文+视频)
 | 
	
		
			
				|  |  | 			String sessionId = patient + "_" + outpatientCode + "_" + ct.getType();
 | 
	
		
			
				|  |  | 			JSONObject obj = imUtil.createTopics(sessionId, consult.getId(), p.getName(), users, messages, imUtil.SESSION_TYPE_EXAMINATION);
 | 
	
		
			
				|  |  | 			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);
 | 
	
		
			
				|  |  | 			
 | 
	
		
			
				|  |  | 			//设置咨询code
 | 
	
		
			
				|  |  | //			wlyyOutpatientDO.setConsult(consult.getCode());
 | 
	
		
			
				|  |  | //			prescriptionDao.save(prescription);
 | 
	
		
			
				|  |  | 			
 | 
	
		
			
				|  |  | 			//6、记录咨询的医生详情
 | 
	
		
			
				|  |  | 			ConsultTeamDoctorDo cd = new ConsultTeamDoctorDo();
 | 
	
		
			
				|  |  | 			cd.setConsult(consult.getId());
 | 
	
		
			
				|  |  | 			cd.setDel("1");
 | 
	
		
			
				|  |  | 			cd.setCzrq(new Date());
 | 
	
		
			
				|  |  | 			cd.setTo(doctorCode);
 | 
	
		
			
				|  |  | 			consultTeamDoctorDao.save(cd);
 | 
	
		
			
				|  |  | 			
 | 
	
		
			
				|  |  | 			//7、发送系统消息提示团队长有未审核的消息
 | 
	
		
			
				|  |  | //			addCheckMessage(prescription, sessionId, p);
 | 
	
		
			
				|  |  | 			
 | 
	
		
			
				|  |  | 			//8、 保存医生咨询信息
 | 
	
		
			
				|  |  | 			// 添加咨询转发记录
 | 
	
		
			
				|  |  | 			// 添加医生咨询日志
 | 
	
		
			
				|  |  | 			addLogs(ct);
 | 
	
		
			
				|  |  | 			
 | 
	
		
			
				|  |  | 			return 1;
 | 
	
		
			
				|  |  | 		}
 | 
	
		
			
				|  |  | 	}
 | 
	
		
			
				|  |  | 	
 | 
	
		
			
				|  |  | 	
 | 
	
		
			
				|  |  | 	/**
 | 
	
		
			
				|  |  | 	 * 居民结束咨询
 | 
	
		
			
				|  |  | 	 * @param consult 咨询CODE
 | 
	
		
			
				|  |  | 	 * @param endOperator 操作人
 | 
	
		
			
				|  |  | 	 * @param endType 1居民 2医生
 | 
	
		
			
				|  |  | 	 * @return
 | 
	
		
			
				|  |  | 	 */
 | 
	
		
			
				|  |  | 	public int finish(String consult, String endOperator, int endType) {
 | 
	
		
			
				|  |  | 		
 | 
	
		
			
				|  |  | 		ConsultTeamDo consultTeam = consultTeamDao.findByConsult(consult);
 | 
	
		
			
				|  |  | 		
 | 
	
		
			
				|  |  | 		ConsultDo cons = consultDao.findOne(consult);
 | 
	
		
			
				|  |  | 		
 | 
	
		
			
				|  |  | 		if (consultTeam.getStatus() == 1) {
 | 
	
		
			
				|  |  | 			return -1;
 | 
	
		
			
				|  |  | 		}
 | 
	
		
			
				|  |  | 		
 | 
	
		
			
				|  |  | 		String endName = "";
 | 
	
		
			
				|  |  | 		String endId = "";
 | 
	
		
			
				|  |  | 		//结束咨询才发送推送给IM文字消息
 | 
	
		
			
				|  |  | 		if (endType == 1) {
 | 
	
		
			
				|  |  | 			BasePatientDO p = basePatientDao.findById(endOperator);
 | 
	
		
			
				|  |  | 			endName = p.getName();
 | 
	
		
			
				|  |  | 			endId = p.getId();
 | 
	
		
			
				|  |  | 		} else {
 | 
	
		
			
				|  |  | 			if (endOperator.equals("admin")) {
 | 
	
		
			
				|  |  | 				endId = "system";
 | 
	
		
			
				|  |  | 				endName = "咨询超时未回复,系统自动";
 | 
	
		
			
				|  |  | 			} else {
 | 
	
		
			
				|  |  | 				BaseDoctorDO d = baseDoctorDao.findById(endOperator);
 | 
	
		
			
				|  |  | 				endId = d.getId();
 | 
	
		
			
				|  |  | 				endName = d.getName();
 | 
	
		
			
				|  |  | 			}
 | 
	
		
			
				|  |  | 		}
 | 
	
		
			
				|  |  | 		
 | 
	
		
			
				|  |  | 		JSONObject obj = imUtil.endTopics(consultTeam.getPatient(), endId, endName, consultTeam.getConsult());
 | 
	
		
			
				|  |  | 		if (obj == null) {
 | 
	
		
			
				|  |  | 			throw new RuntimeException("IM消息结束异常!");
 | 
	
		
			
				|  |  | 		}
 | 
	
		
			
				|  |  | 		if (obj.getInteger("status") == -1) {
 | 
	
		
			
				|  |  | 			throw new RuntimeException(String.valueOf(obj.get("message")));
 | 
	
		
			
				|  |  | 		}
 | 
	
		
			
				|  |  | 
 | 
	
		
			
				|  |  | 		
 | 
	
		
			
				|  |  | 		String name = "";
 | 
	
		
			
				|  |  | 		String returnJson = "";
 | 
	
		
			
				|  |  | 		//推送给IM文字消息
 | 
	
		
			
				|  |  | //		if (endType == 1) {
 | 
	
		
			
				|  |  | //			BasePatientDO p = basePatientDao.findById(endOperator);
 | 
	
		
			
				|  |  | //			name = p.getName();
 | 
	
		
			
				|  |  | //			returnJson = imUtil.sendIM(consultTeam.getPatient(), consultTeam.getDoctor(), "7", name + "结束了本次咨询");
 | 
	
		
			
				|  |  | //		} else {
 | 
	
		
			
				|  |  | //			BaseDoctorDO d = baseDoctorDao.findById(endOperator);
 | 
	
		
			
				|  |  | //			name = d.getName();
 | 
	
		
			
				|  |  | //			returnJson = imUtil.sendIM(consultTeam.getDoctor(), consultTeam.getPatient(), "7", name + "结束了本次咨询");
 | 
	
		
			
				|  |  | //		}
 | 
	
		
			
				|  |  | 		
 | 
	
		
			
				|  |  | //		if (org.apache.commons.lang3.StringUtils.isEmpty(returnJson)) {
 | 
	
		
			
				|  |  | //			throw new RuntimeException("send consult finished IM message failed!");
 | 
	
		
			
				|  |  | ////			throw new Exception("send consult finished IM message failed");
 | 
	
		
			
				|  |  | //		} else {
 | 
	
		
			
				|  |  | //			JSONObject jo = JSON.parseObject(returnJson);            //设置消息ID
 | 
	
		
			
				|  |  | //			consultTeam.setEndMsgId(jo.getInteger("startId") + "");
 | 
	
		
			
				|  |  | //			consultTeam.setEndOperator(endOperator);
 | 
	
		
			
				|  |  | //			consultTeam.setEndType(endType);
 | 
	
		
			
				|  |  | //		}
 | 
	
		
			
				|  |  | 		
 | 
	
		
			
				|  |  | //		org.json.JSONObject group = talkGroupService.findConsultTalkGroup(consultTeam.getConsult());
 | 
	
		
			
				|  |  | //
 | 
	
		
			
				|  |  | //		if (group != null) {
 | 
	
		
			
				|  |  | //			String json = sendGroupIM(endOperator, group.getString("code"), "7", name + "结束了本次咨询");
 | 
	
		
			
				|  |  | //
 | 
	
		
			
				|  |  | //			if (org.apache.commons.lang3.StringUtils.isEmpty(json)) {
 | 
	
		
			
				|  |  | //				throw new Exception("send consult finished IM message failed");
 | 
	
		
			
				|  |  | //			}
 | 
	
		
			
				|  |  | //		}
 | 
	
		
			
				|  |  | 		
 | 
	
		
			
				|  |  | 		consultTeam.setEndMsgId(obj.getString("id"));
 | 
	
		
			
				|  |  | 		cons.setEndTime(new Date());
 | 
	
		
			
				|  |  | 		consultTeam.setEndTime(new Date());
 | 
	
		
			
				|  |  | 		consultTeam.setStatus(1);
 | 
	
		
			
				|  |  | 		consultDao.save(cons);
 | 
	
		
			
				|  |  | 		consultTeamDao.save(consultTeam);
 | 
	
		
			
				|  |  | 		return 1;
 | 
	
		
			
				|  |  | 	}
 | 
	
		
			
				|  |  | 
 | 
	
		
			
				|  |  | }
 |