双重预防项目-国泰新华二开定制版
kongzy
2024-08-14 135a322f44708b1187d37ea4b6859b5d82113e89
src/main/java/com/ruoyi/project/tr/specialCheck/Test/ItemDangerReportScheduleTest.java
@@ -1,224 +1,224 @@
package com.ruoyi.project.tr.specialCheck.Test;
import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.ruoyi.common.utils.BeanCopyUtils;
import com.ruoyi.doublePrevention.entity.CJReport.CJdto.CJReportResultData;
import com.ruoyi.doublePrevention.entity.CJReport.CJdto.Data;
import com.ruoyi.doublePrevention.entity.CJReport.CJdto.ErrorMsgDetails;
import com.ruoyi.doublePrevention.enums.CJReportEnum;
import com.ruoyi.doublePrevention.enums.SyncEnum;
import com.ruoyi.project.tr.riskList.service.IRiskListService;
import com.ruoyi.project.tr.specialCheck.domin.BO.HandlerSpecialCheckReportParam;
import com.ruoyi.project.tr.specialCheck.domin.DTO.SpecialCheckItemDangerLogReportDTO;
import com.ruoyi.project.tr.specialCheck.domin.DTO.SpecialCheckTaskLogReportDTO;
import com.ruoyi.project.tr.specialCheck.domin.TbSpecialCheckItemDangerLog;
import com.ruoyi.project.tr.specialCheck.domin.TbSpecialCheckScoreAndDangerLog;
import com.ruoyi.project.tr.specialCheck.domin.TbSpecialCheckTaskLog;
import com.ruoyi.project.tr.specialCheck.mapper.*;
import org.apache.commons.lang3.ObjectUtils;
import org.bouncycastle.crypto.engines.AESFastEngine;
import org.bouncycastle.crypto.modes.GCMBlockCipher;
import org.bouncycastle.crypto.params.AEADParameters;
import org.bouncycastle.crypto.params.KeyParameter;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;
import org.springframework.transaction.annotation.Transactional;
import java.io.*;
import java.net.HttpURLConnection;
import java.net.URL;
import java.nio.charset.StandardCharsets;
import java.text.SimpleDateFormat;
import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;
import java.util.*;
@Component
public class ItemDangerReportScheduleTest {
    private final Logger logger = LoggerFactory.getLogger(this.getClass());
    @Autowired
    private TbSpecialCheckItemLogMapper itemLogMapper;
    @Autowired
    private TbSpecialCheckScoreAndDangerLogMapper scoreAndDangerLogMapper;
    String token = "GT6gGJV7JV";
    String key = "Bv+NeBolwqg2Pbc1yVwrZA==";
    String iv = "4QC9V8eAiB7tdlgBkMsTAw==";
    public static final int MAC_BIT_SIZE = 128;
    public static String encrypt(String plainText, byte[] key, byte[] iv) {
        String sr;
        try {
            byte[] plainBytes = plainText.getBytes(StandardCharsets.UTF_8);
            GCMBlockCipher cipher = new GCMBlockCipher(new
                    AESFastEngine());
            AEADParameters parameters =
                    new AEADParameters(new KeyParameter(key),
                            MAC_BIT_SIZE, iv, null);
            cipher.init(true, parameters);
            byte[] encryptedBytes = new
                    byte[cipher.getOutputSize(plainBytes.length)];
            int retLen = cipher.processBytes(plainBytes, 0, plainBytes.length,
                    encryptedBytes, 0);
            cipher.doFinal(encryptedBytes, retLen);
            sr = Base64.getEncoder().encodeToString(encryptedBytes);
        } catch (Exception ex) {
            throw new RuntimeException(ex.getMessage());
        }
        return sr;
    }
    @Transactional
//    @Scheduled(cron = "0 0 23 * * ?")    //每天晚上23点执行一次0 0 22,23 * * ?
//    @Scheduled(cron = "0 0 22,23 * * ?")    //每天晚上22、23点执行一次
//    @Scheduled(cron = "0 0/1 * * * ? ")    // 分钟
//    @Scheduled(cron = "0 0/20 * * * ? ")    // 分钟
//    @Scheduled(cron = "0/5 * * * * ?")
    public void execReportDateSchedule() throws UnsupportedEncodingException {
        logger.info("【####】上报数据开始...");
        HttpURLConnection con = null;
        BufferedReader buffer = null;
        int responseCode = 200;
//        SimpleDateFormat dateFormatCheckData= new SimpleDateFormat("yyyy-MM-dd");
//        SimpleDateFormat dateFormat= new SimpleDateFormat("yyyy-MM-dd HH:MM:ss");
        DateTimeFormatter dateFormatCheckData = DateTimeFormatter.ofPattern("yyyy-MM-dd");
        DateTimeFormatter dateFormat = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:MM:ss");
        Date date = new Date();
        //格式化时间,作为token的时间戳
        SimpleDateFormat tokenDate= new SimpleDateFormat("yyyyMMddHHmmssSSS");
        String formatDate = tokenDate.format(date);
        logger.info("【token时间】" + formatDate);
        /**
         * 1、检查项与隐患上报
         * */
        System.out.println("【2】检查项与隐患上报-处理数据...");
//        List<TbSpecialCheckTaskLog> CJRiskAnaUnits = .listReportUnitDate();
        List<TbSpecialCheckScoreAndDangerLog> scoreAndDangerLogs = scoreAndDangerLogMapper.listScoreAndDangerLogReportList();
        if (ObjectUtils.isNotEmpty(scoreAndDangerLogs)){
            //封装上报数据,时间格式化
            List<SpecialCheckItemDangerLogReportDTO> list = new ArrayList<>();
            for (TbSpecialCheckScoreAndDangerLog scoreAndDangerLog : scoreAndDangerLogs) {
                SpecialCheckItemDangerLogReportDTO specialCheckItemDangerLogReportDTO = BeanCopyUtils.copyBean(scoreAndDangerLog, SpecialCheckItemDangerLogReportDTO.class);
                specialCheckItemDangerLogReportDTO.setCreateDate(dateFormat.format(scoreAndDangerLog.getCreateDate()));
                specialCheckItemDangerLogReportDTO.setUpdateDate(dateFormat.format(scoreAndDangerLog.getUpdateDate()));
                specialCheckItemDangerLogReportDTO.setCheckDate(dateFormatCheckData.format(scoreAndDangerLog.getCheckDate()));
                specialCheckItemDangerLogReportDTO.setDeleted(scoreAndDangerLog.getDeleted().toString());
                list.add(specialCheckItemDangerLogReportDTO);
            }
//            logger.info("【【原始数据】】" + JSONObject.toJSONString(list));
            System.out.println("【【原始数据】】" + JSONObject.toJSONString(list));
            String AESReportUnitDate = encrypt(JSONObject.toJSONString(list), key.getBytes(), iv.getBytes());
//            logger.info("【【AES加密结果】】"+ AESReportUnitDate);![](C:/Users/Ad![](C:/Users/Admin/Desktop/所有桌面文件存放/9}AM56M10$6WX){0JRK`$6X.jpg)min/Desktop/所有桌面文件存放/9}AM56M10$6WX){0JRK`$6X.jpg)
            StringBuffer itemDangerResultBuffer = null;
            //上报数据
             try {
             URL url = new URL("http://120.71.182.198:9999/v1/data/receive/receiveCheckDangerList");
             //得到连接对象
             con = (HttpURLConnection) url.openConnection();
             //设置请求类型
             con.setRequestMethod("POST");
             //允许写出
             con.setDoOutput(true);
             //允许读入
             con.setDoInput(true);
             //不使用缓存
             con.setUseCaches(false);
             con.setInstanceFollowRedirects(true);
             //设置请求头
             con.setRequestProperty("X-Access-Token",token+formatDate.toString());
             //设置Content-Type,此处根据实际情况确定
             con.setRequestProperty("Content-Type", "application/json;charset=utf8");
             OutputStream os = con.getOutputStream();
             Map paraMap = new HashMap();
             paraMap.put("data", AESReportUnitDate); /**封装数据*/
                System.out.println("【【加密请求体】】" +  JSONArray.toJSON(paraMap).toString());
                //组装入参,设置请求体
                os.write(JSON.toJSONString(paraMap).getBytes());
                //本段日志,测试成功后,可注释掉
                if (responseCode == HttpURLConnection.HTTP_OK) {
                    //得到响应流
                    InputStream inputStream = con.getInputStream();
                    //将响应流转换成字符串
                    itemDangerResultBuffer = new StringBuffer();
                    String line;
                    buffer = new BufferedReader(new InputStreamReader(inputStream, "UTF-8"));
                    while ((line = buffer.readLine()) != null) {
                        itemDangerResultBuffer.append(line);
                    }
//                    logger.info("result:" + unitResultBuffer.toString());
                    System.out.println("result:" + itemDangerResultBuffer.toString());
                }
            } catch (Exception e) {
                e.printStackTrace();
            }
            //接收返回值,保存返回值
            CJReportResultData CJOverhaulLogResult = JSONObject.parseObject(itemDangerResultBuffer.toString(), CJReportResultData.class);
            //接收返回值,保存返回值
            List<String> errorIdList= new ArrayList<>();
            Data data = JSONObject.parseObject(CJOverhaulLogResult.getData(), Data.class);
            //保存失败结果
            if (ObjectUtils.isNotEmpty(data)){
                for (ErrorMsgDetails errorMsgDetail : data.getErrorMsgDetails()) {
                    errorIdList.add(errorMsgDetail.getId());
                }
            }
            LocalDateTime nowDate = LocalDateTime.now();
            logger.info("【检查项与隐患】数据上报结果:" + CJOverhaulLogResult.getCode());
            //设置所有数据上报成功
            if (CJOverhaulLogResult.getCode().equals(CJReportEnum.SUCCESS.getCode()) || CJOverhaulLogResult.getCode().equals(CJReportEnum.SUCESS_AND_FAIL.getCode())){
                for (TbSpecialCheckScoreAndDangerLog itemDangerLo : scoreAndDangerLogs) {
                    HandlerSpecialCheckReportParam handlerCJReportParam = new HandlerSpecialCheckReportParam();
                    //封装数据
                    handlerCJReportParam.setId(itemDangerLo.getId());
                    handlerCJReportParam.setReportStatus(SyncEnum.SYNC_EXEC_SUCCESS.getCode());
                    handlerCJReportParam.setReportTime(nowDate);
                    scoreAndDangerLogMapper.updateItemDangerReportStatusById(handlerCJReportParam);
                }
            }
            //设置上报失败的数据,上报失败
            if (CJOverhaulLogResult.getCode().equals(CJReportEnum.FAIL.getCode()) || CJOverhaulLogResult.getCode().equals(CJReportEnum.SUCESS_AND_FAIL.getCode())){
                for (String failId : errorIdList) {
                    //上报失败
                    HandlerSpecialCheckReportParam handlerCJReportParam = new HandlerSpecialCheckReportParam();
                    //封装数据
                    handlerCJReportParam.setId(failId);
                    handlerCJReportParam.setReportStatus(SyncEnum.SYNC_EXEC_FAIL.getCode());
                    handlerCJReportParam.setReportTime(nowDate);
                    scoreAndDangerLogMapper.updateItemDangerReportStatusById(handlerCJReportParam);
                }
            }
//            logger.info("【1】安全风险分析单元-数据处理完成");
            System.out.println("【检查项与隐患】-数据处理完成");
        }else {
//            logger.info("【1】风险分析单元-无数据上报昌吉州");
            System.out.println("【检查项与隐患】-无数据上报昌吉州");
        }
    }
}
package com.ruoyi.project.tr.specialCheck.Test;
import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.ruoyi.common.utils.BeanCopyUtils;
import com.ruoyi.doublePrevention.entity.CJReport.CJdto.CJReportResultData;
import com.ruoyi.doublePrevention.entity.CJReport.CJdto.Data;
import com.ruoyi.doublePrevention.entity.CJReport.CJdto.ErrorMsgDetails;
import com.ruoyi.doublePrevention.enums.CJReportEnum;
import com.ruoyi.doublePrevention.enums.SyncEnum;
import com.ruoyi.project.tr.riskList.service.IRiskListService;
import com.ruoyi.project.tr.specialCheck.domin.BO.HandlerSpecialCheckReportParam;
import com.ruoyi.project.tr.specialCheck.domin.DTO.SpecialCheckItemDangerLogReportDTO;
import com.ruoyi.project.tr.specialCheck.domin.DTO.SpecialCheckTaskLogReportDTO;
import com.ruoyi.project.tr.specialCheck.domin.TbSpecialCheckItemDangerLog;
import com.ruoyi.project.tr.specialCheck.domin.TbSpecialCheckScoreAndDangerLog;
import com.ruoyi.project.tr.specialCheck.domin.TbSpecialCheckTaskLog;
import com.ruoyi.project.tr.specialCheck.mapper.*;
import org.apache.commons.lang3.ObjectUtils;
import org.bouncycastle.crypto.engines.AESFastEngine;
import org.bouncycastle.crypto.modes.GCMBlockCipher;
import org.bouncycastle.crypto.params.AEADParameters;
import org.bouncycastle.crypto.params.KeyParameter;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;
import org.springframework.transaction.annotation.Transactional;
import java.io.*;
import java.net.HttpURLConnection;
import java.net.URL;
import java.nio.charset.StandardCharsets;
import java.text.SimpleDateFormat;
import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;
import java.util.*;
@Component
public class ItemDangerReportScheduleTest {
    private final Logger logger = LoggerFactory.getLogger(this.getClass());
    @Autowired
    private TbSpecialCheckItemLogMapper itemLogMapper;
    @Autowired
    private TbSpecialCheckScoreAndDangerLogMapper scoreAndDangerLogMapper;
    String token = "GT6gGJV7JV";
    String key = "Bv+NeBolwqg2Pbc1yVwrZA==";
    String iv = "4QC9V8eAiB7tdlgBkMsTAw==";
    public static final int MAC_BIT_SIZE = 128;
    public static String encrypt(String plainText, byte[] key, byte[] iv) {
        String sr;
        try {
            byte[] plainBytes = plainText.getBytes(StandardCharsets.UTF_8);
            GCMBlockCipher cipher = new GCMBlockCipher(new
                    AESFastEngine());
            AEADParameters parameters =
                    new AEADParameters(new KeyParameter(key),
                            MAC_BIT_SIZE, iv, null);
            cipher.init(true, parameters);
            byte[] encryptedBytes = new
                    byte[cipher.getOutputSize(plainBytes.length)];
            int retLen = cipher.processBytes(plainBytes, 0, plainBytes.length,
                    encryptedBytes, 0);
            cipher.doFinal(encryptedBytes, retLen);
            sr = Base64.getEncoder().encodeToString(encryptedBytes);
        } catch (Exception ex) {
            throw new RuntimeException(ex.getMessage());
        }
        return sr;
    }
    @Transactional
//    @Scheduled(cron = "0 0 23 * * ?")    //每天晚上23点执行一次0 0 22,23 * * ?
//    @Scheduled(cron = "0 0 22,23 * * ?")    //每天晚上22、23点执行一次
//    @Scheduled(cron = "0 0/1 * * * ? ")    // 分钟
//    @Scheduled(cron = "0 0/35 * * * ? ")    // 分钟
//    @Scheduled(cron = "0/5 * * * * ?")
    public void execReportDateSchedule() throws UnsupportedEncodingException {
        logger.info("【####】上报数据开始...");
        HttpURLConnection con = null;
        BufferedReader buffer = null;
        int responseCode = 200;
//        SimpleDateFormat dateFormatCheckData= new SimpleDateFormat("yyyy-MM-dd");
//        SimpleDateFormat dateFormat= new SimpleDateFormat("yyyy-MM-dd HH:MM:ss");
        DateTimeFormatter dateFormatCheckData = DateTimeFormatter.ofPattern("yyyy-MM-dd");
        DateTimeFormatter dateFormat = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:MM:ss");
        Date date = new Date();
        //格式化时间,作为token的时间戳
        SimpleDateFormat tokenDate= new SimpleDateFormat("yyyyMMddHHmmssSSS");
        String formatDate = tokenDate.format(date);
        logger.info("【token时间】" + formatDate);
        /**
         * 1、检查项与隐患上报
         * */
        System.out.println("【2】检查项与隐患上报-处理数据...");
//        List<TbSpecialCheckTaskLog> CJRiskAnaUnits = .listReportUnitDate();
        List<TbSpecialCheckScoreAndDangerLog> scoreAndDangerLogs = scoreAndDangerLogMapper.listScoreAndDangerLogReportList();
        if (ObjectUtils.isNotEmpty(scoreAndDangerLogs)){
            //封装上报数据,时间格式化
            List<SpecialCheckItemDangerLogReportDTO> list = new ArrayList<>();
            for (TbSpecialCheckScoreAndDangerLog scoreAndDangerLog : scoreAndDangerLogs) {
                SpecialCheckItemDangerLogReportDTO specialCheckItemDangerLogReportDTO = BeanCopyUtils.copyBean(scoreAndDangerLog, SpecialCheckItemDangerLogReportDTO.class);
                specialCheckItemDangerLogReportDTO.setCreateDate(dateFormat.format(scoreAndDangerLog.getCreateDate()));
                specialCheckItemDangerLogReportDTO.setUpdateDate(dateFormat.format(scoreAndDangerLog.getUpdateDate()));
                specialCheckItemDangerLogReportDTO.setCheckDate(dateFormatCheckData.format(scoreAndDangerLog.getCheckDate()));
                specialCheckItemDangerLogReportDTO.setDeleted(scoreAndDangerLog.getDeleted().toString());
                list.add(specialCheckItemDangerLogReportDTO);
            }
//            logger.info("【【原始数据】】" + JSONObject.toJSONString(list));
            System.out.println("【【原始数据】】" + JSONObject.toJSONString(list));
            String AESReportUnitDate = encrypt(JSONObject.toJSONString(list), key.getBytes(), iv.getBytes());
//            logger.info("【【AES加密结果】】"+ AESReportUnitDate);![](C:/Users/Ad![](C:/Users/Admin/Desktop/所有桌面文件存放/9}AM56M10$6WX){0JRK`$6X.jpg)min/Desktop/所有桌面文件存放/9}AM56M10$6WX){0JRK`$6X.jpg)
            StringBuffer itemDangerResultBuffer = null;
            //上报数据
             try {
             URL url = new URL("http://120.71.182.198:9999/v1/data/receive/receiveCheckDangerList");
             //得到连接对象
             con = (HttpURLConnection) url.openConnection();
             //设置请求类型
             con.setRequestMethod("POST");
             //允许写出
             con.setDoOutput(true);
             //允许读入
             con.setDoInput(true);
             //不使用缓存
             con.setUseCaches(false);
             con.setInstanceFollowRedirects(true);
             //设置请求头
             con.setRequestProperty("X-Access-Token",token+formatDate.toString());
             //设置Content-Type,此处根据实际情况确定
             con.setRequestProperty("Content-Type", "application/json;charset=utf8");
             OutputStream os = con.getOutputStream();
             Map paraMap = new HashMap();
             paraMap.put("data", AESReportUnitDate); /**封装数据*/
                System.out.println("【【加密请求体】】" +  JSONArray.toJSON(paraMap).toString());
                //组装入参,设置请求体
                os.write(JSON.toJSONString(paraMap).getBytes());
                //本段日志,测试成功后,可注释掉
                if (responseCode == HttpURLConnection.HTTP_OK) {
                    //得到响应流
                    InputStream inputStream = con.getInputStream();
                    //将响应流转换成字符串
                    itemDangerResultBuffer = new StringBuffer();
                    String line;
                    buffer = new BufferedReader(new InputStreamReader(inputStream, "UTF-8"));
                    while ((line = buffer.readLine()) != null) {
                        itemDangerResultBuffer.append(line);
                    }
//                    logger.info("result:" + unitResultBuffer.toString());
                    System.out.println("result:" + itemDangerResultBuffer.toString());
                }
            } catch (Exception e) {
                e.printStackTrace();
            }
            //接收返回值,保存返回值
            CJReportResultData CJOverhaulLogResult = JSONObject.parseObject(itemDangerResultBuffer.toString(), CJReportResultData.class);
            //接收返回值,保存返回值
            List<String> errorIdList= new ArrayList<>();
            Data data = JSONObject.parseObject(CJOverhaulLogResult.getData(), Data.class);
            //保存失败结果
            if (ObjectUtils.isNotEmpty(data)){
                for (ErrorMsgDetails errorMsgDetail : data.getErrorMsgDetails()) {
                    errorIdList.add(errorMsgDetail.getId());
                }
            }
            LocalDateTime nowDate = LocalDateTime.now();
            logger.info("【检查项与隐患】数据上报结果:" + CJOverhaulLogResult.getCode());
            //设置所有数据上报成功
            if (CJOverhaulLogResult.getCode().equals(CJReportEnum.SUCCESS.getCode()) || CJOverhaulLogResult.getCode().equals(CJReportEnum.SUCESS_AND_FAIL.getCode())){
                for (TbSpecialCheckScoreAndDangerLog itemDangerLo : scoreAndDangerLogs) {
                    HandlerSpecialCheckReportParam handlerCJReportParam = new HandlerSpecialCheckReportParam();
                    //封装数据
                    handlerCJReportParam.setId(itemDangerLo.getId());
                    handlerCJReportParam.setReportStatus(SyncEnum.SYNC_EXEC_SUCCESS.getCode());
                    handlerCJReportParam.setReportTime(nowDate);
                    scoreAndDangerLogMapper.updateItemDangerReportStatusById(handlerCJReportParam);
                }
            }
            //设置上报失败的数据,上报失败
            if (CJOverhaulLogResult.getCode().equals(CJReportEnum.FAIL.getCode()) || CJOverhaulLogResult.getCode().equals(CJReportEnum.SUCESS_AND_FAIL.getCode())){
                for (String failId : errorIdList) {
                    //上报失败
                    HandlerSpecialCheckReportParam handlerCJReportParam = new HandlerSpecialCheckReportParam();
                    //封装数据
                    handlerCJReportParam.setId(failId);
                    handlerCJReportParam.setReportStatus(SyncEnum.SYNC_EXEC_FAIL.getCode());
                    handlerCJReportParam.setReportTime(nowDate);
                    scoreAndDangerLogMapper.updateItemDangerReportStatusById(handlerCJReportParam);
                }
            }
//            logger.info("【1】安全风险分析单元-数据处理完成");
            System.out.println("【检查项与隐患】-数据处理完成");
        }else {
//            logger.info("【1】风险分析单元-无数据上报昌吉州");
            System.out.println("【检查项与隐患】-无数据上报昌吉州");
        }
    }
}