GoodsDataFileParser.java 2.36 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;

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) {
			try {
				this.parseFile(listFiles[i]);
			}catch(Exception e) {
				e.printStackTrace();
			}
		}
	}
	
	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;
		}
		
		//写入set
		GoodsInfoExcelDataHeader header = new GoodsInfoExcelDataHeader(this.goodsNamePatternArr, this.goodsSkuNameParrternArr, data.get(0));
		if(!header.available()) {
			this.logStrBuffer.append("=================文件名称["+item.getName()+"]==================\n");
			this.logStrBuffer.append("没找到商品列或规格列\n");
		}
		for(int j=1; j<data.size(); ++j) {
			this.rowSet.add(new GoodsInfoRow(groupInfo, header, data.get(j)));
		}
	}

	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();
	}

}