xyc
2026-11-11 5e9f38f4b1807f862d11a4979efac672724fd5ef
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
package com.trafficaudit.dataimport.service;
 
import com.trafficaudit.dataimport.entity.EnergyVehicleQuarterly;
import org.apache.poi.ss.usermodel.Row;
import org.apache.poi.ss.usermodel.Sheet;
import org.apache.poi.ss.usermodel.Workbook;
import org.apache.poi.xssf.usermodel.XSSFWorkbook;
import org.junit.jupiter.api.Test;
 
import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.io.FileInputStream;
import java.io.InputStream;
 
import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.assertNotNull;
import static org.junit.jupiter.api.Assertions.assertThrows;
import static org.junit.jupiter.api.Assertions.assertTrue;
 
/**
 * 能耗明细「按表头映射」解析测试(不落库):
 * 1) 系统模板 18 列/组;2) 合成官方风格(前导列含统一社会信用代码、15 列/组、只有第①组标表头);3) 负向。
 */
class EnergyImportMappingTest {
 
    private static final String SYS_TEMPLATE = "../docs/能耗/模板_道路货运车辆能源消耗情况(2026).xlsx";
 
    @Test
    void parseSystemTemplate() throws Exception {
        DataImportService svc = new DataImportService();
        DataImportService.EnergyParseResult r;
        try (InputStream in = new FileInputStream(SYS_TEMPLATE)) {
            r = svc.parseEnergyWorkbook(in, "2026-06");
        }
        assertEquals(1284, r.rows.size(), "系统模板应解析出 1284 台车");
        EnergyVehicleQuarterly first = r.rows.get(0).entity;
        assertEquals("鄂AUN210", first.getPlateNo());
        assertEquals("11", first.getVehicleTypeCode());
        assertEquals("02", first.getFuelTypeCode());
        assertEquals(38.3d, first.getMarkedTonnage(), 0.001);
        assertEquals(38.27d, first.getFuelPer100km(), 0.001);
        assertEquals(44.92d, first.getAvgDistance(), 0.001);
        // 车型码 12(冷藏保温车)已按官方码表收录,模板中对应车辆不应再产生未知码提醒
        assertEquals(0, r.warnings.size(), "车型码 12 已收录,不应产生未知码提醒");
    }
 
    @Test
    void parseOfficialStyleLayout() throws Exception {
        byte[] xlsx = buildOfficialStyleWorkbook();
        DataImportService svc = new DataImportService();
        DataImportService.EnergyParseResult r;
        try (InputStream in = new ByteArrayInputStream(xlsx)) {
            r = svc.parseEnergyWorkbook(in, "2025-06");
        }
        assertEquals(2, r.rows.size(), "①组 + ②组(②组无表头,按①组偏移外推)各 1 台");
        EnergyVehicleQuarterly g1 = r.rows.get(0).entity;
        assertEquals("鄂A00001", g1.getPlateNo());
        assertEquals("22010000001", g1.getEnterpriseCode());
        assertEquals(40d, g1.getMarkedTonnage(), 0.001);
        assertEquals(500d, g1.getAvgDistance(), 0.001, "平均运距=周转量/货运量");
        assertEquals(200d, g1.getAvgTripMileage(), 0.001, "单趟平均里程=总行程/趟次数");
        assertEquals(62.5d, g1.getAvgLoadedTonnage(), 0.001, "平均载货吨位=周转量/载货里程");
        assertEquals(30d, g1.getFuelPer100km(), 0.001, "百公里单耗=燃料消耗量/总行程*100");
        EnergyVehicleQuarterly g2 = r.rows.get(1).entity;
        assertEquals("鄂A00002", g2.getPlateNo(), "②组车牌号列应按①组偏移外推");
        assertEquals(20d, g2.getMarkedTonnage(), 0.001);
        assertEquals(10d, g2.getFuelPer100km(), 0.001);
    }
 
    @Test
    void rejectWhenNoPlateColumn() throws Exception {
        byte[] xlsx;
        try (Workbook wb = new XSSFWorkbook()) {
            Sheet sh = wb.createSheet("明细");
            Row h = sh.createRow(0);
            h.createCell(0).setCellValue("所属地区");
            h.createCell(1).setCellValue("企业名称");
            ByteArrayOutputStream bos = new ByteArrayOutputStream();
            wb.write(bos);
            xlsx = bos.toByteArray();
        }
        DataImportService svc = new DataImportService();
        Exception e = assertThrows(Exception.class, () -> {
            try (InputStream in = new ByteArrayInputStream(xlsx)) {
                svc.parseEnergyWorkbook(in, "2025-06");
            }
        });
        assertNotNull(e.getMessage());
        assertTrue(e.getMessage().contains("车牌"), "应提示找不到车牌号列,实际:" + e.getMessage());
    }
 
    /** 合成官方风格:前导 4 列(含统一社会信用代码)+ 5 组 x 15 列;只有第①组写了表头 */
    private byte[] buildOfficialStyleWorkbook() throws Exception {
        String[] groupHeader = {
            "车牌号", "车辆类型", "燃料类型", "出厂年份(年)", "标记吨位/准牵引总质量(吨)",
            "总行程(公里)", "载货里程(公里)", "空驶里程(公里)", "货运量(吨)", "货物周转量(吨公里)",
            "趟次数(个)", "燃料1计量单位", "燃料1消耗量", "燃料2计量单位", "燃料2消耗量"
        };
        try (Workbook wb = new XSSFWorkbook()) {
            Sheet sh = wb.createSheet("第二季度货车能耗明细");
            Row h = sh.createRow(0);
            h.createCell(0).setCellValue("所属地区");
            h.createCell(1).setCellValue("企业代码");
            h.createCell(2).setCellValue("企业名称");
            h.createCell(3).setCellValue("统一社会信用代码");
            for (int i = 0; i < groupHeader.length; i++) {
                h.createCell(4 + i).setCellValue(groupHeader[i]);
            }
            // ②组只有车牌号表头(模拟"表头只在①组写一次"),其余列为空
            h.createCell(4 + 15).setCellValue("车牌号");
 
            Row r = sh.createRow(1);
            r.createCell(0).setCellValue("420100");
            r.createCell(1).setCellValue("22010000001");
            r.createCell(2).setCellValue("测试物流有限公司");
            r.createCell(3).setCellValue("91420000TEST0001X");
            int c = 4;
            r.createCell(c++).setCellValue("鄂A00001");
            r.createCell(c++).setCellValue("11");
            r.createCell(c++).setCellValue("02");
            r.createCell(c++).setCellValue(2020);
            r.createCell(c++).setCellValue(40);
            r.createCell(c++).setCellValue(1000);
            r.createCell(c++).setCellValue(800);
            r.createCell(c++).setCellValue(200);
            r.createCell(c++).setCellValue(100);
            r.createCell(c++).setCellValue(50000);
            r.createCell(c++).setCellValue(5);
            r.createCell(c++).setCellValue("2");
            r.createCell(c++).setCellValue(300);
            // ②组(偏移 15,车牌号列 = 19)
            int c2 = 19;
            r.createCell(c2++).setCellValue("鄂A00002");
            r.createCell(c2++).setCellValue("02");
            r.createCell(c2++).setCellValue("02");
            r.createCell(c2++).setCellValue(2021);
            r.createCell(c2++).setCellValue(20);
            r.createCell(c2++).setCellValue(500);
            r.createCell(c2++).setCellValue(400);
            r.createCell(c2++).setCellValue(100);
            r.createCell(c2++).setCellValue(50);
            r.createCell(c2++).setCellValue(20000);
            r.createCell(c2++).setCellValue(4);
            r.createCell(c2++).setCellValue("2");
            r.createCell(c2++).setCellValue(50);
 
            ByteArrayOutputStream bos = new ByteArrayOutputStream();
            wb.write(bos);
            return bos.toByteArray();
        }
    }
}