| | |
| | | package com.trafficaudit.dataimport.service; |
| | | |
| | | import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper; |
| | | import com.trafficaudit.auditengine.service.TrackMileageCitySourceRuleService; |
| | | import com.trafficaudit.auditengine.service.TrackMileageSelector; |
| | | import com.trafficaudit.common.util.RegionUtil; |
| | | import com.trafficaudit.dataimport.entity.TransportAuthVehicle; |
| | | import com.trafficaudit.dataimport.entity.VehicleTrackMileage; |
| | |
| | | import java.util.List; |
| | | import java.util.Locale; |
| | | import java.util.Map; |
| | | import java.util.Set; |
| | | import java.util.zip.ZipEntry; |
| | | import java.util.zip.ZipOutputStream; |
| | | |
| | |
| | | @Service |
| | | public class TrackMileageCityDistributionService { |
| | | |
| | | private static final String XIAOGAN_CITY = "孝感市"; |
| | | private static final String SHEET_NAME = "车辆数及轨迹"; |
| | | private static final String[] HEADERS = { |
| | | "市州", "企业名称", "总牵引车数", "总挂车数", "其它车辆数", |
| | |
| | | private TransportAuthVehicleMapper transportAuthVehicleMapper; |
| | | @Resource |
| | | private VehicleTrackMileageMapper vehicleTrackMileageMapper; |
| | | @Resource |
| | | private TrackMileageCitySourceRuleService trackCitySourceRuleService; |
| | | |
| | | public byte[] exportZip(String period) throws IOException { |
| | | String normalizedPeriod = normalizePeriod(period); |
| | |
| | | |
| | | Map<String, TransportAuthVehicle> authByName = loadAuthByName(normalizedPeriod); |
| | | Map<String, List<VehicleTrackMileage>> trackByName = loadTrackByName(normalizedPeriod); |
| | | Set<String> mandatoryCities = trackCitySourceRuleService.mandatoryCities(normalizedPeriod); |
| | | |
| | | Map<String, List<DistributionRow>> rowsByCity = new LinkedHashMap<>(); |
| | | for (String city : RegionUtil.cityList()) { |
| | |
| | | |
| | | String key = normalizeEnterpriseName(enterpriseName); |
| | | TransportAuthVehicle auth = authByName.get(key); |
| | | VehicleTrackMileage track = chooseTrack(city, trackByName.get(key)); |
| | | VehicleTrackMileage track = chooseTrack(city, trackByName.get(key), mandatoryCities); |
| | | rowsByCity.get(city).add(new DistributionRow(city, enterpriseName, auth, track)); |
| | | } |
| | | |
| | |
| | | rows.sort(byName); |
| | | } |
| | | |
| | | List<DistributionRow> allRows = new ArrayList<>(); |
| | | for (String city : RegionUtil.cityList()) { |
| | | allRows.addAll(rowsByCity.get(city)); |
| | | } |
| | | |
| | | ByteArrayOutputStream zipBytes = new ByteArrayOutputStream(); |
| | | try (ZipOutputStream zip = new ZipOutputStream(zipBytes)) { |
| | | writeZipEntry( |
| | | zip, |
| | | fileName(normalizedPeriod, "全部企业"), |
| | | buildWorkbook(allRows)); |
| | | for (String city : RegionUtil.cityList()) { |
| | | byte[] workbookBytes = buildCityWorkbook(rowsByCity.get(city)); |
| | | String fileName = fileName(normalizedPeriod, city); |
| | | ZipEntry entry = new ZipEntry(fileName); |
| | | zip.putNextEntry(entry); |
| | | zip.write(workbookBytes); |
| | | zip.closeEntry(); |
| | | writeZipEntry( |
| | | zip, |
| | | fileName(normalizedPeriod, city), |
| | | buildWorkbook(rowsByCity.get(city))); |
| | | } |
| | | } |
| | | return zipBytes.toByteArray(); |
| | | } |
| | | |
| | | private void writeZipEntry(ZipOutputStream zip, String fileName, byte[] workbookBytes) throws IOException { |
| | | ZipEntry entry = new ZipEntry(fileName); |
| | | zip.putNextEntry(entry); |
| | | zip.write(workbookBytes); |
| | | zip.closeEntry(); |
| | | } |
| | | |
| | | public String zipFileName(String period) { |
| | |
| | | return result; |
| | | } |
| | | |
| | | private VehicleTrackMileage chooseTrack(String city, List<VehicleTrackMileage> candidates) { |
| | | private VehicleTrackMileage chooseTrack(String city, List<VehicleTrackMileage> candidates, |
| | | Set<String> mandatoryCities) { |
| | | if (candidates == null || candidates.isEmpty()) { |
| | | return null; |
| | | } |
| | | if (XIAOGAN_CITY.equals(city)) { |
| | | // Xiaogan must always use its own third-party source. Provincial data |
| | | // is not a fallback for any Xiaogan enterprise. |
| | | return latestBySource(candidates, VehicleTrackMileage.SOURCE_CITY_THIRD_PARTY); |
| | | } |
| | | |
| | | VehicleTrackMileage provincial = latestBySource(candidates, VehicleTrackMileage.SOURCE_PROVINCIAL); |
| | | VehicleTrackMileage citySource = latestBySource(candidates, VehicleTrackMileage.SOURCE_CITY_THIRD_PARTY); |
| | | if (provincial == null) { |
| | | return citySource; |
| | | } |
| | | if (citySource == null) { |
| | | return provincial; |
| | | } |
| | | return mileagePerTrackedVehicle(citySource) > mileagePerTrackedVehicle(provincial) |
| | | ? citySource : provincial; |
| | | } |
| | | |
| | | private VehicleTrackMileage latestBySource(List<VehicleTrackMileage> candidates, String source) { |
| | | String requiredCity = mandatoryCities.contains(city) ? city : null; |
| | | VehicleTrackMileage selected = null; |
| | | for (VehicleTrackMileage candidate : candidates) { |
| | | if (source.equals(candidate.getDataSource())) { |
| | | return candidate; |
| | | } |
| | | selected = TrackMileageSelector.choose(selected, candidate, requiredCity); |
| | | } |
| | | return null; |
| | | return selected; |
| | | } |
| | | |
| | | private double effectiveMileage(VehicleTrackMileage row) { |
| | |
| | | return excluded ? mileage : mileage + anomaly; |
| | | } |
| | | |
| | | private double mileagePerTrackedVehicle(VehicleTrackMileage row) { |
| | | int trackedVehicles = row.getTrackedVehicles() == null ? 0 : row.getTrackedVehicles(); |
| | | return trackedVehicles <= 0 ? -1.0 : effectiveMileage(row) / trackedVehicles; |
| | | } |
| | | |
| | | private byte[] buildCityWorkbook(List<DistributionRow> rows) throws IOException { |
| | | private byte[] buildWorkbook(List<DistributionRow> rows) throws IOException { |
| | | try (XSSFWorkbook workbook = new XSSFWorkbook(); |
| | | ByteArrayOutputStream output = new ByteArrayOutputStream()) { |
| | | XSSFSheet sheet = workbook.createSheet(SHEET_NAME); |