package com.trs.web2frame.tool.tyjyh.wcmhy02.si;

import java.net.URLEncoder;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.HashSet;
import java.util.List;
import java.util.Map;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

import com.trs.web2frame.WCMServiceCaller;
import com.trs.web2frame.dispatch.Dispatch;



public class ZWuplod {
	
	
	private static String resouse_path = "D:/TestDataBridge2/";  
	
	/**
	 * 下载开普流文件
	 * @param url
	 * @param fjName
	 * @param resousePath
	 * @throws Exception
	 */
	public static void upload(String url,String fjName) throws Exception{
		System.out.println("下载附件地址："+url);
		String substring = url.substring(url.lastIndexOf("/")+1);
		if(ZhongWenUtil.isContainChinese(substring)) {
		  url=url.substring(0,url.lastIndexOf("/")+1)+URLEncoder.encode(substring);
		}
		HttpFileUpload.downLoadFromUrl(url, fjName, resouse_path);
	}
	
	/**
	 * 处理正文方法
	 * @param content
	 * @param sub
	 * @param regular
	 * @return
	 * @throws Exception 
	 */
    @SuppressWarnings("static-access")
	public static String contentResourceUpload(String content,int sub,String regular,String title,String ID) throws Exception {
    	System.out.println("处理正文开始："+content);
    	
    	String regExImg = regular;//正文中图片 
    	
		List<String> list=getContentResource(regExImg, sub, content);
		
		for(int i = 0 ; i < list.size() ; i++) {
//			Util.log("文章标题："+list.get(i)+"==="+i,"log",0);
			if(list.get(i).contains("webpic")||list.get(i).contains("protect")){
				
				String fjpath=list.get(i);
				System.out.println("正文附件上传之前地址：：：："+resouse_path+fjpath);
				Dispatch upDispatch = WCMServiceCaller.UploadFile(resouse_path+fjpath);
				// 获取上传后返回的文件名
				String file = upDispatch.getUploadShowName();
				if(file.length()>0) {
					if(regular.indexOf("src")>=0){// 迁移时 图片处理格式
						String bq="src";
						String oldUrl=""+bq+"=\""+list.get(i)+"\"";
						String newUrl=""+bq+"=\"/upload/"+file.substring(0,8)+"/"+file.substring(0,10)+"/"+file+"\""+" uploadpic=\""+file+"\"";
						System.out.println("海云地址："+oldUrl);
						System.out.println("海云地址："+newUrl);
						content=content.replace(oldUrl,newUrl);
						System.out.println("替换后得正文：：：：：：：：：：：：：：："+content);
					}else{  // 迁移时 附件类型格式
						String bq="href";
						String oldUrl=""+bq+"=\""+list.get(i)+"\"";
						String newUrl=""+bq+"=\"/upload/"+file.substring(0,8)+"/"+file.substring(0,10)+"/"+file+"\""+" appendix='true'";
						System.out.println("海云地址："+oldUrl);
						System.out.println("海云地址："+newUrl);
						content=content.replace(oldUrl,newUrl);
						System.out.println("替换后得正文：：：：：：：：：：：：：：："+content);
					}

				}

			}else if(list.get(i).contains("W0")||list.get(i).contains("P0")){
				//截取两层目录   第一层截取8  个  第二层截取 10个
				
				
				
			}else{
//				Util.log("文章标题："+title,"非webpic或者protect开头的路径",0);
//				Util.log("无法处理路径："+list.get(i),"非webpic或者protect开头的路径",0);
				System.out.println("无法处理路径："+list.get(i));
			}
		}
		return content;
	
    }
	/**
	 * 获取正文内资源
	 * List<String> list = new ArrayList<String>();
	 */
	public static List<String>  getContentResource(String regExImg,int subStart,String content){
		List<String> list = new ArrayList<String>();
		Pattern patternImg = Pattern.compile(regExImg);
		Matcher matcherImg = patternImg.matcher(content);
		while (matcherImg.find()) {
			String picPath = matcherImg.group();
			picPath = picPath.substring(subStart, picPath.length());
//			String fullPath = getFullPath(picPath);
			System.out.println("匹配到的路径："+picPath.replace("\"", ""));
			list.add(picPath.replace("\"", ""));
		}
		List<String> remove=removeDuplicate(list);
		return remove;
	}
	
	/**
	 * 去除重复数据
	 */
	public static List<String> removeDuplicate(List<String> list) {   
	    HashSet<String> h = new HashSet<String>(list);   
	    list.clear();   
	    list.addAll(h);   
	    return list;   
	}
}
