GoodsDataFileParser.java
3.38 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
package com.taover.bazhuayun.analysis.script.goodscollect;
import java.io.File;
import java.util.ArrayList;
import java.util.Collections;
import java.util.HashSet;
import java.util.Iterator;
import java.util.List;
import java.util.Set;
import com.taover.bazhuayun.analysis.util.ExcelUtil;
import com.taover.util.UtilString;
public class GoodsDataFileParser {
private String fileNameIgnorePreffix;
private StringBuffer logStrBuffer = new StringBuffer();
private Set<GoodsInfoRow> rowSet = new HashSet<GoodsInfoRow>();
private String[] goodsNamePatternArr;
private String[] goodsSkuNameParrternArr;
private GroupInfoExcelData groupInfoData;
public GoodsDataFileParser(String[] goodsNamePatternArr, String[] goodsSkuNameParrternArr, GroupInfoExcelData groupInfoData) {
this.goodsNamePatternArr = goodsNamePatternArr;
this.goodsSkuNameParrternArr = goodsSkuNameParrternArr;
this.groupInfoData = groupInfoData;
}
public void setFileNameIgnorePreffix(String preffix) {
this.fileNameIgnorePreffix = preffix;
}
public void parse(GroupInfoExcelData groupInfoData, File[] listFiles) throws Exception {
for(int i=0; i<listFiles.length; ++i) {
// if(!listFiles[i].getName().contains("8.24海囤新系统报单(730份)8.23 一般贸易货!法国本土版!兰蔻大粉水400ml(1)")) {
// continue;
// }
try {
this.logStrBuffer.append("=================文件名称["+listFiles[i].getName()+"]==================\n");
this.parseFile(listFiles[i]);
}catch(Exception e) {
e.printStackTrace();
this.logStrBuffer.append("ERROR:处理报错"+e.getMessage()+"\n");
}
}
}
private void parseFile(File item) throws Exception {
//获取发件组/发件人
String fileName = item.getName().replaceFirst(this.fileNameIgnorePreffix, "");
GroupInfoExcelDataRow groupInfo = this.groupInfoData.findByFileName(fileName);
//读取Excel文件
List<List<Object>> data = ExcelUtil.readExcelSheet(item, false);
if(data.isEmpty()) {
return;
}
//计算起始行及表头索引
int headerRowIndex = 0;
GoodsInfoExcelDataHeader header = null;
while(headerRowIndex<data.size()) {
List<Object> tempHeaderData = data.get(headerRowIndex);
if(this.notBlankDataCount(tempHeaderData) < 3) {
++headerRowIndex;
continue;
}
header = new GoodsInfoExcelDataHeader(this.goodsNamePatternArr, this.goodsSkuNameParrternArr, tempHeaderData);
if(!header.available()) {
this.logStrBuffer.append("ERROR:没找到商品列或规格列\n");
return;
}
break;
}
//写入结果集
for(int j=headerRowIndex+1; j<data.size(); ++j) {
this.rowSet.add(new GoodsInfoRow(groupInfo, header, data.get(j), fileName));
}
}
private int notBlankDataCount(List<Object> tempHeaderData) {
if(tempHeaderData == null || tempHeaderData.isEmpty()) {
return 0;
}
int count = 0;
for(int i=0; i<tempHeaderData.size(); ++i) {
Object item = tempHeaderData.get(i);
if(item == null) {
continue;
}
String itemStr = UtilString.trimByRegexS(item.toString());
if(!"".equals(itemStr)) {
++count;
}
}
return count;
}
public List<GoodsInfoRow> getResult() {
List<GoodsInfoRow> data = new ArrayList<GoodsInfoRow>();
Iterator<GoodsInfoRow> rowIter = this.rowSet.iterator();
while(rowIter.hasNext()) {
data.add(rowIter.next());
}
Collections.sort(data);
return data;
}
public String getLogString() {
return logStrBuffer.toString();
}
}