吉安感知网项目-后端
linwei
2026-06-04 ad935c07a6cabf05fed9c615c7bd4f67f6d65293
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
package org.sxkj.resource.feign;
 
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import lombok.AllArgsConstructor;
import lombok.SneakyThrows;
import lombok.extern.slf4j.Slf4j;
import org.springblade.core.oss.model.BladeFile;
import org.springblade.core.oss.model.OssFile;
import org.springblade.core.tenant.annotation.NonDS;
import org.springblade.core.tool.utils.FileUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.mock.web.MockMultipartFile;
import org.springframework.scheduling.annotation.Async;
import org.springframework.util.CollectionUtils;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;
import org.sxkj.common.utils.HeaderUtils;
import org.sxkj.common.utils.ffmpeg.FfmpegConverterUtils;
import org.sxkj.resource.builder.OssBuilder;
import org.sxkj.resource.entity.Attach;
import org.sxkj.resource.model.MinioPojo;
import org.sxkj.resource.service.IAttachService;
import org.sxkj.resource.vo.AttachTypeStatisticsVO;
import org.sxkj.resource.vo.AttachVO;
 
import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.util.*;
 
/**
 * 附件远程调用服务
 *
 * @author Chill
 */
@NonDS
@RestController
@AllArgsConstructor
@Slf4j
public class AttachClient implements IAttachClient {
 
    @Autowired
    private IAttachService attachService;
 
    @Autowired
    private OssBuilder ossBuilder;
 
    @Autowired
    private MinioPojo pojo;
 
    /**
     * 保存附件表信息
     *
     * @param attach 附件表信息
     * @return
     */
    @Override
    public Boolean saveAttachInfo(Attach attach) {
        return attachService.saveAttachInfo(attach);
    }
 
    /**
     * 获取任务下的图片信息
     * @param jobs
     * @return
     */
    public List<AttachVO> getAttachData(@RequestParam String jobs) {
        AttachVO attach = new AttachVO();
        attach.setWayLineJobId(jobs);
        attach.setOrderByCreateTime(true);
        attach.setAreaCode(HeaderUtils.getAreaCode());
 
        List<String> integers = Arrays.asList("0", "2", "4", "5");
        attach.setResultTypes(integers);
        List<String> resultTypes = attach.getResultTypes();
        String wordOrderType = attach.getWordOrderType();
        settingResultType(attach, wordOrderType, resultTypes);
 
        if (Objects.nonNull(attach.getAiStatus()) && Objects.nonNull(attach.getResultTypes())
            && attach.getResultTypes().contains(Attach.RESULT_TYPE_AI)) {
            attach.setResultTypes(Arrays.asList(Attach.RESULT_TYPE_AI));
        }
 
        List<String> aiEventMd5s = attachService.findAiEventMd5s(attach.getEventRecordIds());
        attach.setMd5s(aiEventMd5s);
 
        if (!CollectionUtils.isEmpty(attach.getEventRecordIds())) {
            attach.setResultTypes(Arrays.asList(Attach.RESULT_TYPE_AI, Attach.RESULT_TYPE_IMG));
        }
 
        // 使用大分页获取所有数据
        Page<AttachVO> page = new Page<>(1, 2000); // 或者 Integer.MAX_VALUE
        List<AttachVO> aiAttachImages = attachService.findAiAttachImages(page, attach);
 
        return aiAttachImages;
    }
 
    /**
     * 设置结果类型
     * @param attach
     * @param wordOrderType
     * @param resultTypes
     */
    private static void settingResultType(AttachVO attach, String wordOrderType, List<String> resultTypes) {
        if (!StringUtils.isEmpty(wordOrderType) && !CollectionUtils.isEmpty(resultTypes)) {
            if ("-1".equals(wordOrderType)) {
                attach.setWordOrderType(null);
                if (resultTypes.contains(Attach.RESULT_TYPE_IMG)) {
                    resultTypes = Arrays.asList(Attach.RESULT_TYPE_IMG);
                }
            } else {
                if (resultTypes.contains(Attach.RESULT_TYPE_AI)) {
                    resultTypes = Arrays.asList(Attach.RESULT_TYPE_AI);
                }
            }
            attach.setResultTypes(resultTypes);
        }
    }
    @Override
    public BladeFile saveAttachFile(MultipartFile file, String fileName) throws IOException {
        BladeFile bladeFile = ossBuilder.template().putFile(pojo.getBucket(),fileName, file.getInputStream());
        Long attachId = buildAttach(fileName, file.getSize(), bladeFile, null);
        bladeFile.setAttachId(attachId);
        return bladeFile;
    }
 
    @Override
    public BladeFile saveAttachFile(MultipartFile file, String fileName, String type) throws IOException {
        BladeFile bladeFile = ossBuilder.template().putFile(pojo.getBucket(),fileName, file.getInputStream());
        Long attachId = buildAttach(fileName, file.getSize(), bladeFile, type);
        bladeFile.setAttachId(attachId);
        return bladeFile;
    }
 
    /**
     * 附件上传
     * @param file 文件
     * @param fileName 文件名称
     * @return
     * @throws IOException
     */
    @Override
    public BladeFile putFile(MultipartFile file, String fileName) throws IOException {
        BladeFile bladeFile = ossBuilder.template().putFile(fileName, file.getInputStream());
        return bladeFile;
    }
 
    private Long buildAttach(String fileName, Long fileSize, BladeFile bladeFile, String type) {
        String fileExtension = FileUtil.getFileExtension(fileName);
        Attach attach = new Attach();
        attach.setDomainUrl(bladeFile.getDomain());
        attach.setLink(bladeFile.getLink());
        attach.setName(bladeFile.getName());
        attach.setOriginalName(bladeFile.getOriginalName());
        attach.setAttachSize(fileSize);
        attach.setExtension(fileExtension);
        if (null != type) {
            attach.setResultType(type);
        }
        attachService.save(attach);
        return attach.getId();
    }
 
    /**
     * 根据附件ID集合获取去除 cloud-bucket 前缀后的 name 列表
     *
     * @param ids 附件ID集合
     * @return 处理后的 name 列表
     */
    @Override
    public List<String> getAttachNames(List<Long> ids) {
        return attachService.getAttachNames(ids);
    }
 
    @Override
    public Map<String, Object> findMetaDataByName(List<String> names) {
        return attachService.findMetaDataByName(names);
    }
 
 
    /**
     * 查询成果数量
     * @param jobId 任务id
     * @return 成果数量
     */
    @Override
    public Long findResultNumByJobId(@RequestParam  String jobId){
        return attachService.findResultNumByJobId(jobId);
    }
 
    /**
     * 通过md5查询附件信息
     * @param md5
     * @return
     */
    @Override
    public Attach getAttachByMd5(@RequestParam String md5) {
        List<Attach> list = attachService.list(new LambdaQueryWrapper<Attach>().eq(Attach::getMd5, md5));
        return (list != null && !list.isEmpty()) ? list.get(0) : null;
    }
 
    @Override
    public Attach getAttachById(String id) {
        List<Attach> list = attachService.list(new LambdaQueryWrapper<Attach>().eq(Attach::getId, id));
        return (list != null && !list.isEmpty()) ? list.get(0) : null;
    }
 
    @SneakyThrows
    @Override
    @Async
    public void processingYesterdayVideo(Date startTime, Date endTime) {
        // 查询昨日视频
        List<Attach> list = attachService.findAttachInfoByYesterday(1, startTime, endTime);
        log.info("昨日视频数量:" + list.size());
//
        for (int i = 0; i < list.size(); i++) {
            log.info("正在处理第" + (i + 1) + "个视频...");
            Attach attach = list.get(i);
            // 获取视频流
            OssFile ossFile = ossBuilder.template("minio").statFile(pojo.getBucket(), attach.getName().replace(pojo.getBucket() + "/", ""));
 
            String minioPath = pojo.getServerAddress();
            String name = addSmallSuffix(attach.getName(), "_show");
            String path = ossFile.getName().replace(pojo.getBucket() + "/", "");
 
            String mp4TempPath = minioPath + "/temp/" + UUID.randomUUID().toString().toLowerCase();
            // 开始压缩视频
            log.info("原始视频地址:" + minioPath +  path);
            File mediaFile = FfmpegConverterUtils.compressVideo(new File(minioPath +  path), 5, mp4TempPath);
            log.info("第" + (i + 1) + "个视频处理完成...{}", mediaFile.getName());
 
            saveAttachFile(new MockMultipartFile( mediaFile.getName(),
                mediaFile.getName(),
                "application/octet-stream",
                new FileInputStream(mediaFile)), name, Attach.RESULT_TYPE_VEDIO_SHOW);
            log.info("第{}个视频保存入库完成...", (i + 1));
 
        }
        log.debug("昨日视频处理完成...");
    }
 
    private static String addSmallSuffix(String fileName, String suffix) {
        int lastDotIndex = fileName.lastIndexOf(".");
        if (lastDotIndex != -1) {
            // 将文件名分成名称和扩展名两部分
            String name = fileName.substring(0, lastDotIndex);
            String extension = fileName.substring(lastDotIndex);
            // 在名称后面加上_small
            return name + suffix + extension;
        }
        // 如果没有扩展名,直接在末尾加上_small
        return fileName + suffix;
    }
 
    /**
     *
     * @param deviceSn 设备sn
     * @param timestamp 时间戳
     * @param operator 操作人
     * @param workspaceId 工作空间id
     * @return
     */
    @Override
    public Boolean deleteAttach(String deviceSn, long timestamp, Long operator, String workspaceId) {
        return attachService.deleteAttach(deviceSn, timestamp, operator, workspaceId);
     }
 
    /**
     *
     * @param deviceSn
     * @param startDate
     * @param endDate
     * @param resultTypes
     * @param areaCode
     * @return
     */
    @Override
    public List<AttachTypeStatisticsVO> calculateTheThreeDimensionalArea(String deviceSn, String startDate, String endDate, String resultTypes, String areaCode) {
        List<AttachTypeStatisticsVO> attachTypeStatisticsVO = attachService.calculateTheThreeDimensionalArea(deviceSn, startDate, endDate, resultTypes, areaCode);
        return attachTypeStatisticsVO;
    }
}