liujie
2026-04-20 af1bdf94021985a93f7e95854915653b6a2e9580
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
package com.stylefeng.guns.modular.crossCity.server.impl;
 
import cn.hutool.core.date.DateUnit;
import cn.hutool.cron.CronUtil;
import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.mapper.EntityWrapper;
import com.baomidou.mybatisplus.service.impl.ServiceImpl;
import com.stylefeng.guns.core.util.ToolUtil;
import com.stylefeng.guns.modular.crossCity.dao.LinePriceMapper;
import com.stylefeng.guns.modular.crossCity.dao.LineShiftDriverMapper;
import com.stylefeng.guns.modular.crossCity.dao.LineShiftMapper;
import com.stylefeng.guns.modular.crossCity.model.LineShift;
import com.stylefeng.guns.modular.crossCity.model.LineShiftDriver;
import com.stylefeng.guns.modular.crossCity.model.OrderCrossCity;
import com.stylefeng.guns.modular.crossCity.server.ILineService;
import com.stylefeng.guns.modular.crossCity.server.ILineShiftDriverService;
import com.stylefeng.guns.modular.crossCity.server.ILineSiteService;
import com.stylefeng.guns.modular.crossCity.server.IOrderCrossCityService;
import com.stylefeng.guns.modular.crossCity.warpper.DriverShiftListWarpper;
import com.stylefeng.guns.modular.crossCity.warpper.LineShiftItemWarpper;
import com.stylefeng.guns.modular.crossCity.warpper.ScanOrderDetailWarpper;
import com.stylefeng.guns.modular.system.dao.CarModelMapper;
import com.stylefeng.guns.modular.system.dao.CarServiceMapper;
import com.stylefeng.guns.modular.system.dao.ServerCarModelMapper;
import com.stylefeng.guns.modular.system.model.CarModel;
import com.stylefeng.guns.modular.system.model.CarService;
import com.stylefeng.guns.modular.system.service.ICarService;
import com.stylefeng.guns.modular.system.service.IDriverService;
import com.stylefeng.guns.modular.system.util.DateUtil;
import com.stylefeng.guns.modular.system.util.ResultUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
 
import javax.annotation.Resource;
import java.text.SimpleDateFormat;
import java.util.*;
import java.util.stream.Collectors;
 
 
@Service
public class LineShiftDriverServiceImpl extends ServiceImpl<LineShiftDriverMapper, LineShiftDriver> implements ILineShiftDriverService {
 
    @Resource
    private LineShiftDriverMapper lineShiftDriverMapper;
 
    @Autowired
    private DateUtil dateUtil;
 
    @Autowired
    private ICarService carService;
 
    @Autowired
    private IDriverService driverService;
 
    @Resource
    private CarModelMapper carModelMapper;
 
    @Resource
    private LineShiftMapper lineShiftMapper;
 
    @Resource
    private LinePriceMapper linePriceMapper;
 
    @Resource
    private CarServiceMapper carServiceMapper;
 
    @Autowired
    private IOrderCrossCityService orderCrossCityService;
 
    @Autowired
    private ILineService lineService;
 
 
 
    /**
     * 预约班次操作
     * @param lineShiftId
     * @param time
     * @param driverId
     * @return
     * @throws Exception
     */
    @Override
    public synchronized ResultUtil reservation(Integer type, Integer lineShiftId, String time, Integer driverId) throws Exception {
        Map<String, Date> date = dateUtil.getStartAndEndDate(time);
        List<LineShiftDriver> startTime = lineShiftDriverMapper.queryByDriver(lineShiftId, driverId, date.get("startTime"));
        if(type == 1 && startTime.size() > 0){
            return ResultUtil.error("不能重复预约");
        }
        if(type == 2 && startTime.size() == 0){
            return ResultUtil.error("您还没预约呢");
        }
        if(type == 1){//预约操作
            boolean b = this.judgeTime(driverId, lineShiftId, date.get("startTime"));
            if(b){
                return ResultUtil.error("预约间隔超出给定可预约范围");
            }
            int num = lineShiftDriverMapper.queryByDriver(lineShiftId, null, date.get("startTime")).size();
            LineShift lineShift = lineShiftMapper.selectById(lineShiftId);
            Integer carNum = lineShift.getCarNum();
            if(num >= carNum){
                return ResultUtil.error("该班次已经超出了可预约车次");
            }
            CarService query = carServiceMapper.query(3, driverService.selectById(driverId).getCarId());
            if(null == query){
                return ResultUtil.error("车辆未开通该服务");
            }
            Map<String, Object> query1 = linePriceMapper.query(lineShift.getLineId(), query.getServerCarModelId());
            if(null == query1){
                return ResultUtil.error("该线路还未设置服务车型相关数据,请联系管理员!");
            }
            CarModel carModel = carModelMapper.selectById(carService.selectById(driverService.selectById(driverId).getCarId()).getCarModelId());
            LineShiftDriver lineShiftDriver = new LineShiftDriver();
            lineShiftDriver.setLineShiftId(lineShiftId);
            lineShiftDriver.setDriverId(driverId);
            lineShiftDriver.setDay(date.get("startTime"));
            lineShiftDriver.setLaveSeat(carModel.getSeat() - 1);
            String seatNumber = "";
            for(int i = 1; i < carModel.getSeat(); i++){
                seatNumber += i + ",";
            }
            lineShiftDriver.setLaveSeatNumber(seatNumber.substring(0, seatNumber.length() - 1));
            lineShiftDriver.setTotalSeat(carModel.getSeat() - 1);
            lineShiftDriver.setInserTime(new Date());
            lineShiftDriverMapper.insert(lineShiftDriver);
        }
        if(type == 2){//取消预约
            Integer id = startTime.get(0).getId();
            int i = orderCrossCityService.selectCount(
                    new EntityWrapper<OrderCrossCity>()
                            .eq("driverId", driverId)
                            .eq("lineShiftDriverId", id)
                            .eq("isDelete", 1)
                            .in("state", Arrays.asList(2, 3, 4, 5, 6, 7, 8, 9, 11))
            );
            if(i > 0){
                return ResultUtil.error("班次已使用,不允许取消");
            }
 
            lineShiftDriverMapper.deleteById(id);
        }
        return ResultUtil.success();
    }
 
 
 
    /**
     * 判断当前预约的班次时间与之前已经预约的班次时间的间隔是否大于设定的间隔时间
     * @param driverId
     * @param shiftId
     * @param day
     * @return
     */
    public boolean judgeTime(Integer driverId, Integer shiftId, Date day) throws Exception{
        //获取当前预约班次的开始时间
        LineShift lineShift = lineShiftMapper.selectById(shiftId);
        String startTime = lineShift.getStartTime();//需要预约的班次开始时间
        String endTime1 = lineShift.getEndTime();//需要预约的班次结束时间
 
        List<Integer> list = new ArrayList<>();//存储给定天的预约的班次
        List<LineShiftDriver> lineShiftDrivers = lineShiftDriverMapper.queryByDriver(null, driverId, day);
        for(LineShiftDriver lineShiftDriver : lineShiftDrivers){
            list.add(lineShiftDriver.getLineShiftId());
        }
 
        if(list.size() == 0){
            return false;
        }
 
        List<LineShift> lineShifts_ = lineShiftMapper.selectList(new EntityWrapper<LineShift>().eq("lineId", lineShift.getLineId()).eq("state", 1).orderBy("endTime"));
        int index = 0;//需要预约的数据下标
        for(int i = 0; i < lineShifts_.size(); i++){
            if(startTime.equals(lineShifts_.get(i).getStartTime()) && endTime1.equals(lineShifts_.get(i).getEndTime())){
                index = i;
                break;
            }
        }
 
        //找出预约数据最近的预约了的数据
        SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-DD HH:mm:ss");
        Integer shiftInterval = lineService.selectById(lineShift.getLineId()).getShiftInterval();//获取可预约的间隔(分钟)
        long time = simpleDateFormat.parse("1999-01-01 " + startTime + ":00").getTime();//当前预约的班次的开始时间
        long time1 = simpleDateFormat.parse("1999-01-01 " + endTime1 + ":00").getTime();//当前预约的班次的结束时间
        if(index == 0 && lineShifts_.size() > 1){
            for(LineShift lineShift1 : lineShifts_){
                if(list.contains(lineShift1.getId())){
                    long s = simpleDateFormat.parse("1999-01-01 " + lineShift1.getStartTime() + ":00").getTime();
                    long e = simpleDateFormat.parse("1999-01-01 " + lineShift1.getEndTime() + ":00").getTime();
                    if((s - time1) < (Long.valueOf(shiftInterval) * 60 * 1000)){
                        return true;
                    }
                }
            }
            return false;
        }
        if(index == lineShifts_.size() - 1 && lineShifts_.size() > 1){
            for(LineShift lineShift1 : lineShifts_){
                if(list.contains(lineShift1.getId())){
                    long s = simpleDateFormat.parse("1999-01-01 " + lineShift1.getStartTime() + ":00").getTime();
                    long e = simpleDateFormat.parse("1999-01-01 " + lineShift1.getEndTime() + ":00").getTime();
                    if((time - e) < (Long.valueOf(shiftInterval) * 60 * 1000)){
                        return true;
                    }
                }
            }
            return false;
        }
        if(index > 0 && index < lineShifts_.size() - 1 && lineShifts_.size() > 2){
            for(int i = index + 1; i < lineShifts_.size(); i++){
                if(list.contains(lineShifts_.get(i).getId())){
                    long s = simpleDateFormat.parse("1999-01-01 " + lineShifts_.get(i).getStartTime() + ":00").getTime();
                    long e = simpleDateFormat.parse("1999-01-01 " + lineShifts_.get(i).getEndTime() + ":00").getTime();
                    if((s - time1) < (Long.valueOf(shiftInterval) * 60 * 1000)){
                        return true;
                    }
                }
            }
            for(int i = 0; i < index; i++){
                if(list.contains(lineShifts_.get(i).getId())){
                    long s = simpleDateFormat.parse("1999-01-01 " + lineShifts_.get(i).getStartTime() + ":00").getTime();
                    long e = simpleDateFormat.parse("1999-01-01 " + lineShifts_.get(i).getEndTime() + ":00").getTime();
                    if((time - e) < (Long.valueOf(shiftInterval) * 60 * 1000)){
                        return true;
                    }
                }
            }
            return false;
        }
        return false;
    }
 
    /**
     * 【202601】查询司机排班列表
     * @param driverId 司机id
     * @param date 日期(yyyy-MM-dd),为空则默认当天
     * @return
     * @throws Exception
     */
    @Override
    public DriverShiftListWarpper queryDriverShiftList(Integer driverId, String date) throws Exception {
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
 
        // 如果日期为空,默认当天
        if (ToolUtil.isEmpty(date)) {
            date = sdf.format(new Date());
        }
 
        DriverShiftListWarpper result = new DriverShiftListWarpper();
 
        // 获取司机的服务车型ID(跨城业务 type=3)
        Integer serverCarModelId = null;
        Integer carId = driverService.selectById(driverId).getCarId();
        if (carId != null) {
            CarService carServiceQuery = carServiceMapper.query(3, carId);
            if (carServiceQuery != null) {
                serverCarModelId = carServiceQuery.getServerCarModelId();
            }
        }
 
        // 查询司机可预约的班次列表(包括已预约和未预约的)
        List<Map<String, Object>> shiftList = lineShiftDriverMapper.queryDriverAvailableShifts(driverId, date);
 
        // 按线路分组
        Map<Integer, List<Map<String, Object>>> lineGroupMap = new LinkedHashMap<>();
        Map<Integer, String> lineNameMap = new HashMap<>();
 
        for (Map<String, Object> shift : shiftList) {
            Integer lineId = Integer.valueOf(shift.get("lineId").toString());
            String lineName = shift.get("lineName").toString();
            lineNameMap.put(lineId, lineName);
 
            if (!lineGroupMap.containsKey(lineId)) {
                lineGroupMap.put(lineId, new ArrayList<>());
            }
            lineGroupMap.get(lineId).add(shift);
        }
 
        // 构建线路列表
        List<DriverShiftListWarpper.LineItemWarpper> lineList = new ArrayList<>();
        Date now = new Date();
        SimpleDateFormat timeFormat = new SimpleDateFormat("HH:mm");
        String currentTime = timeFormat.format(now);
        String today = sdf.format(now);
 
        for (Map.Entry<Integer, List<Map<String, Object>>> entry : lineGroupMap.entrySet()) {
            DriverShiftListWarpper.LineItemWarpper lineItem = new DriverShiftListWarpper.LineItemWarpper();
            lineItem.setLineId(entry.getKey());
            lineItem.setLineName(lineNameMap.get(entry.getKey()));
 
            // 查询线路单价
            Double unitPrice = null;
            if (serverCarModelId != null) {
                Map<String, Object> priceInfo = linePriceMapper.query(entry.getKey(), serverCarModelId);
                if (priceInfo != null && priceInfo.get("content") != null) {
                    // 解析 content JSON,获取 num1(单人价格)
                    String content = priceInfo.get("content").toString();
                    JSONObject jsonObject = JSON.parseObject(content);
                    String num1 = jsonObject.getString("num1");
                    if (num1 != null && !num1.isEmpty()) {
                        unitPrice = Double.parseDouble(num1);
                    }
                }
            }
            lineItem.setUnitPrice(unitPrice);
 
            List<LineShiftItemWarpper> shifts = new ArrayList<>();
            for (Map<String, Object> shiftMap : entry.getValue()) {
                LineShiftItemWarpper shiftItem = new LineShiftItemWarpper();
                shiftItem.setLineShiftId(Integer.valueOf(shiftMap.get("lineShiftId").toString()));
                shiftItem.setStartTime(shiftMap.get("startTime").toString());
                shiftItem.setEndTime(shiftMap.get("endTime").toString());
 
                // 判断是否已预约
                Integer isReserved = Integer.valueOf(shiftMap.get("isReserved").toString());
 
                if (isReserved == 1) {
                    // 已预约的班次
                    shiftItem.setLineShiftDriverId(Integer.valueOf(shiftMap.get("lineShiftDriverId").toString()));
                    shiftItem.setLaveSeat(Integer.valueOf(shiftMap.get("laveSeat").toString()));
                    shiftItem.setTotalSeat(Integer.valueOf(shiftMap.get("totalSeat").toString()));
 
                    // 判断是否过期
                    String endTime = shiftMap.get("endTime").toString();
                    boolean isExpired = false;
 
                    // 如果是今天,比较时间;如果是过去的日期,则已过期
                    if (date.equals(today)) {
                        // 比较当前时间和班次结束时间
                        isExpired = currentTime.compareTo(endTime) >= 0;
                    } else if (date.compareTo(today) < 0) {
                        // 过去的日期
                        isExpired = true;
                    }
 
                    if (isExpired) {
                        // 已过期
                        shiftItem.setStatus(2);
                        shiftItem.setStatusText("已过期");
                        shiftItem.setCanOperate(false);
                    } else {
                        // 已预约可操作
                        shiftItem.setStatus(1);
                        shiftItem.setStatusText("已预约");
                        shiftItem.setCanOperate(true);
                    }
                } else {
                    // 未预约的班次
                    shiftItem.setLineShiftDriverId(null);
                    shiftItem.setLaveSeat(null);
                    shiftItem.setTotalSeat(null);
 
                    // 判断是否可预约(未过期)
                    String endTime = shiftMap.get("endTime").toString();
                    boolean isExpired = false;
 
                    // 如果是今天,比较时间;如果是过去的日期,则已过期
                    if (date.equals(today)) {
                        // 比较当前时间和班次结束时间
                        isExpired = currentTime.compareTo(endTime) >= 0;
                    } else if (date.compareTo(today) < 0) {
                        // 过去的日期
                        isExpired = true;
                    }
 
                    if (isExpired) {
                        // 已过期,不可预约
                        shiftItem.setStatus(2);
                        shiftItem.setStatusText("已过期");
                        shiftItem.setCanOperate(false);
                    } else {
                        // 可预约
                        shiftItem.setStatus(3);
                        shiftItem.setStatusText("可预约");
                        shiftItem.setCanOperate(false);
                    }
                }
 
                shifts.add(shiftItem);
            }
            lineItem.setShifts(shifts);
            lineList.add(lineItem);
        }
 
        result.setLineList(lineList);
        return result;
    }
 
    @Override
    public ResultUtil<ScanOrderDetailWarpper> getDetail(Integer lineShiftDriverId) {
        // 1. 查询基础数据
        Map<String, Object> data = lineShiftDriverMapper.getDetail(lineShiftDriverId);
        if (data == null || data.isEmpty()) {
            return ResultUtil.error("班次信息不存在");
        }
 
        // 2. 构建返回对象
        ScanOrderDetailWarpper result = new ScanOrderDetailWarpper();
 
        // 司机信息
        result.setHeadImgUrl(data.get("headImgUrl") != null ? data.get("headImgUrl").toString() : null);
        result.setDriverName(data.get("driverName") != null ? data.get("driverName").toString() : null);
        result.setCarLicensePlate(data.get("carLicensePlate") != null ? data.get("carLicensePlate").toString() : null);
        result.setCarBrand(data.get("carBrand") != null ? data.get("carBrand").toString() : null);
        result.setCarColor(data.get("carColor") != null ? data.get("carColor").toString() : null);
        result.setScore(data.get("score") != null ? Double.parseDouble(data.get("score").toString()) : 0.0);
        result.setOrderCount(data.get("orderCount") != null ? Integer.parseInt(data.get("orderCount").toString()) : 0);
 
        // 线路信息
        result.setLineId(data.get("lineId") != null ? Integer.parseInt(data.get("lineId").toString()) : null);
        result.setLineName(data.get("lineName") != null ? data.get("lineName").toString() : null);
        result.setDay(data.get("day") != null ? data.get("day").toString() : null);
        result.setStartTime(data.get("startTime") != null ? data.get("startTime").toString() : null);
        result.setEndTime(data.get("endTime") != null ? data.get("endTime").toString() : null);
        result.setTotalSeat(data.get("totalSeat") != null ? Integer.parseInt(data.get("totalSeat").toString()) : null);
        result.setLaveSeat(data.get("laveSeat") != null ? Integer.parseInt(data.get("laveSeat").toString()) : null);
        result.setLaveSeatNumber(data.get("laveSeatNumber") != null ? data.get("laveSeatNumber").toString() : null);
 
        // 3. 解析价格JSON获取单人价格
        String priceContent = data.get("priceContent") != null ? data.get("priceContent").toString() : null;
        if (priceContent != null && !priceContent.isEmpty()) {
            JSONObject json = JSON.parseObject(priceContent);
            String num1 = json.getString("num1");
            if (num1 != null && !num1.isEmpty()) {
                result.setUnitPrice(Double.parseDouble(num1));
            }
        }
 
        // 站点信息
        result.setStartSiteId(data.get("startSiteId") != null ? Integer.parseInt(data.get("startSiteId").toString()) : null);
        result.setStartSiteName(data.get("startSiteName") != null ? data.get("startSiteName").toString() : null);
        result.setEndSiteId(data.get("endSiteId") != null ? Integer.parseInt(data.get("endSiteId").toString()) : null);
        result.setEndSiteName(data.get("endSiteName") != null ? data.get("endSiteName").toString() : null);
        // 城市信息
        result.setStartCityName(data.get("startCityName") != null ? data.get("startCityName").toString() : null);
        result.setEndCityName(data.get("endCityName") != null ? data.get("endCityName").toString() : null);
        // 经纬度信息
        result.setStartLonLat(data.get("startLonLat") != null ? data.get("startLonLat").toString() : null);
        result.setEndLonLat(data.get("endLonLat") != null ? data.get("endLonLat").toString() : null);        //
        // 默认点信息(起点和终点)
        // 起点默认点信息
        result.setStartDefaultPointAddress(data.get("startDefaultPointAddress") != null ? data.get("startDefaultPointAddress").toString() : null);
        result.setStartDefaultPointLon(data.get("startDefaultPointLon") != null ? Double.parseDouble(data.get("startDefaultPointLon").toString()) : null);
        result.setStartDefaultPointLat(data.get("startDefaultPointLat") != null ? Double.parseDouble(data.get("startDefaultPointLat").toString()) : null);
 
        // 终点默认点信息
        result.setEndDefaultPointAddress(data.get("endDefaultPointAddress") != null ? data.get("endDefaultPointAddress").toString() : null);
        result.setEndDefaultPointLon(data.get("endDefaultPointLon") != null ? Double.parseDouble(data.get("endDefaultPointLon").toString()) : null);
        result.setEndDefaultPointLat(data.get("endDefaultPointLat") != null ? Double.parseDouble(data.get("endDefaultPointLat").toString()) : null);
        // 其他信息
        result.setServerCarModelId(data.get("serverCarModelId") != null ? Integer.parseInt(data.get("serverCarModelId").toString()) : null);
        result.setDriverId(data.get("driverId") != null ? Integer.parseInt(data.get("driverId").toString()) : null);
        result.setLineShiftDriverId(lineShiftDriverId);
 
        return ResultUtil.success(result);
    }
}