1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
package com.trafficaudit.dataimport.controller;
 
import com.trafficaudit.common.Result;
import com.trafficaudit.dataimport.dto.ImportResult;
import com.trafficaudit.dataimport.entity.ImportBatch;
import com.trafficaudit.dataimport.mapper.ImportBatchMapper;
import com.trafficaudit.dataimport.service.DataImportService;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.GetMapping;
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;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;
 
@Slf4j
@RestController
@RequestMapping("/api/import")
public class DataImportController {
 
    @Resource
    private DataImportService importService;
    @Resource
    private ImportBatchMapper importBatchMapper;
 
    /** 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();
    }
 
 
 
    /** H204 道路货运车辆能源消耗情况(季度能耗明细 + 运政车辆信息) */
    @PostMapping("/energyMonthly")
    public Result<String> importEnergyMonthly(@RequestParam("file") MultipartFile file,
                                              @RequestParam("period") String period) {
        try {
            ImportResult result = importService.importEnergyMonthly(file, period);
            String msg = "H204能耗季报导入完成:成功 " + result.getSuccess() + " 条,失败 " + result.getFail() + " 条(含同报表期运政车辆信息)";
            if (result.getFail() > 0) {
                java.util.List<String> details = result.getFailDetails();
                int shown = Math.min(details.size(), 100);
                for (int i = 0; i < shown; i++) {
                    msg += "\n" + details.get(i);
                }
                if (details.size() > shown) {
                    msg += "\n……共 " + details.size() + " 行失败";
                }
            }
            return Result.ok(msg);
        } catch (Exception e) {
            log.error("energyMonthly import error", e);
            return Result.error("导入失败: " + e.getMessage());
        }
    }
 
    /** 能耗车辆运政信息(独立模板,也可随 H204 季报第二个 sheet 一并导入) */
    @PostMapping("/energyAuth")
    public Result<String> importEnergyAuth(@RequestParam("file") MultipartFile file,
                                           @RequestParam("period") String period) {
        try {
            ImportResult result = importService.importEnergyAuth(file, period);
            String msg = "能耗车辆运政信息导入完成:成功 " + result.getSuccess() + " 条,失败 " + result.getFail() + " 条";
            if (result.getFail() > 0) {
                java.util.List<String> details = result.getFailDetails();
                int shown = Math.min(details.size(), 100);
                for (int i = 0; i < shown; i++) {
                    msg += "\n" + details.get(i);
                }
                if (details.size() > shown) {
                    msg += "\n……共 " + details.size() + " 行失败";
                }
            }
            return Result.ok(msg);
        } catch (Exception e) {
            log.error("energyAuth import error", e);
            return Result.error("导入失败: " + e.getMessage());
        }
    }
 
    /** H203-1表 公路旅客运输月度生产情况(含运政车辆数sheet) */
    @PostMapping("/passengerMonthly")
    public Result<String> importPassengerMonthly(@RequestParam("file") MultipartFile file,
                                                 @RequestParam("period") String period) {
        try {
            ImportResult result = importService.importPassengerMonthly(file, period);
            String msg = "H2031旅客月报导入完成:成功 " + result.getSuccess() + " 条,失败 " + result.getFail() + " 条(含同报表期运政车辆数据)";
            if (result.getFail() > 0) {
                java.util.List<String> details = result.getFailDetails();
                int shown = Math.min(details.size(), 100);
                for (int i = 0; i < shown; i++) {
                    msg += "\n" + details.get(i);
                }
                if (details.size() > shown) {
                    msg += "\n……共 " + details.size() + " 行失败";
                }
            }
            return Result.ok(msg);
        } catch (Exception e) {
            log.error("passengerMonthly import error", e);
            return Result.error("导入失败: " + e.getMessage());
        }
    }
 
 
    /** 公路旅客个体客运量/周转量(企业线下汇报,导入后供分市州表个体行填充) */
    @PostMapping("/passengerIndividual")
    public Result<String> importPassengerIndividual(@RequestParam("file") MultipartFile file,
                                                    @RequestParam("period") String period) {
        try {
            ImportResult result = importService.importPassengerIndividual(file, period);
            String msg = "个体客运量导入完成:成功 " + result.getSuccess() + " 条,失败 " + result.getFail() + " 条";
            if (result.getFail() > 0) {
                java.util.List<String> details = result.getFailDetails();
                int shown = Math.min(details.size(), 100);
                for (int i = 0; i < shown; i++) {
                    msg += "\n" + details.get(i);
                }
                if (details.size() > shown) {
                    msg += "\n……共 " + details.size() + " 行失败";
                }
            }
            return Result.ok(msg);
        } catch (Exception e) {
            log.error("passengerIndividual import error", e);
            return Result.error("导入失败: " + e.getMessage());
        }
    }
 
    /** 公路旅客运政车辆数(独立模板,也可随 H203-1 月报第二个 sheet 一并导入) */
    @PostMapping("/passengerAuth")
    public Result<String> importPassengerAuth(@RequestParam("file") MultipartFile file,
                                              @RequestParam("period") String period) {
        try {
            ImportResult result = importService.importPassengerAuth(file, period);
            String msg = "旅客运政车辆数导入完成:成功 " + result.getSuccess() + " 条,失败 " + result.getFail() + " 条";
            if (result.getFail() > 0) {
                java.util.List<String> details = result.getFailDetails();
                int shown = Math.min(details.size(), 100);
                for (int i = 0; i < shown; i++) {
                    msg += "\n" + details.get(i);
                }
                if (details.size() > shown) {
                    msg += "\n……共 " + details.size() + " 行失败";
                }
            }
            return Result.ok(msg);
        } catch (Exception e) {
            log.error("passengerAuth import error", e);
            return Result.error("导入失败: " + e.getMessage());
        }
    }
 
    /** 城市公交月报(城市公共交通月度运营情况,企业级明细) */
    @PostMapping("/cityBus")
    public Result<String> importCityBus(@RequestParam("file") MultipartFile file,
                                        @RequestParam("period") String period) {
        try {
            ImportResult result = importService.importCityBus(file, period);
            String msg = "城市公交月报导入完成:成功 " + result.getSuccess() + " 条,失败 " + result.getFail() + " 条";
            if (result.getFail() > 0) {
                java.util.List<String> details = result.getFailDetails();
                int shown = Math.min(details.size(), 100);
                for (int i = 0; i < shown; i++) {
                    msg += "\n" + details.get(i);
                }
                if (details.size() > shown) {
                    msg += "\n……共 " + details.size() + " 行失败";
                }
            }
            return Result.ok(msg);
        } catch (Exception e) {
            log.error("cityBus import error", e);
            return Result.error("导入失败: " + e.getMessage());
        }
    }
 
    /** 巡游出租汽车运营服务情况月报(市州级) */
    @PostMapping("/cityTaxi")
    public Result<String> importCityTaxi(@RequestParam("file") MultipartFile file,
                                         @RequestParam("period") String period) {
        try {
            ImportResult result = importService.importCityTaxi(file, period);
            String msg = "巡游出租月报导入完成:成功 " + result.getSuccess() + " 条,失败 " + result.getFail() + " 条";
            if (result.getFail() > 0) {
                java.util.List<String> details = result.getFailDetails();
                int shown = Math.min(details.size(), 100);
                for (int i = 0; i < shown; i++) {
                    msg += "\n" + details.get(i);
                }
                if (details.size() > shown) {
                    msg += "\n……共 " + details.size() + " 行失败";
                }
            }
            return Result.ok(msg);
        } catch (Exception e) {
            log.error("cityTaxi import error", e);
            return Result.error("导入失败: " + e.getMessage());
        }
    }
 
    /** 出租车运政车辆信息(车辆级明细,用于运营车数对比) */
    @PostMapping("/cityTaxiAuth")
    public Result<String> importCityTaxiAuth(@RequestParam("file") MultipartFile file,
                                             @RequestParam("period") String period) {
        try {
            int count = importService.importCityTaxiAuth(file, period);
            return Result.ok("出租车运政车辆导入成功, 共 " + count + " 条记录");
        } catch (Exception e) {
            log.error("cityTaxiAuth import error", e);
            return Result.error("导入失败: " + e.getMessage());
        }
    }
 
    /** 投资月报(全省汇总大表):category=客运站场/物流园区 */
    @PostMapping("/investment")
    public Result<String> importInvestment(@RequestParam("file") MultipartFile file,
                                           @RequestParam("period") String period,
                                           @RequestParam(value = "category", defaultValue = "客运站场") String category) {
        try {
            ImportResult result = importService.importInvestment(file, period, category);
            String msg = "投资月报导入完成(" + category + "):成功 " + result.getSuccess() + " 条,失败 " + result.getFail() + " 条";
            if (result.getFail() > 0) {
                java.util.List<String> details = result.getFailDetails();
                int shown = Math.min(details.size(), 100);
                for (int i = 0; i < shown; i++) {
                    msg += "\n" + details.get(i);
                }
                if (details.size() > shown) {
                    msg += "\n……共 " + details.size() + " 行失败";
                }
            }
            return Result.ok(msg);
        } catch (Exception e) {
            log.error("investment import error", e);
            return Result.error("导入失败: " + e.getMessage());
        }
    }
 
    /** 投资市州单表批量导入(M2):扫描 docs/投资/输入/{类别}/{X月} 目录全部市州报表,category=客运站场/物流园区 */
    @PostMapping("/investCityBatch")
    public Result<Object> importInvestCityBatch(@RequestParam("period") String period,
                                                @RequestParam(value = "category", defaultValue = "客运站场") String category) {
        try {
            Map<String, Object> result = importService.importInvestCityDir(period, category);
            return Result.ok(result);
        } catch (Exception e) {
            log.error("investCityBatch import error", e);
            return Result.error("导入失败: " + e.getMessage());
        }
    }
 
    /** 投资系统导出(审核比对基准) */
    @PostMapping("/investmentSystem")
    public Result<String> importInvestmentSystem(@RequestParam("file") MultipartFile file,
                                                 @RequestParam("period") String period) {
        try {
            int count = importService.importInvestmentSystem(file, period);
            return Result.ok("投资系统导出导入成功, 共 " + count + " 条记录");
        } catch (Exception e) {
            log.error("investmentSystem import error", e);
            return Result.error("导入失败: " + e.getMessage());
        }
    }
    /** 运政车辆 */
    @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());
        }
    }
    /** 目录浏览器:path 为空返回项目 docs 数据目录,返回子目录与当前目录 Excel 文件 */
    @GetMapping("/dirs")
    public Result<Map<String, Object>> dirs(@RequestParam(value = "path", required = false) String path) {
        try {
            Map<String, Object> result = importService.listDirs(path);
            return Result.ok(result);
        } catch (Exception e) {
            log.error("list dirs error", e);
            return Result.error("目录浏览失败: " + e.getMessage());
        }
    }
 
    /** 按路径导入单个 Excel 文件(前端逐文件导入显示进度);报表期优先从文件名识别 */
    @PostMapping("/fromDirFile")
    public Result<Map<String, Object>> importFromDirFile(@RequestParam("type") String type,
                                                         @RequestParam(value = "period", required = false) String period,
                                                         @RequestParam(value = "skipImported", defaultValue = "true") boolean skipImported,
                                                         @RequestParam("path") String path) {
        try {
            Map<String, Object> result = importService.importDirFileByPath(type, period, skipImported, path);
            return Result.ok(result);
        } catch (Exception e) {
            log.error("fromDirFile import error", e);
            return Result.error("导入失败: " + e.getMessage());
        }
    }
 
    /** 从服务端文件夹批量导入:扫描 dir 下全部 .xlsx/.xls,按 type 逐个导入;报表期优先从文件名识别 */
    @PostMapping("/fromDir")
    public Result<Object> importFromDir(@RequestParam("type") String type,
                                        @RequestParam(value = "period", required = false) String period,
                                        @RequestParam(value = "skipImported", defaultValue = "true") boolean skipImported,
                                        @RequestParam("dir") String dir) {
        try {
            Map<String, Object> result = importService.importFromDir(type, period, skipImported, dir);
            return Result.ok(result);
        } catch (Exception e) {
            log.error("fromDir import error", e);
            return Result.error("导入失败: " + e.getMessage());
        }
    }
 
    /** 导入批次历史(数据导入页展示最近导入结果) */
    @GetMapping("/batches")
    public Result<Map<String, Object>> batches(@RequestParam(value = "type", required = false) String type,
                                               @RequestParam(value = "period", required = false) String period,
                                               @RequestParam(value = "limit", defaultValue = "20") int limit) {
        int safeLimit = Math.min(Math.max(limit, 1), 200);
        LambdaQueryWrapper<ImportBatch> w = new LambdaQueryWrapper<ImportBatch>()
            .eq(type != null && !type.trim().isEmpty(), ImportBatch::getImportType, type)
            .eq(period != null && !period.trim().isEmpty(), ImportBatch::getReportPeriod, period)
            .orderByDesc(ImportBatch::getId)
            .last("limit " + safeLimit);
        List<ImportBatch> list = importBatchMapper.selectList(w);
        Map<String, Object> result = new LinkedHashMap<>();
        result.put("records", list);
        return Result.ok(result);
    }
 
}