|  | @ -0,0 +1,268 @@
 | 
	
		
			
				|  |  | package com.yihu.jw.care.job.consult;
 | 
	
		
			
				|  |  | 
 | 
	
		
			
				|  |  | import com.alibaba.fastjson.JSONObject;
 | 
	
		
			
				|  |  | import com.yihu.jw.care.util.http.HttpUtils;
 | 
	
		
			
				|  |  | 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;
 | 
	
		
			
				|  |  | 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 java.util.Date;
 | 
	
		
			
				|  |  | 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;
 | 
	
		
			
				|  |  |     @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.....");
 | 
	
		
			
				|  |  |             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());
 | 
	
		
			
				|  |  |         }
 | 
	
		
			
				|  |  |     }
 | 
	
		
			
				|  |  | 
 | 
	
		
			
				|  |  |     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")));
 | 
	
		
			
				|  |  |         }
 | 
	
		
			
				|  |  | 
 | 
	
		
			
				|  |  |         //结束咨询才发送推送给IM文字消息
 | 
	
		
			
				|  |  | //        if (endType == 1) {
 | 
	
		
			
				|  |  | //            BasePatientDO p = patientDao.findOne(endOperator);
 | 
	
		
			
				|  |  | //            endName = p.getName();
 | 
	
		
			
				|  |  | //            if (consultTeam.getType() == 2 || consultTeam.getType() == 8) {
 | 
	
		
			
				|  |  | //                String openId = p.getOpenid();
 | 
	
		
			
				|  |  | //
 | 
	
		
			
				|  |  | //                WechatTemplateConfig templateConfig = templateConfigDao.findByScene("template_doctor_survey", "zxpj");
 | 
	
		
			
				|  |  | //                String first = templateConfig.getFirst();
 | 
	
		
			
				|  |  | //                first = first.replace("key1", (endName == null ? "" : endName));
 | 
	
		
			
				|  |  | //                String keyword1 = templateConfig.getKeyword1();
 | 
	
		
			
				|  |  | //
 | 
	
		
			
				|  |  | //                org.json.JSONObject json = new org.json.JSONObject();
 | 
	
		
			
				|  |  | //                SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
 | 
	
		
			
				|  |  | //                json.put("keyword1", keyword1);
 | 
	
		
			
				|  |  | //                json.put("keyword2", sdf.format(new Date()));
 | 
	
		
			
				|  |  | //                json.put("toUser", p.getCode());
 | 
	
		
			
				|  |  | //                json.put("represented", p.getCode());//被代理人
 | 
	
		
			
				|  |  | //                json.put("remark", "");
 | 
	
		
			
				|  |  | //                json.put("consult", consult);
 | 
	
		
			
				|  |  | //                if (consultTeam.getType() == 8) {
 | 
	
		
			
				|  |  | //                    first = first.replace("key2", "续方");
 | 
	
		
			
				|  |  | //                } else {
 | 
	
		
			
				|  |  | //                    first = first.replace("key2", "");
 | 
	
		
			
				|  |  | //                }
 | 
	
		
			
				|  |  | //                json.put("first", first);
 | 
	
		
			
				|  |  | //                if (StringUtils.isNotBlank(p.getOpenid())) {
 | 
	
		
			
				|  |  | //                    pushMsgTask.putWxMsg(accessTokenUtils.getAccessToken(), 17, openId, p.getName(), json);
 | 
	
		
			
				|  |  | //                } else {
 | 
	
		
			
				|  |  | //                    //发送代理人
 | 
	
		
			
				|  |  | //                    org.json.JSONArray jsonArray = weiXinOpenIdUtils.getAgentOpenId(p.getCode(), p.getOpenid());
 | 
	
		
			
				|  |  | //                    if (jsonArray != null && jsonArray.length() > 0) {
 | 
	
		
			
				|  |  | //                        for (int i = 0; i < jsonArray.length(); i++) {
 | 
	
		
			
				|  |  | //                            org.json.JSONObject j = jsonArray.getJSONObject(i);
 | 
	
		
			
				|  |  | //                            Patient member = (Patient) j.get("member");
 | 
	
		
			
				|  |  | //                            org.json.JSONObject data = json;
 | 
	
		
			
				|  |  | //                            data.remove("toUser");
 | 
	
		
			
				|  |  | //                            data.put("toUser", member.getCode());
 | 
	
		
			
				|  |  | //                            data.remove("first");
 | 
	
		
			
				|  |  | //                            data.put("first", weiXinOpenIdUtils.getTitleMes(p, j.getInt("relation"), p.getName()) + first);
 | 
	
		
			
				|  |  | //                            pushMsgTask.putWxMsg(accessTokenUtils.getAccessToken(), 17, member.getOpenid(), p.getName(), data);
 | 
	
		
			
				|  |  | //                        }
 | 
	
		
			
				|  |  | //                    }
 | 
	
		
			
				|  |  | //                }
 | 
	
		
			
				|  |  | //            }
 | 
	
		
			
				|  |  | //        } else {
 | 
	
		
			
				|  |  | //            if (consultTeam.getType() == 2 || consultTeam.getType() == 8) {
 | 
	
		
			
				|  |  | //                Patient p = patientDao.findByCode(consultTeam.getPatient());
 | 
	
		
			
				|  |  | //                String name = p.getName();
 | 
	
		
			
				|  |  | //                String openId = p.getOpenid();
 | 
	
		
			
				|  |  | //
 | 
	
		
			
				|  |  | //                WechatTemplateConfig templateConfig = templateConfigDao.findByScene("template_doctor_survey", "zxpj");
 | 
	
		
			
				|  |  | //
 | 
	
		
			
				|  |  | //                if (endOperator.equals("admin")) {
 | 
	
		
			
				|  |  | //                    templateConfig = templateConfigDao.findByScene("template_doctor_survey", "zxcspj");
 | 
	
		
			
				|  |  | //                }
 | 
	
		
			
				|  |  | //                String first = templateConfig.getFirst();
 | 
	
		
			
				|  |  | //                first = first.replace("key1", (name == null ? "" : name));
 | 
	
		
			
				|  |  | //                String keyword1 = templateConfig.getKeyword1();
 | 
	
		
			
				|  |  | //
 | 
	
		
			
				|  |  | //                org.json.JSONObject json = new org.json.JSONObject();
 | 
	
		
			
				|  |  | //                SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
 | 
	
		
			
				|  |  | //                json.put("keyword1", keyword1);
 | 
	
		
			
				|  |  | //                json.put("keyword2", sdf.format(new Date()));
 | 
	
		
			
				|  |  | //                json.put("toUser", p.getCode());
 | 
	
		
			
				|  |  | //                json.put("represented", p.getCode());//被代理人
 | 
	
		
			
				|  |  | //                json.put("remark", "");
 | 
	
		
			
				|  |  | //                json.put("consult", consult);
 | 
	
		
			
				|  |  | //
 | 
	
		
			
				|  |  | //                if (consultTeam.getType() == 8) {
 | 
	
		
			
				|  |  | //                    first += "续方";
 | 
	
		
			
				|  |  | //                    first = first.replace("key2", "续方");
 | 
	
		
			
				|  |  | //                } else {
 | 
	
		
			
				|  |  | //                    first = first.replace("key2", "");
 | 
	
		
			
				|  |  | //                }
 | 
	
		
			
				|  |  | //                /*if (endOperator.equals("admin")) {
 | 
	
		
			
				|  |  | //                    first += "咨询超时未回复,系统自动关闭,请及时对咨询医生进行评价。";
 | 
	
		
			
				|  |  | //                }else {
 | 
	
		
			
				|  |  | //                    first += "咨询已结束,请及时对咨询医生进行评价。";
 | 
	
		
			
				|  |  | //                }*/
 | 
	
		
			
				|  |  | //                json.put("first", first);
 | 
	
		
			
				|  |  | //                if (StringUtils.isNotBlank(p.getOpenid())) {
 | 
	
		
			
				|  |  | //                    pushMsgTask.putWxMsg(accessTokenUtils.getAccessToken(), 17, openId, p.getName(), json);
 | 
	
		
			
				|  |  | //                } else {
 | 
	
		
			
				|  |  | //                    //发送代理人
 | 
	
		
			
				|  |  | //                    org.json.JSONArray jsonArray = weiXinOpenIdUtils.getAgentOpenId(p.getCode(), p.getOpenid());
 | 
	
		
			
				|  |  | //                    if (jsonArray != null && jsonArray.length() > 0) {
 | 
	
		
			
				|  |  | //                        for (int i = 0; i < jsonArray.length(); i++) {
 | 
	
		
			
				|  |  | //                            org.json.JSONObject j = jsonArray.getJSONObject(i);
 | 
	
		
			
				|  |  | //                            Patient member = (Patient) j.get("member");
 | 
	
		
			
				|  |  | //                            org.json.JSONObject data = json;
 | 
	
		
			
				|  |  | //                            data.remove("toUser");
 | 
	
		
			
				|  |  | //                            data.put("toUser", member.getCode());
 | 
	
		
			
				|  |  | //                            data.remove("first");
 | 
	
		
			
				|  |  | //                            data.put("first", weiXinOpenIdUtils.getTitleMes(p, j.getInt("relation"), p.getName()) + first);
 | 
	
		
			
				|  |  | //                            pushMsgTask.putWxMsg(accessTokenUtils.getAccessToken(), 17, member.getOpenid(), p.getName(), data);
 | 
	
		
			
				|  |  | //                        }
 | 
	
		
			
				|  |  | //                    }
 | 
	
		
			
				|  |  | //                }
 | 
	
		
			
				|  |  | //
 | 
	
		
			
				|  |  | //            }
 | 
	
		
			
				|  |  | //        }
 | 
	
		
			
				|  |  | 
 | 
	
		
			
				|  |  |         consultTeam.setEndMsgId(obj.getString("id"));
 | 
	
		
			
				|  |  |         cons.setEndTime(new Date());
 | 
	
		
			
				|  |  |         consultTeam.setEndTime(new Date());
 | 
	
		
			
				|  |  |         consultTeam.setStatus(1);
 | 
	
		
			
				|  |  |         consultDao.save(cons);
 | 
	
		
			
				|  |  |         consultTeamDao.save(consultTeam);
 | 
	
		
			
				|  |  |         return 1;
 | 
	
		
			
				|  |  |     }
 | 
	
		
			
				|  |  | 
 | 
	
		
			
				|  |  | }
 |