| | |
| | | List<TransportAuthVehicle> list = new ArrayList<>(); |
| | | int failRows = 0; |
| | | try (Workbook wb = WorkbookFactory.create(file.getInputStream())) { |
| | | Sheet sheet = wb.getSheetAt(0); |
| | | requireHeaderCols(sheet, "运政车辆", "企业名称", "总牵引车数", "总挂车数", "其它车辆数", "挂车总吨位(吨)", "其它车辆总吨位(吨)"); |
| | | Map<String, Integer> colMap = buildHeaderMap(sheet.getRow(0)); |
| | | for (int r = 1; r <= sheet.getLastRowNum(); r++) { |
| | | Row row = sheet.getRow(r); |
| | | if (row == null) continue; |
| | | String name = gs(row, colMap, "企业名称"); |
| | | if (name == null || name.trim().isEmpty()) continue; |
| | | TransportAuthVehicle entity = new TransportAuthVehicle(); |
| | | entity.setReportPeriod(period); |
| | | entity.setEnterpriseName(name); |
| | | entity.setTractorCount(gi(row, colMap, "总牵引车数")); |
| | | entity.setTrailerCount(gi(row, colMap, "总挂车数")); |
| | | entity.setOtherCount(gi(row, colMap, "其它车辆数")); |
| | | entity.setTrailerTons(gd(row, colMap, "挂车总吨位(吨)")); |
| | | entity.setOtherTons(gd(row, colMap, "其它车辆总吨位(吨)")); |
| | | list.add(entity); |
| | | } |
| | | } catch (Exception e) { |
| | | throw e; |
| | | list = TransportAuthWorkbookParser.parse(wb, period); |
| | | } |
| | | |
| | | // 幂等:同报表期重新导入,先删除旧数据 |
| | |
| | | int aboveScale = 0; |
| | | int passenger = 0; |
| | | for (VehicleTrackMileage entity : list) { |
| | | entity.setDataSource(VehicleTrackMileage.SOURCE_PROVINCIAL); |
| | | entity.setCityName("全省"); |
| | | if (VehicleTrackMileage.CATEGORY_PASSENGER.equals(entity.getEnterpriseCategory())) { |
| | | passenger++; |
| | | } else { |
| | |
| | | |
| | | // Replacement is transactional so a failed import cannot leave a half-replaced month. |
| | | trackMileageMapper.delete(new LambdaQueryWrapper<VehicleTrackMileage>() |
| | | .eq(VehicleTrackMileage::getReportPeriod, period)); |
| | | .eq(VehicleTrackMileage::getReportPeriod, period) |
| | | .eq(VehicleTrackMileage::getDataSource, VehicleTrackMileage.SOURCE_PROVINCIAL) |
| | | .eq(VehicleTrackMileage::getCityName, "全省")); |
| | | deleteRuleResults("H2032", period); |
| | | |
| | | int success = 0; |
| | |
| | | } |
| | | } |
| | | Long batchId = recordBatch(file.getOriginalFilename(), "TRACK_MILEAGE", period, list.size(), success, 0, null); |
| | | attachBatchId("vehicle_track_mileage", period, batchId); |
| | | attachProvincialTrackBatchId(period, batchId); |
| | | log.info("Track mileage imported: {} rows for {} (above scale={}, passenger={})", |
| | | success, period, aboveScale, passenger); |
| | | return success; |
| | | } |
| | | |
| | | /** Imports and merges city third-party mileage files for one report period. */ |
| | | @Transactional(rollbackFor = Exception.class) |
| | | public synchronized int importCityTrackMileage(List<MultipartFile> files, String period, |
| | | String cityName) throws Exception { |
| | | if (files == null || files.isEmpty()) { |
| | | throw new IllegalArgumentException("Please select at least one city track mileage file"); |
| | | } |
| | | String city = RegionUtil.normalizeCityName(cityName); |
| | | if (city == null || !RegionUtil.CITY_LIST.contains(city)) { |
| | | throw new IllegalArgumentException("Invalid city name: " + cityName); |
| | | } |
| | | |
| | | List<Workbook> workbooks = new ArrayList<>(); |
| | | List<VehicleTrackMileage> list; |
| | | try { |
| | | for (MultipartFile file : files) { |
| | | workbooks.add(WorkbookFactory.create(file.getInputStream())); |
| | | } |
| | | Set<String> standardNames = loadLatestCityStandardEnterprises(period, city); |
| | | list = CityTrackMileageWorkbookParser.parse(workbooks, period, city, standardNames); |
| | | } finally { |
| | | for (Workbook workbook : workbooks) { |
| | | try { |
| | | workbook.close(); |
| | | } catch (IOException e) { |
| | | log.warn("Failed to close city track workbook", e); |
| | | } |
| | | } |
| | | } |
| | | |
| | | int aboveScale = 0; |
| | | int unregistered = 0; |
| | | for (VehicleTrackMileage entity : list) { |
| | | if (VehicleTrackMileage.CATEGORY_UNREGISTERED.equals(entity.getEnterpriseCategory())) { |
| | | unregistered++; |
| | | } else { |
| | | aboveScale++; |
| | | } |
| | | } |
| | | |
| | | trackMileageMapper.delete(new LambdaQueryWrapper<VehicleTrackMileage>() |
| | | .eq(VehicleTrackMileage::getReportPeriod, period) |
| | | .eq(VehicleTrackMileage::getDataSource, VehicleTrackMileage.SOURCE_CITY_THIRD_PARTY) |
| | | .eq(VehicleTrackMileage::getCityName, city)); |
| | | deleteRuleResults("H2032", period); |
| | | |
| | | for (VehicleTrackMileage entity : list) { |
| | | try { |
| | | trackMileageMapper.insert(entity); |
| | | } catch (Exception e) { |
| | | throw new IllegalStateException("City track mileage insert failed: city=" + city |
| | | + ", category=" + entity.getEnterpriseCategory() |
| | | + ", enterprise=" + entity.getEnterpriseName(), e); |
| | | } |
| | | } |
| | | |
| | | String batchFile = files.stream().map(MultipartFile::getOriginalFilename) |
| | | .filter(name -> name != null && !name.trim().isEmpty()).collect(Collectors.joining(", ")); |
| | | if (batchFile.length() > 1000) batchFile = batchFile.substring(0, 1000) + "..."; |
| | | Long batchId = recordBatch(batchFile, "TRACK_MILEAGE_CITY", period, list.size(), list.size(), 0, null); |
| | | attachCityTrackBatchId(period, city, batchId); |
| | | log.info("City track mileage imported: city={}, period={}, rows={}, aboveScale={}, unregistered={}", |
| | | city, period, list.size(), aboveScale, unregistered); |
| | | return list.size(); |
| | | } |
| | | |
| | | private Set<String> loadLatestCityStandardEnterprises(String period, String city) { |
| | | List<H2032EnterpriseMonthly> periods = h2032Mapper.selectList( |
| | | new LambdaQueryWrapper<H2032EnterpriseMonthly>() |
| | | .select(H2032EnterpriseMonthly::getReportPeriod) |
| | | .le(H2032EnterpriseMonthly::getReportPeriod, period) |
| | | .orderByDesc(H2032EnterpriseMonthly::getReportPeriod) |
| | | .last("LIMIT 1")); |
| | | if (periods.isEmpty()) { |
| | | throw new IllegalStateException("No H2032 enterprise list found at or before " + period); |
| | | } |
| | | String listPeriod = periods.get(0).getReportPeriod(); |
| | | String regionPrefix = RegionUtil.codeOfCity(city); |
| | | if (regionPrefix == null || regionPrefix.length() < 4) { |
| | | throw new IllegalArgumentException("Cannot resolve region code for city: " + city); |
| | | } |
| | | regionPrefix = regionPrefix.substring(0, 4); |
| | | List<H2032EnterpriseMonthly> enterprises = h2032Mapper.selectList( |
| | | new LambdaQueryWrapper<H2032EnterpriseMonthly>() |
| | | .select(H2032EnterpriseMonthly::getEnterpriseName) |
| | | .eq(H2032EnterpriseMonthly::getReportPeriod, listPeriod) |
| | | .likeRight(H2032EnterpriseMonthly::getRegionCode, regionPrefix)); |
| | | Set<String> names = new LinkedHashSet<>(); |
| | | for (H2032EnterpriseMonthly enterprise : enterprises) { |
| | | if (enterprise.getEnterpriseName() != null && !enterprise.getEnterpriseName().trim().isEmpty()) { |
| | | names.add(enterprise.getEnterpriseName().trim()); |
| | | } |
| | | } |
| | | if (names.isEmpty()) { |
| | | throw new IllegalStateException("No above-scale enterprises found for " + city |
| | | + " in H2032 period " + listPeriod); |
| | | } |
| | | return names; |
| | | } |
| | | |
| | | private void attachProvincialTrackBatchId(String period, Long batchId) { |
| | | if (batchId == null) return; |
| | | int rows = jdbcTemplate.update("UPDATE vehicle_track_mileage SET import_batch_id=? " + |
| | | "WHERE report_period=? AND data_source=? AND city_name=? AND import_batch_id IS NULL", |
| | | batchId, period, VehicleTrackMileage.SOURCE_PROVINCIAL, "全省"); |
| | | if (rows > 0) { |
| | | log.info("Provincial track import batch attached: period={}, batch={}, rows={}", period, batchId, rows); |
| | | } |
| | | } |
| | | |
| | | private void attachCityTrackBatchId(String period, String city, Long batchId) { |
| | | if (batchId == null) return; |
| | | int rows = jdbcTemplate.update("UPDATE vehicle_track_mileage SET import_batch_id=? " + |
| | | "WHERE report_period=? AND data_source=? AND city_name=? AND import_batch_id IS NULL", |
| | | batchId, period, VehicleTrackMileage.SOURCE_CITY_THIRD_PARTY, city); |
| | | if (rows > 0) { |
| | | log.info("City track import batch attached: city={}, period={}, batch={}, rows={}", |
| | | city, period, batchId, rows); |
| | | } |
| | | } |
| | | |
| | | // ========== 规上规下拆分 ========== |
| | |
| | | CLEAR_TYPE_TO_BATCH.put("h2032", new String[]{"H2032"}); |
| | | CLEAR_TYPE_TO_BATCH.put("h202", new String[]{"H202"}); |
| | | CLEAR_TYPE_TO_BATCH.put("transportAuth", new String[]{"TRANSPORT_AUTH"}); |
| | | CLEAR_TYPE_TO_BATCH.put("trackMileage", new String[]{"TRACK_MILEAGE"}); |
| | | CLEAR_TYPE_TO_BATCH.put("trackMileage", new String[]{"TRACK_MILEAGE", "TRACK_MILEAGE_CITY"}); |
| | | CLEAR_TYPE_TO_BATCH.put("scaleSplit", new String[]{"SCALE_SPLIT"}); |
| | | CLEAR_TYPE_TO_BATCH.put("freightTurnover", new String[]{"FREIGHT_TURNOVER"}); |
| | | CLEAR_TYPE_TO_BATCH.put("passengerMonthly", new String[]{"H2031"}); |