huliguo
2 天以前 b21549bfd07dd43525d453d9a0282e25260c092c
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
package com.stylefeng.guns.modular.system.service.impl;
 
import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONArray;
import com.baomidou.mybatisplus.mapper.EntityWrapper;
import com.baomidou.mybatisplus.mapper.Wrapper;
import com.baomidou.mybatisplus.plugins.Page;
import com.baomidou.mybatisplus.service.impl.ServiceImpl;
import com.stylefeng.guns.core.util.ToolUtil;
import com.stylefeng.guns.modular.system.dao.TaskDetailMapper;
import com.stylefeng.guns.modular.system.model.*;
import com.stylefeng.guns.modular.system.model.enums.VideoChannelEnum;
import com.stylefeng.guns.modular.system.model.vo.*;
import com.stylefeng.guns.modular.system.service.*;
import com.stylefeng.guns.modular.system.util.ExcelUtil;
import com.stylefeng.guns.modular.system.util.ResultUtil;
import com.stylefeng.guns.modular.system.util.quartz.QuartzUtil;
import org.apache.poi.hssf.usermodel.*;
import org.apache.poi.ss.usermodel.*;
import org.apache.poi.ss.util.CellRangeAddress;
import org.apache.poi.util.IOUtils;
import org.quartz.JobKey;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.format.annotation.DateTimeFormat;
import org.springframework.stereotype.Service;
 
import javax.servlet.ServletOutputStream;
import javax.servlet.http.HttpServletResponse;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.net.URL;
import java.net.URLConnection;
import java.net.URLEncoder;
import java.text.SimpleDateFormat;
import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;
import java.util.*;
import java.util.stream.Collectors;
 
/**
 * @author zhibing.pu
 * @Date 2024/12/17 20:23
 */
@Service
public class TaskDetailServiceImpl extends ServiceImpl<TaskDetailMapper, TaskDetail> implements ITaskDetailService {
    
    
    @Autowired
    private QuartzUtil quartzUtil;
    
    @Autowired
    private IPatrolTaskService patrolTaskService;
    
    @Autowired
    private ITaskDetailVehiclesService taskDetailVehiclesService;
    
    @Autowired
    private ITaskDetailVehiclesChannelService taskDetailVehiclesChannelService;
    
    @Autowired
    private IUserService userService;
    
    @Autowired
    private IIconService iconService;
    
    
    
    
    
    
    /**
     * 获取巡查任务列表数据
     * @param vo
     * @return
     */
    @Override
    public List<TaskDetailList> getTaskDetailList(Page<TaskDetailList> pageInfo, TaskDetailListVo vo) {
        return this.baseMapper.getTaskDetailList(pageInfo, vo);
    }
    
    
    /**
     * 删除任务
     * @param ids
     */
    @Override
    public void delTaskDetail(List<String> ids) {
        List<TaskDetail> taskDetails = this.selectBatchIds(ids);
        for (TaskDetail taskDetail : taskDetails) {
            JobKey jobKey = new JobKey(taskDetail.getId().toString());
            quartzUtil.deleteQuartzTask(jobKey);
            taskDetail.setDelFlag(1);
            this.updateById(taskDetail);
        }
    }
    
    /**
     * 获取任务记录列表
     * @param pageInfo
     * @param vo
     * @return
     */
    @Override
    public List<TaskRecordList> getTaskRecordList(Page<TaskRecordList> pageInfo, TaskRecordListVo vo) {
        return this.baseMapper.getTaskRecordList(pageInfo, vo);
    }
    
    
    /**
     * 获取任务记录详情
     * @param id
     * @return
     */
    @Override
    public TaskRecordInfo getTaskRecordInfo(Integer id) {
        TaskDetail taskDetail = this.selectById(id);
        TaskRecordInfo info = new TaskRecordInfo();
        info.setId(id);
        List<TaskDetailVehicles> taskDetailVehiclesList = taskDetailVehiclesService.selectList(new EntityWrapper<TaskDetailVehicles>().eq("task_detail_id", id));
        info.setVehicleNum(taskDetailVehiclesList.size());
        long unexecutedQuantity = taskDetailVehiclesList.stream().filter(s -> s.getStatus() == 1).count();
        info.setUnexecutedQuantity(unexecutedQuantity);
        PatrolTask patrolTask = patrolTaskService.selectById(taskDetail.getPatrolTaskId());
        info.setType(patrolTask.getType());
        info.setName(patrolTask.getName());
        info.setStatus(taskDetail.getStatus());
        info.setExecutionTime(taskDetail.getExecutionTime().format(DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss")));
        //执行状态(1=待执行,2=执行中,3=成功,4=离线,5=失败)
        long offlineNum = taskDetailVehiclesList.stream().filter(s -> s.getStatus() == 4).count();
        info.setOfflineNum(offlineNum);
        long normalNum = taskDetailVehiclesList.stream().filter(s -> s.getStatus() == 3).count();
        info.setNormalNum(normalNum);
        long abnormalNum = taskDetailVehiclesList.stream().filter(s -> s.getStatus() == 5).count();
        info.setAbnormalNum(abnormalNum);
        long underway = taskDetailVehiclesList.stream().filter(s -> s.getStatus() == 2).count();
        info.setUnderway(underway);
        return info;
    }
    
    
    /**
     * 获取任务记录中的车船数据
     * @param vo
     * @return
     */
    @Override
    public PictureDetailsVehicle getPictureDetailsVehicle(PictureDetailsVo vo) {
        PictureDetailsVehicle info = new PictureDetailsVehicle();
        Wrapper<TaskDetailVehicles> wrapper = new EntityWrapper<TaskDetailVehicles>().eq("task_detail_id", vo.getId());
        if(ToolUtil.isNotEmpty(vo.getVehicle())){
            JSONArray jsonArray = JSON.parseArray(vo.getVehicle());
            List<String> vehicleIdList = new ArrayList<>();
            for (int i = 0; i < jsonArray.size(); i++) {
                Integer vehicleId = jsonArray.getJSONObject(i).getInteger("vehicleId");
                Integer vehicleType = jsonArray.getJSONObject(i).getInteger("vehicleType");
                vehicleIdList.add(vehicleId + "_" + vehicleType);
            }
            wrapper.in("vehicleIdUnique", vehicleIdList);
        }
        if(null != vo.getStatus() && !vo.getStatus().isEmpty()){
            wrapper.in("status", vo.getStatus());
        }
 
        if(null != vo.getSysStatus() && !vo.getSysStatus().isEmpty()){
            Wrapper<TaskDetailVehiclesChannel> channelWrapper = new EntityWrapper<TaskDetailVehiclesChannel>()
                    .eq("task_detail_id", vo.getId())
                    .in("sys_status", vo.getSysStatus());
            if (null != vo.getHavePic()){
                if(vo.getHavePic().equals("0")){
                    channelWrapper.isNull("image_url");
                }else if(vo.getHavePic().equals("1")){
                    channelWrapper.isNotNull("image_url");
                }
            }
            List<TaskDetailVehiclesChannel> sys_status = taskDetailVehiclesChannelService.selectList(channelWrapper);
            List<Integer> collect = sys_status.stream().map(TaskDetailVehiclesChannel::getTaskDetailVehiclesId).collect(Collectors.toList());
            if(collect.size() == 0){
                collect.add(-1);
            }
            wrapper.in("id", collect);
        }
        if(null != vo.getArtificialStatus()&& !vo.getArtificialStatus().isEmpty()){
            Wrapper<TaskDetailVehiclesChannel> channelWrapper = new EntityWrapper<TaskDetailVehiclesChannel>()
                    .eq("task_detail_id", vo.getId())
                    .in("artificial_status", vo.getArtificialStatus());
            if (null != vo.getHavePic()){
                if(vo.getHavePic().equals("0")){
                    channelWrapper.isNull("image_url");
                }else if(vo.getHavePic().equals("1")){
                    channelWrapper.isNotNull("image_url");
                }
            }
            List<TaskDetailVehiclesChannel> sys_status = taskDetailVehiclesChannelService.selectList(channelWrapper);
            List<Integer> collect = sys_status.stream().map(TaskDetailVehiclesChannel::getTaskDetailVehiclesId).collect(Collectors.toList());
            if(collect.size() == 0){
                collect.add(-1);
            }
            wrapper.in("id", collect);
        }
        
        List<TaskDetailVehicles> taskDetailVehiclesList = taskDetailVehiclesService.selectList(wrapper.groupBy("vehicleIdUnique"));
        List<TaskDetailVehicles> vehiclesList = taskDetailVehiclesList.stream().filter(s -> s.getVehicleType() == 1).collect(Collectors.toList());
        List<VehicleVo> vehicle = new ArrayList<>();
        for (TaskDetailVehicles taskDetailVehicles : vehiclesList) {
            VehicleVo vehicleVo = new VehicleVo();
            BeanUtils.copyProperties(taskDetailVehicles, vehicleVo);
            vehicle.add(vehicleVo);
        }
        info.setVehicle(vehicle);
        List<TaskDetailVehicles> shipList = taskDetailVehiclesList.stream().filter(s -> s.getVehicleType() == 2).collect(Collectors.toList());
        List<VehicleVo> ship = new ArrayList<>();
        for (TaskDetailVehicles taskDetailVehicles : shipList) {
            VehicleVo vehicleVo = new VehicleVo();
            BeanUtils.copyProperties(taskDetailVehicles, vehicleVo);
            ship.add(vehicleVo);
        }
        info.setShip(ship);
        return info;
    }
    
    
    
    
    /**
     * 获取任务记录详情中的通道数据
     * @param vo
     * @return
     */
    @Override
    public List<PictureDetails> getPictureDetails(Page<PictureDetails> pageInfo, PictureDetailsVo vo) {
        Wrapper<TaskDetailVehicles> wrapper = new EntityWrapper<TaskDetailVehicles>().eq("task_detail_id", vo.getId());
        if(ToolUtil.isNotEmpty(vo.getVehicle())){
            JSONArray jsonArray = JSON.parseArray(vo.getVehicle());
            List<String> vehicleIdList = new ArrayList<>();
            for (int i = 0; i < jsonArray.size(); i++) {
                Integer vehicleId = jsonArray.getJSONObject(i).getInteger("vehicleId");
                Integer vehicleType = jsonArray.getJSONObject(i).getInteger("vehicleType");
                vehicleIdList.add(vehicleId + "_" + vehicleType);
            }
            wrapper.in("vehicleIdUnique", vehicleIdList);
        }
        if(null != vo.getStatus() && !vo.getStatus().isEmpty()){
            wrapper.in("status", vo.getStatus());
        }
        List<TaskDetailVehicles> taskDetailVehiclesList = taskDetailVehiclesService.selectList(wrapper);
        List<Integer> collect = taskDetailVehiclesList.stream().map(TaskDetailVehicles::getId).collect(Collectors.toList());
        if(collect.size() > 0){
            collect.add(-1);
        }
        List<PictureDetails> pictureDetails = taskDetailVehiclesChannelService.getPictureDetails(pageInfo, collect, vo.getSysStatus(), vo.getArtificialStatus(), vo.getHavePic() != null ? Integer.valueOf(vo.getHavePic()) : null);
        List<Icon> icons = iconService.selectList(null);
        for (PictureDetails pictureDetail : pictureDetails) {
            String videoChannel = pictureDetail.getVideoChannel();
            String name = VideoChannelEnum.getName(Integer.valueOf(videoChannel));
            pictureDetail.setVideoChannel(name);
            //异常
            if(pictureDetail.getStatus() == 5){
                pictureDetail.setImageUrl(icons.stream().filter(s->s.getType() == 2).findFirst().get().getUrl());
            }else if(ToolUtil.isEmpty(pictureDetail.getImageUrl())){
                //没有图片返回
                pictureDetail.setImageUrl(icons.stream().filter(s->s.getType() == 3).findFirst().get().getUrl());
            }
            //离线
            if(pictureDetail.getStatus() == 4){
                pictureDetail.setImageUrl(icons.stream().filter(s->s.getType() == 1).findFirst().get().getUrl());
            }
        }
        return pictureDetails;
    }
    
    
    /**
     * 人工审核
     * @param vo
     * @return
     */
    @Override
    public ResultUtil manualAudit(ManualAuditVo vo, String userId) {
        List<TaskDetailVehiclesChannel> taskDetailVehiclesChannels = taskDetailVehiclesChannelService.selectBatchIds(vo.getId());
        for (TaskDetailVehiclesChannel taskDetailVehiclesChannel : taskDetailVehiclesChannels) {
            /*if(null != taskDetailVehiclesChannel.getArtificialStatus() && 1 != taskDetailVehiclesChannel.getArtificialStatus()){
                return ResultUtil.error("不能重复审核");
            }*/
            taskDetailVehiclesChannel.setArtificialStatus(vo.getStatus());
            taskDetailVehiclesChannel.setRemark(vo.getRemark());
            taskDetailVehiclesChannel.setArtificialCreateTime(LocalDateTime.now());
            taskDetailVehiclesChannel.setArtificialUserId(Integer.valueOf(userId));
//            taskDetailVehiclesChannelService.updateById(taskDetailVehiclesChannel);
            taskDetailVehiclesChannelService.updateAudit(taskDetailVehiclesChannel);//需要将Remark字段设置为null
        }
        return ResultUtil.success();
    }
    
    
    /**
     * 下载任务记录数据
     * @param ids
     * @param code
     * @param status
     * @param response
     */
    /*@Override
    public void downloadTaskRecord(List<String> ids, String code, List<Integer> status,Integer sysStatus, Integer type,String typeName,HttpServletResponse response) {
        List<Map<String, Object>> mapList = this.baseMapper.getDownloadTaskRecord(ids, code, status,sysStatus,type,typeName);
        try {
            HSSFWorkbook hssfWorkbook = new HSSFWorkbook();
            HSSFSheet hssfSheet = hssfWorkbook.createSheet();
            hssfSheet.setDefaultColumnWidth(20);
            hssfSheet.setDefaultRowHeight((short) 800);
            //设置单元格合并
            for (int i = 0; i < 10; i++) {
                CellRangeAddress cellRangeAddress = new CellRangeAddress(0, 1, i, i);
                hssfSheet.addMergedRegion(cellRangeAddress);
            }
            CellRangeAddress cellRangeAddress = new CellRangeAddress(0, 0, 10, 16);
            hssfSheet.addMergedRegion(cellRangeAddress);
            // 创建单元格样式
            HSSFCellStyle style = hssfWorkbook.createCellStyle();
            style.setVerticalAlignment(VerticalAlignment.CENTER); // 垂直居中
            style.setAlignment(HorizontalAlignment.CENTER); // 水平居中
            HSSFFont font = hssfWorkbook.createFont();
            font.setBold(true);
            style.setFont(font);
            
            HSSFCellStyle style1 = hssfWorkbook.createCellStyle();
            style1.setVerticalAlignment(VerticalAlignment.CENTER); // 垂直居中
            style1.setAlignment(HorizontalAlignment.CENTER); // 水平居中
            
            HSSFRow hssfRow = hssfSheet.createRow(0);//设置第一行数据(标题)
            List<String> titles = Arrays.asList("序号", "任务名称", "任务类型", "执行时间", "执行状态", "车牌号", "运输公司", "年审有效期", "巡检状态", "车辆轨迹图片", "货箱通道");
            for (int l = 0; l < titles.size(); l++) {
                HSSFCell hssfCell = hssfRow.createCell(l);
                hssfCell.setCellType(CellType.STRING);//设置表格类型
                hssfCell.setCellStyle(style);
                hssfCell.setCellValue(titles.get(l));
            }
            hssfRow = hssfSheet.createRow(1);//设置第一行数据(标题)
            titles = Arrays.asList("图片", "系统审核", "系统审核时间", "人工审核", "审核意见", "审核人", "审核时间");
            for (int l = 0; l < titles.size(); l++) {
                HSSFCell hssfCell = hssfRow.createCell(l + 10);
                hssfCell.setCellType(CellType.STRING);//设置表格类型
                hssfCell.setCellStyle(style);
                hssfCell.setCellValue(titles.get(l));
            }
            List<Icon> icons = iconService.selectList(null);
            HSSFPatriarch patriarch = hssfSheet.createDrawingPatriarch();
            for (int i = 0; i < mapList.size(); i++) {
                hssfRow = hssfSheet.createRow(i + 2);
                
                Map<String, Object> map = mapList.get(i);
                //序号
                HSSFCell hssfCell = hssfRow.createCell(0);
                hssfCell.setCellType(CellType.STRING);//设置表格类型
                hssfCell.setCellStyle(style1);
                hssfCell.setCellValue(i + 1);
                //任务名称
                HSSFCell hssfCell1 = hssfRow.createCell(1);
                hssfCell1.setCellType(CellType.STRING);//设置表格类型
                hssfCell1.setCellStyle(style1);
                hssfCell1.setCellValue("");
                if(null != map.get("name")){
                    hssfCell1.setCellValue(map.get("name").toString());
                }
                //任务类型
                HSSFCell hssfCell2 = hssfRow.createCell(2);
                hssfCell2.setCellType(CellType.STRING);//设置表格类型
                hssfCell2.setCellStyle(style1);
                hssfCell2.setCellValue("");
                if(null != map.get("taskType")){
                    hssfCell2.setCellValue(map.get("taskType").toString());
                }
                //执行时间
                HSSFCell hssfCell3 = hssfRow.createCell(3);
                hssfCell3.setCellType(CellType.STRING);//设置表格类型
                hssfCell3.setCellStyle(style1);
                hssfCell3.setCellValue("");
                if(null != map.get("executionTime")){
                    hssfCell3.setCellValue(map.get("executionTime").toString());
                }
                //执行状态
                HSSFCell hssfCell4 = hssfRow.createCell(4);
                hssfCell4.setCellType(CellType.STRING);//设置表格类型
                hssfCell4.setCellStyle(style1);
                hssfCell4.setCellValue("");
                if(null != map.get("status")){
                    Integer status1 = Integer.valueOf(map.get("status").toString());
                    //执行状态(1=待执行,2=进行中,3=成功,4=失败)
                    hssfCell4.setCellValue(1 == status1 ? "待执行" : 2 == status1 ? "进行中" : 3 == status1 ? "成功" : "失败");
                    //异常
                    if(status1 == 5){
                        map.put("imageUrl", icons.stream().filter(s->s.getType() == 2).findFirst().get().getUrl());
                    }else if(ToolUtil.isEmpty(map.get("imageUrl"))){
                        //没有图片返回
                        map.put("imageUrl", icons.stream().filter(s->s.getType() == 3).findFirst().get().getUrl());
                    }
                    //离线
                    if(status1 == 4){
                        map.put("imageUrl", icons.stream().filter(s->s.getType() == 1).findFirst().get().getUrl());
                    }
                }
                //车牌号
                HSSFCell hssfCell5 = hssfRow.createCell(5);
                hssfCell5.setCellType(CellType.STRING);//设置表格类型
                hssfCell5.setCellStyle(style1);
                hssfCell5.setCellValue("");
                if(null != map.get("vehicleNum")){
                    hssfCell5.setCellValue(map.get("vehicleNum").toString());
                }
                //运输公司
                HSSFCell hssfCell6 = hssfRow.createCell(6);
                hssfCell6.setCellType(CellType.STRING);//设置表格类型
                hssfCell6.setCellStyle(style1);
                hssfCell6.setCellValue("");
                if(null != map.get("companyName")){
                    hssfCell6.setCellValue(map.get("companyName").toString());
                }
                //年审有效期
                HSSFCell hssfCell7 = hssfRow.createCell(7);
                hssfCell7.setCellType(CellType.STRING);//设置表格类型
                hssfCell7.setCellStyle(style1);
                hssfCell7.setCellValue("");
                if(null != map.get("inspectPeriodEnd")){
                    hssfCell7.setCellValue(map.get("inspectPeriodEnd").toString());
                }
                //巡检状态
                HSSFCell hssfCell8 = hssfRow.createCell(8);
                hssfCell8.setCellType(CellType.STRING);//设置表格类型
                hssfCell8.setCellStyle(style1);
                hssfCell8.setCellValue("");
                if(null != map.get("vehiclesStatus")){
                    //审核状态(1=未执行,2=正常,3=异常)
                    Integer vehiclesStatus = Integer.valueOf(map.get("vehiclesStatus").toString());
                    hssfCell8.setCellValue(1 == vehiclesStatus ? "未执行" : 2 == vehiclesStatus ? "正常" : "异常");
                }
                //车辆轨迹图片
                HSSFCell hssfCell9 = hssfRow.createCell(9);
                hssfCell9.setCellType(CellType.STRING);//设置表格类型
                hssfCell9.setCellStyle(style1);
                hssfCell9.setCellValue("");
                //图片
                if(null != map.get("imageUrl")){
                    try {
                        URL url = new URL(map.get("imageUrl").toString());
                        URLConnection urlConnection = url.openConnection();
                        urlConnection.connect();
                        InputStream inputStream = urlConnection.getInputStream();
                        byte[] bytes = IOUtils.toByteArray(inputStream);
                        int pictureIdx = hssfWorkbook.addPicture(bytes, Workbook.PICTURE_TYPE_PNG);
                        inputStream.close();
                        
                        HSSFClientAnchor anchor = new HSSFClientAnchor(0, 0, 0, 0, (short) 10, i + 2, (short) 11, i + 3);
                        //在Excel中添加图片
                        HSSFPicture picture = patriarch.createPicture(anchor, pictureIdx);
                        //设置图片尺寸
                        //picture.resize(0.5, 0.18);
                    }catch (Exception e){
                        e.printStackTrace();
                    }
                }else{
                    HSSFCell hssfCell10 = hssfRow.createCell(10);
                    hssfCell10.setCellType(CellType.STRING);//设置表格类型
                    hssfCell10.setCellStyle(style1);
                    hssfCell10.setCellValue("");
                }
                //系统审核
                HSSFCell hssfCell11 = hssfRow.createCell(11);
                hssfCell11.setCellType(CellType.STRING);//设置表格类型
                hssfCell11.setCellStyle(style1);
                hssfCell11.setCellValue("");
                if(null != map.get("sysStatus")){
                    //系统审核状态(1=未执行,2=正常,3=异常)
                    Integer sysStatus1 = Integer.valueOf(map.get("sysStatus").toString());
                    hssfCell11.setCellValue(1 == sysStatus1 ? "未执行" : 2 == sysStatus1 ? "正常" : "异常");
                }
                //系统审核时间
                HSSFCell hssfCell12 = hssfRow.createCell(12);
                hssfCell12.setCellType(CellType.STRING);//设置表格类型
                hssfCell12.setCellStyle(style1);
                hssfCell12.setCellValue("");
                if(null != map.get("sysCreateTime")){
                    hssfCell12.setCellValue(map.get("sysCreateTime").toString());
                }
                //人工审核
                HSSFCell hssfCell13 = hssfRow.createCell(13);
                hssfCell13.setCellType(CellType.STRING);//设置表格类型
                hssfCell13.setCellStyle(style1);
                hssfCell13.setCellValue("");
                if(null != map.get("artificialStatus")){
                    //人工审核状态(1=未执行,2=正常,3=异常)
                    Integer artificialStatus = Integer.valueOf(map.get("artificialStatus").toString());
                    hssfCell13.setCellValue(1 == artificialStatus ? "未执行" : 2 == artificialStatus ? "正常" : "异常");
                }
                //审核意见
                HSSFCell hssfCell14 = hssfRow.createCell(14);
                hssfCell14.setCellType(CellType.STRING);//设置表格类型
                hssfCell14.setCellStyle(style1);
                hssfCell14.setCellValue("");
                if(null != map.get("remark")){
                    hssfCell14.setCellValue(map.get("remark").toString());
                }
                //审核人
                HSSFCell hssfCell15 = hssfRow.createCell(15);
                hssfCell15.setCellType(CellType.STRING);//设置表格类型
                hssfCell15.setCellStyle(style1);
                hssfCell15.setCellValue("");
                if(null != map.get("artificialUser")){
                    hssfCell15.setCellValue(map.get("artificialUser").toString());
                }
                //审核时间
                HSSFCell hssfCell16 = hssfRow.createCell(16);
                hssfCell16.setCellType(CellType.STRING);//设置表格类型
                hssfCell16.setCellStyle(style1);
                hssfCell16.setCellValue("");
                if(null != map.get("artificialCreateTime")){
                    hssfCell16.setCellValue(map.get("artificialCreateTime").toString());
                }
            }
            
            SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
            response.setHeader("Content-Disposition", "attachment; filename=" + URLEncoder.encode("任务记录(" + sdf.format(new Date()) + ").xls", "utf-8"));
            response.setContentType("application/vnd.ms-excel");
            ServletOutputStream out = response.getOutputStream();
            hssfWorkbook.write(out);
            out.flush();
            out.close();
        }catch (Exception e){
            e.printStackTrace();
        }
    }
    */
    @Override
    public void downloadTaskRecord(List<String> ids, String code, List<Integer> status,Integer sysStatus, Integer type,String typeName,HttpServletResponse response){
        List<Map<String, Object>> mapList = this.baseMapper.getDownloadTaskRecord(ids, code, status, sysStatus, type, typeName);
        try {
            HSSFWorkbook hssfWorkbook = new HSSFWorkbook();
            // 1. 按 name 分组(先获取所有唯一 name)
            Set<Object> nameSet = new HashSet<>();
            for (Map<String, Object> map : mapList) {
                nameSet.add(map.get("name")); // 收集所有不重复的 name
            }
 
            // 2. 创建单元格样式(提取为公共方法,避免重复代码)
            HSSFCellStyle titleStyle = createTitleStyle(hssfWorkbook);
            HSSFCellStyle contentStyle = createContentStyle(hssfWorkbook);
            List<Icon> icons = iconService.selectList(null);
 
            // 3. 为每个 name 创建一个 Sheet
            for (Object name : nameSet) {
                if (name == null) continue; // 跳过 name 为 null 的情况
 
                // 创建 Sheet(以 name 为 Sheet 名称,可根据需求自定义)
                HSSFSheet sheet = hssfWorkbook.createSheet(name.toString());
                sheet.setDefaultColumnWidth(20);
                sheet.setDefaultRowHeight((short) 800);
 
                // 设置表头(与原逻辑一致)
                initSheetHeader(sheet, hssfWorkbook, titleStyle);
 
                // 筛选当前 name 的数据
                List<Map<String, Object>> groupData = mapList.stream()
                        .filter(map -> name.equals(map.get("name")))
                        .collect(Collectors.toList());
 
                // 填充当前 Sheet 的数据
                fillSheetData(sheet, hssfWorkbook, groupData, contentStyle, icons);
            }
 
            // 4. 输出 Excel 到响应
            SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
            response.setHeader("Content-Disposition", "attachment; filename=" + URLEncoder.encode("任务记录(" + sdf.format(new Date()) + ").xls", "utf-8"));
            response.setContentType("application/vnd.ms-excel");
            ServletOutputStream out = response.getOutputStream();
            hssfWorkbook.write(out);
            out.flush();
            out.close();
 
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
    // -------------------- 以下是抽取的工具方法 --------------------
 
    /**
     * 创建标题样式(加粗、居中)
     */
    private HSSFCellStyle createTitleStyle(HSSFWorkbook workbook) {
        HSSFCellStyle style = workbook.createCellStyle();
        style.setVerticalAlignment(VerticalAlignment.CENTER);
        style.setAlignment(HorizontalAlignment.CENTER);
        HSSFFont font = workbook.createFont();
        font.setBold(true);
        style.setFont(font);
        return style;
    }
 
    /**
     * 创建内容样式(居中,不加粗)
     */
    private HSSFCellStyle createContentStyle(HSSFWorkbook workbook) {
        HSSFCellStyle style = workbook.createCellStyle();
        style.setVerticalAlignment(VerticalAlignment.CENTER);
        style.setAlignment(HorizontalAlignment.CENTER);
        return style;
    }
 
    /**
     * 初始化 Sheet 表头(合并单元格 + 标题)
     */
    private void initSheetHeader(HSSFSheet sheet, HSSFWorkbook workbook, HSSFCellStyle titleStyle) {
        // 合并单元格
        for (int i = 0; i < 10; i++) {
            CellRangeAddress cellRangeAddress = new CellRangeAddress(0, 1, i, i);
            sheet.addMergedRegion(cellRangeAddress);
        }
        CellRangeAddress cellRangeAddress = new CellRangeAddress(0, 0, 10, 16);
        sheet.addMergedRegion(cellRangeAddress);
 
        // 第一行标题
        HSSFRow row0 = sheet.createRow(0);
        List<String> titles0 = Arrays.asList("序号", "任务名称", "任务类型", "执行时间", "执行状态", "车牌号", "运输公司", "年审有效期", "巡检状态", "车辆轨迹图片", "货箱通道");
        for (int i = 0; i < titles0.size(); i++) {
            HSSFCell cell = row0.createCell(i);
            cell.setCellType(CellType.STRING);
            cell.setCellStyle(titleStyle);
            cell.setCellValue(titles0.get(i));
        }
 
        // 第二行标题
        HSSFRow row1 = sheet.createRow(1);
        List<String> titles1 = Arrays.asList("图片", "系统审核", "系统审核时间", "人工审核", "审核意见", "审核人", "审核时间");
        for (int i = 0; i < titles1.size(); i++) {
            HSSFCell cell = row1.createCell(i + 10);
            cell.setCellType(CellType.STRING);
            cell.setCellStyle(titleStyle);
            cell.setCellValue(titles1.get(i));
        }
    }
 
    /**
     * 填充 Sheet 数据(根据分组数据填充行)
     */
    private void fillSheetData(HSSFSheet sheet, HSSFWorkbook workbook, List<Map<String, Object>> dataList, HSSFCellStyle contentStyle, List<Icon> icons) {
        HSSFPatriarch patriarch = sheet.createDrawingPatriarch(); // 用于插入图片
 
        for (int i = 0; i < dataList.size(); i++) {
            HSSFRow row = sheet.createRow(i + 2); // 从第3行开始(索引2)
            Map<String, Object> map = dataList.get(i);
 
            // 1. 序号
            setCellValue(row, 0, (i + 1) + "", contentStyle);
 
            // 2. 任务名称
            setCellValue(row, 1, map.get("name"), contentStyle);
 
            // 3. 任务类型
            setCellValue(row, 2, map.get("taskType"), contentStyle);
 
            // 4. 执行时间
            setCellValue(row, 3, map.get("executionTime"), contentStyle);
 
            // 5. 执行状态(带状态转换逻辑)
            Object statusObj = map.get("status");
            String statusValue = "";
            if (statusObj != null) {
                Integer status = Integer.valueOf(statusObj.toString());
                statusValue = status == 1 ? "待执行" : status == 2 ? "进行中" : status == 3 ? "成功" : "失败";
 
                // 处理图片逻辑(根据状态设置默认图片)
                if (status == 5) {
                    map.put("imageUrl", icons.stream().filter(s -> s.getType() == 2).findFirst().orElse(null).getUrl());
                } else if (ToolUtil.isEmpty(map.get("imageUrl"))) {
                    map.put("imageUrl", icons.stream().filter(s -> s.getType() == 3).findFirst().orElse(null).getUrl());
                }
                if (status == 4) {
                    map.put("imageUrl", icons.stream().filter(s -> s.getType() == 1).findFirst().orElse(null).getUrl());
                }
            }
            setCellValue(row, 4, statusValue, contentStyle);
 
            // 6. 车牌号
            setCellValue(row, 5, map.get("vehicleNum"), contentStyle);
 
            // 7. 运输公司
            setCellValue(row, 6, map.get("companyName"), contentStyle);
 
            // 8. 年审有效期
            setCellValue(row, 7, map.get("inspectPeriodEnd"), contentStyle);
 
            // 9. 巡检状态
            Object vehiclesStatusObj = map.get("vehiclesStatus");
            String vehiclesStatusValue = "";
            if (vehiclesStatusObj != null) {
                Integer vehiclesStatus = Integer.valueOf(vehiclesStatusObj.toString());
                vehiclesStatusValue = vehiclesStatus == 1 ? "未执行" : vehiclesStatus == 2 ? "正常" : "异常";
            }
            setCellValue(row, 8, vehiclesStatusValue, contentStyle);
 
            // 10. 车辆轨迹图片(占位,实际图片在第10列插入)
            setCellValue(row, 9, "", contentStyle);
 
            // 11. 图片(插入图片到第10列)
            Object imageUrlObj = map.get("imageUrl");
            if (imageUrlObj != null) {
                try {
                    URL url = new URL(imageUrlObj.toString());
                    URLConnection conn = url.openConnection();
                    conn.connect();
                    InputStream is = conn.getInputStream();
                    byte[] bytes = IOUtils.toByteArray(is);
                    int pictureIdx = workbook.addPicture(bytes, Workbook.PICTURE_TYPE_PNG);
                    is.close();
 
                    // 图片位置:第10列(索引10),当前行(i+2)
                    HSSFClientAnchor anchor = new HSSFClientAnchor(0, 0, 0, 0, (short) 10, i + 2, (short) 11, i + 3);
                    patriarch.createPicture(anchor, pictureIdx);
                } catch (Exception e) {
                    e.printStackTrace();
                }
            }
 
            // 12. 系统审核
            Object sysStatusObj = map.get("sysStatus");
            String sysStatusValue = "";
            if (sysStatusObj != null) {
                Integer sysStatus = Integer.valueOf(sysStatusObj.toString());
                sysStatusValue = sysStatus == 1 ? "未执行" : sysStatus == 2 ? "正常" : "异常";
            }
            setCellValue(row, 11, sysStatusValue, contentStyle);
 
            // 13. 系统审核时间
            setCellValue(row, 12, map.get("sysCreateTime"), contentStyle);
 
            // 14. 人工审核
            Object artificialStatusObj = map.get("artificialStatus");
            String artificialStatusValue = "";
            if (artificialStatusObj != null) {
                Integer artificialStatus = Integer.valueOf(artificialStatusObj.toString());
                artificialStatusValue = artificialStatus == 1 ? "未执行" : artificialStatus == 2 ? "正常" : "异常";
            }
            setCellValue(row, 13, artificialStatusValue, contentStyle);
 
            // 15. 审核意见
            setCellValue(row, 14, map.get("remark"), contentStyle);
 
            // 16. 审核人
            setCellValue(row, 15, map.get("artificialUser"), contentStyle);
 
            // 17. 审核时间
            setCellValue(row, 16, map.get("artificialCreateTime"), contentStyle);
        }
    }
 
    /**
     * 通用单元格赋值方法(处理 null 情况)
     */
    private void setCellValue(HSSFRow row, int cellIndex, Object value, HSSFCellStyle style) {
        HSSFCell cell = row.createCell(cellIndex);
        cell.setCellType(CellType.STRING);
        cell.setCellStyle(style);
        cell.setCellValue(value != null ? value.toString() : "");
    }
}