package six.com.crawler.work.plugs;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.apache.commons.lang3.StringUtils;
import org.jsoup.nodes.Element;
import org.jsoup.select.Elements;

import six.com.crawler.entity.Page;
import six.com.crawler.entity.ResultContext;
import six.com.crawler.utils.ArrayListUtils;
import six.com.crawler.work.AbstractCrawlWorker;
import six.com.crawler.work.extract.Extracter;
import six.com.crawler.work.space.RedisWorkSpace;

/**
 * 抓取宁波住宅与房地产网(楼层单元状态的信息)
 * @author 38342
 * @version v1.0
 * date:20170301
 */
public class NbCnnbfdcRoomStateInfoWorker extends AbstractCrawlWorker{
	
	RedisWorkSpace<Page> roomInfoQueue;
	String iframeCss = "td>iframe[id=mapbarframe]";
	
	private Map<String,String> roomStates = new HashMap<String,String>();

	@Override
	protected void insideInit() {
		roomInfoQueue = new RedisWorkSpace<Page>(getManager().getRedisManager(), "nb_cnnbfdc_room_info",Page.class);
	}

	@Override
	protected void beforeDown(Page doingPage) {
	}

	@Override
	protected void beforeExtract(Page doingPage) {
		Element iframe = doingPage.getDoc().select(iframeCss).first();
		if (null == iframe) {
			throw new RuntimeException("don't find iframe:" + iframeCss);
		}
		String src = iframe.attr("src");
		Page iframePage = new Page(doingPage.getSiteCode(), 1, src, src);
		iframePage.setReferer(doingPage.getFinalUrl());
		getDowner().down(iframePage);
		doingPage.setPageSrc(iframePage.getPageSrc());
		//获取所有状态信息
		if(roomStates.isEmpty()){
			String allCssQuery = "table[width='600']>tbody>tr>td>font";
			Elements elements2 = doingPage.getDoc().select(allCssQuery);
			if(null==elements2){
				throw new RuntimeException("don't find state node:" + allCssQuery);
			}
			for(Element ets : elements2){
				String key = ets.attr("color");
				String value = ets.ownText();
				value=StringUtils.remove(value,":");
				value=StringUtils.remove(value," ");
				roomStates.put(key, value);
			}
		}
		String unitId = doingPage.getMeta("unitId").get(0);
		List<String> unitIds = new ArrayList<String>();
		List<String> roomNos = new ArrayList<String>();
		String styleCssQuery = "table[id]>tbody>tr>td>table";
		Elements styleElements = doingPage.getDoc().select(styleCssQuery);
		List<String> roomStateList = new ArrayList<String>();
		List<String> roomIds=new ArrayList<String>();
		List<String> contractNos=new ArrayList<String>();
		
		for (Element et : styleElements) {
			String bgColor = "";
			String style = et.attr("style");
			if(style!=null){
				String[] ss = style.split(";");
				for (String string : ss) {
					if(string.contains("background-color")){
						bgColor = string.split(":")[1];
						break;
					}
				}
			}
			String roomState = roomStates.get(bgColor);
			roomState = roomState.replace(": ", "");
			Elements elements = et.select("tbody>tr:eq(1)>td>a");
			if(elements==null || elements.size()==0){
				//此时匹配的是未备案的房间号
				elements = et.select("tbody>tr:eq(1)>td");
				roomIds.add("");
				String contractNo=et.attr("title");
				if(contractNo!=null && contractNo.contains("合同编号")){
					contractNo=contractNo.split("：")[1];
				}else{
					contractNo="";
				}
				contractNos.add(contractNo);
			}else{
				roomIds.add(StringUtils.substringBetween(elements.first().attr("onclick"), "javascript:window.open(\"openRoomData.aspx?roomId=", "\",\""));
				contractNos.add("");
			}
			String s = elements.first().ownText();
			roomNos.add(s);
			roomStateList.add(roomState);
			unitIds.add(unitId);
		}
		
		doingPage.getMetaMap().put("roomState", roomStateList);
		doingPage.getMetaMap().put("unitId", unitIds);
		doingPage.getMetaMap().put("roomNo", roomNos);
		doingPage.getMetaMap().put("roomId", roomIds);
		doingPage.getMetaMap().put("contractNo", contractNos);
	}

	@Override
	protected void afterExtract(Page doingPage, ResultContext resultContext) {
	}

	@Override
	protected void onComplete(Page doingPage, ResultContext resultContext) {
		String cssQuery = "table[id]";
		Elements roomStateElements = doingPage.getDoc().select(cssQuery);
		List<String> roomNos = resultContext.getExtractResult("roomNo");
		if(roomNos!=null){
			for(int i=0;i<roomNos.size();i++){
				String id = roomStateElements.get(i).attr("id");
				id = id.replace("room", "");
				String pageUrl = "http://newhouse.cnnbfdc.com/openRoomData.aspx?roomId=" + id;
				Page roomPage = new Page(doingPage.getSiteCode(), 1, pageUrl, pageUrl);
				roomPage.getMetaMap().put("projectId", doingPage.getMeta("projectId"));
				roomPage.getMetaMap().put("projectName", doingPage.getMeta("projectName"));
				roomPage.getMetaMap().put("unitName", doingPage.getMeta("unitName"));
				roomPage.getMetaMap().put("unitId", doingPage.getMeta("unitId"));
				roomPage.getMetaMap().put("roomId", ArrayListUtils.asList(id));
				roomPage.setReferer(doingPage.getFinalUrl());
				if(!roomInfoQueue.isDone(roomPage.getPageKey())){
					roomInfoQueue.push(roomPage);
				}
			}
		}
	}

	@Override
	public boolean insideOnError(Exception t, Page doingPage) {
		return false;
	}
}

@@ -2434,16 +2434,190 @@
 %3E();%0A%09%09%0A
+%09%09Elements rooms=doingPage.getDoc().select(%22table%5Bid%5D%22);%0A%09%09for (Element room:rooms) %7B%0A%09%09%09String roomId=room.attr(%22id%22).replaceAll(%22room%22, %22%22);%0A%09%09%09roomIds.add(roomId);%0A%09%09%7D%0A%09%09%0A
 %09%09for (E
@@ -3171,29 +3171,8 @@
 %22);%0A
-%09%09%09%09roomIds.add(%22%22);%0A
 %09%09%09%09
@@ -3390,152 +3390,8 @@
 se%7B%0A
-%09%09%09%09roomIds.add(StringUtils.substringBetween(elements.first().attr(%22onclick%22), %22javascript:window.open(%5C%22openRoomData.aspx?roomId=%22, %22%5C%22,%5C%22%22));%0A
 %09%09%09%09

修改roomId的获取。