package main;
import java.io.IOException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Map.Entry;
import org.apache.commons.lang.StringUtils;
import org.apache.log4j.Logger;
import org.dom4j.DocumentException;
import com.google.common.collect.Lists;
import common.Utils;
import cputils.DataItem;
import cputils.XmlUtils;
public class StatUtil {
private static final Logger logger = Logger.getLogger(StatUtil.class);
public static void main(String[] args) throws IOException, DocumentException {
// String s = "<strong class='c_1e50a2'>16</strong>";
//System.out.println(s.replaceAll("<strong class='c_1e50a2'>", "").replaceAll("</strong>", ""));
//statRes("2014038");
// logger.debug(System.currentTimeMillis());
// logger.info(System.currentTimeMillis());
// logger.error(System.currentTimeMillis());
/*List<String> indexList = Lists.newArrayList();
for (int i = 2014001; i <=2014049; i++) {
String indexStr = statRes(i+"","tb");
if (StringUtils.isNotBlank(indexStr)) {
indexList.add(indexStr);
}
//System.out.println(indexStr);
}
Map<String, Map<Integer, Integer>> res = Utils.makeMap(indexList, "2014049");
System.out.println(XmlUtils.makeSortedMap(res, true));*/
List<String> indexList1 = Lists.newArrayList();
for (int i = 2014001; i <=2014090; i++) {
String indexStr = statRes(i+"","wy");
if (StringUtils.isNotBlank(indexStr)) {
indexList1.add(indexStr);
}
System.out.println(i+":"+indexStr);
}
Map<String, Map<Integer, Integer>> res = Utils.makeMap(indexList1, "2014049");
System.out.println(XmlUtils.makeSortedMap(res, true));
}
public static String statRes(String issueNo,String from) throws IOException, DocumentException{
Map<String, Map<Integer, Integer>> res = stat(issueNo, 1, from);
// System.out.println(issueNo+">>"+res);
String indexStr = "";
if (res!=null) {
Map<Integer, Integer> bMap = res.get("b");
// System.out.println(issueNo+">>"+bMap.keySet());
int b = 1;
StatItem item = new StatItem();
for (Entry<Integer, Integer> e : bMap.entrySet()) {
item.addItem(b++, e.getKey(), e.getValue(), 1);
//System.out.print(e.getKey()+" ");
}
Map<Integer, Integer> aMap = res.get("a");
//System.out.println(issueNo+">>"+aMap.keySet());
b = 1;
for (Entry<Integer, Integer> e : aMap.entrySet()) {
item.addItem(b++, e.getKey(), e.getValue(), 2);
//System.out.print(e.getKey()+" ");
}
//System.out.println(bMap);
//System.out.println(aMap);
XmlUtils utils = new XmlUtils();
DataItem dataitem = utils.getDataItem(issueNo);
if(dataitem == null){
logger.error(issueNo+":kj is null...");
return null;
}
String[] allArr = dataitem.getOpencode().split(" ");
// System.out.println("KJ:"+dataitem.getOpencode());
List<Integer> bList = new ArrayList<Integer>(),aList = new ArrayList<Integer>();
int maxIndex = issueNo.length() == 5?4:5;
for (int i = 0; i < allArr.length; i++) {
if (i<=maxIndex) {
bList.add(Integer.parseInt(allArr[i]));
}else {
aList.add(Integer.parseInt(allArr[i]));
}
}
//System.out.println(dataitem.getOpencode());
//System.out.println(bList+" "+aList);
// System.out.print(issueNo+": ");
int aSize = 0;
for(StatItem item2:item.list){
//System.out.println(item2);
String outStr = (item2.index+" ").length()==3?(" "+item2.index):(" "+item2.index);
//outStr+="("+item2.number+","+item2.count+")";
if (bList.indexOf(item2.number)>-1&&item2.position==1
||aList.indexOf(item2.number)>-1&&item2.position==2) {
// System.out.print(outStr);
if(item2.position==1){
indexStr += item2.index+" ";
}else{
if (aSize == 0) {
indexStr += ":"+item2.index;
}else {
indexStr += " "+item2.index;
}
++aSize;
}
}
}
// System.out.println();
}
return indexStr;
}
private static Map<String, Map<Integer, Integer>> stat(String issueNo,int is_not_full,String from){
Map<String, Map<Integer, Integer>> res = new HashMap<String, Map<Integer,Integer>>();
String fileName = from+"/"+from + "_" + issueNo;
List<String> list = new ArrayList<String>();
try {
switch (is_not_full) {
case 1:
fileName = fileName+"_full";
list = XmlUtils.getHMData(fileName);
break;
case 2:
fileName = fileName+"_notfull";
list = XmlUtils.getHMData(fileName);
break;
case 100:
list = XmlUtils.getHMData(fileName+"_notfull");
list.addAll(XmlUtils.getHMData(fileName+"_full"));
fileName = fileName+"_all";
break;
default:
break;
}
} catch (IOException e) {
logger.error(fileName+":"+e.getMessage());
return null;
}
if (list.isEmpty()) {
logger.info(fileName+" 内容为空。");
return null;
}
res = Utils.makeMap(list, issueNo);
//Utils.print(fileName+" line:"+list.size());
res = XmlUtils.makeSortedMap(res, true);
return res;
}
}
class StatItem{
@Override
public String toString() {
return "index:"+index+" number:"+number+" count:"+count+" position:"+position;
}
public Integer index;
public Integer number;
public Integer count;
public Integer position;
public List<StatItem> list = new ArrayList<StatItem>();
public StatItem(){}
public StatItem(Integer index,Integer number,Integer count,Integer position){
this.index = index;
this.number = number;
this.count = count;
this.position = position;
}
public StatItem getIndex(Integer number,Integer position){
for(StatItem item1:list){
if (item1.number == number && item1.position == position) {
return item1;
}
}
return null;
}
public void addItem(Integer index,Integer number,Integer count,Integer position){
list.add(new StatItem( index, number, count, position));
}
}