吉安感知网项目-后端
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
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
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
/*
 *      Copyright (c) 2018-2028, Chill Zhuang All rights reserved.
 *
 *  Redistribution and use in source and binary forms, with or without
 *  modification, are permitted provided that the following conditions are met:
 *
 *  Redistributions of source code must retain the above copyright notice,
 *  this list of conditions and the following disclaimer.
 *  Redistributions in binary form must reproduce the above copyright
 *  notice, this list of conditions and the following disclaimer in the
 *  documentation and/or other materials provided with the distribution.
 *  Neither the name of the dreamlu.net developer nor the names of its
 *  contributors may be used to endorse or promote products derived from
 *  this software without specific prior written permission.
 *  Author: Chill 庄骞 (smallchill@163.com)
 */
package org.sxkj.resource.service.impl;
 
import com.alibaba.fastjson.JSON;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import io.minio.GetObjectArgs;
import io.minio.MinioClient;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.apache.ibatis.annotations.Param;
import org.springblade.core.mp.base.BaseServiceImpl;
import org.springblade.core.secure.utils.AuthUtil;
import org.springblade.core.tool.api.R;
import org.springblade.core.tool.utils.BeanUtil;
import org.springblade.core.tool.utils.Func;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;
import org.springframework.util.CollectionUtils;
import org.springframework.util.ObjectUtils;
import org.sxkj.common.constant.CacheConstant;
import org.sxkj.common.enums.AttachResultTypeEnum;
import org.sxkj.common.enums.TypeOfOutcome;
import org.sxkj.common.func.Streams;
import org.sxkj.common.model.ResponseResult;
import org.sxkj.common.query.PaginationUtils;
import org.sxkj.common.redis.RedisOpsUtils;
import org.sxkj.common.utils.*;
import org.sxkj.resource.builder.DevicePermissionBuilder;
import org.sxkj.resource.builder.OssBuilder;
import org.sxkj.resource.dto.WaylineJobInfoQueryParam;
import org.sxkj.resource.entity.Attach;
import org.sxkj.resource.entity.Oss;
import org.sxkj.resource.mapper.AttachMapper;
import org.sxkj.resource.model.FileMetadataDTO;
import org.sxkj.resource.model.MinioPojo;
import org.sxkj.resource.model.PositionDTO;
import org.sxkj.resource.param.AttachPageParam;
import org.sxkj.resource.service.IAttachService;
import org.sxkj.resource.util.MinioFileDownloader;
import org.sxkj.resource.vo.*;
import org.sxkj.system.cache.SysCache;
import org.sxkj.system.feign.ISysClient;
import org.sxkj.system.vo.TreeVo;
import org.sxkj.tools.feign.ITifConvertClient;
import org.sxkj.tools.model.TifConvertResult;
 
import java.io.File;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.time.LocalDateTime;
import java.time.LocalTime;
import java.time.ZoneId;
import java.util.*;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.TimeUnit;
import java.util.stream.Collectors;
import java.util.zip.Deflater;
import java.util.zip.ZipEntry;
import java.util.zip.ZipOutputStream;
 
/**
 * 附件表 服务实现类
 *
 * @author Chill
 */
@Service
@Slf4j
public class AttachServiceImpl extends BaseServiceImpl<AttachMapper, Attach> implements IAttachService {
 
    /**
     * 对象存储构建类
     */
    @Autowired
    private OssBuilder ossBuilder;
    @Autowired
    private ISysClient sysClient;
 
    @Autowired
    private MinioPojo minioPojo;
 
    @Autowired
    private DevicePermissionBuilder permissionBuilder;
 
    @Autowired
    private ITifConvertClient tifConvertClient;
 
    @Value("${odm.down.temp.zipBasePath}")
    private String localSaveDir;
 
    @Value("${odm.down.temp.zipPreFix}")
    private String downPath;
 
 
    @Override
    public IPage<AttachVO> selectAttachPage(IPage<AttachVO> page, AttachPageParam attach) {
        // 创建部门
        if (!AuthUtil.isAdministrator()) {
            List<Long> deptList = SysCache.getDeptChildIds(Long.valueOf(AuthUtil.getDeptId()));
            attach.setDeptList(deptList);
        }
        List<AttachVO> attachVOS = baseMapper.selectAttachPage(page, attach);
        return page.setRecords(attachVOS);
    }
 
 
    @Override
    public IPage<AttachVO> findAttachImages(IPage<AttachVO> page, AttachVO attach) {
        attach.setAreaCode(HeaderUtils.getAreaCodeHeaderAreaCode(attach.getAreaCode()));
        if (Objects.isNull(attach.getResultTypes())) {
            // 使用常量定义避免魔法数字
            // attach.setResultTypes(Arrays.asList(TypeOfOutcome.IMAGE.getType(),
            //     TypeOfOutcome.VIDEO.getType(),
            //     TypeOfOutcome.AI.getType(),
            //     TypeOfOutcome.SURVEY.getType(),
            //     TypeOfOutcome.PANORAMA.getType()));
        }
        List<Long> deptIdList = SysCache.getDeptChildIds(Long.valueOf(AuthUtil.getDeptId()));
        String permissionCondition = permissionBuilder.buildDataPermissionCondition(AuthUtil.getUserId(), "attach");
        List<AttachVO> aiImages = baseMapper.findAttachImages(page, attach, deptIdList, permissionCondition, AuthUtil.getUserId());
        return page.setRecords(aiImages);
    }
 
 
    /**
     * ai数据
     *
     * @param page
     * @param attach
     * @return
     */
    @Override
    public List<AttachVO> findAiAttachImages(IPage page, AttachVO attach) {
        List<Long> deptIdList = SysCache.getDeptChildIds(Long.valueOf(AuthUtil.getDeptId()));
        String permissionCondition = permissionBuilder.buildDataPermissionCondition(AuthUtil.getUserId(), "attach");
        if (StringUtils.isNotEmpty(attach.getWayLineJobId())) {
            attach.setWayLineJobIdList(Arrays.asList(attach.getWayLineJobId().split(",")));
        }
        return baseMapper.findAiAttachImages(page, attach, deptIdList, permissionCondition);
    }
 
    @Override
    public List<String> findAiEventMd5s(List<Long> eventRecordIds) {
        if (CollectionUtils.isEmpty(eventRecordIds)) {
            return null;
        }
        List<EventImgMd5VO> list = baseMapper.findAllMd5ByAi(eventRecordIds);
        // 原有的图片
        List<String> aiMd5s = baseMapper.findAiMd5s(eventRecordIds);
        List<List<String>> allMd5 = aiMd5s.stream()
            .map(entity -> Arrays.asList(entity.split(",")))
            .collect(Collectors.toList());
        // 原始图片id
//        List<String> allIds = Streams.flatMap(allMd5);
        List<String> md5s = Streams.toList(list, entity -> !StringUtils.isEmpty(entity.getMd5()), EventImgMd5VO::getMd5);
//        List<String> md5Orgs = Streams.toList(list, entity -> !StringUtils.isEmpty(entity.getOriginalMd5()), EventImgMd5VO::getOriginalMd5);
//        allIds.removeAll(md5Orgs);
        List<String> all = new ArrayList<>(md5s);
//        all.addAll(allIds);
        if (CollectionUtils.isEmpty(all)) {
            return Arrays.asList("0");
        } else {
            return all;
        }
    }
 
 
    @Override
    public List<DeptAndSizeVo> getDeptAllSize(String startTime, String endTime) {
        return baseMapper.getDeptAllSize(startTime, endTime);
    }
 
    /**
     * 保存附件表信息
     *
     * @param attach 附件表信息
     * @return
     */
    @Override
    public Boolean saveAttachInfo(Attach attach) {
        if (isExist(attach)) {
            return false;
        }
        // 保存
        return save(attach);
    }
 
 
    /**
     * 判断附件是否已存在
     *
     * @param attach
     * @return
     */
    private Boolean isExist(Attach attach) {
        if (null != attach.getId()) {
            return true;
        }
        Boolean flag = false;
        // 判断是否已入库,根据附件名称判断(重复的不再入库)
        Integer number = baseMapper.getAttachNumberByParam(attach);
        if (number > 0) {
            flag = true;
        }
        return flag;
    }
 
    /**
     * 删除 附件表及对应的附件信息
     *
     * @param ids
     * @return
     */
    @Override
    public Boolean removeBatchAndDataByIds(List<Long> ids) {
        boolean flag = false;
        int i = 0;
        for (Long id : ids) {
            Attach attach = getById(id);
            // 删除数据
            boolean remove = removeById(id);
            if (remove) {
                i++;
                attach.setName(attach.getName().replaceAll("cloud-bucket/", ""));
                // 删除对应的数据
                ossBuilder.template().removeFile(attach.getName());
            }
        }
        if (i == ids.size()) {
            flag = true;
        }
        return flag;
    }
 
    @Override
    public List<String> getAttachNames(List<Long> ids) {
        if (ids == null || ids.isEmpty()) {
            return Collections.emptyList();
        }
 
        // 查询附件列表
        List<Attach> attaches = baseMapper.selectBatchIds(ids);
 
        // 处理 name 字段,去除 cloud-bucket 前缀
        return attaches.stream().map(attach -> {
            String name = attach.getName();
            if (name != null && name.startsWith("cloud-bucket/")) {
                return name.substring("cloud-bucket/".length());
            }
            return name; // 如果不匹配,原样返回
        }).collect(Collectors.toList());
 
    }
 
    /**
     * @param param
     * @return
     */
    private List<AttachVO> getAttachList(AttachmentDownloadParam param) {
        // 查询附件列表
        List<AttachVO> attaches = baseMapper.getAttachList(param);
        // 处理 name 字段,去除 cloud-bucket 前缀
        attaches.stream().forEach(attach -> {
            String name = attach.getName();
            if (name != null && name.startsWith("cloud-bucket/")) {
                attach.setName(name.substring("cloud-bucket/".length()));
            }
        });
        return attaches;
    }
 
    /**
     * 历史正射图片处理
     *
     * @return
     */
    @Override
    public Boolean hisOrtPicHandler() {
        // 查询正射的图片信息
        List<Attach> list = baseMapper.getHisOrtPicList();
        // 处理正射的图片
        for (Attach attach : list) {
            // 判断图片是否存在,不存在则删除
            // 通过路径获取文件
            String path = attach.getOriginalName();
            File file = new File(path);
 
            // 检查文件是否存在
            if (!file.exists()) {
                removeById(attach);
                log.error("文件 {} 不存在!删除记录", path);
                continue;
            }
            // 存在情况进行图片压缩
            // 先判断是否已经压缩过了,如果已经压缩过了,就不再压缩
            String smallTargetName = "_small.jpeg";
            File smallOutFile = new File(file.getParent(), file.getName().replace(".tif", smallTargetName));
            // 检查文件是否存在
            if (smallOutFile.exists()) {
                log.info("文件 {} 存在!不再压缩", path);
                continue;
            }
            // 图片压缩
            log.info("开始压缩tif图片,路径:{}", path);
            Map<String, Object> map = new HashMap<>(1);
            map.put("ort_path", path);
            TifConvertResult tifConvertResult = tifConvertClient.tifConvJpeg(map);
            log.info("tif 图片压缩结果:{}", tifConvertResult);
 
        }
        return null;
    }
 
 
    /**
     * 查询媒体属性根据图片,名称
     *
     * @param names
     * @return
     */
    @Override
    public Map<String, Object> findMetaDataByName(@Param("names") List<String> names) {
        if (CollectionUtils.isEmpty(names)) {
            return Collections.emptyMap();
        }
        String cloudBucket = "cloud-bucket";
        names = Streams.toList(names, name -> cloudBucket + name);
        List<Attach> metas = baseMapper.findMetaDataByName(names);
        Map<String, Object> map = Streams.toMap(metas, entity -> entity.getOriginalName().replace(cloudBucket, ""), entity -> entity.getMetadata());
        return map;
    }
 
    @Override
    public void saveMd5() {
        LambdaQueryWrapper<Attach> wrapper = Wrappers.<Attach>lambdaQuery()
            .select(Attach::getId, Attach::getLink)
            .isNull(Attach::getMd5);
 
        PaginationUtils.processInPages(
            baseMapper,   // 你的Mapper实例
            500,            // 每页大小
            wrapper,        // 固定查询条件
            page -> {       // 每页数据处理
                List<Attach> attachList = page.getRecords();
                // 业务处理逻辑
                saveMd5Data(attachList);
            });
 
    }
 
    public void saveMd5Data(List<Attach> list) {
        if (CollectionUtils.isEmpty(list)) {
            return;
        }
        for (Attach attach : list) {
            LambdaUpdateWrapper<Attach> update = Wrappers.<Attach>lambdaUpdate()
                .set(Attach::getMd5, attach.getMd5())
                .eq(Attach::getId, attach.getId());
            baseMapper.update(update);
        }
    }
 
 
    /**
     * 获取地图上事件位置及数量
     *
     * @param dto 参数
     * @return list
     */
    @Override
    public TreeVo mapAttachEvents(AttachQueryParam dto) {
        // 所有区域数据
        Object regionResultz = RedisOpsUtils.get(CacheConstant.REGION_INFO + dto.getAreaCode());
        if (Objects.isNull(regionResultz)) {
            R<TreeVo> regionResult = sysClient.getRegionTreeByPrentCode(dto.getAreaCode());
            RedisOpsUtils.setWithExpire(CacheConstant.REGION_INFO + dto.getAreaCode(), regionResult.getData(), TimeUnit.HOURS.toSeconds(1));
            regionResultz = regionResult.getData();
        }
 
        TreeVo data = regionResultz == null ? null : (TreeVo) regionResultz;
        String areaCode = HeaderUtils.getAreaCode(dto.getAreaCode());
        dto.setAreaCode(areaCode);
 
        // 查询所有区域的数据
        List<AttachStatisticsBo> list = baseMapper.mapAreaCodeAttachStatistics(dto);
        // 获取附件数据
        List<AttachMapVo> attachList = baseMapper.mapAttachList(dto);
        Map<String, List<AttachMapVo>> attachMap = Streams.groupBy(attachList, AttachMapVo::getAreaCode);
        // 县下面的所有数量 key=区域code,value =数量
        Map<String, Integer> map = Streams.toMap(list, AttachStatisticsBo::getDictKey, AttachStatisticsBo::getNum);
        // 县区域下的所有事件数量
        TreeVo treeVo = buildTreeVo(data, map, attachMap);
        return treeVo;
    }
 
 
    /**
     * 构建树
     *
     * @param data     区域树
     * @param map      key=区域code,value=事件数
     * @param eventMap key=区域code,value=事件列表
     * @return
     */
    public static TreeVo buildTreeVo(TreeVo data, Map<String, Integer> map, Map<String, List<AttachMapVo>> eventMap) {
        if (CollectionUtils.isEmpty(map) || data == null) {
            return null;
        }
        String id = data.getId();
        // 明细事件列表
        List jobEventList = eventMap.get(id);
        data.setData(jobEventList);
        List<TreeVo> children = data.getChildrens();
        // 总数
        Integer totalNum = Streams.reduce(children, entity -> NoNullUtils.integer(map.get(entity.getId())), 0, Integer::sum);
        data.setNumber(Double.valueOf(totalNum));
        Integer num = NoNullUtils.integer(map.get(data.getId()));
        if (num > 0) {
            data.setNumber(Double.valueOf(num));
        }
        if (!CollectionUtils.isEmpty(children)) {
            for (TreeVo treeVo : children) {
                buildTreeVo(treeVo, map, eventMap);
            }
        }
        return data;
    }
 
    @Autowired
    @Qualifier("downloadTaskExecutor")
    private ExecutorService downloadTaskExecutor;
 
 
    @Override
    public AttachInfoVO getAttachInfo(Long id) {
        AttachInfoVO attachVO = baseMapper.getAttachInfo(id);
        if (attachVO != null) {
            // 不为空,获取经纬度
            if (!ObjectUtils.isEmpty(attachVO.getMetadata())) {
                FileMetadataDTO metadata = JSON.parseObject(attachVO.getMetadata().toString(), FileMetadataDTO.class);
                PositionDTO shootPosition = metadata.getShootPosition();
                ValidUtil.assertNull(shootPosition, "媒体数据不存在经纬度!");
                Double lat = shootPosition.getLat();
                Double lng = shootPosition.getLng();
            }
        }
        return attachVO;
    }
 
    @Override
    public List<AttachInfoVO> getAttachInfoByJobId(String jobId) {
        List<AttachInfoVO> attachVO = baseMapper.getAttachInfoByJobId(jobId);
        // 不为空,获取经纬度
        attachVO.forEach(item -> {
            if (!ObjectUtils.isEmpty(item.getMetadata())) {
                FileMetadataDTO metadata = JSON.parseObject(item.getMetadata().toString(), FileMetadataDTO.class);
                PositionDTO shootPosition = metadata.getShootPosition();
                Double lat = shootPosition.getLat();
                Double lng = shootPosition.getLng();
            }
        });
        return attachVO;
    }
 
    @Override
    public int updateFileName(Long id, String nickName) {
        UpdateWrapper<Attach> objectUpdateWrapper = new UpdateWrapper<>();
        objectUpdateWrapper.set("nick_name", nickName);
        objectUpdateWrapper.eq("id", id);
        return baseMapper.update(null, objectUpdateWrapper);
    }
 
 
    /**
     * 删除无用的链接地址
     */
    @Override
    public void deletedNotExistsUrl() {
        LambdaQueryWrapper<Attach> wrapper = Wrappers.<Attach>lambdaQuery()
            .select(Attach::getId, Attach::getLink, Attach::getMd5)
            .like(Attach::getLink, ".jpeg");
 
        PaginationUtils.processInPages(
            baseMapper,   // 你的Mapper实例
            500,            // 每页大小
            wrapper,        // 固定查询条件
            page -> {       // 每页数据处理
                List<Attach> attachList = page.getRecords();
                // 业务处理逻辑
                for (Attach attach : attachList) {
                    String link = attach.getLink();
                    boolean validate = HttpUtils.validateUrl(link);
                    if (!validate) {
                        // 进行删除
                        baseMapper.update(Wrappers.<Attach>lambdaUpdate().set(Attach::getIsDeleted, 1).eq(Attach::getId, attach.getId()));
                        // 删除事件里关联数据
                        baseMapper.deletedEventData(attach.getMd5());
                    } else {
 
                    }
                }
 
            });
 
    }
 
    /**
     * 查询成果数量
     *
     * @param jobId
     * @return
     */
    @Override
    public Long findResultNumByJobId(String jobId) {
        return baseMapper.selectCount(Wrappers.<Attach>lambdaQuery()
            .eq(Attach::getIsDeleted, 0)
            .eq(Attach::getPatrolTaskId, jobId).in(Attach::getResultType, AttachResultTypeEnum.getTheAttachmentType()));
    }
 
    @Override
    /**
     * 批量删除媒体文件及其关联文件,并软删除数据库记录。
     *
     * @param ids 要删除的记录ID列表
     * @return 成功更新的数据库记录条数
     */
    public int deleteMediaFile(List<Long> ids) {
        if (ids == null || ids.isEmpty()) {
            log.warn("传入的ID列表为空,无需执行删除操作。");
            return 0;
        }
 
        // --- 步骤 1: 查询初始要删除的源文件记录 ---
        List<Attach> initialAttaches = baseMapper.selectBatchIds(ids);
        if (initialAttaches == null || initialAttaches.isEmpty()) {
            log.warn("根据传入的ID列表 {} 未找到任何数据库记录。", ids);
            return 0;
        }
 
        final String BUCKET_NAME = minioPojo.getBucket();
 
        // --- 步骤 2: 准备所有待处理的数据列表 ---
        List<String> minioObjectsToDelete = new ArrayList<>();     // 用于MinIO物理删除
        List<String> relatedFileNamesToSearchInDb = new ArrayList<>(); // 用于数据库二次查询
 
        for (Attach attach : initialAttaches) {
            String fullPath = attach.getName(); // fullPath 是 "bucket/key"
 
            // 从完整路径中剥离出 bucket,得到纯粹的 object key
            String objectKey = fullPath;
            String prefix = BUCKET_NAME + "/";
            if (objectKey.startsWith(prefix)) {
                objectKey = objectKey.substring(prefix.length());
            }
 
            // 2.1 添加原始文件的 object key 到MinIO删除列表
            minioObjectsToDelete.add(objectKey);
 
            // 2.2 根据文件类型,构造其所有关联文件的 key 和 name
            if ("mp4".equals(attach.getExtension())) {
                String showKey = objectKey.replaceAll("(\\.[^.]+)$", "_show$1");
                String smallJpgKey = objectKey.replaceAll("(\\.[^.]+)$", "_small.jpg");
 
                minioObjectsToDelete.add(showKey);
                minioObjectsToDelete.add(smallJpgKey);
 
                // 将关联文件的完整 name (含bucket) 添加到数据库查询列表
                relatedFileNamesToSearchInDb.add(BUCKET_NAME + "/" + showKey);
                relatedFileNamesToSearchInDb.add(BUCKET_NAME + "/" + smallJpgKey);
 
            } else if ("jpeg".equals(attach.getExtension()) || "jpg".equals(attach.getExtension())) {
                String showKey = objectKey.replaceAll("(\\.[^.]+)$", "_show$1");
                String smallKey = objectKey.replaceAll("(\\.[^.]+)$", "_small$1");
                String markKey = objectKey.replaceAll("(\\.[^.]+)$", "_mark$1");
 
                minioObjectsToDelete.add(showKey);
                minioObjectsToDelete.add(smallKey);
                minioObjectsToDelete.add(markKey);
 
                relatedFileNamesToSearchInDb.add(BUCKET_NAME + "/" + showKey);
                relatedFileNamesToSearchInDb.add(BUCKET_NAME + "/" + smallKey);
                relatedFileNamesToSearchInDb.add(BUCKET_NAME + "/" + markKey);
            }
        }
 
        // --- 步骤 3: 执行MinIO物理文件删除 ---
        log.info("准备从MinIO批量删除 {} 个对象...", minioObjectsToDelete.size());
        MinioFileDownloader.deleteObjects(
            minioPojo.getPath(),
            minioPojo.getAccessKey(),
            minioPojo.getSecretKey(),
            BUCKET_NAME,
            minioObjectsToDelete
        );
        log.info("MinIO对象删除请求已成功发送。");
 
        // --- 步骤 4: 执行数据库软删除 (包括源文件和所有关联文件) ---
 
        // 4.1 初始化最终待更新的ID列表,包含源文件ID
        List<Long> allIdsToUpdate = new ArrayList<>(ids);
 
        // 4.2 查询所有关联文件的ID
        if (!relatedFileNamesToSearchInDb.isEmpty()) {
            LambdaQueryWrapper<Attach> queryWrapper = new LambdaQueryWrapper<>();
            queryWrapper
                .select(Attach::getId)
                .in(Attach::getName, relatedFileNamesToSearchInDb);
 
            List<Attach> relatedAttaches = baseMapper.selectList(queryWrapper);
 
            if (relatedAttaches != null && !relatedAttaches.isEmpty()) {
                List<Long> relatedIds = relatedAttaches.stream()
                    .map(Attach::getId)
                    .collect(Collectors.toList());
                log.info("查询到 {} 个关联文件的数据库记录ID需要被软删除。", relatedIds.size());
                allIdsToUpdate.addAll(relatedIds);
            }
        }
 
        // 4.3 去重并执行最终的批量更新
        List<Long> finalIdsToUpdate = allIdsToUpdate.stream().distinct().collect(Collectors.toList());
        if (finalIdsToUpdate.isEmpty()) {
            log.warn("逻辑执行完毕,但没有需要更新的数据库记录。");
            return 0;
        }
 
        LambdaUpdateWrapper<Attach> updateWrapper = new LambdaUpdateWrapper<>();
        updateWrapper
            .set(Attach::getIsDeleted, 1)
            .in(Attach::getId, finalIdsToUpdate);
 
        int updatedRows = baseMapper.update(null, updateWrapper);
        log.info("成功软删除了 {} 条数据库记录 (包括源文件和所有关联文件)。", updatedRows);
 
        return updatedRows;
    }
 
    @Override
    public List<AttachTypeStatisticsVO> attachTypeStatistics(AttachStatisticsVo attachStatisticsVo) {
 
        List<Long> deptIdList = SysCache.getDeptChildIds(Long.valueOf(AuthUtil.getDeptId()));
        String permissionCondition = permissionBuilder.buildDataPermissionCondition(AuthUtil.getUserId(), "attach");
        attachStatisticsVo.setAreaCode(HeaderUtils.getAreaCode());
        attachStatisticsVo.setResultTypeList(Arrays.asList(3, 10));
        List<AttachTypeStatisticsVO> attachTypeStatisticsVOS = baseMapper.attachTypeStatistics(attachStatisticsVo, deptIdList, permissionCondition);
        AttachTypeStatisticsVO attachTypeStatisticsVOS1 = baseMapper.calculateTheThreeDimensionalArea(attachStatisticsVo, deptIdList, permissionCondition);
        if (attachTypeStatisticsVOS.size() == 0) {
            for (int i = 1; i <= 5; i++) {
                attachTypeStatisticsVOS.add(createDefaultTypeVO(i + ""));
            }
        }
        for (AttachTypeStatisticsVO attachTypeStatisticsVO : attachTypeStatisticsVOS) {
            if (attachTypeStatisticsVO.getType().equals("3")) {
                if (attachTypeStatisticsVOS1 != null && attachTypeStatisticsVOS1.getArea() != null) {
                    // 修复NumberFormatException错误,将area值转换为Long时先转换为Double再取整
                    attachTypeStatisticsVO.setCount(attachTypeStatisticsVOS1.getArea());
                } else {
                    // 如果没有三维面积数据,则设置默认值0
                    attachTypeStatisticsVO.setCount(0d);
                }
            }
        }
        return attachTypeStatisticsVOS;
    }
 
    @Override
    public List<Attach> findAttachInfoByYesterday(int resultType, Date startTime, Date endTime) {
 
        // 获取昨天日期范围
        LocalDateTime yesterdayStart = LocalDateTime.now().minusDays(1).with(LocalTime.MIN);
        LocalDateTime yesterdayEnd = LocalDateTime.now().minusDays(1).with(LocalTime.MAX);
 
        // 如何开始时间和结束时间有值按传递的参数获取
        if (startTime != null && endTime != null) {
            yesterdayStart = LocalDateTime.ofInstant(startTime.toInstant(), ZoneId.systemDefault());
            yesterdayEnd = LocalDateTime.ofInstant(startTime.toInstant(), ZoneId.systemDefault());
        }
 
        QueryWrapper<Attach> queryWrapper = new QueryWrapper<>();
        // 使用传入的resultType参数
        queryWrapper.eq("result_type", resultType);
        // 使用between
        queryWrapper.between("create_time", yesterdayStart, yesterdayEnd);
 
        return baseMapper.selectList(queryWrapper);
    }
 
    private AttachTypeStatisticsVO createDefaultTypeVO(String type) {
        AttachTypeStatisticsVO vo = new AttachTypeStatisticsVO();
        vo.setType(type);
        vo.setCount(0d);
        return vo;
    }
 
    @Override
    public List<LineColumnDateVo> getManageAttachTypeStatistics(WaylineJobInfoQueryParam param) {
        param.setAreaCode(HeaderUtils.getAreaCode());
        List<Long> deptIdList = SysCache.getDeptChildIds(Long.valueOf(AuthUtil.getDeptId()));
        String permissionCondition = permissionBuilder.buildDataPermissionCondition(AuthUtil.getUserId(), "attach");
        List<AttachTypeStatisticsVO> attachTypeStatisticsVOS = baseMapper.getManageAttachTypeStatistics(param, deptIdList, permissionCondition);
        // attachTypeStatisticsVOS 统计总数
        double total = attachTypeStatisticsVOS.stream().mapToDouble(vo -> vo.getCount()).sum();
        List<String> xAxisData = XAxisDataInitializer.initXAxisDataToNow(param.getDateEnum());
        List<LineColumnDateVo> list = new ArrayList<>();
        for (int index = 0; index < xAxisData.size(); index++) {
            String xAxisDatum = xAxisData.get(index);
            LineColumnDateVo lineColumnDateVo = new LineColumnDateVo();
            List<ChartDataVo> dataVoList = attachTypeStatisticsVOS.stream().filter(item -> item.getTimeStr().equals(xAxisDatum))
                .map(item -> {
                    ChartDataVo chartDataVo = new ChartDataVo();
                    chartDataVo.setValue(item.getCount());
                    chartDataVo.setName(item.getType());
                    return chartDataVo;
                }).collect(Collectors.toList());
            lineColumnDateVo.setData(dataVoList);
            lineColumnDateVo.setName(ChartDataVo.formatTime(param.getDateEnum(), xAxisDatum, index));
            lineColumnDateVo.setTotal(total);
            list.add(lineColumnDateVo);
        }
        return list;
    }
 
    /**
     * @param deviceSn
     * @param timestamp
     * @param operator
     * @param workspaceId
     * @return
     */
    @Override
    public Boolean deleteAttach(String deviceSn, long timestamp, Long operator, String workspaceId) {
        return baseMapper.deleteAttach(deviceSn, timestamp, operator, workspaceId);
    }
 
    @Override
    public List<Attach> getlotInfoList(Long lotInfoId) {
        return baseMapper.getlotInfoList(lotInfoId);
    }
 
    /**
     * 统计三维面积
     *
     * @param deviceSn
     * @param startDate
     * @param endDate
     * @param resultTypes
     * @return
     */
    @Override
    public List<AttachTypeStatisticsVO> calculateTheThreeDimensionalArea(String deviceSn, String startDate, String endDate, String resultTypes, String areaCode) {
        AttachStatisticsVo attachStatisticsVo = new AttachStatisticsVo();
        attachStatisticsVo.setDeviceSn(deviceSn);
        attachStatisticsVo.setStartDate(startDate);
        attachStatisticsVo.setEndDate(endDate);
        attachStatisticsVo.setResultTypeList(Func.toIntList(resultTypes));
        attachStatisticsVo.setAreaCode(areaCode);
        // 如果是三维类型,jobtyp = 6  否则就是5 查询倾斜的信息
        attachStatisticsVo.setJobType(attachStatisticsVo.getResultTypeList().contains(3) ? 6 : 5);
        List<Long> deptIdList = SysCache.getDeptChildIds(Long.valueOf(AuthUtil.getDeptId()));
        String permissionCondition = permissionBuilder.buildDataPermissionCondition(AuthUtil.getUserId(), "attach");
        List<AttachTypeStatisticsVO> attachTypeStatisticsVOS = baseMapper.calculateTheThreeDimensionalAreaGroupBy(attachStatisticsVo, deptIdList, permissionCondition);
        return attachTypeStatisticsVOS;
    }
 
    /**
     * 下载附件
     *
     * @param param        下载参数
     * @param outputStream 输出流
     * @return 下载是否成功
     * @throws IOException IO异常
     */
    @Override
    public Boolean downloadByByte(String param, OutputStream outputStream) throws IOException {
        // 步骤1:构建下载参数并获取附件列表
        AttachmentDownloadParam attachmentDownloadParam = new AttachmentDownloadParam();
        attachmentDownloadParam.setAttachIds(Func.toLongList(param));
        List<AttachVO> attachList = getAttachList(attachmentDownloadParam);
        if (CollectionUtils.isEmpty(attachList)) {
            return false;
        }
 
        // 步骤2:获取当前租户ID
        String tenantId = AuthUtil.getTenantId();
        if (StringUtils.isBlank(tenantId)) {
            tenantId = "000000";
        }
 
        // 步骤3:通过OssBuilder获取OSS配置
        Oss oss = ossBuilder.getOss(tenantId, "");
 
        try {
            // 步骤4:创建ZipOutputStream并设置压缩级别
            ZipOutputStream zos = new ZipOutputStream(outputStream);
            zos.setLevel(Deflater.BEST_SPEED);
 
            // 步骤5:设置缓冲区大小
            final int BUFFER_SIZE = 1024 * 1024;
            byte[] buffer = new byte[BUFFER_SIZE];
 
            // 步骤6:根据OSS配置创建MinioClient
            MinioClient minioClient = MinioClient.builder()
                .endpoint(oss.getEndpoint())
                .credentials(oss.getAccessKey(), oss.getSecretKey())
                .build();
 
            // 步骤7:创建文件名集合用于去重
            Set<String> usedFileNames = new HashSet<>();
 
            // 步骤8:遍历附件列表,逐个下载并打包
            for (AttachVO attachVO : attachList) {
                String objectName = attachVO.getName();
                try {
                    // 步骤8.1:从MinIO获取文件流
                    InputStream is = minioClient.getObject(
                        GetObjectArgs.builder()
                            .bucket(oss.getBucketName())
                            .object(objectName)
                            .build());
 
                    // 步骤8.2:提取文件名(不包含路径)
                    String fileName = objectName.substring(objectName.lastIndexOf("/") + 1);
 
                    // 步骤8.3:处理文件名冲突
                    String uniqueFileName = fileName;
                    int counter = 1;
                    while (usedFileNames.contains(uniqueFileName)) {
                        int dotIndex = fileName.lastIndexOf(".");
                        if (dotIndex > 0) {
                            uniqueFileName = fileName.substring(0, dotIndex) + "_" + counter + fileName.substring(dotIndex);
                        } else {
                            uniqueFileName = fileName + "_" + counter;
                        }
                        counter++;
                    }
 
                    // 步骤8.4:记录已使用的文件名
                    usedFileNames.add(uniqueFileName);
 
                    // 步骤8.5:创建ZIP条目并写入文件内容
                    ZipEntry zipEntry = new ZipEntry(uniqueFileName);
                    zos.putNextEntry(zipEntry);
 
                    int length;
                    while ((length = is.read(buffer)) > 0) {
                        zos.write(buffer, 0, length);
                        zos.flush();
                    }
 
                    // 步骤8.6:关闭当前条目和输入流
                    zos.closeEntry();
                    is.close();
                } catch (Exception e) {
                    log.error("处理文件 {} 失败: {}", objectName, e.getMessage());
                }
            }
 
            // 步骤9:完成ZIP文件写入
            zos.finish();
            zos.flush();
 
            return true;
 
        } catch (Exception e) {
            log.error("创建zip文件失败", e);
            return false;
        } finally {
            // 步骤10:确保输出流被正确关闭
            if (outputStream != null) {
                try {
                    outputStream.flush();
                } catch (IOException e) {
                    log.error("Error flushing output stream: {}", e.getMessage());
                }
                try {
                    outputStream.close();
                } catch (IOException e) {
                    log.error("Error closing output stream: {}", e.getMessage());
                }
            }
        }
    }
 
}