package six.com.crawler.work.plugs;

import java.util.List;

import org.apache.commons.lang3.StringUtils;

import six.com.crawler.entity.Page;
import six.com.crawler.entity.PageType;
import six.com.crawler.entity.ResultContext;
import six.com.crawler.http.HttpMethod;
import six.com.crawler.utils.ArrayListUtils;
import six.com.crawler.work.AbstractCrawlWorker;
import six.com.crawler.work.space.WorkSpace;

/**
 * 
 * @author weijiyong@tospur.com
 *
 */
public class DLDCGXProjectListWorker extends AbstractCrawlWorker {

	WorkSpace<Page> projectInfoQueue;

	String pageIndexTemplate = "<<pageIndex>>";

	int pageIndex = 1;

	int pageCount;

	String projectListUrlTemplate = "http://218.25.171.244/InfoLayOut_GX/Config/LoadProcToXML.aspx?pid=Arty_YSXX&csnum=2&cn1=pageindex&cv1="
			+ pageIndexTemplate + "&cn2=DefOrderfldName&cv2=xkzh";

	String refererUrl;

	private Page buildPage(int pageIndex, String refererUrl) {
		String url = StringUtils.replace(projectListUrlTemplate, pageIndexTemplate, String.valueOf(pageIndex));
		Page page = new Page(getSite().getCode(), 1, url, url);
		page.setReferer(refererUrl);
		page.setMethod(HttpMethod.POST);
		page.setType(PageType.XML.value());
		page.getParameters().put("pid", "Arty_YSXX");
		page.getParameters().put("cv1", pageIndex);
		page.getParameters().put("cv2", "xkzh");
		return page;
	}

	@Override
	protected void insideInit() {
		projectInfoQueue = getManager().getWorkSpaceManager().newWorkSpace("dldc_gx_project_info", Page.class);
		Page firstPage = buildPage(pageIndex, refererUrl);// 初始化第一页
		getWorkSpace().clearDoing();
		getWorkSpace().push(firstPage);
	}

	@Override
	protected void beforeDown(Page doingPage) {
		// TODO Auto-generated method stub

	}

	@Override
	protected void beforeExtract(Page doingPage) {
		if (pageCount == -1) {
			pageCount = Integer.parseInt(doingPage.getDoc().select("NewDataSet>Table1>PageCount").first().ownText());
		}
	}

	@Override
	protected void afterExtract(Page doingPage, ResultContext resultContext) {
		// TODO Auto-generated method stub

	}

	@Override
	protected void onComplete(Page doingPage, ResultContext resultContext) {
		String BASE_URL = "http://218.25.171.244/InfoLayOut_GX/Config/LoadProcToXML.aspx?pid=Arty_YSXX&csnum=1&cn1=ysxkid&cv1=";
		List<String> projectIds = resultContext.getExtractResult("projectId");
		if (null != projectIds) {
			for (String projectId : projectIds) {
				String URL = BASE_URL + projectId;
				Page projectInfo = new Page(getSite().getCode(), 1, URL, URL);
				projectInfo.setReferer(doingPage.getFinalUrl());
				projectInfo.setMethod(HttpMethod.GET);
				projectInfo.setType(PageType.XML.value());

				projectInfo.getMetaMap().put("projectId", ArrayListUtils.asList(projectId));
				projectInfoQueue.push(projectInfo);
			}
		}
		// 判断是否还有下一页 有下一页生成下一页丢进当前队列 即可
		pageIndex++;
		if (pageIndex <= pageCount) {
			Page page = buildPage(pageIndex, doingPage.getFinalUrl());// 初始化第一页
			getWorkSpace().push(page);
		}
	}

}

@@ -47,20 +47,68 @@
 va.util.
-List
+HashMap;%0Aimport java.util.List;%0Aimport java.util.Map
 ;%0A%0Aimpor
@@ -681,16 +681,21 @@
 ageCount
+ = -1
 ;%0A%0A%09Stri
@@ -1237,36 +1237,81 @@
 e());%0A%09%09
-page.getParameters()
+%0A%09%09Map%3CString,Object%3E param=new HashMap%3CString,Object%3E();%0A%09%09param
 .put(%22pi
@@ -1332,34 +1332,19 @@
 %22);%0A%09%09pa
-ge.getParameters()
+ram
 .put(%22cv
@@ -1367,26 +1367,11 @@
 %09%09pa
-ge.getParameters()
+ram
 .put
@@ -1387,16 +1387,45 @@
 xkzh%22);%0A
+%09%09page.setParameters(param);%0A
 %09%09return
@@ -1951,34 +1951,26 @@
 c().
-select(%22NewDataSet%3ETable1%3E
+getElementsByTag(%22
 Page
@@ -2367,19 +2367,17 @@
 ojectIds
- = 
+=
 resultCo
@@ -2684,16 +2684,16 @@
 d.GET);%0A
+
 %09%09%09%09proj
@@ -2731,17 +2731,16 @@
 lue());%0A
-%0A
 %09%09%09%09proj

修复大连高新区项目列表翻页问题，以及转换匹配问题。测试通过