liusheng
2024-09-18 907641e56c2085aaa81f267946dc3e3e9fca73e7
ruoyi-quartz/src/main/java/com/ruoyi/quartz/task/RyTask.java
@@ -1,15 +1,14 @@
package com.ruoyi.quartz.task;
import com.alibaba.fastjson2.JSON;
import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.JsonNode;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.alibaba.fastjson2.JSONObject;
import com.google.common.reflect.TypeToken;
import com.google.gson.JsonObject;
import com.google.gson.JsonArray;
import com.google.gson.Gson;
import com.google.gson.JsonArray;
import com.google.gson.JsonObject;
import com.ruoyi.common.core.redis.RedisCache;
import com.ruoyi.common.enums.ServiceFromEnum;
import com.ruoyi.common.utils.DtoConversionUtils;
import com.ruoyi.common.utils.RSAPublicKeyExample;
import com.ruoyi.common.utils.StringUtils;
import com.ruoyi.common.utils.http.HttpUtils;
@@ -19,14 +18,19 @@
import com.smartor.service.*;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.collections4.CollectionUtils;
import org.apache.commons.lang3.math.NumberUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Component;
import org.springframework.transaction.PlatformTransactionManager;
import org.springframework.transaction.annotation.Transactional;
import java.io.UnsupportedEncodingException;
import java.lang.reflect.Type;
import java.net.URLEncoder;
import java.nio.charset.StandardCharsets;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.time.LocalDate;
import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;
import java.util.*;
@@ -51,6 +55,8 @@
    @Autowired
    private IServiceSubtaskService iServiceSubtaskService;
    @Autowired
    private PlatformTransactionManager transactionManager;
    @Value("${localIP}")
    private String localIP;
@@ -58,6 +64,8 @@
    @Value("${pub_key}")
    private String pub_key;
    @Value("${thirdWXUrl}")
    private String thirdWXUrl;
    @Value("${req_path}")
    private String req_path;
@@ -85,6 +93,9 @@
    @Autowired
    IPatMedOuthospService patMedOuthospService;
    @Autowired
    ISvyTaskTemplateService svyTaskTemplateService;
    @Value("${hosp_info_url}")
    private String hospInfoUrl;
@@ -125,15 +136,43 @@
     */
    public void dealHisData() {
        //将患者信息、出院、门诊数据全部采集
        getInHospInfo("0");
        getInHospInfo("1");
        // 获取当前日期和时间
        LocalDateTime endTime = LocalDateTime.now();
        // 获取前一天的日期和时间
        LocalDateTime startTime = endTime.minusDays(1);
        getInHospInfo("0", startTime, endTime);
        getInHospInfo("1", startTime, endTime);
        //调用dealOupHospInfo数据处理方法,处理完后,将“出院表的check_flag=1”
        //获取当前日期 (精确到天)往前推相应天数(在指定的基础模板中找对应的天数),
        //循环患者信息,根据患者"出院诊断ICD值”获取到相当的模板(获取取相应的天数);
        //根据查询出来的模板,去固定任务(固定任务,已经将模板也固定了)去匹配模板
        //根据匹配出来的模板和任务与患者信息,将数据插入到子任务表中
        iPatMedInhospService.dealOupHospInfo();
    }
    public void dealHisData2() {
        //将患者信息、出院、门诊数据全部采集
        // 指定的开始日期
        LocalDate endDate = LocalDate.of(2024, 9, 1);
        // 当前日期
        LocalDate currentDate = LocalDate.now();
        // 定义日期格式
        DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyy-MM-dd ");
        // 从开始日期到当前日期逐天打印
        for (LocalDate date = endDate; !date.isAfter(currentDate); date = date.plusDays(1)) {
            System.out.println(date.format(formatter));
            getInHospInfo("0", date.minusDays(1).atStartOfDay(), date.atStartOfDay());
            getInHospInfo("1", date.minusDays(1).atStartOfDay(), date.atStartOfDay());
        }
    }
    /**
     * 处理患者信息,进入子任务表
     */
    public void dealOutHospInfo() {
        iPatMedInhospService.dealOutHospInfo();
    }
    /**
@@ -145,24 +184,58 @@
        st.setDelFlag("0");
        st.setLongTask(1);
        List<ServiceTask> serviceTasks = serviceTaskMapper.selectServiceTaskList(st);
        log.error("serviceTasks进来了吗?{}", serviceTasks);
        for (ServiceTask serviceTask : serviceTasks) {
            //通过taskId,获取未执行的子任务信息
            ServiceSubtaskVO serviceSubtask = new ServiceSubtaskVO();
            serviceSubtask.setTaskid(serviceTask.getTaskid());
            serviceSubtask.setSendstate(2L);
            List<ServiceSubtask> selectServiceSubtaskList = iServiceSubtaskService.selectServiceSubtaskList(serviceSubtask);
            log.error("selectServiceSubtaskList进来了吗?{}", selectServiceSubtaskList);
            if (CollectionUtils.isNotEmpty(selectServiceSubtaskList)) {
                for (ServiceSubtask serviceSubtask1 : selectServiceSubtaskList) {
                    //如果患者的发送日期是今天,则进行发送
                    boolean dateToday = isDateToday(serviceSubtask1.getLongSendTime());
                    if (dateToday) {
                        CommonTaskcallMQ commonTaskcallMQ = new CommonTaskcallMQ();
                        commonTaskcallMQ.setTaskid(serviceTask.getTaskid());
                        commonTaskcallMQ.setPreachform(serviceSubtask1.getServiceform());
                        sfHandle(commonTaskcallMQ, localIP);
                        //发送完成后,修改状态
                        serviceSubtask1.setSendstate(3L);
                        iServiceSubtaskService.updateServiceSubtask(serviceSubtask1);
                    try {
                        boolean dateToday = isDateToday(serviceSubtask1.getLongSendTime());
                        if (dateToday) {
                            //先判断患者是不是还要发送
                            PatArchive patArchive = patArchiveService.selectPatArchiveByPatid(serviceSubtask1.getPatid());
                            if (!Objects.isNull(patArchive) && patArchive.getNotrequiredFlag() == "1") {
                                //不需要发送
                                serviceSubtask1.setResult("error");
                                serviceSubtask1.setRemark(patArchive.getNotrequiredreason());
                                serviceSubtask1.setSendstate(4L);
                                serviceSubtask1.setFinishtime(new Date());
                                iServiceSubtaskService.insertServiceSubtask(serviceSubtask1);
                                continue;
                            }
                            log.error("dateToday进来了吗?{}", dateToday);
                            CommonTaskcallMQ commonTaskcallMQ = new CommonTaskcallMQ();
                            commonTaskcallMQ.setTaskid(serviceTask.getTaskid());
                            commonTaskcallMQ.setPreachform(serviceSubtask1.getServiceform());
                            commonTaskcallMQ.setSendType("2");
                            Map<String, Object> map = sfHandlle(commonTaskcallMQ, localIP);
                            //发送完成后,修改状态
                            serviceSubtask1.setSendstate(3L);
                            if ((Boolean) map.get("succ") == false) {
                                serviceSubtask1.setResult("success");
                            } else {
                                serviceSubtask1.setResult("error");
                                serviceSubtask1.setRemark(map.get("msg").toString());
                            }
                            iServiceSubtaskService.updateServiceSubtask(serviceSubtask1);
                        }
                    } catch (Exception e) {
                        String guid = UUID.randomUUID().toString();
                        log.error("长期任务,发送失败原因:{},{}", e.getMessage(), guid);
                        serviceSubtask1.setResult("error");
                        serviceSubtask1.setRemark("系统错误");
                        serviceSubtask1.setSendstate(5L);
                        serviceSubtask1.setFinishtime(new Date());
                        serviceSubtask1.setGuid(guid);
                        iServiceSubtaskService.insertServiceSubtask(serviceSubtask1);
                    }
                }
            }
@@ -191,14 +264,15 @@
        return (calendarToCheck.get(Calendar.YEAR) == todayYear && calendarToCheck.get(Calendar.MONTH) == todayMonth && calendarToCheck.get(Calendar.DAY_OF_MONTH) == todayDay);
    }
    public void sfHandle(CommonTaskcallMQ commonTaskcallMQ, String ip) {
    public Map<String, Object> sfHandlle(CommonTaskcallMQ commonTaskcallMQ, String ip) {
        log.error("sfHandlle进来了吗?{}", commonTaskcallMQ);
        //判断一下commonTaskcallMQ中的stopstate是否与ivr_task中的一致,不一致,则说明是暂停了
        ServiceTask ivrTask1 = serviceTaskMapper.selectServiceTaskByTaskid(commonTaskcallMQ.getTaskid());
        if (StringUtils.isNotEmpty(commonTaskcallMQ.getPreachform())) {
            //如何任务发送方式不为空
            String[] split = commonTaskcallMQ.getPreachform().split(",");
            System.out.println("split的值为:" + split);
            log.error("split进来了吗?{}", split);
            for (String serviceFrom : split) {
                String descByCode = ServiceFromEnum.getDescByCode(Integer.valueOf(serviceFrom));
                //这里可以考虑用策略模式优化一下,不然太难看了
@@ -259,10 +333,10 @@
                    }
                } else if (descByCode.equals("多媒体")) {
                    //多媒体
                    return;
                    return null;
                } else if (descByCode.equals("纸质")) {
                    //纸质
                    return;
                    return null;
                } else if (descByCode.equals("短信")) {
                    //短信
//                        http://localhost:8099/followvisit/particty?param1=3&param2=348
@@ -278,7 +352,6 @@
                        sendMagParam.setUrl(ip + ":" + req_path + "/followvisit/particty?param1=" + taskId + "&param2=" + patid + "&param5=false");
                        Boolean aBoolean = sendService.sendMsg(sendMagParam);
                        //任务发送记录
                        ServiceSubtaskRecord serviceSubtaskRecord = new ServiceSubtaskRecord();
                        serviceSubtaskRecord.setTaskid(serviceSubtask.getTaskid().toString());
@@ -288,24 +361,24 @@
                        serviceSubtaskRecord.setStartTime(System.currentTimeMillis());
                        serviceSubtaskRecord.setResult(aBoolean == true ? "成功" : "失败");
                        serviceSubtaskRecordService.insertServiceSubtaskRecord(serviceSubtaskRecord);
                        return null;
                    }
                } else if (descByCode.equals("公众号")) {
                    log.error("公众号进来了吗?{}", descByCode);
                    //公众号
                    RSAPublicKeyExample rsaPublicKeyExample = new RSAPublicKeyExample();
                    String taskId = rsaPublicKeyExample.encryptedData(ivrTask1.getTaskid().toString(), pub_key);
                    for (ServiceSubtask serviceSubtask : selectServiceSubtaskList) {
                        log.error("serviceSubtask---------进来了吗?{}", serviceSubtask);
                        String patid = rsaPublicKeyExample.encryptedData(serviceSubtask.getPatid().toString(), pub_key);
                        SendMagParam sendMagParam = new SendMagParam();
                        sendMagParam.setType("5");
                        sendMagParam.setUrl(ip + ":" + req_path + "/outsideChain?param1=" + taskId + "&param2=" + patid + "&param3=" + ivrTask1.getTaskName() + "&param5=false");
                        log.info("链接完整路径:{}", ip + ":" + req_path + "/outsideChain?param1=" + taskId + "&param2=" + patid + "&param3=" + ivrTask1.getTaskName() + "&param5=false");
                        //这个模板ID先写死,后面做成可选
                        sendMagParam.setTmpCode("oG3pJHPVWpE81DmZsua_2tKwgJ97r0qz37z56ns7NB4");
                        Map map = new HashMap();
                        map.put("first", ivrTask1.getTaskName());
                        sendMagParam.setContent(JSON.toJSONString(map));
                        sendMagParam.setOpenid(serviceSubtask.getOpenid());
                        Boolean aBoolean = sendService.sendMsg(sendMagParam);
                        String url = null;
                        try {
                            url = ip + ":" + req_path + "/outsideChainwt?param1=" + taskId + "&param2=" + patid + "&param3=" + URLEncoder.encode(ivrTask1.getTaskName(), StandardCharsets.UTF_8.toString()) + "&param5=false";
                        } catch (UnsupportedEncodingException e) {
                            e.printStackTrace();
                        }
                        String wxCode = getWXCode(serviceSubtask.getSfzh(), url, serviceSubtask.getTaskName(), serviceSubtask.getTaskDesc(), "无");
                        Map<String, Object> map = JSONObject.parseObject(wxCode, Map.class);
                        //任务发送记录
                        ServiceSubtaskRecord serviceSubtaskRecord = new ServiceSubtaskRecord();
@@ -314,20 +387,26 @@
                        serviceSubtaskRecord.setTasktype(serviceSubtask.getType());
                        serviceSubtaskRecord.setPreachform("4");
                        serviceSubtaskRecord.setStartTime(System.currentTimeMillis());
                        serviceSubtaskRecord.setResult(aBoolean == true ? "成功" : "失败");
                        serviceSubtaskRecord.setResult("success");
                        if (!map.isEmpty() && (Boolean) map.get("succ") == false)
                            serviceSubtaskRecord.setResult("error");
                        serviceSubtaskRecordService.insertServiceSubtaskRecord(serviceSubtaskRecord);
                        return map;
                    }
                }
                //通知 MQ 消息已被接收,可以ACK(从队列中删除)了   (这个需要根据业务再去处理ACK)
            }
        }
        return null;
    }
    /**
     * 获出院患者信息
     * cry    出入院: 0 入院   1 出院
     */
    private void getInHospInfo(String cry) {
    @Transactional
    public void getInHospInfo(String cry, LocalDateTime startTime, LocalDateTime endTime) {
        // 获取当前日期和时间
        LocalDateTime currentDateTime = LocalDateTime.now();
        // 获取前一天的日期和时间
@@ -336,12 +415,12 @@
        DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss.S");
        Map<String, String> req = new HashMap<>();
        if (cry.equals("1")) {
            req.put("startTime", previousDateTime.format(formatter));
            req.put("endTime", currentDateTime.format(formatter));
            req.put("startTime", startTime.format(formatter));
            req.put("endTime", endTime.format(formatter));
            req.put("currStatus", "4");
        } else if (cry.equals("0")) {
            req.put("admissStartDate", previousDateTime.format(formatter));
            req.put("admissEndDate", currentDateTime.format(formatter));
            req.put("admissStartDate", startTime.format(formatter));
            req.put("admissEndDate", endTime.format(formatter));
            req.put("currStatus", "2");
        }
        Map<String, String> header = new HashMap<>();
@@ -362,12 +441,23 @@
            for (ThiedInhospInfo thiedInhospInfo : thiedInhospInfoList) {
                //通过身份证号查询该患者在患者表是否存在
                PatArchive patArchive = new PatArchive();
                patArchive.setIdcardno(thiedInhospInfo.getPatiIdCardNo());
                List<PatArchive> patArchives = patArchiveService.selectPatArchiveList(patArchive);
                if (CollectionUtils.isEmpty(patArchives)) {
                List<PatArchive> patArchives = null;
                patArchive.setIdcardno(thiedInhospInfo.getPatiIdCardNo().trim());
                if (StringUtils.isNotEmpty(thiedInhospInfo.getPatiIdCardNo().trim())) {
                    patArchives = patArchiveService.selectPatArchiveList(patArchive);
                } else {
                    //患者信息为空,直接 下一个
                    continue;
                }
                if (CollectionUtils.isEmpty(patArchives) || patArchives.size() == 0) {
                    patArchive.setPatientno(thiedInhospInfo.getPatiMediaNo());
                    patArchive.setSourcefrom(2);
                    patArchive.setPattype("2");
                    if (thiedInhospInfo.getOutWayId().equals("4")) {
                        //如果为4就是死亡
                        patArchive.setNotrequiredFlag("1");
                        patArchive.setNotrequiredreason(thiedInhospInfo.getOutWayName());
                    }
                    patArchive.setSex(thiedInhospInfo.getPatiRecordGender().equals("男") ? 1L : 2L);
                    patArchive.setNation(thiedInhospInfo.getPatiNation());
                    patArchive.setNativePlace(thiedInhospInfo.getPatiNationality());
@@ -378,9 +468,8 @@
                    } catch (ParseException e) {
                        e.printStackTrace();
                    }
                    String age = thiedInhospInfo.getAge().replaceAll("岁", "");
                    boolean creatable = NumberUtils.isCreatable(age);
                    if (creatable) patArchive.setAge(StringUtils.isNotEmpty(age) ? Long.valueOf(age) : null);
                    patArchive.setAge(Long.valueOf(thiedInhospInfo.getTreateAge()));
                    patArchive.setAgeUnit(thiedInhospInfo.getTreatAgeunit());
                    patArchive.setTelcode(thiedInhospInfo.getCompanyTelNum());
                    patArchive.setRelativetelcode(thiedInhospInfo.getContactPersonPhone());
                    patArchive.setDelFlag("0");
@@ -388,11 +477,16 @@
                    patArchive.setUpdateTime(new Date());
                    patArchive.setName(thiedInhospInfo.getPatiRecordName());
                    patArchiveService.insertPatArchive(patArchive);
                } else {
                    patArchive = patArchives.get(0);
                }
                log.error("入参的patArchive:{}", patArchive);
                PatMedInhosp patMedInhosp = new PatMedInhosp();
                patMedInhosp.setPatid(patArchive.getId());
                patMedInhosp.setPatno(patArchive.getPatientno());
                patMedInhosp.setPatname(patArchive.getName());
                patMedInhosp.setNurseId(thiedInhospInfo.getNurseId());
                patMedInhosp.setNurseName(thiedInhospInfo.getNurseName());
                patMedInhosp.setSerialnum(thiedInhospInfo.getInpatientId());
                if (cry.equals("0")) {
                    patMedInhosp.setInhospstate("0");
@@ -419,20 +513,24 @@
                patMedInhosp.setDeptname(thiedInhospInfo.getAdmissDeptName());
                patMedInhosp.setBedNo(thiedInhospInfo.getAdmissBedNo());
                patMedInhosp.setDelFlag("0");
                patMedInhosp.setLeaveldeptcode(thiedInhospInfo.getCurrWardId());
                patMedInhosp.setLeaveldeptname(thiedInhospInfo.getCurrWardName());
//                patMedInhosp.setLeaveldeptcode(thiedInhospInfo.getCurrDeptCode());
                patMedInhosp.setLeaveldeptcode(thiedInhospInfo.getCurrDeptId());
                patMedInhosp.setLeaveldeptname(thiedInhospInfo.getCurrDeptName());
                patMedInhosp.setLeavediagname(thiedInhospInfo.getDiagName());
                patMedInhosp.setLeaveicd10code(thiedInhospInfo.getDiagIcd10());
                patMedInhosp.setDrname(thiedInhospInfo.getDoctName());
                patMedInhosp.setDrcode(thiedInhospInfo.getDoctId());
                patMedInhosp.setOutWayId(thiedInhospInfo.getOutWayId());
                patMedInhosp.setOutWayName(thiedInhospInfo.getOutWayName());
                if (cry.equals("0")) {
                    patMedInhosp.setSchemestatus(1L);
                } else if (cry.equals("1")) {
                    patMedInhosp.setSchemestatus(2L);
                }
                patMedInhosp.setLeavehospitaldistrictid(thiedInhospInfo.getCurrWardCode());
                patMedInhosp.setLeavehospitaldistrictcode(thiedInhospInfo.getCurrWardId());
                patMedInhosp.setLeavehospitaldistrictid(thiedInhospInfo.getCurrWardId());
                patMedInhosp.setLeavehospitaldistrictname(thiedInhospInfo.getCurrWardName());
                patMedInhosp.setLeaveldeptid(thiedInhospInfo.getCurrDeptId());
                patMedInhosp.setLeaveldeptid(thiedInhospInfo.getCurrDeptCode());
                patMedInhospService.insertPatMedInhosp(patMedInhosp);
            }
@@ -467,4 +565,19 @@
        }
        return key;
    }
    private String getWXCode(String idcard, String url, String taskName, String taskDesc, String submitprompt) {
        XinHuaWXReq xinHuaWXReq = new XinHuaWXReq();
        xinHuaWXReq.setIdcard(idcard);
        xinHuaWXReq.setUrl(url);
        xinHuaWXReq.setKeyword1("浙中医大二院出院随访");
        xinHuaWXReq.setKeyword2(LocalDateTime.now().format(DateTimeFormatter.ofPattern("yyyy-MM-dd")));
        xinHuaWXReq.setKeyword3(taskName);
        xinHuaWXReq.setKeyword4("疾病康复注意事项");
        String result = HttpUtils.sendPost(thirdWXUrl, new Gson().toJson(xinHuaWXReq));
        JSONObject jsonObject = JSONObject.parseObject(result);
        String code = (String) jsonObject.get("code");
        return code;
    }
}