import java.io.BufferedReader;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStreamReader;
import java.lang.reflect.InvocationTargetException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
import org.apache.commons.beanutils.BeanUtils;
import com.alibaba.common.lang.StringUtil;
import com.alibaba.common.logging.Logger;
import com.alibaba.common.logging.LoggerFactory;
import com.taobao.pamirs.channel.result.CtResult;
import com.taobao.pamirs.commodity.domain.AuctionDO;
import com.taobao.pamirs.point.dataobject.AuctionObj;
public class CSVAnalysis {
private InputStreamReader fr = null;
private BufferedReader br = null;
/**
* 字段转化成对象
*/
private static Map<Integer,String> fieldToObject;
private static final String SPECIAL_CHAR_A = "[^/",//n ]";
private static final String SPECIAL_CHAR_B = "[^/",//n]";
String strRegExp = "/"(("+ SPECIAL_CHAR_A + "*[,//n ])*("+ SPECIAL_CHAR_A + "*/"{2})*)*"+ SPECIAL_CHAR_A + "*/"[ ]*,[ ]*"
+"|"+ SPECIAL_CHAR_B + "*[ ]*,[ ]*"
+ "|/"(("+ SPECIAL_CHAR_A + "*[,//n ])*("+ SPECIAL_CHAR_A + "*/"{2})*)*"+ SPECIAL_CHAR_A + "*/"[ ]*"
+ "|"+ SPECIAL_CHAR_B + "*[ ]*";
private static final Logger log = LoggerFactory.getLogger(CSVAnalysis.class);
static
{
fieldToObject = new HashMap<Integer, String>();
fieldToObject.put(1, "auctionId");
fieldToObject.put(2, "optTag");
fieldToObject.put(3, "expTag");
fieldToObject.put(4, "title");
fieldToObject.put(5, "dsc");
fieldToObject.put(6, "brand");
fieldToObject.put(7, "category");
fieldToObject.put(8, "price");
fieldToObject.put(9, "addProv");
fieldToObject.put(10, "addCity");
fieldToObject.put(11, "quality");
fieldToObject.put(12, "flag");
fieldToObject.put(13, "sales");
fieldToObject.put(14, "sellerrate");
fieldToObject.put(15, "selleruid");
fieldToObject.put(16, "ipv15");
fieldToObject.put(17, "auctionCode");
fieldToObject.put(18, "eventId");
fieldToObject.put(19, "eventTime");
fieldToObject.put(20, "sku_price");
fieldToObject.put(21, "skuSupplyPrice");
fieldToObject.put(22, "commissionRatio");
}
public CSVAnalysis(String f) throws IOException
{
fr = new InputStreamReader(new FileInputStream(f));
}
/**
* 解析csv文件 到一个list中
* 每个单元个为一个String类型记录,每一行为一个list。
* 再将所有的行放到一个总list中
* @return
* @throws IOException
*/
public Map<String,Object> readCSVFile() throws IOException {
Map<String,Object> map = new HashMap<String,Object>();
List<AuctionObj> objs = new ArrayList<AuctionObj>();
br = new BufferedReader(fr);
String rec = null;//一行
String strValue;//一个单元格
//List<List<String>> listFile = new ArrayList<List<String>>();
try {
int key = 0;
//读取一行
while ((rec = br.readLine()) != null) {
// Pattern pCells = Pattern.compile("(/"[^/"]*(/"{2})*[^/"]*/")*[^,]*,");
Pattern pCells = Pattern.compile(strRegExp);
Matcher mCells = pCells.matcher(rec);
List<String> cells = new ArrayList<String>();//每行记录一个list
//读取每个单元格
AuctionObj obj = new AuctionObj();
int i = 1;
while (mCells.find()) {
strValue = mCells.group();
strValue = strValue.replaceAll(
"(?sm)/"?([^/"]*(/"{2})*[^/"]*)/"?.*,", "$1");
strValue = strValue.replaceAll("(?sm)(/"(/"))", "$2");
String value = new String(strValue.getBytes("GBK"),"UTF-8");
cells.add(value);
String s = new String(strValue.getBytes("GBK"),"UTF-8");
System.out.println(s+"_ffffffffff");
if(key > 0){
String p = fieldToObject.get(i);
if(StringUtil.isNotBlank(p)) {
try {
BeanUtils.setProperty(obj, p, value);
} catch (IllegalAccessException e) {
log.error("p:"+p+" strValue:"+value, e);
} catch (InvocationTargetException e) {
log.error("p:"+p+" strValue:"+value, e);
}
}
i ++;
}
}
if(key > 0){
objs.add(obj);
}
key ++;
}
} catch (Exception e) {
e.printStackTrace();
} finally {
if (fr != null) {
fr.close();
}
if (br != null) {
br.close();
}
}
map.put("list", objs);
return map;
}
@SuppressWarnings("unchecked")
public CtResult<Map<String,Object>> checkTgs(Map<String, Object> map,List<AuctionDO> auctionDOs)
{
CtResult<Map<String,Object>> result = new CtResult<Map<String,Object>>();
List<AuctionObj> objs = (List<AuctionObj>)map.get("list");
if(null != objs && objs.size() > 0){
for (AuctionObj auctionObj : objs) {
System.out.println(auctionObj.getAuctionId());
System.out.println(auctionObj.getCommissionRatio());
}
}
return result;
}
public static void main(String[] args) throws Throwable {
CSVAnalysis parser = new CSVAnalysis("D://Downloads//fangjieCN.csv");
List<AuctionDO> auctionDOs = new ArrayList<AuctionDO>();
parser.checkTgs(parser.readCSVFile(),auctionDOs);
}