wlzboy
2026-03-31 61c4c3f45e4257e2e7662f033e2719e62366c632
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
package com.ruoyi.web.controller.system;
 
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.core.page.TableDataInfo;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.system.domain.VehicleInfo;
import com.ruoyi.system.domain.VehicleSyncDTO;
import com.ruoyi.system.domain.vo.VehicleSyncVO;
import com.ruoyi.system.mapper.VehicleInfoMapper;
import com.ruoyi.system.service.IVehicleInfoService;
import com.ruoyi.system.service.IVehicleSyncDataService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;
 
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
 
/**
 * 车辆同步管理Controller
 *
 * @author ruoyi
 */
@RestController
@RequestMapping("/system/vehicleSync")
public class VehicleSyncController extends BaseController {
 
    private static final Logger log = LoggerFactory.getLogger(VehicleSyncController.class);
 
    @Autowired
    private IVehicleSyncDataService vehicleSyncDataService;
 
    @Autowired
    private VehicleInfoMapper vehicleInfoMapper;
 
    @Autowired
    private IVehicleInfoService vehicleInfoService;
 
    /**
     * 查询车辆同步列表(显示旧系统车辆及同步状态)
     */
    @PreAuthorize("@ss.hasPermi('system:vehicleSync:list')")
    @GetMapping("/list")
    public TableDataInfo list() {
        try {
            // 1. 从旧系统获取车辆列表
            List<VehicleSyncDTO> oldVehicles = vehicleSyncDataService.getVehiclesFromSqlServer();
            
            if (oldVehicles == null || oldVehicles.isEmpty()) {
                return getDataTable(new ArrayList<>());
            }
 
            // 2. 转换为VO并检查同步状态
            List<VehicleSyncVO> voList = new ArrayList<>();
            for (VehicleSyncDTO dto : oldVehicles) {
                VehicleSyncVO vo = convertToVO(dto);
                
                // 3. 检查该车辆是否已同步到新系统
                VehicleInfo existVehicle = vehicleInfoMapper.selectVehicleInfoByCarId(dto.getCarId());
                if (existVehicle != null) {
                    vo.setSynced(true);
                    vo.setVehicleId(existVehicle.getVehicleId());
                    vo.setDeptId(existVehicle.getDeptId());
                    if (existVehicle.getDeptName() != null) {
                        vo.setDeptName(existVehicle.getDeptName());
                    }
                } else {
                    vo.setSynced(false);
                }
                
                voList.add(vo);
            }
 
            return getDataTable(voList);
            
        } catch (Exception e) {
            log.error("查询车辆同步列表失败", e);
            return getDataTable(new ArrayList<>());
        }
    }
 
    /**
     * 手动同步单个车辆到新系统
     */
    @PreAuthorize("@ss.hasPermi('system:vehicleSync:sync')")
    @Log(title = "车辆同步", businessType = BusinessType.INSERT)
    @PostMapping("/syncVehicle")
    public AjaxResult syncVehicle(@RequestBody Map<String, Object> params) {
        try {
            Integer carId = (Integer) params.get("carId");
            String vehicleNo = (String) params.get("vehicleNo");
            Long deptId = params.get("deptId") != null ? Long.valueOf(params.get("deptId").toString()) : null;
 
            if (carId == null || vehicleNo == null || deptId == null) {
                return AjaxResult.error("参数不完整:carId、vehicleNo、deptId 不能为空");
            }
 
            // 1. 检查是否已存在
            VehicleInfo existVehicle = vehicleInfoMapper.selectVehicleInfoByCarId(carId);
            if (existVehicle != null) {
                return AjaxResult.error("该车辆已同步,车辆ID: " + existVehicle.getVehicleId());
            }
 
            // 2. 创建新车辆记录
            VehicleInfo newVehicle = new VehicleInfo();
            newVehicle.setCarId(carId);
            newVehicle.setVehicleNo(vehicleNo);
            newVehicle.setDeptId(deptId);
            newVehicle.setStatus("0"); // 默认正常状态
            newVehicle.setCreateBy(getUsername());
 
            // 3. 插入车辆信息
            int result = vehicleInfoMapper.insertVehicleInfo(newVehicle);
            
            if (result > 0) {
                log.info("手动同步车辆成功:carId={}, vehicleNo={}, vehicleId={}", 
                        carId, vehicleNo, newVehicle.getVehicleId());
                return AjaxResult.success("同步成功", newVehicle.getVehicleId());
            } else {
                return AjaxResult.error("同步失败");
            }
 
        } catch (Exception e) {
            log.error("手动同步车辆失败", e);
            return AjaxResult.error("同步失败:" + e.getMessage());
        }
    }
 
    /**
     * 将 DTO 转换为 VO
     */
    private VehicleSyncVO convertToVO(VehicleSyncDTO dto) {
        VehicleSyncVO vo = new VehicleSyncVO();
        vo.setCarId(dto.getCarId());
        vo.setVehicleNo(dto.getCarLicense());
        vo.setCarOrdClass(dto.getCarOrdClass());
        
        // 根据carOrdClass映射分公司名称
        String deptName = mapCarOrdClassToDeptName(dto.getCarOrdClass());
        vo.setDeptName(deptName);
        
        return vo;
    }
 
    /**
     * 根据单据类型编码映射分公司名称
     * 可以从配置或数据库读取,这里简化处理
     */
    private String mapCarOrdClassToDeptName(String carOrdClass) {
        // TODO: 根据实际业务规则映射
        // 可以从 sys_config 或专门的映射表读取
        Map<String, String> mapping = new HashMap<>();
        mapping.put("01", "总公司");
        mapping.put("02", "分公司A");
        mapping.put("03", "分公司B");
        
        return mapping.getOrDefault(carOrdClass, "未知分公司");
    }
}