zhanghua
2025-03-04 0f5901bbc027e2e8d934280ca659734a61f67378
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
package com.ycl.task;
 
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.ycl.common.constant.GlobalQueue;
import com.ycl.entity.video.VideoAlarmReport;
import com.ycl.entity.video.VideoPoint;
import com.ycl.service.caseHandler.IBaseCaseService;
import com.ycl.service.caseHandler.IViolationsService;
import com.ycl.service.oss.OssService;
import com.ycl.service.resources.IImageResourcesService;
import com.ycl.service.video.IVideoAlarmReportService;
import com.ycl.service.video.impl.IVideoPointService;
import com.ycl.vo.casePool.BaseCaseVO;
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.ByteArrayInputStream;
import java.io.InputStream;
import java.io.UnsupportedEncodingException;
import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;
import java.util.ArrayList;
import java.util.List;
 
@Component
public class SynchronizeDHAlarm {
 
    @Autowired
    private OssService ossService;
 
    @Autowired
    private IVideoPointService videoPointService;
 
    @Autowired
    private IVideoAlarmReportService videoAlarmReportService;
 
    @Autowired
    private IViolationsService violationsService;
 
    @Autowired
    private IBaseCaseService baseCaseService;
 
    @Autowired
    private IImageResourcesService imageService;
 
    @Scheduled(cron = "0 */1 * * * ?")
    @Transactional(rollbackFor = Exception.class)
    public void scheduledTask() {
 
        while (GlobalQueue.size() > 0) {
            try {
                VideoAlarmReport videoAlarmReport = GlobalQueue.dequeue();
 
                List<VideoAlarmReport> list = videoAlarmReportService.findByChannelAndAlarmNameAndTime(videoAlarmReport.getChannel(), videoAlarmReport.getAlarmName(), videoAlarmReport.getAlarmTime());
                if (list.size() == 0) {
                    System.out.println("----------------开始保存报警信息:" + videoAlarmReport.getAlarmName());
                    VideoPoint videoPoint = videoPointService.getByChannelId(Integer.parseInt(videoAlarmReport.getChannel()));
 
                    if (videoPoint != null) {
                        videoAlarmReport.setPlatResourceId(videoPoint.getPlatResourceId());
                    }
                    if (videoAlarmReport.getPicByte() != null) {
                        String extension = "jpg";
                        InputStream inputStream = new ByteArrayInputStream(videoAlarmReport.getPicByte());
                        String picData = ossService.uploadImages(inputStream, extension, 0);
 
                        videoAlarmReport.setPicData(picData);
                    }
                    videoAlarmReportService.save(videoAlarmReport);
 
                    List<VideoAlarmReport> videoAlarmReports = new ArrayList<>();
                    videoAlarmReports.add(videoAlarmReport);
                    violationsService.saveFromVideo(videoAlarmReports);
 
                }
            } catch (Exception ex) {
                System.out.println("------------保存报警信息异常:");
 
            }
        }
    }
 
 
    @Scheduled(cron = "0 0 15 * * ?")
    @Transactional(rollbackFor = Exception.class)
    public void deleteAlarmTask() {
        System.out.println("----------------执行清除数据任务:");
        int i = 1;
        while (true) {
            String beginTime = "2020-01-01 00:00:00";
            String endTime = LocalDateTime.now().plusDays(-3).format(DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss"));
            Page<BaseCaseVO> page = baseCaseService.selectVideoInspection(i, 10, null, null, beginTime, endTime, null);
 
            System.out.println("----------------清除数据任务总条数:" + page.getTotal());
            if (page.getRecords().size() > 0) {
                page.getRecords().forEach(baseCaseVO -> {
                    String picData = baseCaseVO.getPicData();
                    String[] urls = picData.split(",");
                    for (String url : urls) {
                        try {
                            ossService.deleteImages(url);
                        } catch (UnsupportedEncodingException e) {
                            throw new RuntimeException(e);
                        }
                    }
                    baseCaseService.removeById(baseCaseVO.getBaseId());
                    imageService.removeById(baseCaseVO.getImageId());
 
                    System.out.println("----------------清除数据数据成功,id:" + baseCaseVO.getBaseId());
                });
            } else {
                break;
            }
            i++;
        }
    }
}