zhangback
5 天以前 3974fad5d836431e417e99220cc07bb5b0aba331
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
package com.ruoyi.tms.service.impl;
 
import java.io.File;
import java.time.LocalDateTime;
import java.time.ZoneId;
import java.util.*;
 
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.ruoyi.common.config.RuoYiConfig;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.utils.*;
 
import javax.annotation.Resource;
 
import com.ruoyi.common.utils.file.FileUploadUtils;
import com.ruoyi.common.utils.uuid.Seq;
import com.ruoyi.system.service.ISysUserService;
import com.ruoyi.tms.domain.*;
import com.ruoyi.tms.mapper.*;
import org.apache.commons.io.FileUtils;
import org.apache.commons.io.FilenameUtils;
import org.springframework.amqp.rabbit.core.RabbitTemplate;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.stereotype.Service;
import org.springframework.scheduling.annotation.Async;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import com.ruoyi.common.constant.Constants;
import com.ruoyi.common.annotation.DataSource;
import com.ruoyi.common.enums.DataSourceType;
import com.ruoyi.common.core.service.BaseService;
 
import com.ruoyi.tms.service.ITmsTripService;
import com.ruoyi.common.core.text.Convert;
 
/**
 * 行程Service业务层处理
 *
 * @author ruoyi
 * @date 2025-11-13
 */
@Service
@Transactional(rollbackFor = Exception.class)
public class TmsTripServiceImpl  extends BaseService<TmsTripMapper, TmsTrip> implements ITmsTripService
{
    protected final Logger logger = LoggerFactory.getLogger(getClass());
    @Resource
    private TmsTripMapper tmsTripMapper;
 
    @Resource
    private TmsDispatchOrderMapper tmsDispatchOrderMapper;
    @Autowired
    private ISysUserService userService;
 
    @Resource
    private TmsDriverDispatchMapper tmsDriverDispatchMapper;
 
    @Resource
    private TmsDriverMapper tmsDriverMapper;
    @Autowired
    private TmsVehicleServiceImpl tmsVehicleServiceImpl;
    @Resource
    private TmsCarKeyDispatchMapper tmsCarKeyDispatchMapper;
    @Autowired
    private RabbitTemplate rabbitTemplate;
 
    @Autowired
    private AsyncPdfService asyncPdfService;
    /**
     * 查询行程
     *
     * @param id 行程ID
     * @return 行程
     */
    @DataSource(DataSourceType.SLAVE)
    @Override
    public TmsTrip selectTmsTripById(Integer id)
    {
        return tmsTripMapper.selectTmsTripById(id);
    }
 
    /**
     * 查询行程 记录数
     *
     * @param tmsTrip 行程
     * @return 行程集合
     */
    @DataSource(DataSourceType.SLAVE)
    @Override
    public int selectTmsTripCount(TmsTrip tmsTrip)
    {
        return tmsTripMapper.selectTmsTripCount(tmsTrip);
    }
 
    /**
     * 查询行程列表
     *
     * @param tmsTrip 行程
     * @return 行程
     */
    @DataSource(DataSourceType.SLAVE)
    @Override
    public List<TmsTrip> selectTmsTripList(TmsTrip tmsTrip)
    {
        return tmsTripMapper.selectTmsTripList(tmsTrip);
    }
 
    @Override
    public List<TmsTrip> tmsTripList(Integer dispatchId) {
        Integer deviceId = userService.getDeviceId(SecurityUtils.getUserId());
        if (deviceId == null){
            return null;
        }
        return tmsTripMapper.selectList(new LambdaQueryWrapper<TmsTrip>()
                .eq(TmsTrip::getDispatchOrderId,dispatchId)
                .eq(TmsTrip::getDriverId,deviceId)
                .orderByDesc(TmsTrip::getId)
        );
    }
 
    /**
     * 查询行程列表 异步 导出
     *
     * @param tmsTrip 行程
     * @param exportKey 导出功能的唯一标识
     * @return 行程集合
     */
    @DataSource(DataSourceType.SLAVE)
    @Async
    @Override
    public void export(TmsTrip tmsTrip,String exportKey) {
 
        super.export(TmsTrip.class,exportKey,"tmsTripData",(pageNum)->{
            PageUtils.startPage(pageNum, Constants.EXPORT_PATE_SIZE);
            return selectTmsTripList(tmsTrip);
        });
    }
 
 
    /**
     * 新增行程
     *
     * @param tmsTrip 行程
     * @return 结果
     */
    @Override
    public int insertTmsTrip(TmsTrip tmsTrip) throws Exception {
        Integer dispatchOrderId = tmsTrip.getDispatchOrderId();
        TmsDispatchOrder tmsDispatchOrder = tmsDispatchOrderMapper.selectTmsDispatchOrderById(dispatchOrderId);
        if (tmsDispatchOrder == null){
            throw new RuntimeException("未找到调度单数据");
        }
        if (tmsDispatchOrder.getStatus() != 1 && tmsDispatchOrder.getStatus() != 2){
            throw new RuntimeException("调度单数据状态异常");
        }
        TmsDriver tmsDriver = tmsDriverMapper.selectTmsDriverById(tmsDispatchOrder.getMainDriverId());
        if (tmsDriver == null){
            throw new RuntimeException("未找到司机数据");
        }
 
 
        tmsDispatchOrder.setStatus(2);
        tmsDispatchOrderMapper.updateTmsDispatchOrder(tmsDispatchOrder);
        tmsTrip.setDriverName(tmsDispatchOrder.getMainDriverName());
        tmsTrip.setVehicleId(tmsDispatchOrder.getVehicleId());
        tmsTrip.setVehicleNumber(tmsDispatchOrder.getLicensePlate());
        tmsTrip.setCreateBy(SecurityUtils.getUsername());
        tmsTrip.setCreateTime(DateUtils.getNowDate());
        tmsTripMapper.insertTmsTrip(tmsTrip);
        if (StringUtils.isNotEmpty(tmsTrip.getSignImg())){
            asyncPdfService.generateTripPdfAsync(tmsTrip,tmsDispatchOrder,tmsDriver);
        }
        // 完成身体检查
        if (tmsTrip.getTripType() == 1){
            // 添加 领钥匙 指令记录
            TmsVehicle tmsVehicle = tmsVehicleServiceImpl.selectTmsVehicleById(tmsDispatchOrder.getVehicleId());
 
            TmsCarKeyDispatch tmsCarKeyDispatch = new TmsCarKeyDispatch();
            tmsCarKeyDispatch.setDispatchOrderId(dispatchOrderId);
            tmsCarKeyDispatch.setDispatchOrderNo(tmsDispatchOrder.getDispatchNo());
            tmsCarKeyDispatch.setDriverId(tmsDriver.getId());
            tmsCarKeyDispatch.setDriverMobile(tmsDriver.getMobile());
            tmsCarKeyDispatch.setDriverName(tmsDriver.getDriverName());
 
            tmsCarKeyDispatch.setVehicleId(tmsDispatchOrder.getVehicleId());
            tmsCarKeyDispatch.setVehicleLicensePlate(tmsDispatchOrder.getLicensePlate());
            tmsCarKeyDispatch.setVehicleCarKeyNo(tmsVehicle.getCarKeyNo());
 
            LocalDateTime plus2Hours = LocalDateTime.now().plusHours(2);
            Date date = Date.from(plus2Hours.atZone(ZoneId.systemDefault()).toInstant());
 
            tmsCarKeyDispatch.setCarKeyExpirationTime(date);
            tmsCarKeyDispatch.setStatus(0);
            tmsCarKeyDispatchMapper.insertTmsCarKeyDispatch(tmsCarKeyDispatch);
 
            sendCarKeyDelayMessage(tmsCarKeyDispatch.getId(),  2 * 60 * 60 * 1000L);
 
        }
 
 
        return 1;
    }
 
    /**
     * 发送延迟消息
     * @param carKeyId car_key id
     * @param delayMillis 延迟毫秒数
     */
    public void sendCarKeyDelayMessage(Integer carKeyId, long delayMillis) {
        rabbitTemplate.convertAndSend(
                "car_key_delay_exchange",
                "car_key_delay",
                carKeyId,
                message -> {
                    message.getMessageProperties().setExpiration(String.valueOf(delayMillis));
                    return message;
                }
        );
    }
 
 
    @Override
    public AjaxResult submitDropHook(TmsTrip tmsTrip){
        Integer dispatchOrderId = tmsTrip.getDispatchOrderId();
        TmsDispatchOrder tmsDispatchOrder = tmsDispatchOrderMapper.selectTmsDispatchOrderById(dispatchOrderId);
        if (tmsDispatchOrder == null){
            throw new RuntimeException("未找到调度单数据");
        }
        if (tmsDispatchOrder.getStatus() != 6){
            throw new RuntimeException("调度单数据状态异常");
        }
        tmsDispatchOrderMapper.update(
                new LambdaUpdateWrapper<TmsDispatchOrder>()
                        .eq(TmsDispatchOrder::getId, dispatchOrderId)
                        .set(TmsDispatchOrder::getStatus, 7)
                        .set(TmsDispatchOrder::getMainDriverId, null)
                        .set(TmsDispatchOrder::getMainDriverName, null)
                        .set(TmsDispatchOrder::getVehicleId, null)
                        .set(TmsDispatchOrder::getLicensePlate, null)
        );
 
        tmsDriverDispatchMapper.update(
                new LambdaUpdateWrapper<TmsDriverDispatch>()
                        .eq(TmsDriverDispatch::getDispatchId, dispatchOrderId)
                        .eq(TmsDriverDispatch::getDriverId, tmsDispatchOrder.getMainDriverId())
                        .set(TmsDriverDispatch::getStatus, 3)
        );
        tmsTrip.setDriverName(tmsDispatchOrder.getMainDriverName());
        tmsTrip.setVehicleId(tmsDispatchOrder.getVehicleId());
        tmsTrip.setVehicleNumber(tmsDispatchOrder.getLicensePlate());
        tmsTrip.setCreateBy(SecurityUtils.getUsername());
        tmsTrip.setCreateTime(DateUtils.getNowDate());
         tmsTripMapper.insertTmsTrip(tmsTrip);
         return AjaxResult.success();
    }
    @Override
    public AjaxResult submitPickHook(TmsTrip tmsTrip){
        Integer dispatchOrderId = tmsTrip.getDispatchOrderId();
        TmsDispatchOrder tmsDispatchOrder = tmsDispatchOrderMapper.selectTmsDispatchOrderById(dispatchOrderId);
        if (tmsDispatchOrder == null){
            throw new RuntimeException("未找到调度单数据");
        }
        if (tmsDispatchOrder.getStatus() != 7){
            throw new RuntimeException("调度单数据状态异常");
        }
        Integer driverId = tmsTrip.getDriverId();
        TmsDriver tmsDriver = tmsDriverMapper.selectTmsDriverById(driverId);
        TmsDriverDispatch driverDispatch = tmsDriverDispatchMapper.selectOne(new LambdaUpdateWrapper<TmsDriverDispatch>()
                .eq(TmsDriverDispatch::getDispatchId, dispatchOrderId)
                .eq(TmsDriverDispatch::getDriverId, driverId)
                .eq(TmsDriverDispatch::getStatus, 2)
        );
        if (driverDispatch == null){
            throw new RuntimeException("未找到关联数据");
        }
 
 
        TmsVehicle tmsVehicle = tmsVehicleServiceImpl.selectTmsVehicleById(driverDispatch.getVehicleId());
 
        tmsDispatchOrderMapper.update(
                new LambdaUpdateWrapper<TmsDispatchOrder>()
                        .eq(TmsDispatchOrder::getId, dispatchOrderId)
                        .set(TmsDispatchOrder::getStatus, 2)
                        .set(TmsDispatchOrder::getMainDriverId, tmsDriver.getId())
                        .set(TmsDispatchOrder::getMainDriverName, tmsDriver.getDriverName())
                        .set(TmsDispatchOrder::getVehicleId, tmsVehicle.getId())
                        .set(TmsDispatchOrder::getLicensePlate, tmsVehicle.getLicensePlate())
        );
        tmsDriverDispatchMapper.update(
                new LambdaUpdateWrapper<TmsDriverDispatch>()
                        .eq(TmsDriverDispatch::getDispatchId, dispatchOrderId)
                        .eq(TmsDriverDispatch::getDriverId, tmsTrip.getDriverId())
                        .set(TmsDriverDispatch::getStatus, 4)
        );
 
        tmsTrip.setVehicleId(tmsDispatchOrder.getVehicleId());
        tmsTrip.setVehicleNumber(tmsDispatchOrder.getLicensePlate());
        tmsTrip.setDriverName(tmsDriver.getDriverName());
        tmsTrip.setCreateBy(SecurityUtils.getUsername());
        tmsTrip.setCreateTime(DateUtils.getNowDate());
         tmsTripMapper.insertTmsTrip(tmsTrip);
         return AjaxResult.success();
    }
 
 
    /**
     * 新增行程[批量]
     *
     * @param tmsTrips 行程
     * @return 结果
     */
    @Override
    public int insertTmsTripBatch(List<TmsTrip> tmsTrips)
    {
        int rows = tmsTripMapper.insertTmsTripBatch(tmsTrips);
        return rows;
    }
 
    /**
     * 修改行程
     *
     * @param tmsTrip 行程
     * @return 结果
     */
    @Override
    public int updateTmsTrip(TmsTrip tmsTrip)
    {
        tmsTrip.setUpdateTime(DateUtils.getNowDate());
        return tmsTripMapper.updateTmsTrip(tmsTrip);
    }
 
    /**
     * 修改行程[批量]
     *
     * @param tmsTrips 行程
     * @return 结果
     */
    @Override
    public int updateTmsTripBatch(List<TmsTrip> tmsTrips){
        return tmsTripMapper.updateTmsTripBatch(tmsTrips);
    }
 
    /**
     * 删除行程对象
     *
     * @param ids 需要删除的数据ID
     * @return 结果
     */
    @Override
    public int deleteTmsTripByIds(String ids)
    {
        return deleteTmsTripByIds(Convert.toIntArray(ids));
    }
 
    /**
     * 删除行程对象
     *
     *
     * @param ids 需要删除的数据ID
     * @return 结果
     */
    @Override
    public int deleteTmsTripByIds(Integer[] ids)
    {
        return tmsTripMapper.deleteTmsTripByIds(ids);
    }
 
    /**
     * 删除行程信息
     *
     * @param id 行程ID
     * @return 结果
     */
    @Override
    public int deleteTmsTripById(Integer id)
    {
        return tmsTripMapper.deleteTmsTripById(id);
    }
}