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

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

public class StartAppendix {

	public static void main(String[] args) throws Exception {
		List<Map<String, Object>> list = new ArrayList();
		String condition = ConfigConstant.SQLCONDITIONCHNL;
		StringBuilder sb = new StringBuilder(
				"SELECT wc.channelid,wd.docid,wd.doctitle,doctype,olddocid FROM wcmappendix wa\r\n"
						+ " LEFT JOIN wcmdocument wd ON wa.appdocid=wd.docid\r\n"
						+ " LEFT JOIN wcmchannel wc ON wc.channelid=wd.docchannel\r\n"
						+ " WHERE wd.siteid=73 AND appfile LIKE '%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("docid"));
				oPostData.put("ChannelId", set.getInt("channelid"));// 目标栏目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"));
				if(getRelFileInfoByDocId!=null) {
					oPostData.put("docRelFile", getRelFileInfoByDocId.get("docRelFile"));
					oPostData.put("docRelPic", getRelFileInfoByDocId.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<Map<String, Object>> fileList = sit.GetFilesInfoByDocId(docId);
			if (fileList != null) {
				JSONArray jsonArray = JSONArray.fromObject(fileList);
				for (int f = 0; f < jsonArray.size(); f++) {
					JSONObject jsonObject = JSONObject.fromObject(jsonArray.get(f));
					String pathfile = jsonObject.get("APPFILE").toString();
					String appFlag = jsonObject.get("APPFLAG").toString();// 附件类型
					String sUploadFile = "";
					String rootstr = "D:\\download\\sjj\\";
					if (!appFlag.equals("40")) {
						String type = pathfile.substring(0, 2);

						if ("W0".equals(type)) {
							rootstr += "webpic/";
						} else {
							rootstr = "protect/";
						}
						String first = pathfile.substring(0, 8);
						String second = pathfile.substring(0, 10);
						String result = rootstr + first + "/" + second + "/" + pathfile;
						Dispatch results = WCMServiceCaller.UploadFile(result);
						sUploadFile = results.getUploadShowName();
						if (CMyString.isEmpty(sUploadFile))
							continue;
					}
					String srcFile = jsonObject.get("SRCFILE").toString();
					String appDesc = jsonObject.get("APPDESC").toString();

					JSONObject docRelObj = new JSONObject();
					docRelObj.put("SrcFile", srcFile);
					docRelObj.put("AppFile", sUploadFile);
					docRelObj.put("AppDesc", appDesc);
					docRelObj.put("AppendixId", 0);

					// 判断如果是文件
					if (appFlag.equals("10") || appFlag.equals("70") || appFlag.equals("130")) {
						docRelFileJson.add(docRelObj);
					}
					// 判断如果是图片
					if (appFlag.equals("20")) {
						picRelFileJson.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;
		}
	}

}