P15GEN2\59518
2025-10-10 9f6890646993d16260d4201d613c092132856127
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
package foundation.io.file.pull;
 
import org.apache.poi.xssf.model.SharedStringsTable;
import org.apache.poi.xssf.model.StylesTable;
import org.xml.sax.Attributes;
import org.xml.sax.SAXException;
import org.xml.sax.helpers.DefaultHandler;
 
import foundation.data.entity.Entity;
import foundation.io.mapping.FieldMappingRuntime;
import foundation.io.object.Check;
import foundation.io.object.Titles;
import foundation.monitor.Progressor;
import foundation.util.Util;
 
 
public class SheetHandler extends DefaultHandler {
 
    private int titleRowNo;
    private int dataRowNo;
    private DataBucket dataBucket;
    private CellRuntime cellRuntime;
    private Titles titles;
    private Entity entity;
    private int rowNo;
    private String lineErrorMsg;
    private boolean lineEmpty;
    private boolean vIsOpen;
 
    private String ioBatchId;
    private Progressor progressor;
    
 
    public SheetHandler(Progressor progressor, String ioBatchId, SharedStringsTable sharedStrings, StylesTable stylesTable, DataBucket dataBucket, int titleRowNo, int dataRowNo) {
        this.dataBucket = dataBucket;
        this.titleRowNo = titleRowNo;
        this.dataRowNo = dataRowNo;
        
        this.ioBatchId = ioBatchId;
        this.progressor = progressor;
        
        cellRuntime = new CellRuntime(sharedStrings, stylesTable);
        titles = dataBucket.getTitles();
        rowNo = 0;
    }
    
    private void outputOneRow() throws Exception {
        if (titleRowNo == rowNo) {
            boolean matchSuccess = checkMapping();
            if (!matchSuccess) {
                throw new Exception("导入列名匹配失败");
            }
        }
        else if (lineEmpty) {
            lineErrorMsg = "line empty;";
        }
        
        if (!Util.isEmpty(lineErrorMsg)) {
            entity.set("is_error", true);
            entity.set("error_message", lineErrorMsg);
            progressor.appendLine("line " + rowNo + ":" + lineErrorMsg);
        }
    }
    
    private void onComplete() {
        
    }
    
    private boolean checkMapping() {
        return dataBucket.checkTitleMapping();
    }
 
    @Override
    public void startElement(String uri, String localName, String name, Attributes attributes) throws SAXException {
        vIsOpen = false;
        
        if ("c".equals(name)) {
            cellRuntime.readAttributes(attributes);
            cellRuntime.clearRawData();
        }
        else if ("is".equals(name) || "t".equals(name) || "inlineStr".equals(name) || "v".equals(name)) {
            vIsOpen = true;
        }
        else if ("row".equals(name)) {
            rowNo = parseRowno(attributes);
            
            if (rowNo >= dataRowNo) {
                entity = dataBucket.addOneLine(progressor, rowNo);
            }
        }            
    }
 
    @Override
    public void endElement(String uri, String localName, String name) throws SAXException {
        try {
            if ("v".equals(name) || "t".equals(name) ) {
                if (cellRuntime.isIgnore()) {
                    return;
                }
                
                if (titleRowNo == rowNo) {
                    titles.add(cellRuntime.parseToString());
                }
                else if (entity != null) {
                    int columnIndex = cellRuntime.getColumnIndex();
                    FieldMappingRuntime mappingRuntime = dataBucket.getRuntimeMapping(columnIndex);
                    int entityIndex = mappingRuntime.getFromIndex();
                    String fromName = mappingRuntime.getFromName();
                    Check check = dataBucket.getCheckRule(columnIndex);
                    
                    String value = cellRuntime.parseToString();
                    String errorMsg = "";
                    
                    //1.2 check;
                    if (check.dataType()) {
                        errorMsg = errorMsg + entity.valueCheck(entityIndex, value);
                    }
                    
                    if (check.empty()) {
                        if (Util.isEmpty(value)) {
                            errorMsg = errorMsg + "不能为空;";
                        }
                    }
                    
                    if (!Util.isEmpty(errorMsg)){
                        lineErrorMsg = lineErrorMsg + "[" + fromName + "]" + errorMsg;
                    }
                    
                    entity.set(entityIndex, value);
                    lineEmpty = false;
                }
            }
            else if ("c".equals(name) ) {
                if (titleRowNo != rowNo && entity != null && Util.isEmpty(cellRuntime.getRawDataString())) {
                    int columnIndex = cellRuntime.getColumnIndex();
                    FieldMappingRuntime mappingRuntime = dataBucket.getRuntimeMapping(columnIndex);
                    int entityIndex = mappingRuntime.getFromIndex();
                    String fromName = mappingRuntime.getFromName();
                    Check check = dataBucket.getCheckRule(columnIndex);
                    
                    String value = cellRuntime.parseToString();
                    //1. 若单元格匹配头,  更新
                    String errorMsg = "";
                    
                    //1.2 check;
                    if (check.empty()) {
                        if (Util.isEmpty(value)) {
                            errorMsg = errorMsg + "不能为空;";
                        }
                    }
                    
                    if (!Util.isEmpty(errorMsg)){
                        lineErrorMsg = lineErrorMsg + "[" + fromName + "]" + errorMsg;
                    }
                }
            }
            else if ("row".equals(name)) {
                outputOneRow();
            }
            else if ("worksheet".equals(name)) {
                onComplete();
            }            
        }
        catch (Exception e) {
            e.printStackTrace();
            throw new SAXException(e);
        }        
    }
    
    @Override
    public void characters(char[] ch, int start, int length) throws SAXException {
        if (vIsOpen) {
            cellRuntime.appendValue(ch, start, length);
        }
    }
 
    private int parseRowno(Attributes attributes) {
        rowNo = Integer.parseInt(attributes.getValue(0));
        return rowNo;
    }
 
}