package com.trafficaudit.dataimport.controller;
|
|
import com.trafficaudit.common.Result;
|
import com.trafficaudit.dataimport.dto.ImportResult;
|
import com.trafficaudit.dataimport.service.DataImportService;
|
import lombok.extern.slf4j.Slf4j;
|
import org.springframework.web.bind.annotation.PostMapping;
|
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;
|
|
@Slf4j
|
@RestController
|
@RequestMapping("/api/import")
|
public class DataImportController {
|
|
@Resource
|
private DataImportService importService;
|
|
/** H203-2表 道路货物运输月度生产情况 */
|
@PostMapping("/h2032")
|
public Result<String> importH2032(@RequestParam("file") MultipartFile file,
|
@RequestParam("period") String period) {
|
try {
|
ImportResult result = importService.importH2032(file, period);
|
return Result.ok(buildH2032Message(result));
|
} catch (Exception e) {
|
log.error("H2032 import error", e);
|
return Result.error("导入失败: " + e.getMessage());
|
}
|
}
|
|
private String buildH2032Message(ImportResult r) {
|
if (r.getFail() == 0) {
|
return "H2032月报导入成功,共 " + r.getSuccess() + " 条记录";
|
}
|
StringBuilder sb = new StringBuilder("H2032月报导入完成:成功 " + r.getSuccess() + " 条,失败 " + r.getFail() + " 条。失败明细:");
|
java.util.List<String> details = r.getFailDetails();
|
int shown = Math.min(details.size(), 100);
|
for (int i = 0; i < shown; i++) {
|
sb.append("\n").append(details.get(i));
|
}
|
if (details.size() > shown) {
|
sb.append("\n……共 ").append(details.size()).append(" 行失败");
|
}
|
return sb.toString();
|
}
|
|
|
/** 运政车辆 */
|
@PostMapping("/transportAuth")
|
public Result<String> importTransportAuth(@RequestParam("file") MultipartFile file,
|
@RequestParam("period") String period) {
|
try {
|
int count = importService.importTransportAuth(file, period);
|
return Result.ok("运政车辆导入成功, 共 " + count + " 条记录");
|
} catch (Exception e) {
|
log.error("transportAuth import error", e);
|
return Result.error("导入失败: " + e.getMessage());
|
}
|
}
|
|
/** 轨迹里程 */
|
@PostMapping("/trackMileage")
|
public Result<String> importTrackMileage(@RequestParam("file") MultipartFile file,
|
@RequestParam("period") String period) {
|
try {
|
int count = importService.importTrackMileage(file, period);
|
return Result.ok("轨迹里程导入成功, 共 " + count + " 条记录");
|
} catch (Exception e) {
|
log.error("trackMileage import error", e);
|
return Result.error("导入失败: " + e.getMessage());
|
}
|
}
|
|
/** 模板_货运量周转量.xlsx(每月导入:左半今年1-N月,右半去年1-12月) */
|
@PostMapping("/freightTurnover")
|
public Result<String> importFreightTurnover(@RequestParam("file") MultipartFile file,
|
@RequestParam("period") String period) {
|
try {
|
int count = importService.importFreightTurnover(file, period);
|
return Result.ok("货运量周转量导入成功, 共 " + count + " 条记录");
|
} catch (Exception e) {
|
log.error("freightTurnover import error", e);
|
return Result.error("导入失败: " + e.getMessage());
|
}
|
}
|
|
/** 规上规下拆分运输量 */
|
@PostMapping("/scaleSplit")
|
public Result<String> importScaleSplit(@RequestParam("file") MultipartFile file,
|
@RequestParam("period") String period) {
|
try {
|
int count = importService.importScaleSplit(file, period);
|
return Result.ok("规上规下拆分导入成功, 共 " + count + " 条记录");
|
} catch (Exception e) {
|
log.error("scaleSplit import error", e);
|
return Result.error("导入失败: " + e.getMessage());
|
}
|
}
|
}
|