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 oldVehicles = vehicleSyncDataService.getVehiclesFromSqlServer(); if (oldVehicles == null || oldVehicles.isEmpty()) { return getDataTable(new ArrayList<>()); } // 2. 转换为VO并检查同步状态 List 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 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 mapping = new HashMap<>(); mapping.put("01", "总公司"); mapping.put("02", "分公司A"); mapping.put("03", "分公司B"); return mapping.getOrDefault(carOrdClass, "未知分公司"); } }