xyc
2026-08-14 1f4a6cc6045f47861c3e8d49a782afc3e11e3843
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
# -*- coding: utf-8 -*-
import io
 
path = r'C:\Users\jcxiong\Documents\Codex\MyProject\trafficAudit\traffic-audit-server\src\main\java\com\trafficaudit\reportexport\service\ReportExportService.java'
with io.open(path, encoding='utf-8') as f:
    content = f.read()
has_crlf = '\r\n' in content
lines = content.split('\n')
 
def func_span(name):
    start = next(i for i, l in enumerate(lines) if l.strip().startswith('public byte[] ' + name + '('))
    end = len(lines)
    for i in range(start+1, len(lines)):
        if lines[i].strip().startswith('public byte[] '):
            end = i
            break
    return start, end
 
def process_rank(func, metric, unit, title_suffix, city_writer, province_writer):
    fs, fe = func_span(func)
    i = next(i for i in range(fs, fe) if lines[i].strip() == 'int rowIdx = 3;')
    j = i
    while j > fs and 'Row head1 = sheet.createRow(' not in lines[j]:
        j -= 1
    assert 'Row head1' in lines[j], func + ' head1 not found'
    lines[j:i] = ['        rowIdx = writeRankHead(sheet, rowIdx, "%s", "%s");' % (metric, unit)]
    auto_i = next(m for m in range(j+1, fe) if lines[m].strip() == 'autoWidth(sheet, 18);')
    insert = [
        '',
        '        // 本月区块(当月标题 + 累计数据,与样例结构一致)',
        '        sheet.createRow(rowIdx++);',
        '        Row title2 = sheet.createRow(rowIdx++);',
        '        title2.createCell(0).setCellValue(year + "年" + month + "月全省分市州累计完成" + "%s");' % title_suffix,
        '        rowIdx = writeRankHead(sheet, rowIdx, "%s", "%s");' % (metric, unit),
        '        rowIdx = %s;' % province_writer,
        '        for (String city : RegionUtil.cityList()) {',
        '            rowIdx = %s;' % city_writer,
        '        }',
        '',
        '        // 末尾空行(与样例结构对齐)',
        '        for (int i = 0; i < 4; i++) sheet.createRow(rowIdx++);',
        '',
    ]
    lines[auto_i:auto_i] = insert
    print('processed', func)
 
process_rank('exportTurnoverRank', '周转量', '万吨公里', '公路货物运输周转量情况',
             'writeTurnoverRankRows(sheet, rowIdx, city, cumMap, false)',
             'writeTurnoverRankRows(sheet, rowIdx, null, cumMap, true)')
process_rank('exportFreightRank', '货运量', '万吨', '公路货运量情况',
             'writeRankRows(sheet, rowIdx, city, cumMap, h2032FreightCum, h2032FreightYoy, false)',
             'writeRankRows(sheet, rowIdx, null, cumMap, h2032FreightCum, h2032FreightYoy, true)')
 
# Change A: blank row before head2 in exportFreightTurnover
blank = [i for i, l in enumerate(lines) if l.strip() == 'sheet.createRow(rowIdx++);']
target = [i for i in blank if i+1 < len(lines) and 'Row head2 = sheet.createRow(rowIdx++);' in lines[i+1]]
assert len(target) == 1, target
del lines[target[0]]
print('removed blank row at', target[0]+1)
 
# helper before writeRankRows
anchor = next(i for i, l in enumerate(lines) if l.strip().startswith('private int writeRankRows('))
helper = [
    '    /** 排名表双行表头 */',
    '    private int writeRankHead(Sheet sheet, int rowIdx, String metric, String unit) {',
    '        Row h1 = sheet.createRow(rowIdx++);',
    '        h1.createCell(0).setCellValue("市州");',
    '        h1.createCell(1).setCellValue("规上" + metric);',
    '        h1.createCell(4).setCellValue("规上增速");',
    '        h1.createCell(6).setCellValue("规下" + metric);',
    '        h1.createCell(9).setCellValue("规下增速");',
    '        h1.createCell(11).setCellValue("合计" + metric);',
    '        h1.createCell(14).setCellValue("合计增速");',
    '        h1.createCell(16).setCellValue("分市州规上规下占比");',
    '',
    '        Row h2 = sheet.createRow(rowIdx++);',
    '        h2.createCell(1).setCellValue("累计完成    (万" + unit + ")");',
    '        h2.createCell(2).setCellValue("排名");',
    '        h2.createCell(3).setCellValue("占全省比重");',
    '        h2.createCell(4).setCellValue("同比");',
    '        h2.createCell(5).setCellValue("增速    排名");',
    '        h2.createCell(6).setCellValue("累计完成    (万" + unit + ")");',
    '        h2.createCell(7).setCellValue("排名");',
    '        h2.createCell(8).setCellValue("占全省比重");',
    '        h2.createCell(9).setCellValue("同比");',
    '        h2.createCell(10).setCellValue("增速排名");',
    '        h2.createCell(11).setCellValue("累计完成    (万" + unit + ")");',
    '        h2.createCell(12).setCellValue("排名");',
    '        h2.createCell(13).setCellValue("占全省比重");',
    '        h2.createCell(14).setCellValue("同比");',
    '        h2.createCell(15).setCellValue("增速排名");',
    '        return rowIdx;',
    '    }',
    '',
    '',
]
lines[anchor:anchor] = helper
print('helper inserted')
 
new_content = '\n'.join(lines)
if has_crlf:
    new_content = new_content.replace('\n', '\r\n')
with io.open(path, 'w', encoding='utf-8', newline='') as f:
    f.write(new_content)
print('saved')