zhangback
2026-03-10 92052d1fa583816a92ac7a070aa17a62b96fa605
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
package com.ruoyi.api.third.controller;
 
import java.util.*;
import java.util.stream.Collectors;
 
import cn.hutool.json.JSONUtil;
import com.ruoyi.cwgl.domain.DispatchOrder;
import com.ruoyi.tms.mapper.*;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.core.domain.entity.SysDictData;
import com.ruoyi.common.utils.StringUtils;
import com.ruoyi.api.third.util.ApiAuthUtil;
import com.ruoyi.system.mapper.SysDictDataMapper;
import com.ruoyi.tms.domain.*;
import com.ruoyi.tms.domain.vo.*;
import com.ruoyi.tms.service.ITmsDispatchOrderService;
 
import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
 
/**
 * AI调度专用第三方接口 (无需权限, 使用appId/secret认证)
 *
 * @author ruoyi
 * @date 2026-03-06
 */
@RestController
@RequestMapping("/api/third/aiDispatch")
public class TmsAiDispatchController extends BaseController
{
    @Resource
    private TransportRouteViMapper transportRouteViMapper;
 
    @Resource
    private TmsCustomerInfoMapper tmsCustomerInfoMapper;
 
    @Resource
    private TmsServiceProviderMapper tmsServiceProviderMapper;
 
    @Resource
    private TmsDriverMapper tmsDriverMapper;
 
    @Resource
    private TmsVehicleMapper tmsVehicleMapper;
 
    @Resource
    private TmsConsignorMapper tmsConsignorMapper;
 
    @Autowired
    private ITmsDispatchOrderService tmsDispatchOrderService;
 
    @Autowired
    private SysDictDataMapper sysDictDataMapper;
 
    @Autowired
    private ApiAuthUtil apiAuthUtil;
 
    /**
     * 验证客户是否存在,且返回客户信息接口
     * @param customerName
     * @return
     */
    @GetMapping("/getCustomerData")
    public AjaxResult getCustomerData(  @RequestParam(value = "customerName", required = true) String customerName){
        logger.info("getCustomerData:{}",customerName);
        TmsCustomerInfo tmsCustomerInfo = tmsCustomerInfoMapper.selectOne(new LambdaQueryWrapper<TmsCustomerInfo>()
                .like(TmsCustomerInfo::getCustomerFullName, customerName)
                .last("limit 1")
        );
        if (tmsCustomerInfo == null){
            return error("客户不存在");
        }else{
            return AjaxResult.success(tmsCustomerInfo);
        }
 
    }
 
    /**
     * 验证车辆是否存在,且返回车辆信息接口
     * @param licensePlate
     * @param licenseHk
     * @param licenseMo
     * @return
     */
    @GetMapping("/getVehicleData")
    public AjaxResult getVehicleData(
            @RequestParam(value = "licensePlate", required = false) String licensePlate,
            @RequestParam(value = "licenseHk", required = false) String licenseHk,
            @RequestParam(value = "licenseMo", required = false) String licenseMo
    ){
        logger.info("getVehicleData:{},{},{}",licensePlate,licenseHk,licenseMo);
 
        TmsVehicle tmsCustomerInfo = tmsVehicleMapper.selectOne(new LambdaQueryWrapper<TmsVehicle>()
                .like(StringUtils.isNotEmpty(licensePlate),TmsVehicle::getLicensePlate, licensePlate)
                .like(StringUtils.isNotEmpty(licenseHk),TmsVehicle::getLicenseHk, licenseHk)
                .like(StringUtils.isNotEmpty(licenseMo),TmsVehicle::getLicenseMo, licenseMo)
                .last("limit 1")
        );
        if (tmsCustomerInfo == null){
            return error("车辆不存在");
        }else{
            return AjaxResult.success(tmsCustomerInfo);
        }
 
    }
 
 
    /**
     * 获取司机接口 (公开接口, 无需认证)
     * 支持按名称筛选
     *
     * @param driverName 司机名称(可选,模糊查询)
     * @return 司机列表
     */
    @GetMapping("/getDriverData")
    public AjaxResult getDriverData(
            @RequestParam(value = "driverName", required = true) String driverName)
    {
        logger.info("getVehicleData:{}",driverName);
 
        TmsDriver tmsDriver = tmsDriverMapper.selectOne(new LambdaQueryWrapper<TmsDriver>()
                .like(TmsDriver::getDriverName, driverName)
                .last("limit 1")
        );
 
        if (tmsDriver == null){
            return error("司机信息不存在");
        }else{
            return AjaxResult.success(tmsDriver);
        }
 
    }
    @GetMapping("/getVehicleProviderData")
    public AjaxResult getVehicleProviderData(
            @RequestParam(value = "serviceName", required = true) String serviceName)
    {
        logger.info("getVehicleProviderData:{}",serviceName);
 
        TmsServiceProvider provider = tmsServiceProviderMapper.selectOne(new LambdaQueryWrapper<TmsServiceProvider>()
                .like(TmsServiceProvider::getServiceName, serviceName)
                .last("limit 1")
        );
 
        if (provider == null){
            return error("车辆服务商信息不存在");
        }else{
            return AjaxResult.success(provider);
        }
 
    }
 
    @GetMapping("/getConsignorData")
    public AjaxResult getConsignorData(
            @RequestParam(value = "consignorName", required = true) String consignorName,
            @RequestParam(value = "customerName", required = true) String customerName)
    {
        logger.info("getConsignorData:{}",consignorName);
 
        TmsConsignor tmsConsignor = tmsConsignorMapper.selectOne(new LambdaQueryWrapper<TmsConsignor>()
                .like(TmsConsignor::getConsignorName, consignorName)
                .eq(TmsConsignor::getCustomerName, customerName)
                .last("limit 1")
        );
 
        if (tmsConsignor == null){
            return error("装卸货点不存在");
        }else{
            return AjaxResult.success(tmsConsignor);
        }
    }
 
 
 
    /**
     * 路线验证接口 (需要appId/secret认证)
     *
     * @param vo 路线验证请求
     * @param request HTTP请求
     * @return 验证结果
     */
    @PostMapping("/validateRoute")
    public AjaxResult validateRoute(@RequestBody RouteValidationVO vo, HttpServletRequest request)
    {
        logger.info("validateRoute:{}", JSONUtil.toJsonStr(vo));
        AiValidationResultVO result = new AiValidationResultVO();
        List<String> errors = new ArrayList<>();
        List<String> warnings = new ArrayList<>();
 
        // 验证必填字段
        if (StringUtils.isEmpty(vo.getOriginCity())) {
            errors.add("起始城市不能为空");
        }
        if (StringUtils.isEmpty(vo.getDestCity())) {
            errors.add("目的地城市不能为空");
        }
        if (StringUtils.isEmpty(vo.getVehicleType())) {
            errors.add("车辆类型不能为空");
        }
 
        // 如果有错误,直接返回
        if (!errors.isEmpty()) {
            result.setIsValid(false);
            result.setErrors(errors);
            result.setWarnings(warnings);
            return success(result);
        }
 
        // 查询匹配的路线
        LambdaQueryWrapper<TransportRouteVi> wrapper = new LambdaQueryWrapper<>();
        if (StringUtils.isNotEmpty(vo.getOriginCity())) {
            wrapper.like(TransportRouteVi::getTransportRoute, vo.getOriginCity());
        }
        if (StringUtils.isNotEmpty(vo.getDestCity())) {
            wrapper.like(TransportRouteVi::getTransportRoute, vo.getDestCity());
        }
        wrapper.eq(TransportRouteVi::getVehicleType, vo.getVehicleType());
        wrapper.eq(TransportRouteVi::getCustomerFullName, vo.getCustomerName());
 
        List<TransportRouteVi> routes = transportRouteViMapper.selectList(wrapper);
 
        Map<String, Object> data = new HashMap<>();
        data.put("matchedRoutes", routes);
        data.put("routeCount", routes.size());
 
        if (routes.isEmpty()) {
            warnings.add("未找到匹配的运输路线");
        }
 
        result.setIsValid(routes.size() > 0);
        result.setErrors(errors);
        result.setWarnings(warnings);
        result.setData(data);
 
        return success(result);
    }
 
    /**
     * 创建调度单接口 (AI优化版, 需要appId/secret认证)
     *
     * @param vo AI调度单请求对象
     * @param request HTTP请求
     * @return 创建结果
     */
    @PostMapping("/createOrder")
    public AjaxResult createOrder(@RequestBody TmsDispatchOrder order, HttpServletRequest request)
    {
        // appId/secret 认证
        if (!apiAuthUtil.validateAppAuth(request)) {
            return apiAuthUtil.getUnauthorizedResult();
        }
 
        try {
            // 验证必填字段
            if (order.getMainDriverId() == null || order.getLicensePlate() == null) {
                return error("派车信息不能为空");
            }
            // 设置初始状态为待分配 (0)
            order.setStatus(0);
 
            // 保存订单
            int result = tmsDispatchOrderService.insertTmsDispatchOrder2(order);
 
            if (result > 0) {
                // 返回新创建的订单信息
                Map<String, Object> data = new HashMap<>();
                data.put("orderNo", order.getDispatchNo());
                data.put("customerName", order.getCustomerName());
                data.put("originCity", order.getShipperRegionLabel());
                data.put("destCity", order.getReceiverRegionLabel());
                data.put("cargoName", order.getCargoName());
                data.put("status", order.getStatus());
                return success(data);
            } else {
                return error("创建调度单失败");
            }
        } catch (Exception e) {
            logger.error("创建调度单异常: ", e);
            return error("创建调度单异常: " + e.getMessage());
        }
    }
 
    /**
     * 获取字典数据接口 (公开接口, 无需认证)
     * 支持多个字典类型,用逗号分隔
     *
     * @param dictTypes 字典类型,逗号分隔
     * @return 字典数据
     */
    @GetMapping("/getDictData")
    public AjaxResult getDictData(@RequestParam(value = "dictTypes", required = false) String dictTypes)
    {
        Map<String, List<DictDataVO>> dictDataMap = new HashMap<>();
 
        if (StringUtils.isNotEmpty(dictTypes)) {
            String[] types = dictTypes.split(",");
            for (String type : types) {
                String trimmedType = type.trim();
                // 调用Mapper按字典类型查询字典数据
                List<SysDictData> dictDataList = sysDictDataMapper.selectDictDataByType(trimmedType);
 
                // 转换为VO列表
                List<DictDataVO> voList = new ArrayList<>();
                if (dictDataList != null && !dictDataList.isEmpty()) {
                    voList = dictDataList.stream().map(dictData -> {
                        DictDataVO vo = new DictDataVO(
                            dictData.getDictLabel(),
                            dictData.getDictValue(),
                            dictData.getDictType()
                        );
                        return vo;
                    }).collect(Collectors.toList());
                }
 
                dictDataMap.put(trimmedType, voList);
            }
        }
 
        return success(dictDataMap);
    }
 
 
 
    /**
     * 批量验证订单数据接口 (需要appId/secret认证)
     *
     * @param orders 调度单列表
     * @param request HTTP请求
     * @return 验证结果
     */
    @PostMapping("/batchValidateOrders")
    public AjaxResult batchValidateOrders(@RequestBody List<AiDispatchOrderVO> orders, HttpServletRequest request)
    {
        // appId/secret 认证
        if (!apiAuthUtil.validateAppAuth(request)) {
            return apiAuthUtil.getUnauthorizedResult();
        }
 
        List<Map<String, Object>> validationResults = new ArrayList<>();
 
        for (int i = 0; i < orders.size(); i++) {
            AiDispatchOrderVO order = orders.get(i);
            Map<String, Object> result = new HashMap<>();
 
            result.put("index", i);
            result.put("customerName", order.getCustomerName());
 
            List<String> errors = new ArrayList<>();
 
            // 验证必填字段
            if (StringUtils.isEmpty(order.getCustomerName())) {
                errors.add("客户名称不能为空");
            }
            if (StringUtils.isEmpty(order.getOriginCity())) {
                errors.add("起始城市不能为空");
            }
            if (StringUtils.isEmpty(order.getDestCity())) {
                errors.add("目的地城市不能为空");
            }
            if (StringUtils.isEmpty(order.getLoadingDate())) {
                errors.add("装车日期不能为空");
            }
 
            result.put("isValid", errors.isEmpty());
            result.put("errors", errors);
 
            validationResults.add(result);
        }
 
        Map<String, Object> response = new HashMap<>();
        response.put("totalCount", orders.size());
        response.put("validCount", validationResults.stream()
                .filter(r -> (Boolean) r.get("isValid"))
                .count());
        response.put("invalidCount", validationResults.stream()
                .filter(r -> !(Boolean) r.get("isValid"))
                .count());
        response.put("details", validationResults);
 
        return success(response);
    }
}