package com.trafficaudit.freightforecast.controller; import com.trafficaudit.common.Result; import com.trafficaudit.freightforecast.dto.BelowForecastRequest; import com.trafficaudit.freightforecast.dto.FreightForecastAnalysis; import com.trafficaudit.freightforecast.entity.FreightForecastAboveEnterprise; import com.trafficaudit.freightforecast.entity.FreightForecastBaseMonth; import com.trafficaudit.freightforecast.entity.FreightForecastBelowResult; import com.trafficaudit.freightforecast.entity.FreightForecastReportRound; import com.trafficaudit.freightforecast.service.FreightForecastExcelExporter; import com.trafficaudit.freightforecast.service.FreightForecastService; import lombok.extern.slf4j.Slf4j; import org.springframework.http.ContentDisposition; import org.springframework.http.HttpHeaders; import org.springframework.http.MediaType; import org.springframework.http.ResponseEntity; import org.springframework.web.bind.annotation.GetMapping; import org.springframework.web.bind.annotation.PostMapping; import org.springframework.web.bind.annotation.RequestBody; import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.RequestParam; import org.springframework.web.bind.annotation.RestController; import org.springframework.web.multipart.MultipartFile; import javax.annotation.Resource; import java.nio.charset.StandardCharsets; import java.util.LinkedHashMap; import java.util.List; import java.util.Map; @Slf4j @RestController @RequestMapping("/api/freight-forecast") public class FreightForecastController { @Resource private FreightForecastService service; @Resource private FreightForecastExcelExporter exporter; @GetMapping("/overview") public Result> overview(@RequestParam String period) { try { return Result.ok(service.overview(period)); } catch (Exception e) { return error(e); } } @GetMapping("/base") public Result> base(@RequestParam int year) { try { return Result.ok(service.baseMonths(year)); } catch (Exception e) { return error(e); } } @PostMapping("/base/import") public Result> importBase(@RequestParam("file") MultipartFile file, @RequestParam int year) { try { List rows = service.importBase(file, year); Map data = new LinkedHashMap<>(); data.put("count", rows.size()); data.put("year", year); data.put("rows", rows); return Result.ok(data); } catch (Exception e) { log.error("Freight forecast base import failed", e); return error(e); } } @PostMapping("/above/calculate") public Result> calculateAbove(@RequestParam String period) { try { return Result.ok(service.calculateAbove(period)); } catch (Exception e) { log.error("Freight forecast above calculation failed", e); return error(e); } } @GetMapping("/above/history") public Result> aboveHistory(@RequestParam int statYear, @RequestParam String period) { try { return Result.ok(service.aboveHistory(statYear, period)); } catch (Exception e) { log.error("Freight forecast above history query failed", e); return error(e); } } @GetMapping("/above") public Result> above( @RequestParam String period, @RequestParam(required = false) String city, @RequestParam(required = false) String keyword) { try { return Result.ok(service.aboveRows(period, city, keyword)); } catch (Exception e) { return error(e); } } @GetMapping("/below") public Result> below(@RequestParam String period) { try { return Result.ok(service.belowResults(period)); } catch (Exception e) { return error(e); } } @PostMapping("/below/calculate") public Result calculateBelow(@RequestBody BelowForecastRequest request) { try { return Result.ok(service.calculateBelow(request)); } catch (Exception e) { log.error("Freight forecast below calculation failed", e); return error(e); } } @GetMapping("/report/rounds") public Result> reportRounds(@RequestParam String period) { try { return Result.ok(service.reportRounds(period)); } catch (Exception e) { return error(e); } } @PostMapping("/report/import") public Result importReport( @RequestParam("file") MultipartFile file, @RequestParam String period, @RequestParam String stage, @RequestParam(required = false) String note) { try { return Result.ok(service.importReport(file, period, stage, note)); } catch (Exception e) { log.error("Freight forecast report import failed", e); return error(e); } } @GetMapping("/report/analysis") public Result analysis( @RequestParam String period, @RequestParam(defaultValue = "SCALE_SPLIT_HISTORY_PLUS_MODULE") String belowHistorySource, @RequestParam(defaultValue = "HISTORY_PLUS_MODULE") String forecastHistorySource) { try { return Result.ok(service.analyze(period, belowHistorySource, forecastHistorySource)); } catch (Exception e) { log.error("Freight forecast analysis failed", e); return error(e); } } @GetMapping("/report/export") public ResponseEntity export( @RequestParam String period, @RequestParam(defaultValue = "SCALE_SPLIT_HISTORY_PLUS_MODULE") String belowHistorySource, @RequestParam(defaultValue = "HISTORY_PLUS_MODULE") String forecastHistorySource) throws Exception { FreightForecastAnalysis analysis = service.analyze(period, belowHistorySource, forecastHistorySource); byte[] bytes = exporter.exportDifferenceRateReport(analysis); String year = period.substring(0, 4); int month = Integer.parseInt(period.substring(5, 7)); String fileName = year + "年" + month + "月规上上报差异率情况.xlsx"; HttpHeaders headers = new HttpHeaders(); headers.setContentType(MediaType.parseMediaType( "application/vnd.openxmlformats-officedocument.spreadsheetml.sheet")); headers.setContentDisposition(ContentDisposition.attachment() .filename(fileName, StandardCharsets.UTF_8).build()); headers.setContentLength(bytes.length); return ResponseEntity.ok().headers(headers).body(bytes); } @GetMapping("/report/enterprise-export-by-city") public ResponseEntity exportEnterprisesByCity( @RequestParam String period, @RequestParam(defaultValue = "SCALE_SPLIT_HISTORY_PLUS_MODULE") String belowHistorySource, @RequestParam(defaultValue = "HISTORY_PLUS_MODULE") String forecastHistorySource) throws Exception { FreightForecastAnalysis analysis = service.analyze(period, belowHistorySource, forecastHistorySource); byte[] bytes = exporter.exportCityEnterpriseReports(analysis); String year = period.substring(0, 4); int month = Integer.parseInt(period.substring(5, 7)); String fileName = year + "年" + month + "月规上企业上报差异率_分市州.zip"; HttpHeaders headers = new HttpHeaders(); headers.setContentType(MediaType.parseMediaType("application/zip")); headers.setContentDisposition(ContentDisposition.attachment() .filename(fileName, StandardCharsets.UTF_8).build()); headers.setContentLength(bytes.length); return ResponseEntity.ok().headers(headers).body(bytes); } private Result error(Exception e) { String message = e.getMessage(); return Result.error(message == null || message.trim().isEmpty() ? "货运测算处理失败" : message); } }