longz
2026-06-04 da780b14b8db78043d2c0f27d3bfd73111e6d550
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
package com.stylefeng.guns.modular.crossCity.server.impl;
 
import com.alibaba.fastjson.JSON;
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.LineShiftDriverMapper;
import com.stylefeng.guns.modular.crossCity.dao.LineShiftMapper;
import com.stylefeng.guns.modular.crossCity.dao.LineSiteMapper;
import com.stylefeng.guns.modular.crossCity.model.LineShift;
import com.stylefeng.guns.modular.crossCity.model.LineShiftDriver;
import com.stylefeng.guns.modular.crossCity.model.LineSite;
import com.stylefeng.guns.modular.crossCity.model.OrderCrossCity;
import com.stylefeng.guns.modular.crossCity.server.ILineSiteService;
import com.stylefeng.guns.modular.crossCity.server.IOrderCrossCityService;
import com.stylefeng.guns.modular.specialTrain.model.OrderPrivateCar;
import com.stylefeng.guns.modular.specialTrain.server.IOrderPrivateCarService;
import com.stylefeng.guns.modular.system.util.SystemException;
import com.stylefeng.guns.modular.taxi.model.OrderTaxi;
import com.stylefeng.guns.modular.taxi.service.IOrderTaxiService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Service;
 
import javax.annotation.Resource;
import java.math.BigDecimal;
import java.text.SimpleDateFormat;
import java.util.*;
 
 
@Service
public class LineSiteServiceImpl extends ServiceImpl<LineSiteMapper, LineSite> implements ILineSiteService {
 
    @Resource
    private LineSiteMapper lineSiteMapper;
 
    @Resource
    private LineShiftDriverMapper lineShiftDriverMapper;
 
    @Resource
    private LineShiftMapper lineShiftMapper;
    
    @Autowired
    private IOrderTaxiService orderTaxiService;
    
    @Autowired
    private IOrderPrivateCarService orderPrivateCarService;
    
    @Autowired
    private IOrderCrossCityService orderCrossCityService;
    
    @Resource
    private RedisTemplate<String, Object> redisTemplate;
    
    
    /**
     * 根据线路id获取排班数据
     *
     * @param lineId
     * @param day
     * @return
     * @throws Exception
     */
    @Override
    public List<Map<String, Object>> queryDriver(Integer lineId, String day, Integer driverId) throws Exception {
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
        Calendar c = Calendar.getInstance();
        c.setTime(new Date());
        c.set(c.get(Calendar.YEAR), c.get(Calendar.MONTH), c.get(Calendar.DAY_OF_MONTH), 0, 0, 0);
        c.set(Calendar.MILLISECOND, 0);
        if(c.getTimeInMillis() > sdf.parse(day).getTime()){
            throw new SystemException("日期不能小于当天");
        }
 
        Boolean chooseSeat = false;
        if (driverId != null) {
            Integer chooseSeatValue = lineSiteMapper.queryChooseSeatByDriverId(driverId);
            chooseSeat = (chooseSeatValue != null && chooseSeatValue.intValue() == 1);
        }
 
        if (lineId != null && lineId < 0) {
            List<Map<String, Object>> result = lineSiteMapper.queryDriverBySelfTripId(Math.abs(lineId), sdf.parse(day), driverId);
            filterAllOrdersFinishedShift(result);
            for (Map<String, Object> map : result) {
                map.put("chooseSeat", chooseSeat);
            }
            return result;
        }
        List<Map<String, Object>> list = new ArrayList<>();
        List<LineShift> lineShifts = lineShiftMapper.queryByLineId(lineId);
        List<Map<String, Object>> s = lineSiteMapper.queryDriver(lineId, sdf.parse(day), driverId);
        Calendar calendar = Calendar.getInstance();
        calendar.setTime(sdf.parse(day));
        String vehicle = (String) redisTemplate.opsForValue().get("VEHICLE");
        List<Integer> integers = new ArrayList<>();
        if(ToolUtil.isNotEmpty(vehicle)){
            integers = JSON.parseArray(vehicle).toJavaList(Integer.class);
        }
        for(LineShift lineShift : lineShifts){
            boolean b = true;
            String[] split = lineShift.getEndTime().split(":");
            calendar.set(Calendar.HOUR_OF_DAY, Integer.valueOf(split[0]));
            calendar.set(Calendar.MINUTE, Integer.valueOf(split[1]));
            if(calendar.getTimeInMillis() < System.currentTimeMillis()){//结束时间小于当前时间不显示
                continue;
            }
 
            Map<String, Object> data = new HashMap<>();
            for(Map<String, Object> map : s){
                //先判断司机是否在限制接单范围内
                boolean bo = false;
                for(Integer integer : integers){
                    if(integer.compareTo(Integer.valueOf(map.get("driverId").toString())) == 0){
                        bo = true;
                        break;
                    }
                }
                if(bo){
                    continue;
                }
 
 
                int i = orderTaxiService.selectCount(new EntityWrapper<OrderTaxi>().eq("driverId", map.get("driverId")).in("state", Arrays.asList(2, 3, 4, 5, 11)).eq("DATE_FORMAT(travelTime, '%Y-%m-%d')", day));
                if(i > 0){
                    continue;
                }
                i = orderPrivateCarService.selectCount(new EntityWrapper<OrderPrivateCar>().eq("driverId", map.get("driverId")).in("state", Arrays.asList(2, 3, 4, 5, 11)).eq("DATE_FORMAT(travelTime, '%Y-%m-%d')", day));
                if(i > 0){
                    continue;
                }
                List<OrderCrossCity> driverId1 = orderCrossCityService.queryListOrder(Integer.valueOf(map.get("driverId").toString()), lineShift.getId(), Arrays.asList(5), day);
                if(driverId1.size() > 0){
                    continue;
                }
                if(lineShift.getId().intValue() == Integer.valueOf(map.get("id").toString()).intValue()){
                    if (isAllOrdersFinishedShift(map.get("lineShiftDriverId"))) {
                        b = false;
                        continue;
                    }
                    if(Integer.valueOf(map.get("laveSeat").toString()) <= 0){
                        continue;
                    }
                    b = false;
                    BigDecimal price = getDefaultDestinationPrice(map);
                    map.put("price", price == null ? "0" : price.toPlainString());
                    if (chooseSeat){
                        map.put("chooseSeat", chooseSeat);
                    }else if (driverId == null){
                        Integer chooseSeatValue = lineSiteMapper.queryChooseSeatByDriverId(Integer.parseInt(map.get("driverId").toString()));
                        map.put("chooseSeat", chooseSeatValue != null && chooseSeatValue.intValue() == 1);
                    }
                    data = map;
                    list.add(data);
                }
            }
            if(b){
                data.put("id", lineShift.getId());
                data.put("startTime", lineShift.getStartTime() + "-" + lineShift.getEndTime());
                data.put("chooseSeat", chooseSeat);
                list.add(data);
            }
        }
        return list;
    }
 
    private BigDecimal getDefaultDestinationPrice(Map<String, Object> map) {
        if (map == null) {
            return null;
        }
        Integer totalSeat = map.get("totalSeat") == null ? null : Integer.valueOf(map.get("totalSeat").toString());
        BigDecimal price = totalSeat != null && totalSeat == 6 ? getBigDecimal(map.get("defaultPrice7")) : getBigDecimal(map.get("defaultPrice5"));
        return price == null ? getBigDecimal(map.get("defaultPrice5")) : price;
    }
 
    private BigDecimal getBigDecimal(Object value) {
        if (value == null || ToolUtil.isEmpty(String.valueOf(value))) {
            return null;
        }
        try {
            return new BigDecimal(String.valueOf(value));
        } catch (Exception e) {
            return null;
        }
    }
 
    private void filterAllOrdersFinishedShift(List<Map<String, Object>> result) {
        if (result == null || result.size() == 0) {
            return;
        }
        Iterator<Map<String, Object>> iterator = result.iterator();
        while (iterator.hasNext()) {
            Map<String, Object> map = iterator.next();
            if (isAllOrdersFinishedShift(map.get("lineShiftDriverId"))) {
                iterator.remove();
            }
        }
    }
 
    private boolean isAllOrdersFinishedShift(Object lineShiftDriverId) {
        if (lineShiftDriverId == null) {
            return false;
        }
        int orderCount = orderCrossCityService.selectCount(new EntityWrapper<OrderCrossCity>()
                .eq("lineShiftDriverId", lineShiftDriverId)
                .eq("isDelete", 1)
                .ne("state", 10));
        if (orderCount <= 0) {
            return false;
        }
        int finishCount = orderCrossCityService.selectCount(new EntityWrapper<OrderCrossCity>()
                .eq("lineShiftDriverId", lineShiftDriverId)
                .eq("isDelete", 1)
                .in("state", Arrays.asList(6, 8, 9)));
        return orderCount == finishCount;
    }
 
    /**
     * 根据司机排班数据id获取数据(剩余座位号和总座位数)
     * @param id
     * @return
     * @throws Exception
     */
    @Override
    public Map<String, Object> querySeat(Integer id) throws Exception {
        Map<String, Object> map = new HashMap<>();
        LineShiftDriver lineShiftDriver = lineShiftDriverMapper.selectById(id);
        map.put("totalNumber", lineShiftDriver.getTotalSeat());
        map.put("number", lineShiftDriver.getLaveSeat());
        map.put("content", lineShiftDriver.getLaveSeatNumber());
        return map;
    }
}