GoodsDataFileParser.java 3.38 KB
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();
	}

}