|  | @ -2,7 +2,15 @@ package com.yihu.jw.care.job.consult;
 | 
	
		
			
				|  |  | 
 | 
	
		
			
				|  |  | import com.alibaba.fastjson.JSONObject;
 | 
	
		
			
				|  |  | import com.yihu.jw.care.util.http.HttpUtils;
 | 
	
		
			
				|  |  | import com.yihu.jw.util.http.HttpClientUtil;
 | 
	
		
			
				|  |  | 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.patient.BasePatientDO;
 | 
	
		
			
				|  |  | import com.yihu.jw.im.dao.ConsultDao;
 | 
	
		
			
				|  |  | import com.yihu.jw.im.dao.ConsultTeamDao;
 | 
	
		
			
				|  |  | import com.yihu.jw.im.util.ImUtil;
 | 
	
		
			
				|  |  | import com.yihu.jw.patient.dao.BasePatientDao;
 | 
	
		
			
				|  |  | import org.quartz.Job;
 | 
	
		
			
				|  |  | import org.quartz.JobExecutionContext;
 | 
	
		
			
				|  |  | import org.quartz.JobExecutionException;
 | 
	
	
		
			
				|  | @ -11,8 +19,8 @@ import org.slf4j.LoggerFactory;
 | 
	
		
			
				|  |  | import org.springframework.beans.factory.annotation.Autowired;
 | 
	
		
			
				|  |  | import org.springframework.beans.factory.annotation.Value;
 | 
	
		
			
				|  |  | import org.springframework.jdbc.core.JdbcTemplate;
 | 
	
		
			
				|  |  | import org.springframework.web.context.support.SpringBeanAutowiringSupport;
 | 
	
		
			
				|  |  | 
 | 
	
		
			
				|  |  | import java.util.Date;
 | 
	
		
			
				|  |  | import java.util.List;
 | 
	
		
			
				|  |  | import java.util.Map;
 | 
	
		
			
				|  |  | 
 | 
	
	
		
			
				|  | @ -31,13 +39,22 @@ public class FinishConsultJob implements Job {
 | 
	
		
			
				|  |  |     private JdbcTemplate jdbcTemplate;
 | 
	
		
			
				|  |  |     @Autowired
 | 
	
		
			
				|  |  |     private HttpUtils httpUtil;
 | 
	
		
			
				|  |  |     @Autowired
 | 
	
		
			
				|  |  |     private ImUtil imUtil;
 | 
	
		
			
				|  |  |     @Autowired
 | 
	
		
			
				|  |  |     private ConsultTeamDao consultTeamDao;
 | 
	
		
			
				|  |  |     @Autowired
 | 
	
		
			
				|  |  |     private BasePatientDao patientDao;
 | 
	
		
			
				|  |  |     @Autowired
 | 
	
		
			
				|  |  |     private BaseDoctorDao doctorDao;
 | 
	
		
			
				|  |  |     @Autowired
 | 
	
		
			
				|  |  |     private ConsultDao consultDao;
 | 
	
		
			
				|  |  | 
 | 
	
		
			
				|  |  |     @Override
 | 
	
		
			
				|  |  |     public void execute(JobExecutionContext jobExecutionContext) throws JobExecutionException {
 | 
	
		
			
				|  |  |         try{
 | 
	
		
			
				|  |  |             //在线咨询
 | 
	
		
			
				|  |  |             logger.info("FinishConsultJob start.....");
 | 
	
		
			
				|  |  |             SpringBeanAutowiringSupport.processInjectionBasedOnCurrentContext(this);
 | 
	
		
			
				|  |  |             StringBuffer sql = new StringBuffer();
 | 
	
		
			
				|  |  |             sql.append("SELECT s.id session_id, s.name session_name, s.create_date session_create_time, s.last_message_time, ")
 | 
	
		
			
				|  |  |             .append("t.id topic_id, t.name topic_name, t.create_time topic_create_time, t.start_message_id ")
 | 
	
	
		
			
				|  | @ -46,18 +63,19 @@ public class FinishConsultJob implements Job {
 | 
	
		
			
				|  |  |             .append("SELECT id FROM ").append(imDb).append(".doctors d where d.id<>t.patient) ")
 | 
	
		
			
				|  |  |             .append("AND UNIX_TIMESTAMP(now()) - UNIX_TIMESTAMP(s.last_message_time) > 24*3600 ")
 | 
	
		
			
				|  |  |             .append("ORDER BY t.create_time");
 | 
	
		
			
				|  |  |             JSONObject userAgent = new JSONObject();
 | 
	
		
			
				|  |  |             userAgent.put("uid","admin");
 | 
	
		
			
				|  |  |             userAgent.put("admin_token","0a5c5258-8863-4b07-a3f9-88c768528ab4");
 | 
	
		
			
				|  |  |             userAgent.put("token","");
 | 
	
		
			
				|  |  |             userAgent.put("imei","admin_imei");
 | 
	
		
			
				|  |  | 
 | 
	
		
			
				|  |  |             List<Map<String,Object>> list = jdbcTemplate.queryForList(sql.toString());
 | 
	
		
			
				|  |  |             list.stream().forEach(one->{
 | 
	
		
			
				|  |  | 
 | 
	
		
			
				|  |  |                 String topicId = one.get("topic_id").toString();
 | 
	
		
			
				|  |  |                 String topicName = one.get("topic_name").toString();
 | 
	
		
			
				|  |  |                 logger.info("Got topic to terminate, " + topicId + ": " + topicName + ", last active time: " + one.get("last_message_time"));
 | 
	
		
			
				|  |  |                 String response = httpUtil.sendLoginGet(server_url+"/doctor/consult/finish_consult","consult="+topicId,userAgent.toString());
 | 
	
		
			
				|  |  |                 logger.info("FinishConsultJob response....."+response);
 | 
	
		
			
				|  |  |                 try {
 | 
	
		
			
				|  |  |                     logger.info("Got topic to terminate, " + topicId + ": " + topicName + ", last active time: " + one.get("last_message_time"));
 | 
	
		
			
				|  |  |                    Integer response =  finishConsult(topicId,"admin",2);
 | 
	
		
			
				|  |  |                     logger.info("FinishConsultJob response....."+response);
 | 
	
		
			
				|  |  |                 } catch (Exception e) {
 | 
	
		
			
				|  |  |                     e.printStackTrace();
 | 
	
		
			
				|  |  |                 }
 | 
	
		
			
				|  |  |             });
 | 
	
		
			
				|  |  | 
 | 
	
		
			
				|  |  |             logger.info("FinishConsultJob 处理医生回复后,居民进入了咨询但是没有回复的 咨询也要自动结束.....");
 | 
	
	
		
			
				|  | @ -81,9 +99,13 @@ public class FinishConsultJob implements Job {
 | 
	
		
			
				|  |  | 
 | 
	
		
			
				|  |  |                 List<Map<String,Object>> temp = jdbcTemplate.queryForList(sql2.toString(),sessionId);
 | 
	
		
			
				|  |  |                 if(temp!=null&&temp.size()>=0){
 | 
	
		
			
				|  |  |                     logger.info("Got topic to terminate, " + topicId + ": " + topicName + ", last active time: " + one.get("last_message_time"));
 | 
	
		
			
				|  |  |                     String response = httpUtil.sendLoginGet(server_url+"/doctor/consult/finish_consult","consult="+topicId,userAgent.toString());
 | 
	
		
			
				|  |  |                     logger.info("FinishConsultJob response....."+response);
 | 
	
		
			
				|  |  |                     try {
 | 
	
		
			
				|  |  |                         logger.info("Got topic to terminate, " + topicId + ": " + topicName + ", last active time: " + one.get("last_message_time"));
 | 
	
		
			
				|  |  |                         Integer response =  finishConsult(topicId,"admin",2);
 | 
	
		
			
				|  |  |                         logger.info("FinishConsultJob response....."+response);
 | 
	
		
			
				|  |  |                     } catch (Exception e) {
 | 
	
		
			
				|  |  |                         e.printStackTrace();
 | 
	
		
			
				|  |  |                     }
 | 
	
		
			
				|  |  |                 }
 | 
	
		
			
				|  |  |             });
 | 
	
		
			
				|  |  | 
 | 
	
	
		
			
				|  | @ -93,4 +115,55 @@ public class FinishConsultJob implements Job {
 | 
	
		
			
				|  |  |         }
 | 
	
		
			
				|  |  |     }
 | 
	
		
			
				|  |  | 
 | 
	
		
			
				|  |  |     public int finishConsult(String consult, String endOperator, int endType) throws Exception {
 | 
	
		
			
				|  |  | 
 | 
	
		
			
				|  |  |         ConsultTeamDo consultTeam = consultTeamDao.findByConsult(consult);
 | 
	
		
			
				|  |  |         ConsultDo cons = consultDao.findOne(consult);
 | 
	
		
			
				|  |  | 
 | 
	
		
			
				|  |  |         if (consultTeam.getStatus() == 1) {
 | 
	
		
			
				|  |  |             return -1;
 | 
	
		
			
				|  |  |         }
 | 
	
		
			
				|  |  | 
 | 
	
		
			
				|  |  | //        //新增续方咨询结束判断 (续方未审核时不可由医生或居民关闭)
 | 
	
		
			
				|  |  | //        if (consultTeam.getType() == 8) {
 | 
	
		
			
				|  |  | //            Prescription prescription = prescriptionDao.findByCode(consultTeam.getRelationCode());
 | 
	
		
			
				|  |  | //            if (prescription.getStatus() == 0) {
 | 
	
		
			
				|  |  | //                return -2;
 | 
	
		
			
				|  |  | //            }
 | 
	
		
			
				|  |  | //        }
 | 
	
		
			
				|  |  | 
 | 
	
		
			
				|  |  |         String endName = "";
 | 
	
		
			
				|  |  |         String endId = "";
 | 
	
		
			
				|  |  |         //结束咨询才发送推送给IM文字消息
 | 
	
		
			
				|  |  |         if (endType == 1) {
 | 
	
		
			
				|  |  |             BasePatientDO p = patientDao.findById(endOperator);
 | 
	
		
			
				|  |  |             endName = p.getName();
 | 
	
		
			
				|  |  |             endId = p.getId();
 | 
	
		
			
				|  |  |         } else {
 | 
	
		
			
				|  |  |             if (endOperator.equals("admin")) {
 | 
	
		
			
				|  |  |                 endId = "system";
 | 
	
		
			
				|  |  |                 endName = "咨询超时未回复,系统自动";
 | 
	
		
			
				|  |  |             } else {
 | 
	
		
			
				|  |  |                 BaseDoctorDO d = doctorDao.findOne(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")));
 | 
	
		
			
				|  |  |         }
 | 
	
		
			
				|  |  |         consultTeam.setEndMsgId(obj.getString("id"));
 | 
	
		
			
				|  |  |         cons.setEndTime(new Date());
 | 
	
		
			
				|  |  |         consultTeam.setEndTime(new Date());
 | 
	
		
			
				|  |  |         consultTeam.setStatus(1);
 | 
	
		
			
				|  |  |         consultDao.save(cons);
 | 
	
		
			
				|  |  |         consultTeamDao.save(consultTeam);
 | 
	
		
			
				|  |  |         return 1;
 | 
	
		
			
				|  |  |     }
 | 
	
		
			
				|  |  | 
 | 
	
		
			
				|  |  | }
 |