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

import java.sql.SQLException;
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.util.Log_Exception;

import net.sf.json.JSONObject;

public class StartErrorDoc {

	public static void main(String[] args) {
		StartErrorDoc wcm=new StartErrorDoc();
		wcm.StartMethord();
	}

	private void StartMethord() {
		ServiceImplWcm wcm=new ServiceImplWcm();
        List<Map<String,Object>> msgMaps=wcm.GetDocFromLogmsg();
        for (Map<String, Object> msgMap : msgMaps) {	
        	String docid = msgMap.get("docid").toString();
        	String chnlid = msgMap.get("chnlid").toString();
        	String channelIdFor = wcm.GetNewChannelId(chnlid);
        	String oldDocid = " and wd.docid="+docid;
        	List<Map<String, Object>> docList = wcm.GetAllDocInfoFromOldDB(chnlid,oldDocid);
        	if(docList!=null&&docList.size()>0){
        		Map<String,Object> map = docList.get(0);
                Map<String,Object> oPostData=new HashMap<>();
                oPostData.put("ObjectId", 0);
                oPostData.put("ChannelId", Integer.parseInt(channelIdFor));// 目标栏目id
                oPostData.put("CurrUserName", "dev");
                String wcmRecId=map.get("RECID").toString();//文档记录Id
                String wcmDocId=map.get("DOCID").toString();//文档ID
                String docTitle=map.get("DOCTITLE").toString();
                if(map.get("DOCTYPE")!=null){
                    oPostData.put("DOCTYPE", map.get("DOCTYPE"));//判断如果是文件类型文档
                    if(map.get("DOCTYPE").toString().equals("40")){
                    	String filename = map.get("DOCFILENAME").toString();
                        Map<String,Object> fileData=new HashMap<>();
                        fileData.put("path", ConfigConstant.REMOTFILEEURL);
                        fileData.put("from", filename);
                        fileData.put("CurrUserName", "dev");
                        Dispatch result = null;
						try {
							result = WCMServiceCaller.Call("sxgov_dataexchange", "movePic", fileData, true);
						} catch (Exception e) {
							e.printStackTrace();
						}
                        String sUploadFile = result.getResponseText();
                        if(CMyString.isEmpty(sUploadFile))continue;
                        oPostData.put("DocFileName",sUploadFile);
                        oPostData.put("DOCFILE",sUploadFile);
                    }
                }else{
                    oPostData.put("DOCTYPE",CMSConstants.CONTENT_BODY_HTML);
                }
                //标题-必填
                if (map.get("DOCTITLE") != null) {
                    oPostData.put("DOCTITLE", docTitle);
                }
                
                if (map.get("SUBDOCTITLE") != null) {
					oPostData.put("ShortTitle", map.get("SUBDOCTITLE"));
					oPostData.put("SUBDOCTITLE", map.get("SUBDOCTITLE"));
				}
				if (map.get("DOCKEYWORDS") != null) {
					String keyword = (String) map.get("DOCKEYWORDS");
					keyword = keyword.replaceAll("<br/>", "");//
					keyword = keyword.replaceAll("<br>", "");//
					oPostData.put("DOCKEYWORDS", keyword);
				}

				if (map.get("DOCPEOPLE") != null) {
					oPostData.put("DOCPEOPLE", map.get("DOCPEOPLE"));
				}
				if (map.get("DOCSOURCENAME") != null) {
					oPostData.put("DOCSOURCENAME", map.get("DOCSOURCENAME"));
				}
				if (map.get("DOCAUTHOR") != null) {
					oPostData.put("DOCAUTHOR", map.get("DOCAUTHOR"));
				}
				if (map.get("DOCRELTIME") != null) {
					oPostData.put("DOCRELTIME", map.get("DOCRELTIME"));
				}
				if (map.get("CRTIME") != null) {
					oPostData.put("CRTIME", map.get("CRTIME"));
				}
				if (map.get("DOCABSTRACT") != null) {
					oPostData.put("DOCABSTRACT", map.get("DOCABSTRACT"));
				}
				if (map.get("DOCLINK") != null) {
					oPostData.put("DOCLINK", map.get("DOCLINK"));
				}
				
				if (map.get("DOCCONTENT") != null) {
					oPostData.put("DOCCONTENT", map.get("DOCCONTENT"));
				}
				
				String docOrder = "0";
				if (map.get("DOCORDER") != null) {
					docOrder = map.get("DOCORDER").toString();
				}
				
				oPostData.put("oldDocid", wcmDocId);
				
				//处理附件
				Map<String, Object> getRelFileInfoByDocId = wcm.GetRelFileInfoByDocId(wcmDocId);
				oPostData.put("docRelFile", getRelFileInfoByDocId.get("docRelFile"));
				oPostData.put("docRelPic", getRelFileInfoByDocId.get("docRelPic"));
				
				// 内容中图片和附件需要特殊处理
				if (map.get("DOCHTMLCON") != null) {
					String htmlContent = map.get("DOCHTMLCON").toString();
					// 处理内容中附件
					String ret_htmlContent = wcm.GetRetHtmlContent(htmlContent);
					if (!CMyString.isEmpty(ret_htmlContent)) {
						htmlContent = ret_htmlContent;
					}
					oPostData.put("DOCHTMLCON", htmlContent);
				} else {
					oPostData.put("DocHtmlCon", "&nbsp;");
					oPostData.put("DOCCONTENT", "&nbsp;");
				}
				
				if(map.get("DOCTYPE").toString().equals("10")) {
                	oPostData.put("DOCTYPE",CMSConstants.CONTENT_BODY_HTML);
                	oPostData.put("DOCHTMLCON", map.get("DOCCONTENT"));
                	oPostData.put("DOCCONTENT", map.get("DOCCONTENT"));
                }
				
				try {
                    Dispatch result = WCMServiceCaller.Call("gov_webdocument", "saveDocumentInWeb", oPostData, true);
                    if(result!=null){
                        JSONObject rObject = JSONObject.fromObject(result.getResponseText().toString());
                        if (rObject.get("ISSUCCESS").toString().equals("true")){
                            JSONObject dataObject=JSONObject.fromObject(rObject.getString("DATA"));
                            String hyRecId=dataObject.getString("RECID");
                            String hyDocId=dataObject.getString("DOCID");
                            
                            //记录原始文章的docorder
                            int res = wcm.updateChnldoc(hyRecId,docOrder);
                            if(res<1){
                                Log_Exception.Logger_txt("chnldoc更新失败,海云recid:"+hyRecId+",wcm排序字段:"+docOrder);
                            }
                            
                            Map<String,Object> resultMap=new HashMap<>();
                            resultMap=wcm.GetResultInfo(Integer.valueOf(wcmRecId));//
                            
                            //存在更新，不存在插入
                            if(resultMap!=null&&resultMap.size()>0){
                                Boolean isSuc=false;
                                isSuc=wcm.UpdateResult(Integer.valueOf(wcmRecId), Integer.valueOf(hyRecId), Integer.valueOf(wcmDocId),Integer.valueOf(hyDocId));
                                if(!isSuc){
                                    Log_Exception.Logger_txt("结果表 "+wcmRecId+" :更新失败！");
                                }
                            }else{
                                Boolean isSuc=false;
                                isSuc=wcm.InsertResult(Integer.valueOf(wcmRecId), Integer.valueOf(hyRecId), Integer.valueOf(wcmDocId),Integer.valueOf(hyDocId),map.get("DOCTITLE").toString(),map.get("CRTIME").toString());
                                if(!isSuc){
                                    Log_Exception.Logger_txt("结果表 "+wcmRecId+" :插入失败！");
                                }
                            }
                        }else{
                            String messageInfo=rObject.getString("MSG");
                            Log_Exception.Logger_txt("失败！RecID："+wcmRecId+" 标题："+docTitle+"错误信息："+messageInfo);

                            //记录文档失败信息
                            Boolean isSuc=false;
                            Map<String,Object> Messagemap=wcm.GetLogMessageInfo(Integer.valueOf(wcmRecId));
                            if(Messagemap!=null&&Messagemap.size()>0){
                                isSuc=wcm.UpdateLogMessageInfo(Integer.valueOf(wcmRecId), messageInfo);
                            }else{
                                isSuc=wcm.InsertLogMessageInfo(Integer.valueOf(wcmRecId), docTitle, messageInfo);
                            }
                            if(!isSuc){
                                Log_Exception.Logger_txt("错误信息表 "+wcmRecId+" :插入/更新失败！");
                            }
                        }
                    }
				}catch(Exception e) {
					Boolean isSuc=false;
//					try {
//						wcm.InsertLogMessageInfo(Integer.valueOf(wcmRecId), docTitle, "网络连接失败");
//					} catch (NumberFormatException | SQLException e1) {
//						e1.printStackTrace();
//					}
//                    if(!isSuc){
//                        Log_Exception.Logger_txt("错误信息表 "+wcmRecId+" :插入失败！");
//                    }
					e.printStackTrace();
				}
        	}
        }
		
	}

}
