liusheng
2024-12-11 10aaa035f5be0312304d20f022bdb714a8f4900a
ruoyi-quartz/src/main/java/com/ruoyi/quartz/task/RyTask.java
@@ -12,22 +12,27 @@
import com.ruoyi.common.core.redis.RedisCache;
import com.ruoyi.common.enums.ServiceFromEnum;
import com.ruoyi.common.utils.DtoConversionUtils;
import com.ruoyi.common.utils.HttpUtil;
import com.ruoyi.common.utils.RSAPublicKeyExample;
import com.ruoyi.common.utils.StringUtils;
import com.ruoyi.common.utils.http.HttpUtils;
import com.smartor.common.SendService;
import com.smartor.domain.*;
import com.smartor.mapper.ServiceSubtaskMapper;
import com.smartor.mapper.ServiceTaskMapper;
import com.smartor.mapper.SysUserImportMapper;
import com.smartor.service.*;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.collections4.CollectionUtils;
import org.apache.commons.lang3.ObjectUtils;
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.File;
import java.io.FileWriter;
import java.io.IOException;
import java.io.UnsupportedEncodingException;
import java.lang.reflect.Type;
@@ -39,6 +44,7 @@
import java.text.SimpleDateFormat;
import java.time.LocalDate;
import java.time.LocalDateTime;
import java.time.LocalTime;
import java.time.ZoneId;
import java.time.format.DateTimeFormatter;
import java.util.*;
@@ -55,6 +61,9 @@
    private IServiceThirdDataService iServiceThirdDataService;
    @Autowired
    private IServiceOutPathService iServiceOutPathService;
    @Autowired
    private IServiceSubtaskAnswerService serviceSubtaskAnswerService;
    @Autowired
@@ -64,6 +73,9 @@
    private IServiceSubtaskService iServiceSubtaskService;
    @Autowired
    private IServicePatientTempService iServicePatientTempService;
    @Autowired
    private PlatformTransactionManager transactionManager;
    @Autowired
@@ -71,6 +83,9 @@
    @Value("${localIP}")
    private String localIP;
    @Value("${xhsmsPath}")
    private String xhsmsPath;
    @Value("${pub_key}")
    private String pub_key;
@@ -85,7 +100,7 @@
    private ServiceTaskMapper serviceTaskMapper;
    @Autowired
    private SendService sendService;
    private ServiceSubtaskMapper serviceSubtaskMapper;
    @Autowired
    private RedisCache redisCache;
@@ -107,6 +122,9 @@
    @Autowired
    ISvyTaskTemplateService svyTaskTemplateService;
    @Autowired
    IIvrTaskTemplateService iIvrTaskTemplateService;
    @Value("${hosp_info_url}")
    private String hospInfoUrl;
@@ -177,8 +195,13 @@
        //将患者信息、出院、门诊数据全部采集
        // 获取当前日期和时间
        LocalDateTime endTime = LocalDateTime.now();
        endTime = endTime.with(LocalTime.MIN);
//        String dateStr = "2024/11/30 00:00:00";
//        DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyy/MM/dd HH:mm:ss");
//        LocalDateTime endTime = LocalDateTime.parse(dateStr, formatter);
        // 获取前一天的日期和时间
        LocalDateTime startTime = endTime.minusDays(1);
        startTime = startTime.with(LocalTime.MIN);
        getInHospInfo("0", startTime, endTime);
        getInHospInfo("1", startTime, endTime);
@@ -215,7 +238,18 @@
    }
    /**
     * 长期任务执行
     * 处理患者信息,进入子任务表(微信小程序)
     */
    public void dealOutHospInfoXHC() {
        PatMedInhosp patMedInhosp = new PatMedInhosp();
        //获取未处理的数据
        List<ServicePatientTemp> servicePatientTemps = iServicePatientTempService.selectServicePatientTempList(new ServicePatientTemp());
    }
    /**
     * 长期任务执行(电话)
     */
    public void longTaskSend() {
        //获取任务信息
@@ -277,85 +311,154 @@
                ivrTaskcall.setSendstate(2L);
                List<ServiceSubtask> selectServiceSubtaskList = iServiceSubtaskService.selectServiceSubtaskList(ivrTaskcall);
                if (descByCode.equals("电话")) {
                    String value = "";
                    for (ServiceSubtask serviceSubtask : selectServiceSubtaskList) {
                        ServiceTask ivrTask = serviceTaskMapper.selectServiceTaskByTaskid(serviceSubtask.getTaskid());
                        if (ivrTask.getSendState() != null && ivrTask.getSendState() == 3 || ivrTask.getSendState() != null && ivrTask.getSendState() == 4) {
                            //如何任务被“暂停”或“终止”
                            break;
                        }
                        //获取到value值最少的key
                        String key = getKey();
                        if (commonTaskcallMQ.getSendType().equals("2")) {
                            //说明是立即发送
                            String value = redisCache.getCacheObject("cache-0");
                            ServiceSubtaskVO serviceSubtask1 = new ServiceSubtaskVO();
                            serviceSubtask1.setTaskid(commonTaskcallMQ.getTaskid());
                            List<ServiceSubtask> selectServiceSubtaskList1 = iServiceSubtaskService.selectServiceSubtaskList(serviceSubtask1);
                            for (ServiceSubtask serviceSubtask2 : selectServiceSubtaskList1) {
                        boolean dateToday = isDateToday(serviceSubtask.getLongSendTime());
                        //说明是立即发送(先把消息放到cache-0中,   displayNo不知道怎么配置)
                        if (dateToday) {
                            try {
//                                ServiceSubtaskVO serviceSubtask1 = new ServiceSubtaskVO();
//                                serviceSubtask1.setTaskid(ivrTask.getTaskid());
//                                List<ServiceSubtask> selectServiceSubtaskList1 = iServiceSubtaskService.selectServiceSubtaskList(serviceSubtask1);
//                                for (ServiceSubtask serviceSubtask2 : selectServiceSubtaskList1) {
                                if (StringUtils.isEmpty(value)) {
                                    value = "," + serviceSubtask2.getId().toString() + ",";
                                    value = "," + serviceSubtask.getId().toString() + ",";
                                } else {
                                    value = value + "," + serviceSubtask2.getId().toString() + ",";
                                    if (!value.contains("," + serviceSubtask.getId().toString()))
                                        value = value + "," + serviceSubtask.getId().toString() + ",";
                                }
//                                }
                            } catch (Exception e) {
                                String guid = UUID.randomUUID().toString();
                                log.error("长期任务,电话发送失败原因:{},{}", e.getMessage(), guid);
                                serviceSubtask.setResult("error");
                                serviceSubtask.setRemark("系统错误");
                                serviceSubtask.setSendstate(3L);
                                serviceSubtask.setFinishtime(new Date());
                                serviceSubtask.setGuid(guid);
                                iServiceSubtaskService.updateServiceSubtask(serviceSubtask);
                            }
                            redisCache.setCacheObject("cache-0", value);
                        } else {
                            //非立即发送
                            String value = redisCache.getCacheObject(key);
                            ServiceSubtaskVO serviceSubtask1 = new ServiceSubtaskVO();
                            serviceSubtask1.setTaskid(commonTaskcallMQ.getTaskid());
                            List<ServiceSubtask> selectServiceSubtaskList1 = iServiceSubtaskService.selectServiceSubtaskList(serviceSubtask1);
                            for (ServiceSubtask serviceSubtask2 : selectServiceSubtaskList1) {
                                if (StringUtils.isEmpty(value)) {
                                    value = "," + serviceSubtask2.getId().toString() + ",";
                                } else {
                                    value = value + "," + serviceSubtask2.getId().toString() + ",";
                                }
                            }
                            redisCache.setCacheObject(key, value);
                            //在子任务表里记录一下
                            String uuid = UUID.randomUUID().toString();
                            serviceSubtask.setResult("success");
                            serviceSubtask.setRemark("长期任务,电话进入队列成功");
                            serviceSubtask.setGuid(uuid);
                            serviceSubtask.setSendstate(3L);
                            serviceSubtask.setFinishtime(new Date());
                            serviceSubtaskMapper.updateServiceSubtask(serviceSubtask);
                            //任务发送记录
                            ServiceSubtaskRecord serviceSubtaskRecord = new ServiceSubtaskRecord();
                            serviceSubtaskRecord.setTaskid(serviceSubtask.getTaskid().toString());
                            serviceSubtaskRecord.setSubtaskId(serviceSubtask.getId());
                            serviceSubtaskRecord.setUuid(uuid);
                            serviceSubtaskRecord.setTasktype(serviceSubtask.getType());
                            serviceSubtaskRecord.setPreachform("3");
                            serviceSubtaskRecord.setStartTime(System.currentTimeMillis());
                            serviceSubtaskRecord.setRemark("长期任务,电话进入队列成功");
                            serviceSubtaskRecordService.insertServiceSubtaskRecord(serviceSubtaskRecord);
                        }
                        //任务发送记录(这个代码应该写在phoneTask中,先写在这,后面再改)
                        ServiceSubtaskRecord serviceSubtaskRecord = new ServiceSubtaskRecord();
                        serviceSubtaskRecord.setTaskid(serviceSubtask.getTaskid().toString());
                        serviceSubtaskRecord.setUuid(UUID.randomUUID().toString());
                        serviceSubtaskRecord.setTasktype(serviceSubtask.getType());
                        serviceSubtaskRecord.setPreachform("3");
                        serviceSubtaskRecord.setStartTime(System.currentTimeMillis());
                        serviceSubtaskRecordService.insertServiceSubtaskRecord(serviceSubtaskRecord);
//                        //通过多线程的方式去打电话
//                        //通过多线程的方式去打电话(这个是上海的方法)
//                        executorService.submit(new PhoneTask(serviceSubtask, ivrTaskTemplateVO1, redisCache, sendService, phonePath, robotPhoneUtils));
                    }
                    //将要执行的放到缓存中
                    String cache0 = redisCache.getCacheObject("cache-0");
                    if (StringUtils.isNotEmpty(cache0)) {
                        redisCache.setCacheObject("cache-0", cache0 + value);
                    } else if (StringUtils.isEmpty(cache0) && StringUtils.isNotEmpty(value)) {
                        redisCache.setCacheObject("cache-0", value);
                    }
                } else if (descByCode.equals("多媒体")) {
                    //多媒体
                } else if (descByCode.equals("纸质")) {
                    //纸质
                } else if (descByCode.equals("微信小程序")) {
                    //微信小程序
                    for (ServiceSubtask serviceSubtask : selectServiceSubtaskList) {
                        log.error("微信小程序期任务发送---------进来了吗?{}", serviceSubtask);
                    }
                } else if (descByCode.equals("短信")) {
                    //短信
//                        http://localhost:8099/followvisit/particty?param1=3&param2=348
                    //对url中两个参数加密
                    RSAPublicKeyExample rsaPublicKeyExample = new RSAPublicKeyExample();
                    String taskId = rsaPublicKeyExample.encryptedData(ivrTask1.getTaskid().toString(), pub_key);
                    for (ServiceSubtask serviceSubtask : selectServiceSubtaskList) {
                        String patid = rsaPublicKeyExample.encryptedData(serviceSubtask.getPatid().toString(), pub_key);
                        SendMagParam sendMagParam = new SendMagParam();
                        sendMagParam.setType("4");
                        sendMagParam.setPhone(serviceSubtask.getPhone());
                        sendMagParam.setUrl(ip + ":" + req_path + "/followvisit/particty?param1=" + taskId + "&param2=" + patid + "&param5=false");
                        Boolean aBoolean = sendService.sendMsg(sendMagParam);
                        log.error("短信长期任务发送---------进来了吗?{}", serviceSubtask);
                        boolean dateToday = isDateToday(serviceSubtask.getLongSendTime());
                        if (dateToday) {
                            try {
                                //先判断患者是不是还要发送
                                PatArchive patArchive = patArchiveService.selectPatArchiveByPatid(serviceSubtask.getPatid());
                                if (!Objects.isNull(patArchive) && patArchive.getNotrequiredFlag() == "1") {
                                    //不需要发送
                                    serviceSubtask.setResult("error");
                                    serviceSubtask.setRemark(patArchive.getNotrequiredreason());
                                    serviceSubtask.setSendstate(4L);
                                    serviceSubtask.setFinishtime(new Date());
                                    iServiceSubtaskService.insertServiceSubtask(serviceSubtask);
                                    continue;
                                }
                                String patid = rsaPublicKeyExample.encryptedData(serviceSubtask.getPatid().toString(), pub_key);
                                SendMagParam sendMagParam = new SendMagParam();
                                sendMagParam.setType("4");
                                ServiceOutPath serviceOutPath = new ServiceOutPath();
                                serviceOutPath.setParam1(taskId);
                                serviceOutPath.setParam2(patid);
                                serviceOutPath.setParam3(ivrTask1.getTaskName());
                                serviceOutPath.setCreateTime(new Date());
                                iServiceOutPathService.insertServiceOutPath(serviceOutPath);
                                String format = String.format("%03X", serviceOutPath.getId());
                                serviceOutPath.setRadix(format);
                                serviceOutPath.setUpdateTime(new Date());
                                iServiceOutPathService.updateServiceOutPath(serviceOutPath);
                                sendMagParam.setPhone(serviceSubtask.getPhone());
                                sendMagParam.setUrl(ip + ":" + req_path + "/wt?p=" + format);
                                sendMagParam.setContent("【新华医院】您好,邀请您填写出院调查表,请点击" + sendMagParam.getUrl() + "填写。感谢您配合!");
                                Map<String, String> req = new HashMap<>();
                                req.put("phone", sendMagParam.getPhone());
                                req.put("content", sendMagParam.getContent());
                                String isSuccess = HttpUtil.postJsonRequest(xhsmsPath, new Gson().toJson(req));
                                //任务发送记录
                                ServiceSubtaskRecord serviceSubtaskRecord = new ServiceSubtaskRecord();
                                serviceSubtaskRecord.setTaskid(serviceSubtask.getTaskid().toString());
                                serviceSubtaskRecord.setUuid(UUID.randomUUID().toString());
                                serviceSubtaskRecord.setTasktype(serviceSubtask.getType());
                                serviceSubtaskRecord.setPreachform("4");
                                serviceSubtaskRecord.setStartTime(System.currentTimeMillis());
                                serviceSubtaskRecord.setResult("success");
                                if (StringUtils.isNotEmpty(isSuccess) && !isSuccess.equals("true"))
                                    serviceSubtaskRecord.setResult("error");
                                serviceSubtaskRecordService.insertServiceSubtaskRecord(serviceSubtaskRecord);
                        //任务发送记录
                        ServiceSubtaskRecord serviceSubtaskRecord = new ServiceSubtaskRecord();
                        serviceSubtaskRecord.setTaskid(serviceSubtask.getTaskid().toString());
                        serviceSubtaskRecord.setUuid(UUID.randomUUID().toString());
                        serviceSubtaskRecord.setTasktype(serviceSubtask.getType());
                        serviceSubtaskRecord.setPreachform("4");
                        serviceSubtaskRecord.setStartTime(System.currentTimeMillis());
                        serviceSubtaskRecord.setResult(aBoolean == true ? "成功" : "失败");
                        serviceSubtaskRecordService.insertServiceSubtaskRecord(serviceSubtaskRecord);
                                //发送完成后,修改状态
                                serviceSubtask.setSendstate(3L);
                                if (StringUtils.isNotEmpty(isSuccess) && !isSuccess.equals("true")) {
                                    serviceSubtask.setResult("success");
                                    serviceSubtask.setRemark("短信发送成功");
                                } else {
                                    serviceSubtask.setResult("error");
                                    serviceSubtask.setSendstate(5L);
                                    serviceSubtask.setRemark("短信发送失败");
                                }
                                iServiceSubtaskService.updateServiceSubtask(serviceSubtask);
                            } catch (Exception e) {
                                String guid = UUID.randomUUID().toString();
                                log.error("长期任务,短信发送失败原因:{},{}", e.getMessage(), guid);
                                serviceSubtask.setResult("error");
                                serviceSubtask.setRemark("系统错误");
                                serviceSubtask.setSendstate(5L);
                                serviceSubtask.setFinishtime(new Date());
                                serviceSubtask.setGuid(guid);
                                iServiceSubtaskService.updateServiceSubtask(serviceSubtask);
                            }
                        }
                    }
                } else if (descByCode.equals("公众号")) {
                    log.error("公众号进来了吗?{}", descByCode);
@@ -402,12 +505,13 @@
                                    serviceSubtask.setResult("success");
                                } else {
                                    serviceSubtask.setResult("error");
                                    serviceSubtask.setSendstate(5L);
                                    serviceSubtask.setRemark(map.get("msg").toString());
                                }
                                iServiceSubtaskService.updateServiceSubtask(serviceSubtask);
                            } catch (Exception e) {
                                String guid = UUID.randomUUID().toString();
                                log.error("长期任务,发送失败原因:{},{}", e.getMessage(), guid);
                                log.error("长期任务,公众号发送失败原因:{},{}", e.getMessage(), guid);
                                serviceSubtask.setResult("error");
                                serviceSubtask.setRemark("系统错误");
                                serviceSubtask.setSendstate(5L);
@@ -434,8 +538,8 @@
        DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss.S");
        Map<String, String> req = new HashMap<>();
        if (cry.equals("1")) {
            req.put("startTime", startTime.format(formatter));
            req.put("endTime", endTime.format(formatter));
            req.put("cashStartDate", startTime.format(formatter));
            req.put("cashEndDate", endTime.format(formatter));
            req.put("currStatus", "4");
        } else if (cry.equals("0")) {
            req.put("admissStartDate", startTime.format(formatter));
@@ -456,6 +560,32 @@
        Type resultType = new TypeToken<List<ThiedInhospInfo>>() {
        }.getType();
        List<ThiedInhospInfo> thiedInhospInfoList = gson.fromJson(resultArray, resultType);
        // 创建一个新文件
        String filename = null;
        if (cry.equals("0")) filename = "入院信息" + System.currentTimeMillis();
        if (cry.equals("1")) filename = "出院信息" + System.currentTimeMillis();
        File file = new File("D:\\public\\华卓集成平台数据\\" + filename);
        if (!file.exists()) {
            // 如果文件不存在,则创建新文件
            try {
                if (file.createNewFile()) {
                    System.out.println("成功创建了新文件!");
                } else {
                    System.out.println("失败!文件无法创建!");
                }
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
        // 将数据写入到文件中
        try {
            FileWriter writer = new FileWriter(file);
            writer.write(result);
        } catch (IOException e) {
            e.printStackTrace();
        }
        if (CollectionUtils.isNotEmpty(thiedInhospInfoList)) {
            for (ThiedInhospInfo thiedInhospInfo : thiedInhospInfoList) {
                //通过身份证号查询该患者在患者表是否存在
@@ -478,7 +608,7 @@
                        patArchive.setNotrequiredreason(thiedInhospInfo.getOutWayName());
                    }
                    if (cry.equals("1") && StringUtils.isEmpty(thiedInhospInfo.getOutWayId())) {
                        //如果为4就是死亡
                        //出院方式为空
                        patArchive.setNotrequiredFlag("1");
                        patArchive.setNotrequiredreason("出院方式为空");
                    }
@@ -506,7 +636,9 @@
                    patArchive.setName(thiedInhospInfo.getPatiRecordName());
                    patArchiveService.insertPatArchive(patArchive);
                } else {
                    patArchive = patArchives.get(0);
                    patArchive.setId(patArchives.get(0).getId());
                    patArchiveService.update(patArchive);
//                    patArchive = patArchives.get(0);
                }
                log.error("入参的patArchive:{}", patArchive);
@@ -532,8 +664,8 @@
                    e.printStackTrace();
                }
                try {
                    if (StringUtils.isNotEmpty(thiedInhospInfo.getOutDate()))
                        patMedInhosp.setEndtime(new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").parse(thiedInhospInfo.getOutDate()));
                    if (StringUtils.isNotEmpty(thiedInhospInfo.getCashDate()))
                        patMedInhosp.setEndtime(new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").parse(thiedInhospInfo.getCashDate()));
                } catch (ParseException e) {
                    e.printStackTrace();
                }
@@ -565,9 +697,13 @@
                inhosp.setPatno(patArchive.getPatientno());
                inhosp.setSerialnum(patMedInhosp.getSerialnum());
                List<PatMedInhosp> patMedInhosps = patMedInhospService.selectPatMedInhospList(inhosp);
                log.error("是否需要更新:{}", patMedInhosps.size());
                if (CollectionUtils.isNotEmpty(patMedInhosps)) {
                    patMedInhosp.setInhospid(patMedInhosps.get(0).getInhospid());
                    log.error("------患者出入院修改信息:{}", patMedInhosp);
                    patMedInhospService.updatePatMedInhosp(patMedInhosp);
                } else {
                    log.error("----患者出入院新增信息:{}", patMedInhosp);
                    patMedInhospService.insertPatMedInhosp(patMedInhosp);
                }
@@ -615,7 +751,15 @@
        String result = HttpUtils.sendPost(thirdWXUrl, new Gson().toJson(xinHuaWXReq));
        JSONObject jsonObject = JSONObject.parseObject(result);
        log.error("jsonObject的数据为:{}", jsonObject.toString());
//        String code = (String) jsonObject.get("code");
        return jsonObject.toString();
    }
//    public static void main(String[] args) {
//        LocalDateTime originalDateTime = LocalDateTime.now();
//        LocalDateTime startOfDay = originalDateTime.with(LocalTime.MIN);
//        System.out.println("Original DateTime: " + originalDateTime);
//        System.out.println("Start of Day: " + startOfDay);
//    }
}