package com.trs.web2frame.tool.tyjyh.wcmhy06_szf;

import java.sql.Connection;
import java.sql.ResultSet;
import java.sql.Statement;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.jsoup.Jsoup;
import org.jsoup.nodes.Document;
import org.jsoup.nodes.Element;
import org.jsoup.select.Elements;

import com.trs.cms.CMSConstants;
import com.trs.infra.util.CMyString;
import com.trs.web2frame.WCMServiceCaller;
import com.trs.web2frame.dispatch.Dispatch;
import com.trs.web2frame.tool.tyjyh.ConfigConstant;
import com.trs.web2frame.tool.tyjyh.ConnectionUtil;
import com.trs.web2frame.tool.tyjyh.ConnectionUtil2;
import com.trs.web2frame.tool.tyjyh.Log_Exception;
import com.trs.web2frame.tool.tyjyh.wcmhy06_szf.ServiceImplWcm;
import com.trs.web2frame.tool.tyjyh.wcmhy06_szf.StartUpdateTime;

import net.sf.json.JSONArray;
import net.sf.json.JSONObject;

public class StartUpdateTime {

	public static void main(String[] args) throws Exception {
		List<Map<String, Object>> list = new ArrayList();
		String condition = ConfigConstant.SQLCONDITIONCHNL;
//		StringBuilder sb = new StringBuilder(
//				"SELECT * FROM wcmdocument w WHERE w.SHORTTITLE='30' AND w.siteid=59 AND docstatus>0\r\n");
		StringBuilder sb = new StringBuilder("SELECT w.doclink,w.olddocid,w.doctitle,w.metadataid,wd.doctype,wd.docchannel,w.dochtmlcon FROM WCMMetaTableGovOpenDataYbj w\r\n" + 
				"LEFT JOIN wcmdocument wd ON w.metadataid=wd.docid\r\n" + 
				"WHERE wd.siteid=24 AND wd.docstatus>=0");
		Connection connection = null;
		Statement statement = null;
		ResultSet set = null;
		try {
			connection = ConnectionUtil2.getConnection();
			statement = connection.createStatement();
			set = statement.executeQuery(sb.toString());
			int count2 = 0;
			while (set.next()) {
				System.out.println(++count2+"##" +set.getString("doctitle"));
				Map<String, Object> oPostData = new HashMap<>();
				oPostData.put("ObjectId", set.getInt("metadataid"));
				oPostData.put("ChannelId", set.getInt("docchannel"));// 目标栏目id
				oPostData.put("DOCTYPE", set.getInt("doctype"));
				oPostData.put("DOCTITLE", set.getString("doctitle"));
				if(null!=set.getString("doclink")) {
					oPostData.put("doclink", set.getString("doclink"));
				}
				oPostData.put("CurrUserName", "dev");
				String htmlContent = set.getString("dochtmlcon");
				oPostData.put("DOCHTMLCON", htmlContent);
				String oldidxid = findFiled(set.getString("olddocid"));
				oPostData.put("OLDIDXID", oldidxid);
				if(null==oldidxid)continue;
				Dispatch result = WCMServiceCaller.Call("gov_webdocument", "saveDocumentInDynamicPage", oPostData, true);
			}
		} catch (Exception e) {
			e.printStackTrace();
		} finally {
			ConnectionUtil.closeConnection(set, statement, connection);
		}
	}

	private static String findFiled(String string) {
		String sb = "select xidxid from wcmdocument where  docid="+string;
		Connection connection = null;
		Statement statement = null;
		ResultSet set = null;
		String xidxid = "";
		try {
			connection = ConnectionUtil.getConnection("mysql");
			statement = connection.createStatement();
			set = statement.executeQuery(sb.toString());
			while (set.next()) {
				xidxid = set.getString("xidxid");
			}
		} catch (Exception e) {
			e.printStackTrace();
		} finally {
			ConnectionUtil.closeConnection(set, statement, connection);
		}
		return xidxid;
	}


}