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
package foundation.io.file.pull;
 
import java.util.ArrayList;
import java.util.List;
 
import foundation.data.entity.Entity;
import foundation.data.entity.EntitySet;
import foundation.data.meta.field.FieldsRuntime;
import foundation.data.object.DataObject;
import foundation.io.mapping.FieldMapping;
import foundation.io.mapping.FieldMappingRuntime;
import foundation.io.mapping.IOMappings;
import foundation.io.mapping.MappingsRuntime;
import foundation.io.object.Check;
import foundation.io.object.Titles;
import foundation.monitor.Progressor;
 
public class DataBucket {
 
    private int batchCount;
    private Titles titles;
    private List<EntitySet> dataList;
    private int dataCount;
    private int pos;
    private DataObject dataObject;
    private EntitySet currentSet;
    private List<Check> checkList;
    private IOMappings fieldMappings;
    private MappingsRuntime mappingSetRuntime;
    
    public DataBucket(int batchCount) {
        this.batchCount = batchCount;
        
        pos = -1;
    }
    
    public Titles getTitles() {
        return titles;
    }
 
    public void addMapping(String title, int columnIndex) throws Exception {
        FieldMapping fieldMapping = fieldMappings.getItemByToField(title);
        
        if (fieldMapping == null) {
            throw new Exception("列 【" + title + "】匹配失败");
        }
        FieldMappingRuntime mappingRuntime = mappingSetRuntime.getRuntimeMappingByFrom(fieldMapping.getFromName());
        
        int fromIndex = 0; FieldsRuntime fieldMetas = null;
        
        fieldMetas = dataObject.getTableFieldMetas();
 
        fromIndex = fieldMetas.getIndex(fieldMapping.getFromName());
        if (mappingRuntime == null) {
            mappingRuntime = new FieldMappingRuntime(fieldMapping, fromIndex, fieldMapping.getIndexNo());
        }
        
        mappingSetRuntime.addItem(mappingRuntime);
        addCheck(fieldMapping.getCheckRule());
    }
 
    public long getDataSize() {
        return dataCount;
    }
 
    public EntitySet getNext() {
        pos++;
        
        if (pos >= dataList.size()) {
            return null;
        }
        
        return dataList.get(pos);
    }
 
    public Entity addOneLine(Progressor progressor, int rowNo) {
        if (currentSet == null || currentSet.isFull()) {
            try {
                if (currentSet != null) {
                    saveToBD(progressor);
                }
                
                currentSet = dataObject.createTableEntitySet(batchCount);
            }
            catch (Exception e) {
                e.printStackTrace();
            }
        }
 
        dataCount = rowNo;
        return currentSet.append();
    }
 
    public void saveToBD(Progressor progressor) throws Exception {
        if (currentSet == null) {
            return ;
        }
        
        progressor.appendLine("正在插入" +  pos + "-" + dataCount +  "条数据到临时明细表");
        int cnt = dataObject.batchInsertEntitySet(currentSet);
        
        pos = pos + cnt;
        clearData();
    }
 
    private void clearData() {
        currentSet.clear();
    }
 
    public void setDataObject(DataObject dataObject) {
        this.dataObject = dataObject;
    }
 
    public Check getCheckRule(int columnIndex) {
        return checkList.get(columnIndex);
    }
 
    public void addCheck(Check oneCheck) {
        if (checkList == null) {
            checkList = new ArrayList<Check>();
        }
        
        checkList.add(oneCheck);
    }
    
    public boolean checkTitleMapping() {
        for (FieldMapping fieldMapping : fieldMappings.getList()) {
            String toName = fieldMapping.getToName();
            toName = toName.toLowerCase();
            if (mappingSetRuntime.getRuntimeMappingByTo(toName) == null) {
                return false;
            }
        }
        return true;
    }
 
    public FieldMappingRuntime getRuntimeMapping(int columnIndex) {
        return mappingSetRuntime.getRuntimeMapping(columnIndex);
    }
 
}