| 
					
				 | 
			
			
				@ -0,0 +1,96 @@ 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				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 org.quartz.Job; 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				import org.quartz.JobExecutionContext; 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				import org.quartz.JobExecutionException; 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				import org.slf4j.Logger; 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				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.List; 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				import java.util.Map; 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				/** 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				 * 居民24小时未回复的咨询自动关闭 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				 * Created by yeshijie on 2017/9/10. 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				 */ 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				public class FinishConsultJob implements Job { 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				    private static Logger logger = LoggerFactory.getLogger(FinishConsultJob.class); 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				    @Value("${im.data_base_name}") 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				    private String imDb; 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				    @Value("${cloudServer.server_url}") 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				    private String server_url; 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				    @Autowired 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				    private JdbcTemplate jdbcTemplate; 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				    @Autowired 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				    private HttpUtils httpUtil; 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				    @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 ") 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				            .append("FROM ").append(imDb).append(".sessions s,").append(imDb).append(".wlyy_consults t ") 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				            .append("WHERE s.id = t.session_id AND s.type=23  AND t.end_message_id IS NULL AND s.last_content_type in(1,2,3,4,15,16,17) and s.last_sender_id IN ( ") 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				            .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); 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				            }); 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				            logger.info("FinishConsultJob 处理医生回复后,居民进入了咨询但是没有回复的 咨询也要自动结束....."); 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				            sql = new StringBuffer(); 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				            sql.append("SELECT s.id session_id,t.id topic_id, t.name topic_name ") 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				             .append("FROM ").append(imDb).append(".sessions s,").append(imDb).append(".wlyy_consults t ") 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				             .append("WHERE s.id = t.session_id AND s.type=23 AND t.end_message_id IS NULL AND t.reply=1 AND s.last_content_type =14 ") 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				             .append("AND UNIX_TIMESTAMP(now()) - UNIX_TIMESTAMP(s.last_message_time) > 24*3600  "); 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				            StringBuffer sql2 = new StringBuffer(); 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				            sql2.append("SELECT a.id from ( ") 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				             .append(" SELECT * from ").append(imDb).append(".muc_messages WHERE session_id=? ORDER BY `timestamp` desc LIMIT 1,1 ") 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				             .append(") a, base_doctor d WHERE a.sender_id=d.id "); 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				            //查找超过24小时,且最后一条消息是居民进入系统的会话id 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				            List<Map<String,Object>> sessionIdList = jdbcTemplate.queryForList(sql.toString()); 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				            sessionIdList.stream().forEach(one->{ 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				                String sessionId = one.get("session_id").toString(); 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				                String topicId = one.get("topic_id").toString(); 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				                String topicName = one.get("topic_name").toString(); 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				                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); 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				                } 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				            }); 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				            logger.info("FinishConsultJob end....."); 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				        }catch (Exception e){ 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				            logger.error("FinishConsultJob error....."+e.getMessage()); 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				        } 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				    } 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				} 
			 |