package com.trs.web2frame.tool.tyjyh.wcmhy05_nynct.wz;

import java.sql.Connection;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

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

import com.trs.infra.util.CMyString;
import com.trs.web2frame.WCMServiceCaller;
import com.trs.web2frame.dispatch.Dispatch;
import com.trs.web2frame.tool.tyjyh.ConnectionUtil;
import com.trs.web2frame.tool.tyjyh.ConnectionUtil2;
import com.trs.web2frame.tool.tyjyh.FujianUtil;
import com.trs.web2frame.tool.tyjyh.wcmhy05_nynct.ConfigConstant;
import com.trs.web2frame.util.Log_Exception;

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

public class ServiceImplWcm {
    private String sqlType = ConfigConstant.SQLTYPE;
    
    
	public List<Map<String, Object>> GetDocFromLogmsg() {
		StringBuilder sb = new StringBuilder("select docid,chnlid from WCMCHNLDOC where recid in(\r\n" + 
				" select wcmrecid from LOGMESSAGEINFO where MESSAGEINFO like '栏目没有设置视图！%' )");
		List<Map<String,Object>> list = new ArrayList<>();
		Connection connection = null;
		Statement statement = null;
		ResultSet set = null;
		try {
			connection = ConnectionUtil.getConnection(sqlType);
			statement = connection.createStatement();
			set = statement.executeQuery(sb.toString());
			while (set.next()) {
				Map<String,Object> map=new HashMap<>();
				map.put("docid", set.getString("docid"));
				map.put("chnlid", set.getString("chnlid"));
				list.add(map);
			}
		} catch (Exception e) {
			e.printStackTrace();
		} finally {
			ConnectionUtil.closeConnection(set, statement, connection);
		}
		return list;
	}
    
    
    
	public String GetHyRecidFromNewDb(String docid,String chnlid) {
		StringBuilder sb = new StringBuilder("select recid from wcmchnldoc where docid="+docid+" and chnlid="+chnlid );
		Connection connection = null;
		Statement statement = null;
		ResultSet set = null;
		String recid = "";
		try {
			connection = ConnectionUtil2.getConnection();
			statement = connection.createStatement();
			set = statement.executeQuery(sb.toString());
			while (set.next()) {
				recid = set.getString("recid");
			}
		} catch (Exception e) {
			e.printStackTrace();
		} finally {
			ConnectionUtil2.closeConnection(set, statement, connection);
		}
		return recid;
	}
    
	public List<Map<String, Object>> GetQuoteDocFromOldDB(String channelId) {
		StringBuilder sb = new StringBuilder(" select t.NEWCHANELID,n.HYRECID,n.HYDOCID,wcd.DOCORDER from wcmchnldoc wcd \r\n" + 
				" left join wcmchannel wc on wcd.chnlid=wc.channelid \r\n" + 
				//" left join temp_new_chnl_copy t on t.chnlname=wc.chnlname \r\n" + 
				" left join CHANNEL_MAPPING t on t.OLDCHANELID=wc.channelid \r\n" +
				" left join nw_result n on n.WCMDOCID=wcd.DOCID\r\n" + 
				" where chnlid="+channelId+" and modal>1 and docstatus=10 order by docorder");
		List<Map<String,Object>> list = new ArrayList<>();
		Connection connection = null;
		Statement statement = null;
		ResultSet set = null;
		try {
			connection = ConnectionUtil.getConnection(sqlType);
			statement = connection.createStatement();
			set = statement.executeQuery(sb.toString());
			while (set.next()) {
				Map<String,Object> map=new HashMap<>();
				map.put("recid", set.getString("HYRECID"));
				map.put("chnlid", set.getString("NEWCHANELID"));
				
				map.put("hydocid", set.getString("HYDOCID"));
				map.put("docorder", set.getString("DOCORDER"));
				list.add(map);
			}
		} catch (Exception e) {
			e.printStackTrace();
		} finally {
			ConnectionUtil.closeConnection(set, statement, connection);
		}
		return list;
	}
    
    /**
     * 向wcmchnldoc插入排序字段
     */
	public int updateChnldoc(String hyRecId, String docOrder) {
		Connection connection = null;
		Statement statement = null;
		int result = 0;
		String sql = "update wcmchnldoc set olddocOrder="+docOrder+" where recid="+hyRecId;
		try {
			connection = ConnectionUtil2.getConnection();
			statement = connection.createStatement();
			result = statement.executeUpdate(sql);
		} catch(Exception e) {
			e.printStackTrace();
			Log_Exception.Error_txt("updateChnldoc()方法异常！"+e.getMessage());
		} finally {
			ConnectionUtil2.closeConnection(statement,connection);
		}
		return result;
		
	}
    
    /**
     * 根据DOcID获取文档相关的附件信息
     * @param docId
     * @return
     */
    public 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 = "";
                       if(!appFlag.equals("40")){
                           Map<String,Object> oPostData=new HashMap<>();
                           oPostData.put("path", ConfigConstant.REMOTFILEEURL);
                           oPostData.put("from", pathfile);
                           oPostData.put("CurrUserName", "dev");
                           Dispatch result = WCMServiceCaller.Call("sxgov_dataexchange", "movePic", oPostData, true);
                           sUploadFile = result.getResponseText();
                           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;
            }
    }
    
    
    /**
     * 处理内容中的附件
     * @param htmlContent
     * @return
     */
    public String GetRetHtmlContent(String htmlContent){
        //1.找出内容中的所有图片 2.找出内容中的附件 3.调接口上传图片/附件并记录返回的信息 4.替换正文中的对应图片/附件信息
        try {
            Document doc = Jsoup.parse(htmlContent);
            Elements sectionNodes = doc.getAllElements();
            for (int i = 0; i < sectionNodes.size(); i++){
                Element Node = sectionNodes.get(i);
                //找图片
                if ("img".equalsIgnoreCase(Node.nodeName())) {
                    //如果是相对路径
                    if (Node.attr("src") != null) {
                        String temp = Node.attr("src");
                        if(temp.startsWith("W0")||temp.startsWith("P0")) {
                        	Node.attr("src", FujianUtil.getLocalFullPath(temp));
                        }
                    }
                    if (Node.attr("oldsrc") != null && !Node.attr("OLDSRC").equals("")){
                    	Node.removeAttr("oldsrc");
                    }
                }
                //找文件
                if("a".equals(Node.nodeName())){
                    if (Node.attr("href") != null && !Node.attr("href").equals("")&&Node.hasText()){
                        //判断该链接是否是文件
                        String temp=Node.attr("href");
                        if(temp.startsWith("W0")||temp.startsWith("P0")) {
	                        boolean isfile= IsContainFile(temp);
	                        if(isfile){
	                        	Node.attr("href", FujianUtil.getLocalFullPath(temp));
	                            Node.attr("appendix", "true");
	                        }
                        }
                    }
                }

            }
            Elements bodies = doc.getElementsByTag("body");
            if (bodies.size() > 0) {
            	Element body = bodies.get(0);
            	return body.html();
            }
           
            return doc.html();
        } catch (Exception e) {
            Log_Exception.Error_txt("GetAllFileInfoInContent()方法异常" + e.getMessage());
            return null;
        }
    }
    
  //判断字符串中是否包含附件
    public static Boolean IsContainFile(String content){
        if(content!=null&&!content.equals("")) {
            String regExImg = "(.txt|.doc|.xls|.zip|.rar|.pdf|.ppt|.TXT|.DOC|.XLS|.ZIP|.RAR|.PDF|.PPT|.mp3|.swf)";//正文中图片--------------add by miyanzhao20190111
            Pattern patternImg = Pattern.compile(regExImg);
            Matcher matcherImg = patternImg.matcher(content);

            if(matcherImg.find()) {
                return true;
            }else{
                return false;
            }
        }else{
            return false;
        }
    } 
    
    
    
    

    /**
     * 插入结果
     * @param object2 
     * @param object 
     * @param object3 
     * 
     * @throws SQLException
     */
    public Boolean InsertResult(Integer wcmrecid, Integer hyrecid, Integer wcmdocid, Integer hydocid, String object, String object2) throws SQLException {
        Connection connection=null;
        Statement statement =null;
        try {
            if (wcmrecid == null) {
                return false;
            }
            StringBuilder sb = new StringBuilder(
                    "INSERT INTO NW_RESULT(WCMRECID,HYRECID,WCMDOCID,HYDOCID,TITLE,CRTIME) VALUES(" + wcmrecid + "," + hyrecid + "," + wcmdocid +","+ hydocid + ",'" + object + "','" + object2 +"'  )");
            connection = ConnectionUtil.getConnection(sqlType);
            statement = connection.createStatement();
            int count = statement.executeUpdate(sb.toString());
            // ResultSet rs = statement.getResultSet();
            // ConnectionUtil.closeConnection(rs, statement, connection);
            
            if (count != 1) {
                return false;
            } else {
                return true;
            }
        } catch (Exception e) {
            // TODO: handle exception
            Log_Exception.Error_txt("InsertResult 方法异常！" + e.getMessage());
            return false;
        }
        finally{
            if (statement != null) {
                statement.close();
            }
            if (connection != null) {
                connection.close();
            }
        }

    }

    /**
     * 更新结果
     * 
     * @param wcmrecid
     * @param hyrecid
     * @param zwrecid
     * @return
     * @throws SQLException
     */
    public Boolean UpdateResult(Integer wcmrecid, Integer hyrecid, Integer wcmdocid, Integer hydocid) throws SQLException {
        Connection connection=null;
        Statement statement =null;
        try {
            if(wcmrecid==null){
                return false;
            }
            StringBuilder sb=new StringBuilder("UPDATE NW_RESULT SET HYRECID="+hyrecid+",ZWRECID="+wcmdocid+",HYDOCID="+hydocid+" WHERE WCMRECID="+wcmrecid);
            connection = ConnectionUtil.getConnection(sqlType);
            statement = connection.createStatement();
            int count = statement.executeUpdate(sb.toString());
           
            if(count!=1){
                return false;
            }else{
                return true;
            }
        } catch (Exception e) {
            Log_Exception.Error_txt("UpdateResult 方法异常！"+e.getMessage());
            return false;
        }
        finally{
            if(statement!=null){
                statement.close();
            }
            if(connection!=null){
                connection.close();
            }
        }
        
    }
    public Boolean Updatehyrecid(Integer wcmrecid,Integer hyrecid) throws SQLException {
        Connection connection=null;
        Statement statement =null;
        try {
            if(wcmrecid==null){
                return false;
            }
            StringBuilder sb=new StringBuilder("UPDATE NW_RESULT SET HYRECID="+hyrecid+" WHERE WCMRECID="+wcmrecid);
            connection = ConnectionUtil.getConnection(sqlType);
            statement = connection.createStatement();
            int count = statement.executeUpdate(sb.toString());
           
            if(count!=1){
                return false;
            }else{
                return true;
            }
        } catch (Exception e) {
            //TODO: handle exception
            Log_Exception.Error_txt("Updatehyrecid 方法异常！"+e.getMessage());
            return false;
        }
        finally{
            if(statement!=null){
                statement.close();
            }
            if(connection!=null){
                connection.close();
            }
        }
        
    }
    /**
     * 获取结果信息
     * @param wcmrecid
     * @return
     */
    public Map<String,Object> GetResultInfo(Integer wcmrecid){
        Map<String,Object> map=new HashMap<>();
        try {
            if(wcmrecid!=null){
                StringBuilder sb=new StringBuilder("SELECT * FROM NW_RESULT WHERE WCMRECID="+wcmrecid);
                Connection connection = ConnectionUtil.getConnection(sqlType);
                Statement statement = connection.createStatement();
                ResultSet set = statement.executeQuery(sb.toString());
                while (set.next()){
                    map.put("wcmrecid", set.getString("WCMRECID"));//文档记录ID
                    map.put("hyrecid", set.getString("HYRECID"));//文档记录ID
                    map.put("zwrecid", set.getString("ZWRECID"));//文档记录ID
                }
                ConnectionUtil.closeConnection(set, statement, connection);
                return map;
            }else{
                return null;
            }
        } catch (Exception e) {
            //TODO: handle exception
            Log_Exception.Error_txt("GetResultInfo方法异常！"+e.getMessage());
            return null;
        }
        
    }
    /**
     * 插入错误信息
     * 
     * @throws SQLException
     */
    public Boolean InsertLogMessageInfo(Integer wcmrecid, String docTitle, String logMessage) throws SQLException {
        Connection connection=null;
        Statement statement =null;
        try {
            if (wcmrecid == null) {
                return false;
            }
            SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");// 设置日期格式
            String timeNow = df.format(new Date()).toString();
            StringBuilder sb = new StringBuilder(
                    "INSERT INTO LOGMESSAGEINFO(WCMRECID,DOCTITLEINFO,MESSAGEINFO,RETIME) VALUES(" + wcmrecid + ",'" + docTitle + "','" + logMessage + "','"+timeNow+"')");
            connection = ConnectionUtil.getConnection(sqlType);
            statement = connection.createStatement();
            int count = statement.executeUpdate(sb.toString());
            // ResultSet rs = statement.getResultSet();
            // ConnectionUtil.closeConnection(rs, statement, connection);
            
            if (count != 1) {
                return false;
            } else {
                return true;
            }
        } catch (Exception e) {
            // TODO: handle exception
            Log_Exception.Error_txt("InsertLogMessageInfo 方法异常！" + e.getMessage());
            return false;
        }
        finally{
            if (statement != null) {
                statement.close();
            }
            if (connection != null) {
                connection.close();
            }
        }

    }

    /**
     * 更新错误信息
     * 
     */
    public Boolean UpdateLogMessageInfo(Integer wcmrecid, String logMessage) throws SQLException {
        Connection connection=null;
        Statement statement =null;
        try {
            if(wcmrecid==null){
                return false;
            }
            SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");// 设置日期格式
            String timeNow = df.format(new Date()).toString();
            StringBuilder sb=new StringBuilder("UPDATE LOGMESSAGEINFO SET MESSAGEINFO='"+logMessage+"',RETIME='"+timeNow+"' WHERE WCMRECID="+wcmrecid);
            connection = ConnectionUtil.getConnection(sqlType);
            statement = connection.createStatement();
            int count = statement.executeUpdate(sb.toString());
           
            if(count!=1){
                return false;
            }else{
                return true;
            }
        } catch (Exception e) {
            //TODO: handle exception
            Log_Exception.Error_txt("UpdateLogMessageInfo 方法异常！"+e.getMessage());
            return false;
        }
        finally{
            if(statement!=null){
                statement.close();
            }
            if(connection!=null){
                connection.close();
            }
        }
        
    }

    /**
     * 根据原文档记录id获取迁移错误信息
     * @param wcmrecid
     * @return
     */
    public Map<String,Object> GetLogMessageInfo(Integer wcmrecid){
        Map<String,Object> map=new HashMap<>();
        try {
            if(wcmrecid!=null){
                StringBuilder sb=new StringBuilder("SELECT WCMRECID,DOCTITLEINFO,MESSAGEINFO,RETIME from LOGMESSAGEINFO WHERE WCMRECID="+wcmrecid);
                Connection connection = ConnectionUtil.getConnection(sqlType);
                Statement statement = connection.createStatement();
                ResultSet set = statement.executeQuery(sb.toString());
                while (set.next()){
                    map.put("wcmrecid", set.getString("WCMRECID"));//文档记录ID
                    //map.put("DOCTITLEINFO", set.getString("DOCTITLEINFO"));//
                    //map.put("MESSAGEINFO", set.getString("MESSAGEINFO"));//
                    //map.put("RETIME", set.getString("RETIME"));
                }
                ConnectionUtil.closeConnection(set, statement, connection);
                return map;
            }else{
                return null;
            }
        } catch (Exception e) {
            //TODO: handle exception
            Log_Exception.Error_txt("GetLogMessageInfo方法异常！"+e.getMessage());
            return null;
        }
        
    }

    
    /**
     * 从老库查出该栏目下所有信息
     * @param channelId 
     * @param siteId 
     * @return
     */
    public List<Map<String,Object>> GetAllDocInfoFromOldDB(String channelId,String oldDocid){
        List<Map<String, Object>> oldMaps = new ArrayList<>();
        String conditions=ConfigConstant.SQLCONDITION;
        try {
	            StringBuilder sb=new StringBuilder("select wc.RECID,wc.CHNLID,wc.DOCCHANNEL,wc.DOCID,wc.MODAL,wc.DOCORDER,wd.DOCTYPE,wd.DOCHTMLCON,wd.DOCCONTENT,wd.DOCTITLE,\r\n" + 
	            		" wd.DOCABSTRACT,wd.DOCKEYWORDS,wd.DOCPEOPLE,wd.DOCAUTHOR,wd.DOCPLACE, wd.DOCRELTIME,wd.CRTIME,\r\n" + 
	            		" wd.SUBDOCTITLE,wd.DOCSOURCENAME,wd.DOCFILENAME,wd.DOCLINK\r\n" + 
	            		" from wcmchnldoc wc\r\n" + 
	            		" left join wcmdocument wd on wd.docid=wc.docid\r\n" + 
	            		" where  wd.dockind=0  and wc.DOCSTATUS=10 and wc.modal=1  and wc.CHNLID="+channelId+ conditions + oldDocid +"  order by wc.DOCORDER");//and wc.recid not in(select wcmrecid from nw_result)
            Connection connection = ConnectionUtil.getConnection(sqlType);
            Statement statement = connection.createStatement();
            ResultSet set = statement.executeQuery(sb.toString());
            while (set.next()){
                Map<String, Object> map = new HashMap<>();
                map.put("RECID", set.getString("RECID"));//文档记录ID
                map.put("MODAL", set.getString("MODAL"));//引用类型
                map.put("DOCID", set.getString("DOCID"));
                map.put("DOCTYPE",set.getString("DOCTYPE"));
                map.put("DOCTITLE",set.getString("DOCTITLE"));
                map.put("DOCHTMLCON",set.getString("DOCHTMLCON"));
                map.put("DOCABSTRACT",set.getString("DOCABSTRACT"));
                map.put("DOCKEYWORDS",set.getString("DOCKEYWORDS"));
                map.put("DOCPEOPLE",set.getString("DOCPEOPLE"));
                map.put("DOCPLACE",set.getString("DOCPLACE"));
                map.put("DOCAUTHOR",set.getString("DOCAUTHOR"));
                map.put("DOCRELTIME",set.getString("DOCRELTIME"));
                map.put("CRTIME",set.getString("CRTIME"));
                map.put("SUBDOCTITLE",set.getString("SUBDOCTITLE"));
                map.put("DOCSOURCENAME",set.getString("DOCSOURCENAME"));
                map.put("DOCFILENAME",set.getString("DOCFILENAME"));
                map.put("DOCLINK",set.getString("DOCLINK"));
                map.put("DOCCONTENT",set.getString("DOCCONTENT"));
                map.put("DOCORDER",set.getString("DOCORDER"));
                oldMaps.add(map);

            }
            ConnectionUtil.closeConnection(set, statement, connection);
            return oldMaps;
        } catch (Exception e) {
            Log_Exception.Error_txt("GetAllDocInfoFromOldDB()方法异常"+e.getMessage());
            return null;
        }
    }

    /**
     * 找出当前栏目下所有需要被引用的文档信息
     * @param channelId
     * @return
     */
    public List<Map<String,Object>> GetAllDocNeedQueto(String channelId){
        List<Map<String,Object>> listMap=new ArrayList<>();
        try {
            StringBuilder sb=new StringBuilder("SELECT wcd.RECID,wcd.DOCID,W.DOCTITLE,wcd.MODAL,nc.NEWCHANELID FROM wcmchnldoc wcd LEFT JOIN WCMDOCUMENT W ON W.DOCID=WCD.DOCID "+
            " LEFT JOIN CHANELRELATION_TEST nc ON wcd.CHNLID=nc.OLDCHANELID WHERE MODAL!=1 AND wcd.DOCSTATUS=10 AND wcd.SITEID=8 AND wcd.CHNLID="+channelId+" ORDER BY wcd.DOCORDER");
            Connection connection = ConnectionUtil.getConnection(sqlType);
            Statement statement = connection.createStatement();
            ResultSet set = statement.executeQuery(sb.toString());
            while (set.next()){
                Map<String,Object> map=new HashMap<>();
                map.put("WCM_RECID", set.getString("RECID"));
                map.put("WCM_DOCID", set.getString("DOCID"));
                map.put("HY_newchanelid", set.getString("NEWCHANELID"));
                map.put("WCM_MODAL", set.getString("MODAL"));
                map.put("WCM_DOCTITLE", set.getString("DOCTITLE"));
                listMap.add(map);
            }
            ConnectionUtil.closeConnection(set, statement, connection);
            return listMap;
        } catch (Exception e) {
            //TODO: handle exception
            Log_Exception.Error_txt("GetAllNeedQueto()方法异常"+e.getMessage());
            return null;
        }
       
    }
    /**
     * 根据文档DocID获取最新的海云中的recID
     * @param wcmDocId
     * @return
     */
    public String GetHyRecId(String wcmDocId){
        String HyRecId="";
        try {
            StringBuilder sb=new StringBuilder("SELECT nr.HYRECID FROM NW_RESULT nr LEFT JOIN wcmchnldoc wcd ON nr.WCMRECID=wcd.RECID WHERE wcd.MODAL=1 AND wcd.DOCID="+wcmDocId);
            Connection connection = ConnectionUtil.getConnection(sqlType);
            Statement statement = connection.createStatement();
            ResultSet set = statement.executeQuery(sb.toString());
            while (set.next()){
                HyRecId=set.getString("HYRECID");
            }
            ConnectionUtil.closeConnection(set, statement, connection);
            return HyRecId;
        } catch (Exception e) {
            //TODO: handle exception
            Log_Exception.Error_txt("GetAllNeedQueto()方法异常"+e.getMessage());
            return null;
        }
    }

    /**
     * 获取该栏目下所有需要重新排序的引用文档
     * @param channelId
     * @return
     */
    public List<Map<String,Object>> GetDocsNeedChangeOrder(String channelId){
        List<Map<String,Object>> listMap=new ArrayList<>();
        try {
            StringBuilder sb=new StringBuilder("SELECT wcd.RECID,wcd.DOCID,nc.NEWCHANELID,nr.HYRECID,nr.ZWRECID FROM wcmchnldoc wcd  LEFT JOIN CHANELRELATION_TEST nc "
            +" ON wcd.CHNLID=nc.OLDCHANELID LEFT JOIN NW_RESULT nr ON wcd.RECID=nr.WCMRECID WHERE  wcd.MODAL!=1 AND wcd.DOCSTATUS=10 AND wcd.SITEID=8 AND wcd.CHNLID="+channelId+" ORDER BY wcd.DOCORDER");
            Connection connection = ConnectionUtil.getConnection(sqlType);
            Statement statement = connection.createStatement();
            ResultSet set = statement.executeQuery(sb.toString());
            while (set.next()){
                Map<String,Object> map=new HashMap<>();
                map.put("WCM_RECID", set.getString("RECID"));
                map.put("WCM_DOCID", set.getString("DOCID"));
                map.put("HY_newchanelid", set.getString("NEWCHANELID"));
                map.put("hyrecid", set.getString("HYRECID"));
                map.put("zwrecid", set.getString("ZWRECID"));
                listMap.add(map);
            }
            ConnectionUtil.closeConnection(set, statement, connection);
            return listMap;
        } catch (Exception e) {
            //TODO: handle exception
            Log_Exception.Error_txt("GetNeedChangeOrder()方法异常"+e.getMessage());
            return null;
        }

        
    }
    	/**
	 * 根据文档获取附件信息集合
	 * @param docId
	 * @return
	 * @throws Exception
	 */
	public List<Map<String,Object>> GetFilesInfoByDocId (String docId) throws Exception{
		StringBuilder sb = new StringBuilder("SELECT w.SRCFILE,w.APPFILE,w.APPDESC,w.APPFLAG FROM wcmappendix w where w.APPDOCID="+docId);
		List<Map<String,Object>> list = new ArrayList();
		Connection connection = ConnectionUtil.getConnection(sqlType);
		//Connection connection = ConnectionUtil.getConnection("mysql");
		Statement statement = connection.createStatement();
		ResultSet set = statement.executeQuery(sb.toString());
		
		while (set.next()) {
			Map<String,Object> map=new HashMap<>();
			map.put("SRCFILE", set.getString("SRCFILE"));
			map.put("APPFILE", set.getString("APPFILE"));
			map.put("APPDESC", set.getString("APPDESC"));
			map.put("APPFLAG", set.getString("APPFLAG"));
			list.add(map);
		}
		ConnectionUtil.closeConnection(set, statement, connection);
		return list;
    }
    
    /**
	 * 根据文档id获取关联文档信息
	 * @param docId
	 * @return
	 * @throws Exception
	 */
	public List<Map<String,Object>> queryRelations(String docId) throws Exception{
		List<Map<String,Object>> list=new ArrayList<>();
		StringBuilder sb = new StringBuilder("SELECT WR.CHANNELID,WR.RELDOCID,(SELECT CR.NEWCHANELID FROM CHANELRELATION_TEST cr WHERE CR.OLDCHANELID=WR.CHANNELID ) AS NEWCHANNELID,w.DOCTITLE,w.DOCRELTIME FROM WCMRELATION WR LEFT JOIN WCMDOCUMENT w ON WR.RELDOCID=w.DOCID WHERE 1=1 AND WR.DOCID="+docId+" ORDER BY w.CRTIME DESC");
		Connection connection = ConnectionUtil.getConnection(sqlType);
		//Connection connection = ConnectionUtil.getConnection("mysql");
		Statement statement = connection.createStatement();
		ResultSet set = statement.executeQuery(sb.toString());
		while (set.next()) {
			Map<String,Object> map=new HashMap<>();
			map.put("RELDOCID",set.getString("RELDOCID"));
			map.put("DOCTITLE", set.getString("DOCTITLE"));
			map.put("CHANNELID", set.getString("CHANNELID"));
			map.put("NEWCHANNELID", set.getString("NEWCHANNELID"));
			map.put("DOCRELTIME", set.getString("DOCRELTIME"));

			list.add(map);
		}
		ConnectionUtil.closeConnection(set, statement, connection);
		return list;
	}



    	/**
	 * 获取新旧栏目关系
	 */
	public List<Map<String,Object>> queryChanelRelation() throws Exception{
		List<Map<String,Object>> list = new ArrayList();
		String condition = ConfigConstant.SQLCONDITIONCHNL;
//        StringBuilder sb = new StringBuilder("select * from channel_mapping order by ID");
		StringBuilder sb = new StringBuilder("select t.channelid as newchanelid,t.chnlname,wc.CHANNELID as oldchanelid,wc.CHNLNAME from temp_new_chnl_copy t\r\n" + 
				" left join WCMCHANNEL wc on t.chnlname=wc.CHNLNAME\r\n" + 
				" where wc.SITEID="+ConfigConstant.SITEID+" "+condition+"  \r\n" + 
				" ORDER BY wc.CHANNELID");
		Connection connection = ConnectionUtil.getConnection(sqlType);
		Statement statement = connection.createStatement();
		ResultSet set = statement.executeQuery(sb.toString());
		while (set.next()) {
			Map<String,Object> map=new HashMap<>();
			map.put("oldchanelid", set.getString("OLDCHANELID"));
			map.put("newchanelid", set.getString("NEWCHANELID"));
			list.add(map);
		}
		ConnectionUtil.closeConnection(set, statement, connection);
		return list;
    }
    /**
     * 获取栏目关系
     * @param channelId
     * @return
     * @throws Exception
     */
    public String GetNewChannelId(String channelId){
        String NEWCHANNELID="";
        String sb = "select t.channelid as newchanelid,t.chnlname,wc.CHANNELID as oldchanelid,wc.CHNLNAME from temp_new_chnl_copy t left join WCMCHANNEL wc on t.chnlname=wc.CHNLNAME where wc.channelid="+channelId;
		Connection connection = null;
		Statement statement = null;
		ResultSet set = null;
		try {
			connection = ConnectionUtil.getConnection(sqlType);
			statement = connection.createStatement();
			set = statement.executeQuery(sb.toString());
			while (set.next()) {
				NEWCHANNELID=set.getString("newchanelid");
			}
		} catch (Exception e) {
			e.printStackTrace();
		} finally {
			ConnectionUtil.closeConnection(set, statement, connection);
		}
		return NEWCHANNELID;
    }
	/**
	 * 替换分类
	 */
	public String queryClassRelation(String classoldId) throws Exception{
		String classnewId="";
		if(classoldId!=null&&!classoldId.equals("")){
			StringBuilder sb = new StringBuilder("SELECT CLASSNEWID FROM SZF_CLASSRELATION WHERE CLASSOLDID="+classoldId);
			//StringBuilder sb = new StringBuilder("select * from zjw_chanelRelation_yw where ishad ='0' order by id");
			Connection connection = ConnectionUtil.getConnection(sqlType);
			//Connection connection = ConnectionUtil.getConnection("mysql");
			Statement statement = connection.createStatement();
			ResultSet set = statement.executeQuery(sb.toString());
			while (set.next()) {
				
				classnewId=set.getString("CLASSNEWID");
				
			}
			ConnectionUtil.closeConnection(set, statement, connection);
			return classnewId;
		}else{
			return classnewId;
		}
		
	}
    	/**
	 * 替换分类
	 */
	public Map<String,Object> queryZhuTiClassRelation(String classoldId) throws Exception{
		Map<String,Object> classInfoMap=new HashMap<>();
		if(classoldId!=null&&!classoldId.equals("")){
			StringBuilder sb = new StringBuilder("SELECT CLASSNEWID,CLASSOLDID FROM SZF_CLASSRELATIONNEWOLD WHERE CLASSID="+classoldId);
			//StringBuilder sb = new StringBuilder("select * from zjw_chanelRelation_yw where ishad ='0' order by id");
			Connection connection = ConnectionUtil.getConnection(sqlType);
			//Connection connection = ConnectionUtil.getConnection("mysql");
			Statement statement = connection.createStatement();
			ResultSet set = statement.executeQuery(sb.toString());
			while (set.next()) {
				
				//classnewId=set.getString("CLASSNEWID");
				classInfoMap.put("CLASSNEWID", set.getString("CLASSNEWID"));
				classInfoMap.put("CLASSOLDID", set.getString("CLASSOLDID"));
				
			}
			ConnectionUtil.closeConnection(set, statement, connection);
			return classInfoMap;
		}else{
			return null;
		}
		
    }
    
   /**
	 * 根据调整好的分类获取该分类的对应新分类
	 */
	public String queryOldNewClass(String classoldId) throws Exception{
		String classnewId="";
		if(classoldId!=null&&!classoldId.equals("")){
			StringBuilder sb = new StringBuilder("SELECT NEWCLASSID FROM OLDNEWCLASS WHERE OLDCLASSID="+classoldId);
			//StringBuilder sb = new StringBuilder("select * from zjw_chanelRelation_yw where ishad ='0' order by id");
			Connection connection = ConnectionUtil.getConnection(sqlType);
			//Connection connection = ConnectionUtil.getConnection("mysql");
			Statement statement = connection.createStatement();
			ResultSet set = statement.executeQuery(sb.toString());
			while (set.next()) {
				
				classnewId=set.getString("NEWCLASSID");
				
			}
			ConnectionUtil.closeConnection(set, statement, connection);
			return classnewId;
		}else{
			return classnewId;
		}
		
    }
    
    public String queryWZIDByWCMDocID(String DocID)throws Exception{
        String classnewId="";
		if(DocID!=null&&!DocID.equals("")){
            StringBuilder sb = new StringBuilder("SELECT WZINFO FROM DOCCLASSINFOBAK WHERE WCMDOCID="+DocID);
			//StringBuilder sb = new StringBuilder("select * from zjw_chanelRelation_yw where ishad ='0' order by id");
			Connection connection = ConnectionUtil.getConnection(sqlType);
			//Connection connection = ConnectionUtil.getConnection("mysql");
			Statement statement = connection.createStatement();
			ResultSet set = statement.executeQuery(sb.toString());
			while (set.next()) {
				
				classnewId=set.getString("WZINFO");
				
			}
			ConnectionUtil.closeConnection(set, statement, connection);
			return classnewId;
		}else{
			return classnewId;
		}
    }

    public String queryBELONGCLASSByWCMDocID(String DocID)throws Exception{
        String classnewId="";
		if(DocID!=null&&!DocID.equals("")){
            StringBuilder sb = new StringBuilder("SELECT BELONGCLASS FROM DOCCLASSINFOBAK WHERE WCMDOCID="+DocID);
			//StringBuilder sb = new StringBuilder("select * from zjw_chanelRelation_yw where ishad ='0' order by id");
			Connection connection = ConnectionUtil.getConnection(sqlType);
			//Connection connection = ConnectionUtil.getConnection("mysql");
			Statement statement = connection.createStatement();
			ResultSet set = statement.executeQuery(sb.toString());
			while (set.next()) {
				
				classnewId=set.getString("BELONGCLASS");
				
			}
			ConnectionUtil.closeConnection(set, statement, connection);
			return classnewId;
		}else{
			return classnewId;
		}
    }
    
    
    /**
     * 获取结果信息
     * @param object 
     * @param wcmrecid
     * @return
     */
    public Boolean GetNWResultInfo(Object title,Object pubtime, Object object){
        Map<String,Object> map=new HashMap<>();
        try {
        	title = title.toString().replace("\"", "");
        	title = title.toString().replace("\'", "");
        	title = title.toString().replace("\\", "");
        	StringBuilder sb=new StringBuilder("SELECT * FROM NW_RESULT WHERE TITLE='"+title+"' and PUBDATE='"+pubtime+"' and CRTIME='"+object+"' and TYPE IS NULL");
            Connection connection = ConnectionUtil.getConnection(sqlType);
            Statement statement = connection.createStatement();
            ResultSet set = statement.executeQuery(sb.toString());
            
            while (set.next()){
            	ConnectionUtil.closeConnection(set, statement, connection);
            	return true;
            }
            return false;
        } catch (Exception e) {
            //TODO: handle exception
            Log_Exception.Error_txt("GetNWResultInfo方法异常！"+e.getMessage());
            return null;
        }
        
        
    }
    
    public void InsertDubs(Integer wcmrecid, Integer hyrecid, Integer zwrecid, String object, String object2, String object3) throws SQLException {
        Connection connection=null;
        Statement statement =null;
        try {
            StringBuilder sb = new StringBuilder(
                    "INSERT INTO NW_RESULT(WCMRECID,HYRECID,ZWRECID,TITLE,PUBDATE,CRTIME) VALUES(" + wcmrecid + "," + hyrecid + "," + zwrecid + ",'" + object + "','" + object2 +"','" + object3 +"'  )");
            connection = ConnectionUtil.getConnection(sqlType);
            statement = connection.createStatement();
            int count = statement.executeUpdate(sb.toString());
            
        } catch (Exception e) {
            Log_Exception.Error_txt("InsertResult 方法异常！" + e.getMessage());
        }
        finally{
            if (statement != null) {
                statement.close();
            }
            if (connection != null) {
                connection.close();
            }
        }

    }
    
    /**
     * 
     * @return
     * @throws SQLException
     */
    public void UpdateResults(Integer CHNLID) throws SQLException {
        Connection connection=null;
        Statement statement =null;
        try {
            StringBuilder sb=new StringBuilder("UPDATE NW_RESULT SET TYPE="+CHNLID+",TEMP="+0+" WHERE TYPE IS NULL");
            connection = ConnectionUtil.getConnection(sqlType);
            statement = connection.createStatement();
            int count = statement.executeUpdate(sb.toString());
           
        } catch (Exception e) {
            Log_Exception.Error_txt("UpdateResults 方法异常！"+e.getMessage());
        }
        finally{
            if(statement!=null){
                statement.close();
            }
            if(connection!=null){
                connection.close();
            }
        }
        
    }


}