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 com.trs.cis4.gateway.view.form.UploadForm;
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.StartAppendixmzt;

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

public class StartAppendixmzt {

	public static void main(String[] args) throws Exception {
//		StringBuilder sb = new StringBuilder(
//				"SELECT docid,docchannel,doctype,doctitle,olddocid FROM wcmdocument WHERE docchannel in(select channelid from wcmchannel where parentid=65423) AND docstatus>0");
		StringBuilder sb = new StringBuilder(
				"SELECT docid,docchannel,doctype,doctitle,olddocid FROM wcmdocument WHERE docchannel=65432 AND docstatus>0");
		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("docid"));
				oPostData.put("ChannelId", set.getInt("docchannel"));// 目标栏目id
				oPostData.put("DocType", set.getInt("doctype"));
				oPostData.put("doctitle", set.getString("doctitle"));
				oPostData.put("CurrUserName", "dev");

				Map<String, Object> getRelFileInfoByDocId = GetRelFileInfoByDocId(set.getString("olddocid"));
				Map<String, Object> getRelPicInfoByDocId = GetRelPicInfoByDocId(set.getString("olddocid"));
				if(getRelFileInfoByDocId!=null) {
					oPostData.put("docRelFile", getRelFileInfoByDocId.get("docRelFile"));
					oPostData.put("docRelPic", getRelPicInfoByDocId.get("docRelPic"));
				}

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

	public static Map<String, Object> GetRelFileInfoByDocId(String docId) {
		// 获取文档相关的附件信息
		Map<String, Object> map = new HashMap<>();
		JSONArray docRelFileJson = new JSONArray();
		JSONArray picRelFileJson = new JSONArray();
		ServiceImplWcm sit = new ServiceImplWcm();
		try {
			List fileList = GetFilesInfoByDocId(docId);
			if (fileList != null) {
				for (int f = 0; f < fileList.size(); f++) {
					String attpath = fileList.get(f).toString();
					JSONObject docRelObj = new JSONObject();
					Map<String,Object> oPostData=new HashMap<>();
                    oPostData.put("from", "/TRS/HyCloud/IIP/data/WCMData/old_datas/mzt"+attpath);
                    oPostData.put("CurrUserName", "dev");
                    Dispatch result = WCMServiceCaller.Call("sxgov_dataexchange", "movePic", oPostData, true);
                    String sUploadFile = result.getResponseText();
                    
					docRelObj.put("SrcFile", sUploadFile);
					docRelObj.put("AppFile", sUploadFile);
					docRelObj.put("AppDesc", sUploadFile);
					docRelObj.put("AppendixId", 0);
					docRelFileJson.add(docRelObj);
				}
			}
			// 默认视图
			map.put("docRelFile", docRelFileJson.toString());
			map.put("docRelPic", picRelFileJson.toString());
			return map;
		} catch (Exception e) {
			Log_Exception.Error_txt("GetRelFileInfoByDocId()方法异常！" + e.getMessage());
			return null;
		}
	}
	
	public static Map<String, Object> GetRelPicInfoByDocId(String docId) {
		// 获取文档相关的附件信息
		Map<String, Object> map = new HashMap<>();
		JSONArray picRelFileJson = new JSONArray();
		try {
			List fileList = GetpicsInfoByDocId(docId);
			if (fileList != null) {
				for (int f = 0; f < fileList.size(); f++) {
					String attpath = fileList.get(f).toString();
					JSONObject docRelObj = new JSONObject();
					Map<String,Object> oPostData=new HashMap<>();
                    oPostData.put("from", "/TRS/HyCloud/IIP/data/WCMData/old_datas/mzt"+attpath);
                    oPostData.put("CurrUserName", "dev");
                    Dispatch result = WCMServiceCaller.Call("sxgov_dataexchange", "movePic", oPostData, true);
                    String sUploadFile = result.getResponseText();
                    
					docRelObj.put("SrcFile", sUploadFile);
					docRelObj.put("AppFile", sUploadFile);
					docRelObj.put("AppDesc", sUploadFile);
					docRelObj.put("AppendixId", 0);
					picRelFileJson.add(docRelObj);
				}
			}
			// 默认视图
			map.put("docRelPic", picRelFileJson.toString());
			return map;
		} catch (Exception e) {
			Log_Exception.Error_txt("GetRelFileInfoByDocId()方法异常！" + e.getMessage());
			return null;
		}
	}
	
	
	public static List GetFilesInfoByDocId (String docId) throws Exception{
		StringBuilder sb = new StringBuilder("select * from dt_article_albums where article_id="+docId);
		List list = new ArrayList();
		Connection connection = null;
		Statement statement = null;
		ResultSet set = null;
		try {
			connection = ConnectionUtil.getConnection("sqlserver");
			statement = connection.createStatement();
			set = statement.executeQuery(sb.toString());
			while (set.next()) {
				list.add(set.getString("thumb_path"));
			}
		} catch (Exception e) {
			e.printStackTrace();
		} finally {
			ConnectionUtil.closeConnection(set, statement, connection);
		}
		return list;
    }
	
	public static List GetpicsInfoByDocId (String docId) throws Exception{
		StringBuilder sb = new StringBuilder("select img_url from dt_article where id="+docId);
		List list = new ArrayList();
		Connection connection = null;
		Statement statement = null;
		ResultSet set = null;
		try {
			connection = ConnectionUtil.getConnection("sqlserver");
			statement = connection.createStatement();
			set = statement.executeQuery(sb.toString());
			while (set.next()) {
				list.add(set.getString("img_url"));
			}
		} catch (Exception e) {
			e.printStackTrace();
		} finally {
			ConnectionUtil.closeConnection(set, statement, connection);
		}
		return list;
    }

}