| | |
| | | import com.ruoyi.common.core.domain.AjaxResult; |
| | | import com.ruoyi.common.core.domain.entity.SysDept; |
| | | import com.ruoyi.system.domain.VehicleInfo; |
| | | import com.ruoyi.system.domain.VehicleDept; |
| | | import com.ruoyi.system.domain.VehicleSyncDTO; |
| | | import com.ruoyi.system.mapper.SysDeptMapper; |
| | | import com.ruoyi.system.mapper.VehicleInfoMapper; |
| | | import com.ruoyi.system.service.IVehicleInfoService; |
| | | import com.ruoyi.system.service.IVehicleSyncDataService; |
| | | import com.ruoyi.system.service.IVehicleSyncService; |
| | |
| | | import org.springframework.stereotype.Service; |
| | | import org.springframework.transaction.annotation.Transactional; |
| | | |
| | | import java.util.ArrayList; |
| | | import java.util.HashMap; |
| | | import java.util.List; |
| | | import java.util.Map; |
| | |
| | | |
| | | @Autowired |
| | | private SysDeptMapper sysDeptMapper; |
| | | |
| | | @Autowired |
| | | private VehicleInfoMapper vehicleInfoMapper; |
| | | |
| | | |
| | | /** |
| | |
| | | // 查询车辆是否存在 |
| | | VehicleInfo existingVehicle = findVehicleByPlateNumber(plateNumber); |
| | | |
| | | // 解析部门信息 |
| | | Long deptId = parseDeptIdFromCarOrdClass(vehicleDTO.getCarOrdClass()); |
| | | // 解析所有分公司ID(CarOrdClass可能包含多个编码,如:HB,TI) |
| | | List<VehicleDept> vehicleDepts = parseVehicleDepts(vehicleDTO.getCarOrdClass()); |
| | | |
| | | // 设置默认的主部门ID(第一个分公司) |
| | | Long primaryDeptId = null; |
| | | if (!vehicleDepts.isEmpty()) { |
| | | primaryDeptId = vehicleDepts.get(0).getDeptId(); |
| | | } |
| | | |
| | | if (existingVehicle != null) |
| | | { |
| | | // 更新车辆信息 |
| | | existingVehicle.setCarId(vehicleDTO.getCarId()); |
| | | existingVehicle.setDeptId(deptId); |
| | | // 可以选择是否更新其他字段 |
| | | existingVehicle.setDeptId(primaryDeptId); // 设置主部门 |
| | | vehicleInfoService.updateVehicleInfo(existingVehicle); |
| | | |
| | | // 更新车辆-分公司关联 |
| | | syncVehicleDepts(existingVehicle.getVehicleId(), vehicleDepts); |
| | | |
| | | updateCount++; |
| | | log.debug("更新车辆: {} (CarID={}), 部门ID={}", plateNumber, vehicleDTO.getCarId(), deptId); |
| | | log.debug("更新车辆: {} (CarID={}), 关联分公司数: {}", |
| | | plateNumber, vehicleDTO.getCarId(), vehicleDepts.size()); |
| | | } |
| | | else |
| | | { |
| | |
| | | VehicleInfo newVehicle = new VehicleInfo(); |
| | | newVehicle.setVehicleNo(plateNumber); |
| | | newVehicle.setCarId(vehicleDTO.getCarId()); |
| | | newVehicle.setDeptId(deptId); |
| | | newVehicle.setDeptId(primaryDeptId); // 设置主部门 |
| | | newVehicle.setStatus("0"); |
| | | newVehicle.setPlatformCode("LEGACY"); // 标记为旧系统同步 |
| | | newVehicle.setRemark("从旧系统同步,CarID: " + vehicleDTO.getCarId()); |
| | | vehicleInfoService.insertVehicleInfo(newVehicle); |
| | | |
| | | // 新增车辆-分公司关联 |
| | | syncVehicleDepts(newVehicle.getVehicleId(), vehicleDepts); |
| | | |
| | | insertCount++; |
| | | log.debug("新增车辆: {} (CarID={}), 部门ID={}", plateNumber, vehicleDTO.getCarId(), deptId); |
| | | log.debug("新增车辆: {} (CarID={}), 关联分公司数: {}", |
| | | plateNumber, vehicleDTO.getCarId(), vehicleDepts.size()); |
| | | } |
| | | } |
| | | catch (Exception e) |
| | |
| | | } |
| | | |
| | | /** |
| | | * 从 CarOrdClass 解析部门ID,并转换为分公司ID |
| | | * CarOrdClass格式可能是:ZB、HB.TI等 |
| | | * 需要拆分并在sys_dept中匹配dispatch_order_class字段 |
| | | * 从 CarOrdClass 解析多个分公司关联 |
| | | * CarOrdClass格式可能是:HB、HB,TI、HB.TI等 |
| | | * |
| | | * @param carOrdClass 车辆单据类型编码 |
| | | * @return 分公司ID,如果未找到返回null |
| | | * @return 车辆-分公司关联列表 |
| | | */ |
| | | private Long parseDeptIdFromCarOrdClass(String carOrdClass) |
| | | private List<VehicleDept> parseVehicleDepts(String carOrdClass) |
| | | { |
| | | List<VehicleDept> vehicleDepts = new ArrayList<>(); |
| | | |
| | | if (StringUtils.isBlank(carOrdClass)) |
| | | { |
| | | log.debug("CarOrdClass为空,无法解析部门"); |
| | | return null; |
| | | log.debug("CarOrdClass为空,无法解析分公司"); |
| | | return vehicleDepts; |
| | | } |
| | | |
| | | // 拆分CarOrdClass,可能的分隔符:. , 空格 |
| | | // 拆分CarOrdClass,可能的分隔符:, . 空格 |
| | | String[] codes = carOrdClass.split("[.,\\s]+"); |
| | | |
| | | for (String code : codes) |
| | |
| | | |
| | | code = code.trim(); |
| | | |
| | | // 查询匹配dispatch_order_class的部门 |
| | | SysDept dept = findDeptByDispatchOrderClass(code); |
| | | if (dept != null) |
| | | // 查询匹配dispatch_order_class或service_order_class的部门 |
| | | SysDept dept = findDeptByOrderClass(code); |
| | | if (dept != null && dept.getParentId() != null && dept.getParentId() == 100L) |
| | | { |
| | | log.debug("通过dispatch_order_class='{}' 找到部门: {} (ID={})", |
| | | code, dept.getDeptName(), dept.getDeptId()); |
| | | // 只处理分公司(parent_id=100) |
| | | VehicleDept vehicleDept = new VehicleDept(); |
| | | vehicleDept.setDeptId(dept.getDeptId()); |
| | | vehicleDept.setOrderClass(code); |
| | | vehicleDept.setCreateBy("system"); |
| | | vehicleDepts.add(vehicleDept); |
| | | |
| | | // 将部门ID转换为分公司ID |
| | | Long branchCompanyId = sysDeptMapper.selectBranchCompanyIdByDeptId(dept.getDeptId()); |
| | | if (branchCompanyId != null) |
| | | { |
| | | log.debug("将部门ID {} 转换为分公司ID: {}", dept.getDeptId(), branchCompanyId); |
| | | return branchCompanyId; |
| | | } |
| | | else |
| | | { |
| | | log.warn("部门ID {} 无法转换为分公司ID,可能是总公司或数据异常", dept.getDeptId()); |
| | | return null; |
| | | } |
| | | log.debug("通过order_class='{}' 找到分公司: {} (ID={})", |
| | | code, dept.getDeptName(), dept.getDeptId()); |
| | | } |
| | | else |
| | | { |
| | | log.debug("未找到匹配order_class='{}' 的分公司", code); |
| | | } |
| | | } |
| | | |
| | | log.warn("未找到匹配CarOrdClass='{}' 的部门", carOrdClass); |
| | | return null; |
| | | return vehicleDepts; |
| | | } |
| | | |
| | | /** |
| | | * 同步车辆-分公司关联 |
| | | * |
| | | * @param vehicleId 车辆ID |
| | | * @param vehicleDepts 分公司关联列表 |
| | | */ |
| | | private void syncVehicleDepts(Long vehicleId, List<VehicleDept> vehicleDepts) |
| | | { |
| | | if (vehicleId == null || vehicleDepts == null) |
| | | { |
| | | return; |
| | | } |
| | | |
| | | // 先删除旧的关联 |
| | | vehicleInfoMapper.deleteVehicleDeptByVehicleId(vehicleId); |
| | | |
| | | // 再插入新的关联 |
| | | if (!vehicleDepts.isEmpty()) |
| | | { |
| | | for (VehicleDept vd : vehicleDepts) |
| | | { |
| | | vd.setVehicleId(vehicleId); |
| | | } |
| | | vehicleInfoMapper.batchInsertVehicleDept(vehicleDepts); |
| | | log.debug("同步车辆ID={} 的分公司关联,数量: {}", vehicleId, vehicleDepts.size()); |
| | | } |
| | | } |
| | | |
| | | /** |
| | | * 根据dispatch_order_class查询部门 |
| | | * 根据order_class查询部门(同时匹配dispatch_order_class和service_order_class) |
| | | * |
| | | * @param dispatchOrderClass 调度单编码 |
| | | * @param orderClass 编码 |
| | | * @return 部门信息 |
| | | */ |
| | | private SysDept findDeptByDispatchOrderClass(String dispatchOrderClass) |
| | | private SysDept findDeptByOrderClass(String orderClass) |
| | | { |
| | | if (StringUtils.isBlank(dispatchOrderClass)) |
| | | if (StringUtils.isBlank(orderClass)) |
| | | { |
| | | return null; |
| | | } |
| | | |
| | | try |
| | | { |
| | | // 先尝试匹配dispatch_order_class |
| | | SysDept query = new SysDept(); |
| | | query.setDispatchOrderClass(dispatchOrderClass); |
| | | query.setDispatchOrderClass(orderClass); |
| | | List<SysDept> depts = sysDeptMapper.selectDeptList(query); |
| | | |
| | | if (depts != null && !depts.isEmpty()) |
| | | { |
| | | // 返回第一个匹配的部门 |
| | | return depts.get(0); |
| | | } |
| | | |
| | | // 如果没有找到,尝试匹配service_order_class |
| | | query = new SysDept(); |
| | | query.setServiceOrderClass(orderClass); |
| | | depts = sysDeptMapper.selectDeptList(query); |
| | | |
| | | if (depts != null && !depts.isEmpty()) |
| | | { |
| | | return depts.get(0); |
| | | } |
| | | } |
| | | catch (Exception e) |
| | | { |
| | | log.error("查询dispatch_order_class='{}' 的部门失败", dispatchOrderClass, e); |
| | | log.error("查询order_class='{}' 的部门失败", orderClass, e); |
| | | } |
| | | |
| | | return null; |