双重预防项目-国泰新华二开定制版
heheng
2025-06-24 e98eeaaa5766511fdb8e6d5e412eb1c59d1f07ce
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
package com.ruoyi.project.tr.specialCheck.scheduleLogTask;
 
import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.core.toolkit.ObjectUtils;
import com.ruoyi.project.tr.riskList.service.IRiskListService;
import com.ruoyi.project.tr.specialCheck.domin.DownloadDTO.*;
import com.ruoyi.project.tr.specialCheck.domin.TbBaseCheckItem;
import com.ruoyi.project.tr.specialCheck.domin.TbBaseCheckScore;
import com.ruoyi.project.tr.specialCheck.domin.TbBaseCheckTask;
import com.ruoyi.project.tr.specialCheck.domin.TbCheckConfig;
import com.ruoyi.project.tr.specialCheck.mapper.*;
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.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.LocalDate;
import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;
import java.util.*;
 
@Component
public class SpecialCheckTaskDownload {
 
    private final Logger logger = LoggerFactory.getLogger(this.getClass());
 
    @Autowired
    private TbBaseCheckCompanyMapper companyTbRepository;
    @Autowired
    private TbBaseCheckItemMapper itemTbRepository;
    @Autowired
    private TbBaseCheckScoreMapper scoreTbRepository;
    @Autowired
    private TbBaseCheckTaskMapper taskTbRepository;
    @Autowired
    private TbSpecialCheckTaskLogMapper taskSpecialLogMapper;
    @Autowired
    private IRiskListService riskListService;
    @Autowired
    private TbCheckConfigMapper configMapper;
 
    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;
    }
    public static String decrypt(String encryptedText, byte[] key, byte[] iv) {
        String sr;
        try {
            byte[] encryptedBytes = Base64.getDecoder().decode(encryptedText);
            GCMBlockCipher cipher = new GCMBlockCipher(new AESFastEngine());
            AEADParameters parameters = new AEADParameters(new KeyParameter(key), MAC_BIT_SIZE, iv, null);
            cipher.init(false, parameters);
            byte[] decryptedBytes = new byte[cipher.getOutputSize(encryptedBytes.length)];
            int retLen = cipher.processBytes(encryptedBytes, 0, encryptedBytes.length, decryptedBytes, 0);
            cipher.doFinal(decryptedBytes, retLen);
            sr = new String(decryptedBytes, StandardCharsets.UTF_8);
        } 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/15 * * * * ?")
    public void execReportDateSchedule() throws UnsupportedEncodingException {
        logger.info("【####】拉取专项检查数据开始...");
 
        HttpURLConnection con = null;
        BufferedReader buffer = null;
        int responseCode = 200;
 
        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);
 
        // 获取当前日期
        LocalDate currentDate = LocalDate.now();
 
        // 获取今年的1月1号
        LocalDate startOfYear = LocalDate.of(currentDate.getYear(), 1, 1);
 
 
        //String taskId = "84766708-9b18-4133-8acf-08aba67792c7";
 
        String taskId = "a8de03f0-4c3e-43fe-a139-0e4ed34edd35";
        /**
         * 1
         * */
//        logger.info("【####】拉取专项任务数据开始...");
        TbCheckConfig specialCheckConfig = configMapper.getSpecialCheckConfig();
        if (specialCheckConfig.getStatus() == 2){
 
            StringBuffer specialCheckBuffer = null;
 
            //上报数据
             try {
             URL url = new URL("http://120.71.182.198:9999/v1/data/receive/getCheckTask");
             //得到连接对象
             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();
 
                 System.out.println("token:"+token+formatDate.toString());
 
            //本段日志,测试成功后,可注释掉
            if (responseCode == HttpURLConnection.HTTP_OK) {
                //得到响应流
                InputStream inputStream = con.getInputStream();
                //将响应流转换成字符串
                specialCheckBuffer = new StringBuffer();
                String line;
                buffer = new BufferedReader(new InputStreamReader(inputStream, "UTF-8"));
                while ((line = buffer.readLine()) != null) {
                    specialCheckBuffer.append(line);
                }
//                    logger.info("result:" + unitResultBuffer.toString());
                System.out.println("result:" + specialCheckBuffer.toString());
            }
 
            } catch (Exception e) {
                e.printStackTrace();
            }
 
            //接收返回值,保存返回值
            TbBaseCheckTaskDownloadBO checkTaskDownloadDTO = JSONObject.parseObject(specialCheckBuffer.toString(), TbBaseCheckTaskDownloadBO.class);
            //接收返回值,保存返回值
            List<TbBaseCheckTask> tbBaseCheckTasks = JSONArray.parseArray(checkTaskDownloadDTO.getData(), TbBaseCheckTask.class);
            if (tbBaseCheckTasks.size() > 0){
                for (TbBaseCheckTask tbBaseCheckTask : tbBaseCheckTasks) {
                    LocalDateTime taskStartTime = tbBaseCheckTask.getTaskStartTime();
                    LocalDate localDate = taskStartTime.toLocalDate();
                    if (localDate.isAfter(startOfYear)) {
                        logger.info("当前日期大于今年的1月1号");
                        tbBaseCheckTask.setStatus(1);
                        tbBaseCheckTask.setId(tbBaseCheckTask.getTaskId());
                        taskId = tbBaseCheckTask.getTaskId();
 
                        int insert = taskTbRepository.insert(tbBaseCheckTask);
 
                    }
                }
            }
 
            logger.info("【专项检查任务】-读取任务结果:" + checkTaskDownloadDTO.getCode());
            System.out.println("【专项检查任务】-读取任务完成");
        }else {
            System.out.println("【专项检查任务】- 不读取任务");
        }
 
        /**
         * 2
         * */
        logger.info("【####】拉取检查项数据开始...");
        if (specialCheckConfig.getStatus() == 2){
 
            StringBuffer specialCheckBuffer = null;
 
            ItemDownloadRespDTO itemDownloadRespDTO = new ItemDownloadRespDTO();
            TbBaseCheckTask baseCheckTask = taskTbRepository.getBaseCheckTaskByStatus(date);
            Page page = new Page();
            page.setCurrent(String.valueOf(1));
            page.setSize(String.valueOf(1000));
 
            itemDownloadRespDTO.setTaskId(taskId);
            itemDownloadRespDTO.setPage(page);
            System.out.println("请求参数:" + JSONObject.toJSONString(itemDownloadRespDTO));
 
            JSONObject itemDownloadRespDTOJson = new JSONObject();
            itemDownloadRespDTOJson.put("taskId",taskId);
            itemDownloadRespDTOJson.put("page",page);
            System.out.println("【【原始数据2】】" + JSONObject.toJSONString(itemDownloadRespDTOJson));
            String encrypted = encrypt(JSONObject.toJSONString(itemDownloadRespDTOJson), key.getBytes(), iv.getBytes());
            System.out.println("【【加密数据2】】" + encrypted);
            //加密请求数据
            String AESReportUnitDate = encrypt(JSONObject.toJSONString(itemDownloadRespDTO), key.getBytes(), iv.getBytes());
            System.out.println("【【加密数据2】】" + AESReportUnitDate);
            //上报数据
            try {
                URL url = new URL("http://120.71.182.198:9999/v1/data/receive/getCheckItem");
                //得到连接对象
                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", encrypted); /**封装数据*/
               // System.out.println("【【加密请求体】】" +  JSONArray.toJSON(paraMap).toString());
                //组装入参,设置请求体
                os.write(JSON.toJSONString(itemDownloadRespDTOJson).getBytes());
 
                //本段日志,测试成功后,可注释掉
                if (responseCode == HttpURLConnection.HTTP_OK) {
                    //得到响应流
                    InputStream inputStream = con.getInputStream();
                    //将响应流转换成字符串
                    specialCheckBuffer = new StringBuffer();
                    String line;
                    buffer = new BufferedReader(new InputStreamReader(inputStream, "UTF-8"));
                    while ((line = buffer.readLine()) != null) {
                        specialCheckBuffer.append(line);
                    }
//                    logger.info("result:" + unitResultBuffer.toString());
                    System.out.println("result:" + specialCheckBuffer.toString());
                }
 
            } catch (Exception e) {
                e.printStackTrace();
            }
 
            //接收返回值,保存返回值
            ItemDownloadRespBO itemDownloadRespBO = JSONObject.parseObject(specialCheckBuffer.toString(), ItemDownloadRespBO.class);
            //接收返回值,保存返回值
            String data = itemDownloadRespBO.getData();
            if (ObjectUtils.isNotEmpty(data)){
                JSONObject jsonObject = JSON.parseObject(data);
                String data1 = jsonObject.get("data").toString();
                List<TbBaseCheckItem> tbBaseCheckItems = JSONObject.parseArray(data1, TbBaseCheckItem.class);
                    if (ObjectUtils.isNotEmpty(tbBaseCheckItems)){
                        for (TbBaseCheckItem item : tbBaseCheckItems) {
                            item.setStatus(1);
                            item.setId(item.getCheckItemId());
                            int insert = itemTbRepository.insertBatch(item);
                        }
 
                    }
            }
 
            taskTbRepository.updateStatusById(baseCheckTask.getTaskId());
 
            logger.info("【专项任务检查项】-读取检查项结果:" + itemDownloadRespBO.getCode());
            System.out.println("【专项任务检查项】-读取检查项完成");
        }else {
            System.out.println("【专项任务检查项】- 不读取检查项");
        }
 
        /**
         * 3
         * */
        logger.info("【####】拉取评分细则开始...");
        if (specialCheckConfig.getStatus() == 2){
 
            StringBuffer specialCheckBuffer = null;
 
            ScoreDownloadRespDTO scoreDownloadRespDTO = new ScoreDownloadRespDTO();
            TbBaseCheckItem baseItemByStatus = itemTbRepository.getBaseItemByStatus(date);
            Page page = new Page();
            page.setCurrent(String.valueOf(1));
            page.setSize(String.valueOf(1000));
 
            scoreDownloadRespDTO.setTaskId(baseItemByStatus.getTaskId());
            scoreDownloadRespDTO.setCheckItemId(baseItemByStatus.getId());
            scoreDownloadRespDTO.setPage(page);
            System.out.println("【【加密前请求体】】" +JSON.toJSONString(scoreDownloadRespDTO));
            //加密请求数据
            String AESReportUnitDate = encrypt(JSON.toJSONString(scoreDownloadRespDTO), key.getBytes(), iv.getBytes());
            System.out.println("【【加密请求体】】" +AESReportUnitDate);
            String decrypt = decrypt(AESReportUnitDate, key.getBytes(), iv.getBytes());
            System.out.println("【【解密请求体】】" +decrypt);
            //上报数据
            try {
                URL url = new URL("http://120.71.182.198:9999/v1/data/receive/getCheckScore");
                //得到连接对象
                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(scoreDownloadRespDTO).getBytes());
 
                //本段日志,测试成功后,可注释掉
                if (responseCode == HttpURLConnection.HTTP_OK) {
                    //得到响应流
                    InputStream inputStream = con.getInputStream();
                    //将响应流转换成字符串
                    specialCheckBuffer = new StringBuffer();
                    String line;
                    buffer = new BufferedReader(new InputStreamReader(inputStream, "UTF-8"));
                    while ((line = buffer.readLine()) != null) {
                        specialCheckBuffer.append(line);
                    }
//                    logger.info("result:" + unitResultBuffer.toString());
                    System.out.println("result:" + specialCheckBuffer.toString());
                }
 
            } catch (Exception e) {
                e.printStackTrace();
            }
 
            //接收返回值,保存返回值
            ScoreDownloadRespBO scoreDownloadRespBO = JSONObject.parseObject(specialCheckBuffer.toString(), ScoreDownloadRespBO.class);
 
            String data = scoreDownloadRespBO.getData();
            if (ObjectUtils.isNotEmpty(data)){
                JSONObject jsonObject = JSON.parseObject(data);
                String data1 = jsonObject.get("data").toString();
                List<TbBaseCheckScore> tbBaseCheckScores = JSONObject.parseArray(data1, TbBaseCheckScore.class);
                    if (ObjectUtils.isNotEmpty(tbBaseCheckScores)){
                        for (TbBaseCheckScore item : tbBaseCheckScores) {
                            item.setId(item.getScoreId());
                            int insert = scoreTbRepository.save(item);
                        }
 
                    }
            }
 
            itemTbRepository.updateStatusById(baseItemByStatus.getId());
 
            logger.info("【评分细则】-读取结果:" + scoreDownloadRespBO.getCode());
            System.out.println("【评分细则】-读取完成");
        }else {
            System.out.println("【评分细则】- 不读取");
        }
 
 
    }
}