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

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

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.StartLCJAppendix2;

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

public class StartLCJAppendix2 {

	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 WCMMetaTableGovOpenDataZwgk WHERE metadataid in(\r\n" + 
				"      	SELECT docid FROM wcmdocument WHERE siteid=16\r\n" + 
				"    ) AND WJYL IS NOT null");
		Connection connection = null;
		Statement statement = null;
		ResultSet set = null;
		try {
			connection = ConnectionUtil2.getConnection();
			statement = connection.createStatement();
			set = statement.executeQuery(sb.toString());
			while (set.next()) {
				Map<String, Object> oPostData = new HashMap<>();
				oPostData.put("ObjectId", set.getInt("metadataid"));
				oPostData.put("metadataid", set.getInt("metadataid"));
				oPostData.put("ChannelId", set.getInt("channelid"));// 目标栏目id
				oPostData.put("DocType", 20);
				oPostData.put("FBRQ", set.getString("FBRQ"));
				oPostData.put("title", set.getString("title"));
				oPostData.put("description", set.getString("description"));
				oPostData.put("CurrUserName", "dev");

				Map<String, Object> getRelFileInfoByDocId = GetRelFileInfoByDocId( set.getInt("metadataid"),connection);
				if(getRelFileInfoByDocId!=null) {
					oPostData.put("DOCATTACHFILEFIELD", getRelFileInfoByDocId.get("docRelFile"));
				}

				Dispatch result = WCMServiceCaller.Call("gov_webdocument", "saveDocumentInOpenData", oPostData, true);
			}
		} catch (Exception e) {
			e.printStackTrace();
		} finally {
			ConnectionUtil.closeConnection(set, statement, connection);
		}
	}

	public static Map<String, Object> GetRelFileInfoByDocId(int docId, Connection connection) throws SQLException {
		
		Statement statement = null;
		ResultSet set = null;
		statement = connection.createStatement();
		String sb = "select * from wcmappendix where appdocid="+docId;
		set = statement.executeQuery(sb.toString());
		JSONObject docRelObj = new JSONObject();
		while (set.next()) {
			docRelObj.put("SrcFile",  set.getString("SrcFile"));
			docRelObj.put("AppFile",  set.getString("AppFile"));
			docRelObj.put("AppDesc",  set.getString("AppDesc"));
			docRelObj.put("AppendixId",  0);
		}
		statement.close();
		set.close();
		// 获取文档相关的附件信息
		Map<String, Object> map = new HashMap<>();
		// 默认视图
		map.put("docRelFile", docRelObj.toString());
		return map;

	}

}