puzhibing
2026-04-10 aacc83ac6473694c278de0d18de453774009d864
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
package com.xiaopeng.maintenance.service.impl;
 
import com.alibaba.fastjson2.JSONObject;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.StringUtils;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.xiaopeng.api.dto.pushMessage.AliPushMessageDTO;
import com.xiaopeng.api.feignClient.account.XpOperationUserClient;
import com.xiaopeng.api.feignClient.account.XpOperationUserNotificationClient;
import com.xiaopeng.api.feignClient.maintenance.XpCheckLocationClient;
import com.xiaopeng.api.feignClient.pushMessage.XpAliPushMessageClient;
import com.xiaopeng.api.feignClient.pushMessage.XpPushMessageClient;
import com.xiaopeng.api.feignClient.vehicleConvoy.XpFleetClient;
import com.xiaopeng.api.feignClient.vehicleConvoy.XpFleetOperationsClient;
import com.xiaopeng.api.model.account.XpOperationsUser;
import com.xiaopeng.api.model.account.XpOperationsUserNotification;
import com.xiaopeng.api.model.maintenance.*;
import com.xiaopeng.api.model.vehicleConvoy.XpFleet;
import com.xiaopeng.common.core.domain.R;
import com.xiaopeng.common.core.enums.NotificationTypeEnum;
import com.xiaopeng.common.core.utils.poi.ExcelUtil;
import com.xiaopeng.common.core.web.page.PageInfo;
import com.xiaopeng.maintenance.dto.ExportSchedulingDetailDto;
import com.xiaopeng.maintenance.dto.SchedulingDetailDto;
import com.xiaopeng.maintenance.dto.ShiftAttendanceDTO;
import com.xiaopeng.maintenance.mapper.XpCheckLocationMapper;
import com.xiaopeng.maintenance.mapper.XpClockInMapper;
import com.xiaopeng.maintenance.mapper.XpSchedulingDetailMapper;
import com.xiaopeng.maintenance.mapper.XpShiftMapper;
import com.xiaopeng.maintenance.service.IXpSchedulingDetailService;
import com.xiaopeng.maintenance.service.IXpSchedulingService;
import com.xiaopeng.maintenance.service.IXpShiftService;
import com.xiaopeng.maintenance.vo.*;
import org.apache.poi.hssf.usermodel.*;
import org.apache.poi.hssf.util.HSSFColor;
import org.apache.poi.ss.usermodel.*;
import org.apache.poi.ss.util.CellRangeAddress;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
 
import javax.annotation.Resource;
import javax.servlet.ServletOutputStream;
import javax.servlet.http.HttpServletResponse;
import java.net.URLEncoder;
import java.time.DayOfWeek;
import java.time.LocalDate;
import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;
import java.time.format.TextStyle;
import java.time.temporal.TemporalAdjusters;
import java.util.*;
import java.util.concurrent.LinkedBlockingQueue;
import java.util.concurrent.ThreadPoolExecutor;
import java.util.concurrent.TimeUnit;
import java.util.stream.Collectors;
 
/**
 * <p>
 * 排班明细 服务实现类
 * </p>
 *
 * @author mitao
 * @since 2026-02-03
 */
@Service
public class XpSchedulingDetailServiceImpl extends ServiceImpl<XpSchedulingDetailMapper, XpSchedulingDetail> implements IXpSchedulingDetailService {
 
    @Resource
    private XpFleetClient xpFleetClient;
    @Resource
    private XpCheckLocationMapper xpCheckLocationMapper;
    @Resource
    private XpShiftMapper xpShiftMapper;
    @Resource
    private XpClockInMapper xpClockInMapper;
    @Resource
    private IXpSchedulingService xpSchedulingService;
    @Resource
    private IXpShiftService xpShiftService;
    @Resource
    private XpFleetOperationsClient xpFleetOperationsClient;
    @Resource
    private XpOperationUserClient xpOperationUserClient;
    @Resource
    private XpAliPushMessageClient xpAliPushMessageClient;
    @Resource
    private XpOperationUserNotificationClient xpOperationUserNotificationClient;
 
    @Override
    public ShiftAttendanceVO shiftAttendance(ShiftAttendanceDTO dto) {
        // 查询用户所属车队
        XpFleet xpFleet = xpFleetClient.getFleetByOperationsUserId(dto.getOperationsUserId()).getData();
        if (Objects.isNull(xpFleet)) {
            return null;
        }
        // 查询打卡地
        XpCheckLocation xpCheckLocation = xpCheckLocationMapper.selectById(xpFleet.getLocationId());
        if (Objects.isNull(xpCheckLocation)) {
            return null;
        }
        // 查询排班明细
        XpSchedulingDetail schedulingDetail = this.getOne(Wrappers.lambdaQuery(XpSchedulingDetail.class)
                .likeRight(XpSchedulingDetail::getPlanTime, dto.getSchedulingDate())
                .eq(XpSchedulingDetail::getFleetId, xpFleet.getId())
                .last("LIMIT 1"));
        if (Objects.isNull(schedulingDetail)) {
            return null;
        }
        // 查询班次
        XpShift xpShift = xpShiftMapper.selectById(schedulingDetail.getShiftId());
        if (Objects.isNull(xpShift)) {
            return null;
        }
 
        ShiftAttendanceVO shiftAttendanceVO = new ShiftAttendanceVO();
        shiftAttendanceVO.setXpShift(xpShift);
        shiftAttendanceVO.setXpCheckLocation(xpCheckLocation);
        // 查询当前用户打卡信息
        XpClockIn xpClockIn = xpClockInMapper.selectOne(Wrappers.lambdaQuery(XpClockIn.class)
                .eq(XpClockIn::getAppUserId, dto.getOperationsUserId())
                .likeRight(XpClockIn::getClockInTime, dto.getSchedulingDate())
                .last("LIMIT 1"));
        if (Objects.isNull(xpClockIn)) {
            shiftAttendanceVO.setClockIn(false);
            shiftAttendanceVO.setClockOut(false);
        } else {
            shiftAttendanceVO.setXpClockIn(xpClockIn);
            shiftAttendanceVO.setClockIn(true);
            shiftAttendanceVO.setClockOut(xpClockIn.getClockOutTime() != null);
        }
        return shiftAttendanceVO;
    }
 
 
    /**
     * 排班明细分页列表
     *
     * @param query
     * @return
     */
    @Override
    public PageInfo<SchedulingDetailDto> pageList(SchedulingDetailVo query) {
        PageInfo<SchedulingDetailDto> pageInfo = new PageInfo<>(query.getPageNum(), query.getPageSize());
        List<SchedulingDetailDto> schedulingDetailDtos = this.baseMapper.pageList(pageInfo, query.getMonth());
        LocalDate parse1 = LocalDate.parse(query.getMonth() + "-01", DateTimeFormatter.ofPattern("yyyy-MM-dd"));
        //找出月份的最大天
        int dayOfMonth = parse1.with(TemporalAdjusters.lastDayOfMonth()).getDayOfMonth();
        List<XpShift> xpShifts = xpShiftService.list(new QueryWrapper<XpShift>().eq("is_deleted", 0).orderByAsc("id"));
 
        for (SchedulingDetailDto schedulingDetailDto : schedulingDetailDtos) {
            String days = schedulingDetailDto.getDays();
            if (StringUtils.isNotBlank(days)) {
                String[] split = days.split(",");
                List<String> list = Arrays.asList(split);
                for (int i = 1; i <= dayOfMonth; i++) {
                    Map<String, String> map1 = new HashMap<>();
                    for (String s : split) {
                        String[] split1 = s.split("_");
                        if (i == Integer.parseInt(split1[0])) {
                            map1.put("shiftId", split1[1]);
                            map1.put("shiftName", split1[2]);
                            map1.put("shiftColor", split1[3]);
                            break;
                        }
                    }
                    setEnglishDay(i, schedulingDetailDto, map1);
                }
 
                List<Map<String, String>> maps = new ArrayList<>();
                for (XpShift xpShift : xpShifts) {
                    String shiftName = xpShift.getShiftName();
                    long allDay = list.stream().filter(s -> s.contains(shiftName)).count();
                    Map<String, String> map = new HashMap<>();
                    map.put("shiftId", xpShift.getId().toString());
                    map.put("shiftName", shiftName);
                    map.put("shiftColor", xpShift.getShiftColor());
                    map.put("count", allDay + "");
                    maps.add(map);
 
                }
                schedulingDetailDto.setSubtotal(maps);
            }
        }
        pageInfo.setRecords(schedulingDetailDtos);
        return pageInfo;
    }
 
 
    /**
     * 给每天的字段赋值
     *
     * @param day
     * @param dto
     * @param map
     */
    private void setEnglishDay(int day, SchedulingDetailDto dto, Map<String, String> map) {
        switch (day) {
            case 1:
                dto.setOne(map);
                break;
            case 2:
                dto.setTwo(map);
                break;
            case 3:
                dto.setThree(map);
                break;
            case 4:
                dto.setFour(map);
                break;
            case 5:
                dto.setFive(map);
                break;
            case 6:
                dto.setSix(map);
                break;
            case 7:
                dto.setSeven(map);
                break;
            case 8:
                dto.setEight(map);
                break;
            case 9:
                dto.setNine(map);
                break;
            case 10:
                dto.setTen(map);
                break;
            case 11:
                dto.setEleven(map);
                break;
            case 12:
                dto.setTwelve(map);
                break;
            case 13:
                dto.setThirteen(map);
                break;
            case 14:
                dto.setFourteen(map);
                break;
            case 15:
                dto.setFifteen(map);
                break;
            case 16:
                dto.setSixteen(map);
                break;
            case 17:
                dto.setSeventeen(map);
                break;
            case 18:
                dto.setEighteen(map);
                break;
            case 19:
                dto.setNineteen(map);
                break;
            case 20:
                dto.setTwenty(map);
                break;
            case 21:
                dto.setTwentyOne(map);
                break;
            case 22:
                dto.setTwentyTwo(map);
                break;
            case 23:
                dto.setTwentyThree(map);
                break;
            case 24:
                dto.setTwentyFour(map);
                break;
            case 25:
                dto.setTwentyFive(map);
                break;
            case 26:
                dto.setTwentySix(map);
                break;
            case 27:
                dto.setTwentySeven(map);
                break;
            case 28:
                dto.setTwentyEight(map);
                break;
            case 29:
                dto.setTwentyNine(map);
                break;
            case 30:
                dto.setThirty(map);
                break;
            case 31:
                dto.setThirtyOne(map);
                break;
        }
    }
 
    /**
     * 给每天的字段赋值
     *
     * @param day
     * @param dto
     * @param shiftName
     */
    private void setEnglishDay(int day, ExportSchedulingDetailDto dto, String shiftName) {
        switch (day) {
            case 1:
                dto.setOne(shiftName);
                break;
            case 2:
                dto.setTwo(shiftName);
                break;
            case 3:
                dto.setThree(shiftName);
                break;
            case 4:
                dto.setFour(shiftName);
                break;
            case 5:
                dto.setFive(shiftName);
                break;
            case 6:
                dto.setSix(shiftName);
                break;
            case 7:
                dto.setSeven(shiftName);
                break;
            case 8:
                dto.setEight(shiftName);
                break;
            case 9:
                dto.setNine(shiftName);
                break;
            case 10:
                dto.setTen(shiftName);
                break;
            case 11:
                dto.setEleven(shiftName);
                break;
            case 12:
                dto.setTwelve(shiftName);
                break;
            case 13:
                dto.setThirteen(shiftName);
                break;
            case 14:
                dto.setFourteen(shiftName);
                break;
            case 15:
                dto.setFifteen(shiftName);
                break;
            case 16:
                dto.setSixteen(shiftName);
                break;
            case 17:
                dto.setSeventeen(shiftName);
                break;
            case 18:
                dto.setEighteen(shiftName);
                break;
            case 19:
                dto.setNineteen(shiftName);
                break;
            case 20:
                dto.setTwenty(shiftName);
                break;
            case 21:
                dto.setTwentyOne(shiftName);
                break;
            case 22:
                dto.setTwentyTwo(shiftName);
                break;
            case 23:
                dto.setTwentyThree(shiftName);
                break;
            case 24:
                dto.setTwentyFour(shiftName);
                break;
            case 25:
                dto.setTwentyFive(shiftName);
                break;
            case 26:
                dto.setTwentySix(shiftName);
                break;
            case 27:
                dto.setTwentySeven(shiftName);
                break;
            case 28:
                dto.setTwentyEight(shiftName);
                break;
            case 29:
                dto.setTwentyNine(shiftName);
                break;
            case 30:
                dto.setThirty(shiftName);
                break;
            case 31:
                dto.setThirtyOne(shiftName);
                break;
        }
    }
 
 
    /**
     * 保存排班明细
     *
     * @param vo
     * @return
     */
    @Override
    public R saveSchedulingDetail(SaveSchedulingVo vo, Long userId) {
        String month = vo.getMonth();
        List<XpSchedulingDetail> list = this.list(new QueryWrapper<XpSchedulingDetail>().eq("is_deleted", 0).last(" and DATE_FORMAT(plan_time,'%Y-%m') = '" + month + "'"));
        //保存排班数据
        XpScheduling xpScheduling = xpSchedulingService.getOne(new QueryWrapper<XpScheduling>().eq("is_deleted", 0).last(" and DATE_FORMAT(scheduling_date,'%Y-%m') = '" + month + "'"));
        if (null == xpScheduling) {
            xpScheduling = new XpScheduling();
            xpScheduling.setDeleted(false);
            xpScheduling.setCreateTime(LocalDateTime.now());
            xpScheduling.setUpdateTime(LocalDateTime.now());
            xpScheduling.setCreateBy(userId.toString());
            xpScheduling.setUpdateBy(userId.toString());
        }
        xpScheduling.setSchedulingDate(LocalDate.parse(month + "-01", DateTimeFormatter.ofPattern("yyyy-MM-dd")));
        xpScheduling.setCommitUserId(userId);
        xpSchedulingService.saveOrUpdate(xpScheduling);
 
        List<XpShift> xpShifts = xpShiftService.list(new QueryWrapper<XpShift>().eq("is_deleted", 0));
 
        //构建排班明细数据
        List<SaveSchedulingDetailVo> datas = vo.getDatas();
        List<XpSchedulingDetail> schedulingDetails = new ArrayList<>();
        //存放推送消息
        List<AliPushMessageDTO> pushMessage = new ArrayList<>();
        //存放通知数据
        List<XpOperationsUserNotification> notifications = new ArrayList<>();
        for (SaveSchedulingDetailVo data : datas) {
            Long fleetId = data.getFleetId();
            List<SaveSchedulingDetailDay> days = data.getDays();
            for (SaveSchedulingDetailDay day : days) {
                Long shiftId = day.getShiftId();
                if(null == shiftId){
                    continue;
                }
                LocalDate parse = LocalDate.parse(day.getDayTime(), DateTimeFormatter.ofPattern("yyyy-MM-dd"));
                //查询判断数据是否存在
                XpSchedulingDetail schedulingDetail = list.stream().filter(s -> s.getFleetId().equals(fleetId)
                        && s.getPlanTime().equals(parse)
                ).findAny().orElse(null);
                //构建存储对象
                if (null == schedulingDetail) {
                    schedulingDetail = new XpSchedulingDetail();
                    schedulingDetail.setCreateBy(userId.toString());
                    schedulingDetail.setCreateTime(LocalDateTime.now());
                    schedulingDetail.setUpdateBy(userId.toString());
                    schedulingDetail.setUpdateTime(LocalDateTime.now());
                    schedulingDetail.setDeleted(false);
                } else {
                    //构建通知消息
                    Long old_shiftId = schedulingDetail.getShiftId();
                    if (!old_shiftId.equals(shiftId)) {
                        //获取车队的所有人员
                        XpShift xpShift1 = xpShifts.stream().filter(s -> s.getId().equals(old_shiftId)).findAny().orElse(null);
                        XpShift xpShift2 = xpShifts.stream().filter(s -> s.getId().equals(shiftId)).findAny().orElse(null);
 
                        List<Long> operationsIds = xpFleetOperationsClient.getOperationsIdsByFleetId(fleetId).getData();
                        if (null != operationsIds && !operationsIds.isEmpty()) {
                            List<XpOperationsUser> data1 = xpOperationUserClient.listByIds(operationsIds).getData();
                            for (XpOperationsUser xpOperationsUser : data1) {
                                String nickName = xpOperationsUser.getNickName();
                                String message = nickName + "请注意," + month + "你的排班由" + xpShift1.getShiftName() + "变更为了" + xpShift2.getShiftName() + "。详情请联系平台管理员。";
                                //构建消息推送对象
                                AliPushMessageDTO aliPushMessageDTO = buildPushMessage(message, xpOperationsUser);
                                pushMessage.add(aliPushMessageDTO);
                                //构建消息存储
                                XpOperationsUserNotification operationsUserNotification = buildXpOperationsUserNotification(message, aliPushMessageDTO.getBody(), xpOperationsUser);
                                notifications.add(operationsUserNotification);
                            }
                        }
                    }
 
                }
                schedulingDetail.setSchedulingId(xpScheduling.getId());
                schedulingDetail.setFleetId(fleetId);
                schedulingDetail.setShiftId(shiftId);
 
                XpShift xpShift = xpShifts.stream().filter(s -> s.getId().equals(shiftId)).findAny().orElse(null);
                if (null != xpShift) {
                    schedulingDetail.setIsAllDay(xpShift.getShiftName().contains("全天"));
                }
                schedulingDetail.setPlanTime(parse);
                schedulingDetails.add(schedulingDetail);
            }
        }
        //批量保存排班明细数据
 
        //找出需要删除的数据
        Set<Long> collect = schedulingDetails.stream().filter(s -> s.getId() != null).collect(Collectors.toList()).stream().map(XpSchedulingDetail::getId).collect(Collectors.toSet());
        List<XpSchedulingDetail> collect1 = list.stream().filter(s -> !collect.contains(s.getId())).collect(Collectors.toList());
        collect1.forEach(s -> s.setDeleted(true));
        collect1.addAll(schedulingDetails);
        if (!collect1.isEmpty()) {
            this.saveOrUpdateBatch(collect1);
        }
        //异步处理消息通知数据
        pushMessage(pushMessage, notifications);
        return R.ok();
    }
 
 
    /**
     * 构建推送消息对象
     *
     * @param message
     * @param xpOperationsUser
     * @return
     */
    private AliPushMessageDTO buildPushMessage(String message, XpOperationsUser xpOperationsUser) {
        JSONObject body = new JSONObject();
        body.put("content", message);
        body.put("strategy", 4);
        body.put("type", 0);
        body.put("id", 0);
 
        AliPushMessageDTO aliPushMessageDTO = new AliPushMessageDTO();
        aliPushMessageDTO.setDeviceType(xpOperationsUser.getDeviceType());
        aliPushMessageDTO.setAccount(xpOperationsUser.getPhoneNumber());
        aliPushMessageDTO.setTitle("排班变更提示");
        aliPushMessageDTO.setBody(body.toJSONString());
        aliPushMessageDTO.setPushType("NOTICE");
        return aliPushMessageDTO;
    }
 
 
    /**
     * 构建消息存储
     *
     * @param body
     * @param xpOperationsUser
     * @return
     */
    private XpOperationsUserNotification buildXpOperationsUserNotification(String message, String body, XpOperationsUser xpOperationsUser) {
        XpOperationsUserNotification operationsUserNotification = new XpOperationsUserNotification();
        operationsUserNotification.setNotificationType(NotificationTypeEnum.SCHEDULE_CHANGED.getCode());
        operationsUserNotification.setNotificationContent(message);
        operationsUserNotification.setOperationsUserId(xpOperationsUser.getId());
        operationsUserNotification.setReadStatus(0);
        operationsUserNotification.setNotificationExt(body);
        operationsUserNotification.setDataId(0L);
        return operationsUserNotification;
    }
 
 
    /**
     * 异步处理消息推送
     *
     * @param pushMessage
     * @param notifications
     */
    private void pushMessage(List<AliPushMessageDTO> pushMessage, List<XpOperationsUserNotification> notifications) {
        if (pushMessage.isEmpty()) {
            return;
        }
        ThreadPoolExecutor executor = new ThreadPoolExecutor(1, 1, 0L, TimeUnit.MILLISECONDS, new LinkedBlockingQueue<Runnable>());
        executor.execute(() -> {
            xpOperationUserNotificationClient.addNotificationBatch(notifications);
            for (AliPushMessageDTO aliPushMessageDTO : pushMessage) {
                xpAliPushMessageClient.pushMessage(aliPushMessageDTO);
            }
        });
    }
 
 
    /**
     * 导出排班数据
     *
     * @param month
     * @param response
     */
    @Override
    public void exportExcel(String month, HttpServletResponse response) {
        PageInfo<SchedulingDetailDto> pageInfo = new PageInfo<>(0, 10000);
        List<SchedulingDetailDto> schedulingDetailDtos = this.baseMapper.pageList(pageInfo, month);
        //获取月份的最大天数
        int m = Integer.parseInt(month.split("-")[1]);
        LocalDate parse1 = LocalDate.parse(month + "-01", DateTimeFormatter.ofPattern("yyyy-MM-dd"));
        int dayOfMonth = parse1.with(TemporalAdjusters.lastDayOfMonth()).getDayOfMonth();
        HSSFWorkbook hssfWorkbook = new HSSFWorkbook();
        HSSFCellStyle cellStyle = hssfWorkbook.createCellStyle();
        cellStyle.setAlignment(HorizontalAlignment.CENTER);
        cellStyle.setVerticalAlignment(VerticalAlignment.CENTER);
        HSSFSheet hssfSheet = hssfWorkbook.createSheet();
        hssfSheet.setColumnWidth(0, 6 * 256);
        hssfSheet.setDefaultRowHeightInPoints(20f);
 
        HSSFCellStyle cellStyle1 = hssfWorkbook.createCellStyle();
        cellStyle1.setFillForegroundColor(IndexedColors.GREY_40_PERCENT.index);
        cellStyle1.setAlignment(HorizontalAlignment.CENTER);
        cellStyle1.setVerticalAlignment(VerticalAlignment.CENTER);
 
        //添加第一行标题
        HSSFRow hssfRow = hssfSheet.createRow(0);
        List<XpShift> xpShifts = xpShiftService.list(new QueryWrapper<XpShift>().eq("is_deleted", 0));
        //设置单元格数量
        int totalCell = dayOfMonth + 1 + xpShifts.size();
 
        //设置表头样式,合并单元格
        CellRangeAddress region = new CellRangeAddress(0, 1, 0, 0);
        hssfSheet.addMergedRegion(region);
        region = new CellRangeAddress(0, 1, dayOfMonth + 1, totalCell - 1);
        hssfSheet.addMergedRegion(region);
 
 
        //设置单元格数据
        for (int i = 0; i < totalCell; i++) {
            HSSFCell cell = hssfRow.createCell(i);
            cell.setCellStyle(cellStyle1);
            if(0 == i){
                cell.setCellValue("车队");
            }else if(i <= dayOfMonth){
                cell.setCellValue(m + "/" + i);
            }else{
                cell.setCellValue("排班小计");
            }
        }
        //添加第二行标题
        HSSFRow hssfRow1 = hssfSheet.createRow(1);
        //设置单元格数量
        //设置单元格数据
        for (int i = 0; i < totalCell; i++) {
            HSSFCell cell = hssfRow1.createCell(i);
            cell.setCellStyle(cellStyle1);
            if(i > 0 && i <= dayOfMonth){
                LocalDate parse = LocalDate.parse(month + "-" + String.format("%02d", i), DateTimeFormatter.ofPattern("yyyy-MM-dd"));
                String week = "";
                switch (parse.getDayOfWeek()) {
                    case MONDAY:
                        week = "一";
                        break;
                    case TUESDAY:
                        week = "二";
                        break;
                    case WEDNESDAY:
                        week = "三";
                        break;
                    case THURSDAY:
                        week = "四";
                        break;
                    case FRIDAY:
                        week = "五";
                        break;
                    case SATURDAY:
                        week = "六";
                        break;
                    case SUNDAY:
                        week = "日";
                        break;
                }
                cell.setCellValue(week);
            }
        }
        //构建数据部分的内容
        int rowNum = 2;
        for (SchedulingDetailDto schedulingDetailDto : schedulingDetailDtos) {
            HSSFRow row = hssfSheet.createRow(rowNum);
            HSSFCell cell = row.createCell(0);
            cell.setCellStyle(cellStyle);
            cell.setCellValue(schedulingDetailDto.getFleetName());
 
            String days = schedulingDetailDto.getDays();
            if (StringUtils.isNotBlank(days)) {
                String[] split = days.split(",");
                List<String> list = Arrays.asList(split);
                for (int i = 1; i <= dayOfMonth; i++) {
                    String shiftName = "休";
                    for (String s : split) {
                        String[] split1 = s.split("_");
                        if (i == Integer.parseInt(split1[0])) {
                            shiftName = split1[2];
                            break;
                        }
                    }
 
                    HSSFCell cell1 = row.createCell(i);
                    cell1.setCellValue(shiftName);
                }
 
                for (int i = 0; i < xpShifts.size(); i++) {
                    String shiftName = xpShifts.get(i).getShiftName();
                    long allDay = list.stream().filter(s -> s.contains(shiftName)).count();
                    HSSFCell cell1 = row.createCell(dayOfMonth + 1 + i);
                    cell1.setCellStyle(cellStyle);
                    cell1.setCellValue(shiftName + " " + allDay);
                }
            }
            rowNum++;
        }
        try {
            response.setContentType("application/vnd.ms-excel");
            response.setHeader("Content-Disposition", "attachment;filename=" + URLEncoder.encode("排班计划管理.xls", "UTF-8"));
            ServletOutputStream outputStream = response.getOutputStream();
            hssfWorkbook.write(outputStream);
            outputStream.flush();
            outputStream.close();
        }catch (Exception e){
            e.printStackTrace();
        }
    }
 
 
}