BaseDataCacheNew.java
14.5 KB
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
package com.neotel.webbox.capacitynew.data;
import com.alibaba.excel.EasyExcel;
import com.alibaba.excel.ExcelReader;
import com.alibaba.excel.context.AnalysisContext;
import com.alibaba.excel.event.AnalysisEventListener;
import com.alibaba.excel.metadata.Cell;
import com.alibaba.excel.metadata.data.CellData;
import com.alibaba.excel.read.metadata.ReadSheet;
import com.neotel.webbox.capacitynew.bean.BoxData;
import com.neotel.webbox.capacitynew.bean.BoxSpecialColumnContent;
import com.neotel.webbox.capacitynew.bean.BoxTitle;
import com.neotel.webbox.capacitynew.bean.ReelData;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Component;
import org.springframework.util.ResourceUtils;
import javax.annotation.PostConstruct;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.InputStream;
import java.net.URL;
import java.util.*;
@Slf4j
@Component
public class BaseDataCacheNew {
/**
* 料仓基础数据
*/
private static Map<String, BoxData> boxDataMap = new HashMap<>();
/**
* 料仓数据第一级标题
*/
private static List<BoxTitle> boxFirstTitleList = new ArrayList<>();
/**
* 料仓数据第二级标题
*/
private static List<BoxTitle> boxSecondTitleList = new ArrayList<>();
/**
* 料仓数据特殊列标题
*/
private static Map<String, List<BoxSpecialColumnContent>> boxSpecialTitleMap = new HashMap<>();
/**
* 料仓标题循环计数
*/
private static int boxTitleCount = 0;
/**
* 料盘基本数据
*/
private static Map<String, ReelData> reelDataMap = new HashMap<>();
/**
* 料盘表头信息
*/
private static Map<Integer, String> reelTitleMap = new HashMap<>();
@PostConstruct
public void readExcel() {
try {
InputStream fileStream = null;
File file = new File("data_new.xlsx");
if (file.exists()) {
log.info("加载包外数据文件:" + file.getAbsolutePath());
fileStream = new FileInputStream(file);
} else {
URL baseDir = ResourceUtils.getURL("classpath:data_new.xlsx");
fileStream = baseDir.openStream();
log.info("加载默认数据文件:" + baseDir.getPath());
}
try (ExcelReader excelReader = EasyExcel.read(fileStream).build()) {
//开始读取料仓基础数据
ReadSheet readSheet1 = EasyExcel.readSheet(0).head(BoxData.class).headRowNumber(3).registerReadListener(new AnalysisEventListener<BoxData>() {
@Override
public void invoke(BoxData data, AnalysisContext analysisContext) {
//如果特殊列不为空,则进行操作
if (boxSpecialTitleMap != null && !boxSpecialTitleMap.isEmpty()) {
//获取到excel当前行的值
Map<Integer, Cell> cellMap = analysisContext.readRowHolder().getCellMap();
//开始循环遍历值三级标题
for (Map.Entry<String, List<BoxSpecialColumnContent>> entry : boxSpecialTitleMap.entrySet()) {
String specialColumnName = entry.getKey(); //特殊列标题名称
List<BoxSpecialColumnContent> boxSpecialColumnContentList = entry.getValue(); //特殊列下的子标题与坐标
if (boxSpecialColumnContentList != null && boxSpecialColumnContentList.size() > 0) {
List<BoxSpecialColumnContent> boxSpecialColumnContents = new ArrayList<>();
Map<String, List<BoxSpecialColumnContent>> boxSpecialColumnMap = new HashMap<>();
for (BoxSpecialColumnContent boxSpecialColumnContent : boxSpecialColumnContentList) {
Integer index = boxSpecialColumnContent.getIndex();
//根据第三级子标题中的下标,去找内容的值
CellData cellData = (CellData) cellMap.get(index);
if (cellData.getNumberValue() != null || cellData.getStringValue() != null) {
BoxSpecialColumnContent boxSpecialColumnContent1 = new BoxSpecialColumnContent();
boxSpecialColumnContent1.setTitle(boxSpecialColumnContent.getTitle());
boxSpecialColumnContent1.setIndex(index);
boxSpecialColumnContent1.setValue(cellData.getNumberValue() == null ? cellData.getStringValue() : cellData.getNumberValue() + "");
boxSpecialColumnContents.add(boxSpecialColumnContent1);
}
}
if (boxSpecialColumnContents != null && boxSpecialColumnContents.size() > 0) {
boxSpecialColumnMap.put(specialColumnName, boxSpecialColumnContents);
data.setBoxSpecialColumnMap(boxSpecialColumnMap);
}
}
}
}
if (!data.isValid()) {
log.error("料仓表第{}行数据{}异常,忽略", analysisContext.getCurrentRowNum(), data.getBoxName());
} else {
log.info("加载到料仓{}数据", data.getBoxName());
boxDataMap.put(data.getBoxName(), data);
}
}
@Override
public void doAfterAllAnalysed(AnalysisContext analysisContext) {
log.info("料仓数据加载完成,共加载" + boxDataMap.size() + "条有效数据");
}
@Override
public void invokeHeadMap(Map<Integer, String> headMap, AnalysisContext context) {
BoxTitle boxTitle = new BoxTitle();
List<Integer> mergeColumnIndexList = new ArrayList<>();
// boxTitleCount = 0,1,2 分别代表 一,二,三级标题
if (boxTitleCount == 0) {
for (Map.Entry<Integer, String> entry : headMap.entrySet()) {
Integer titleIndex = entry.getKey();
String titleName = entry.getValue();
if (titleName != null) {
if (boxTitle.getTitleName() != null) {
boxTitle.setMergeColumnIndexList(mergeColumnIndexList);
boxFirstTitleList.add(boxTitle);
//如果保存完之后,合并列数和标题内容给置空,给后续标题赋值
boxTitle = new BoxTitle();
mergeColumnIndexList = new ArrayList<>();
}
boxTitle.setTitleName(titleName);
}
mergeColumnIndexList.add(titleIndex);
}
//补一刀,防止后续出现标题一直为空,保存不到第一级标题中
if (boxTitle.getTitleName() != null) {
boxTitle.setMergeColumnIndexList(mergeColumnIndexList);
boxFirstTitleList.add(boxTitle);
}
} else if (boxTitleCount == 1) {
if (boxFirstTitleList != null && boxFirstTitleList.size() > 0) {
int colNumCount = 0;
for (BoxTitle boxFirstTitle : boxFirstTitleList) {
for (Map.Entry<Integer, String> entry : headMap.entrySet()) {
Integer titleIndex = entry.getKey();
String titleName = entry.getValue();
if (boxFirstTitle.getMergeColumnIndexList().contains(titleIndex)) {
if (titleName != null) {
colNumCount++;
if (boxTitle.getTitleName() != null) {
boxTitle.setMergeColumnIndexList(mergeColumnIndexList);
boxSecondTitleList.add(boxTitle);
boxTitle = new BoxTitle();
mergeColumnIndexList = new ArrayList<>();
}
boxTitle.setTitleName(titleName);
}
//判断是否从一开始就为空
if (colNumCount > 0) {
mergeColumnIndexList.add(titleIndex);
}
}
}
//和第一层标题一样,进行补一刀
if (boxTitle.getTitleName() != null) {
boxTitle.setMergeColumnIndexList(mergeColumnIndexList);
boxSecondTitleList.add(boxTitle);
}
}
}
} else if (boxTitleCount == 2) {
if (boxSecondTitleList != null && boxSecondTitleList.size() > 0) {
for (BoxTitle boxSecondTitle : boxSecondTitleList) {
List<BoxSpecialColumnContent> specialColumnList = new ArrayList<>();
for (Map.Entry<Integer, String> entry : headMap.entrySet()) {
Integer titleIndex = entry.getKey();
String titleName = entry.getValue();
if (boxSecondTitle.getMergeColumnIndexList().contains(titleIndex) && titleName != null) {
BoxSpecialColumnContent specialColumnContent = new BoxSpecialColumnContent();
specialColumnContent.setTitle(titleName);
specialColumnContent.setIndex(titleIndex);
specialColumnList.add(specialColumnContent);
}
}
boxSpecialTitleMap.put(boxSecondTitle.getTitleName(), specialColumnList);
}
}
}
boxTitleCount++;
}
}).build();
ReadSheet readSheet2 = EasyExcel.readSheet(1).head(ReelData.class).headRowNumber(1).registerReadListener(new AnalysisEventListener<ReelData>() {
@Override
public void invoke(ReelData reelData, AnalysisContext analysisContext) {
Map<String, Integer> boxSpecialColumn = new HashMap<>();
Map<Integer, Cell> cellMap = analysisContext.readRowHolder().getCellMap();
if (reelTitleMap != null && !reelTitleMap.isEmpty()) {
for (Map.Entry<Integer, String> reelDateHead : reelTitleMap.entrySet()) {
Integer reelTitleIndex = reelDateHead.getKey(); //坐标
String reelTitle = reelDateHead.getValue(); //表头
CellData cellData = (CellData) cellMap.get(reelTitleIndex);
if (cellData != null && cellData.getNumberValue() != null) {
boxSpecialColumn.put(reelTitle, cellData.getNumberValue().intValue());
}
}
}
if (boxSpecialColumn != null && !boxSpecialColumn.isEmpty()) {
reelData.setBoxSpecialColumn(boxSpecialColumn);
}
if (reelData.isValid()) {
log.info("加载到盘{}数据", reelData.getReelSizeStr());
reelDataMap.put(reelData.getReelSizeStr(), reelData);
} else {
log.error("料仓表第{}行数据{}异常,忽略", analysisContext.getCurrentRowNum(), reelData.getReelSizeStr());
}
}
@Override
public void doAfterAllAnalysed(AnalysisContext analysisContext) {
log.info("料盘数据加载完成,共加载" + reelDataMap.size() + "条有效数据");
}
@Override
public void invokeHeadMap(Map<Integer, String> headMap, AnalysisContext context) {
reelTitleMap.putAll(headMap);
}
}).build();
// 这里注意 一定要把sheet1 sheet2 一起传进去,不然有个问题就是03版的excel 会读取多次,浪费性能
excelReader.read(readSheet1, readSheet2);
}
} catch (FileNotFoundException e) {
log.error("未找到数据文件", e);
} catch (Exception ex) {
log.error("读取数据异常", ex);
}
}
public static Collection<BoxData> getBoxDataList() {
return boxDataMap.values();
}
public static BoxData getBoxData(String boxName) {
return boxDataMap.get(boxName);
}
public static Collection<ReelData> getReelDataList() {
return reelDataMap.values();
}
public static ReelData getReelData(String boxName) {
return reelDataMap.get(boxName);
}
}