|  | @ -1,18 +1,34 @@
 | 
	
		
			
				|  |  | package com.yihu.jw.service.channel;
 | 
	
		
			
				|  |  | 
 | 
	
		
			
				|  |  | 
 | 
	
		
			
				|  |  | import com.fasterxml.jackson.databind.ObjectMapper;
 | 
	
		
			
				|  |  | import com.yihu.jw.restmodel.hospital.prescription.WlyyPrescriptionVO;
 | 
	
		
			
				|  |  | import com.yihu.jw.restmodel.web.ListEnvelop;
 | 
	
		
			
				|  |  | import com.yihu.jw.restmodel.web.status.EnvelopStatus;
 | 
	
		
			
				|  |  | import com.yihu.jw.util.http.HttpUtils;
 | 
	
		
			
				|  |  | import io.swagger.annotations.ApiParam;
 | 
	
		
			
				|  |  | 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.stereotype.Component;
 | 
	
		
			
				|  |  | import org.springframework.transaction.annotation.Transactional;
 | 
	
		
			
				|  |  | import org.springframework.web.bind.annotation.RequestParam;
 | 
	
		
			
				|  |  | 
 | 
	
		
			
				|  |  | import java.text.SimpleDateFormat;
 | 
	
		
			
				|  |  | import java.util.HashMap;
 | 
	
		
			
				|  |  | import java.util.List;
 | 
	
		
			
				|  |  | import java.util.Map;
 | 
	
		
			
				|  |  | 
 | 
	
		
			
				|  |  | /**
 | 
	
		
			
				|  |  |  * 处方状态变更
 | 
	
		
			
				|  |  |  * 处方状态更新job
 | 
	
		
			
				|  |  |  * Created by zdm on 2019/7/20.
 | 
	
		
			
				|  |  |  * 互联网医院线上下处方之后保存到中山医院的临时表-(中山医院正式下处方,审核成功之后保存,审核失败不保存)-审核成功之后药房发药,回调互联网医院。
 | 
	
		
			
				|  |  |  * 在正式下处方的时候,互联网医院每两分钟到中山医院获取处方详情:
 | 
	
		
			
				|  |  |  * 1、若是可以正常查询且状态为审方成功,则只需要变更状态,调用im消息通知居民
 | 
	
		
			
				|  |  |  * 2、若可以正常查询处方,状态为失败,则需要将状态改为失败,并根据处方号重新获取新的处方直到成功为止。
 | 
	
		
			
				|  |  |  * 3、若根据处方号查询不到处方,则需要根据门诊就诊记录(有就诊次数的数据)获取处方号,再获取处方状态重复1、2
 | 
	
		
			
				|  |  |  */
 | 
	
		
			
				|  |  | @Component
 | 
	
		
			
				|  |  | @Transactional
 | 
	
	
		
			
				|  | @ -20,49 +36,92 @@ public class PrescriptionStatusUpdateService {
 | 
	
		
			
				|  |  |     private static Logger logger = LoggerFactory.getLogger(PrescriptionStatusUpdateService.class);
 | 
	
		
			
				|  |  |     @Autowired
 | 
	
		
			
				|  |  |     private JdbcTemplate jdbcTemplate;
 | 
	
		
			
				|  |  |     @Value("${hlwyyEntrance.url}")
 | 
	
		
			
				|  |  |     private String hlwyyEntranceUrl;
 | 
	
		
			
				|  |  | 
 | 
	
		
			
				|  |  |     public void autoPush() throws Exception{
 | 
	
		
			
				|  |  |         logger.info("job执行中++++++++++++++++++++++++");
 | 
	
		
			
				|  |  |         //获取所有未结束处方
 | 
	
		
			
				|  |  |         String sql = "SELECT a.id ,a.start_time,a.end_time FROM wlyy.wlyy_channel_recommend_article a WHERE a.del_flag>0 AND a.type=2";
 | 
	
		
			
				|  |  |         //获取所有就诊中已挂号、已下临时处方、并且未结束的处方
 | 
	
		
			
				|  |  |         String sql = "SELECT o.id,o.con_no,o.register_no,p.mapping_code,pre.adm_no,pre.real_order,pre.id preId,pre.dispensary_type,pre.status FROM base.wlyy_outpatient o LEFT JOIN base.wlyy_patient_mapping p ON o.patient=p.patient " +
 | 
	
		
			
				|  |  |                 "LEFT JOIN base.wlyy_prescription pre on o.id=pre.outpatient_id WHERE o.status=1 AND o.con_no IS NOT NULL  AND pre.real_order IS NOT NULL  ";
 | 
	
		
			
				|  |  |         List<Map<String, Object>> list = jdbcTemplate.queryForList(sql);
 | 
	
		
			
				|  |  |         for (Map<String, Object> map : list) {
 | 
	
		
			
				|  |  |             //处方号
 | 
	
		
			
				|  |  |             String realOrder=String.valueOf(map.get("real_order"));
 | 
	
		
			
				|  |  |             //门诊就诊唯一号
 | 
	
		
			
				|  |  |             String admNo=String.valueOf(map.get("adm_no"));
 | 
	
		
			
				|  |  |             //第三方居民唯一号
 | 
	
		
			
				|  |  |             String patientMappingCode=String.valueOf(map.get("mapping_code"));
 | 
	
		
			
				|  |  |             //中山医院居民就诊次数
 | 
	
		
			
				|  |  |             String conNo=String.valueOf(map.get("con_no"));
 | 
	
		
			
				|  |  |             //处方原状态
 | 
	
		
			
				|  |  |             String status=String.valueOf(map.get("status"));
 | 
	
		
			
				|  |  | 
 | 
	
		
			
				|  |  | //        SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
 | 
	
		
			
				|  |  | //        //获取当前时间
 | 
	
		
			
				|  |  | //        String dateString = format.format(new Date());
 | 
	
		
			
				|  |  | //        Date now = DateUtil.strToDate(dateString,"yyyy-MM-dd HH:mm:ss");
 | 
	
		
			
				|  |  | //        //获取自动推送的时间列表
 | 
	
		
			
				|  |  | //        String sql = "SELECT a.id ,a.start_time,a.end_time FROM wlyy.wlyy_channel_recommend_article a WHERE a.del_flag>0 AND a.type=2";
 | 
	
		
			
				|  |  | //        List<Map<String, Object>> list = jdbcTemplate.queryForList(sql);
 | 
	
		
			
				|  |  | //        for (Map<String, Object> map : list) {
 | 
	
		
			
				|  |  | //            String id=String.valueOf(map.get("id"));
 | 
	
		
			
				|  |  | //            if(map.get("start_time") !=null && map.get("end_time")!=null) {
 | 
	
		
			
				|  |  | //                //开始时间
 | 
	
		
			
				|  |  | //                Date startTime = DateUtil.strToDate(map.get("start_time") + "", "yyyy-MM-dd HH:mm:ss");
 | 
	
		
			
				|  |  | //                //结束时间
 | 
	
		
			
				|  |  | //                Date endTime = DateUtil.strToDate(map.get("end_time") + "", "yyyy-MM-dd HH:mm:ss");
 | 
	
		
			
				|  |  | //
 | 
	
		
			
				|  |  | //                int startEqual = now.compareTo(startTime);
 | 
	
		
			
				|  |  | //                int endEqual = now.compareTo(endTime);
 | 
	
		
			
				|  |  | //
 | 
	
		
			
				|  |  | //                if (endEqual >= 0) {
 | 
	
		
			
				|  |  | //                    //当前时间>=结束时间  状态变更为失效
 | 
	
		
			
				|  |  | //                    updateChannelStatusById(id, 0);
 | 
	
		
			
				|  |  | //                }else if (startEqual == 2 ) {
 | 
	
		
			
				|  |  | //                    //-1删除,0失效,1生效,2未生效
 | 
	
		
			
				|  |  | //                    //当前时间>=起始时间  状态变更为生效
 | 
	
		
			
				|  |  | //                    updateChannelStatusById(id, 1);
 | 
	
		
			
				|  |  | //                }
 | 
	
		
			
				|  |  | //
 | 
	
		
			
				|  |  | //            }
 | 
	
		
			
				|  |  | //        }
 | 
	
		
			
				|  |  |             //根据处方号获取处方状态
 | 
	
		
			
				|  |  |             Map<String, Object> params =new HashMap<>();
 | 
	
		
			
				|  |  |             params.put("registerSn",null);
 | 
	
		
			
				|  |  |             params.put("patNo",patientMappingCode);
 | 
	
		
			
				|  |  |             params.put("admNo",admNo);
 | 
	
		
			
				|  |  |             params.put("realOrder",realOrder);
 | 
	
		
			
				|  |  |             //判断处方是否存在
 | 
	
		
			
				|  |  |             boolean preExistFlag=true;
 | 
	
		
			
				|  |  |             List<WlyyPrescriptionVO> wlyyPrescriptionVOS= getWlyyPrescriptionVO(params);
 | 
	
		
			
				|  |  |             //若用处方号获取处方为空,则需要使用居民编号及门诊就诊唯一号查询
 | 
	
		
			
				|  |  |             if(null==wlyyPrescriptionVOS){
 | 
	
		
			
				|  |  |                 preExistFlag=false;
 | 
	
		
			
				|  |  |                 params.remove("realOrder");
 | 
	
		
			
				|  |  |                 wlyyPrescriptionVOS= getWlyyPrescriptionVO(params);
 | 
	
		
			
				|  |  |             }
 | 
	
		
			
				|  |  |             if(null!=wlyyPrescriptionVOS){
 | 
	
		
			
				|  |  |                 //如果状态为100,门诊记录需要变更为结束
 | 
	
		
			
				|  |  | 
 | 
	
		
			
				|  |  |     }
 | 
	
		
			
				|  |  |                 //如果状态为13 则需要保存处方状态
 | 
	
		
			
				|  |  | 
 | 
	
		
			
				|  |  |                 //如果原来状态为13,则需要将处方重新获取
 | 
	
		
			
				|  |  | 
 | 
	
		
			
				|  |  |                 //
 | 
	
		
			
				|  |  | 
 | 
	
		
			
				|  |  |             }
 | 
	
		
			
				|  |  |             }
 | 
	
		
			
				|  |  |         }
 | 
	
		
			
				|  |  | 
 | 
	
		
			
				|  |  |    /* public void updateChannelStatusById(String id,Integer status){
 | 
	
		
			
				|  |  |         String countSql = "update  wlyy.wlyy_channel_recommend_article wp SET wp.del_flag="+status+" ,wp.last_update_time=NOW() WHERE wp.id='"+id+"'";
 | 
	
		
			
				|  |  |         jdbcTemplate.update(countSql);
 | 
	
		
			
				|  |  |     }*/
 | 
	
		
			
				|  |  |     /**
 | 
	
		
			
				|  |  |      * 获取处方详情
 | 
	
		
			
				|  |  |      * @param params
 | 
	
		
			
				|  |  |      * @return
 | 
	
		
			
				|  |  |      * @throws Exception
 | 
	
		
			
				|  |  |      */
 | 
	
		
			
				|  |  |         public  List<WlyyPrescriptionVO> getWlyyPrescriptionVO(Map<String, Object> params)throws Exception{
 | 
	
		
			
				|  |  |             //根据处方号获取处方状态
 | 
	
		
			
				|  |  |             String result=  HttpUtils.doGet(hlwyyEntranceUrl+"/mqsdk/BS16017",params).getContent();
 | 
	
		
			
				|  |  |             ObjectMapper objectMapper=new ObjectMapper();
 | 
	
		
			
				|  |  |             ListEnvelop listEnvelop=objectMapper.readValue(result,ListEnvelop.class);
 | 
	
		
			
				|  |  |             if(null!=listEnvelop&& EnvelopStatus.success.code.equals(listEnvelop.getStatus())&&null!=listEnvelop.getDetailModelList()&&listEnvelop.getDetailModelList().size()>0){
 | 
	
		
			
				|  |  |                 logger.info("result:"+listEnvelop.getDetailModelList());
 | 
	
		
			
				|  |  |                 List<WlyyPrescriptionVO> wlyyPrescriptionVOS=listEnvelop.getDetailModelList();
 | 
	
		
			
				|  |  |                 return wlyyPrescriptionVOS;
 | 
	
		
			
				|  |  |             }
 | 
	
		
			
				|  |  |             return null;
 | 
	
		
			
				|  |  |         }
 | 
	
		
			
				|  |  | 
 | 
	
		
			
				|  |  | 
 | 
	
		
			
				|  |  | 
 | 
	
		
			
				|  |  |     public void updatePrescriptionStatusByAdmNo(String admNo,String realOrder,String status)throws Exception{
 | 
	
		
			
				|  |  |         String  sql="";
 | 
	
		
			
				|  |  |         String outPatientSql="";
 | 
	
		
			
				|  |  |         if ("1".equals(status)) {
 | 
	
		
			
				|  |  |             sql = "UPDATE base.wlyy_prescription p SET p.`status`='13' WHERE p.adm_no='" + admNo + "' AND p.real_order='" + realOrder + "' ";
 | 
	
		
			
				|  |  |         } else if ("2".equals(status)) {
 | 
	
		
			
				|  |  |             //开方成功时候,先用处方号获取本地处方状态是否为开方失败,如果是则需要更新本地的处方
 | 
	
		
			
				|  |  |             sql = "UPDATE base.wlyy_prescription p SET p.`status`='20' WHERE p.adm_no='" + admNo + "' AND p.real_order='" + realOrder + "' ";
 | 
	
		
			
				|  |  |             //变更门诊状态
 | 
	
		
			
				|  |  |             outPatientSql="UPDATE base.wlyy_outpatient p SET p.`status`='2' WHERE p.adm_no='" + admNo + "'";
 | 
	
		
			
				|  |  |             jdbcTemplate.execute(outPatientSql);
 | 
	
		
			
				|  |  |         } else if ("3".equals(status)) {
 | 
	
		
			
				|  |  |             //自取处方结束
 | 
	
		
			
				|  |  |             sql = "UPDATE base.wlyy_prescription p SET p.`status`='100',p.pay_status='1' WHERE p.adm_no='" + admNo + "' AND p.real_order='" + realOrder + "' ";
 | 
	
		
			
				|  |  |         }
 | 
	
		
			
				|  |  |         jdbcTemplate.execute(sql);
 | 
	
		
			
				|  |  |     }
 | 
	
		
			
				|  |  | 
 | 
	
		
			
				|  |  | 
 | 
	
		
			
				|  |  | 
 |