package com.trs.web2frame.tool.tyjyh.common;

import java.io.File;
import java.io.UnsupportedEncodingException;
import java.net.URL;
import java.net.URLEncoder;
import java.sql.Connection;
import java.sql.ResultSet;
import java.sql.Statement;

import org.apache.commons.io.FileUtils;

import com.trs.infra.util.CMyFile;
import com.trs.web2frame.tool.tyjyh.ConnectionUtil2;
import com.trs.web2frame.util.Log_Exception;

import net.sf.json.JSONObject;

/**
 *
 *@auth <a href="mailto:zhangyanan2008@gmail.com">Rainman</a>
 */
public class Common {
	//临时目录
	public static final String TEMP_PATH = "D:\\download\\szfzz\\szf\\zip\\temp\\";
	//XML路径
	public static final String XML_PATH = "D:\\download\\szfzz\\szf\\zip\\xml\\";
	
	public static void downLoadFile(String sHttpFile, String sLocalFile) {
		try {
			String sFilePath = CMyFile.extractFilePath(sHttpFile);
			String sFileName = CMyFile.extractHttpFileName(sHttpFile);
			String sOrigFileName = sFileName;
			// 文件名是否有%20-->空格，记录原始文件名后全部替换
	        if (sFileName.indexOf("%20") > 0) {
	        	sFileName = sFileName.replaceAll("%20", " ");
	        }
	
	        if ((sFileName.indexOf("%") > 0)) {
	            // 替换后，仍有%，认为是已经转义过的路径，不再转义，返回原始记录的文件名
	        	sFileName = sOrigFileName;
	        } else {
	            // 替换后，如果没有%，认为是只有空格之前被转义，属于需要转义的范围
	            try {
	                // 替换+，处理空格转换为加号的问题
	            	sFileName = URLEncoder.encode(sFileName, "UTF-8")
	                        .replaceAll("\\+", "%20");
	
	            } catch (UnsupportedEncodingException ex) {
	                //logger.error(ex);
	            }
	        }
			
			sFilePath = URLEncoder.encode(sFilePath, "UTF-8")
	                .replaceAll("\\+", "%20").replaceAll("%3A", ":").replaceAll("%2F", "/");
			//System.out.println(sFilePath + sFileName);
			FileUtils.copyURLToFile(new URL(sFilePath + sFileName), new File(sLocalFile));
		} catch(Exception e) {
			System.out.println("下载" + sHttpFile + "失败");
		}
	}
	
	public static boolean isImage(String sFileExt) {
		boolean sFileExt_gif=sFileExt.toString().toLowerCase().contains("gif");
		boolean sFileExt_jpg=sFileExt.toString().toLowerCase().contains("jpg");
		boolean sFileExt_jpeg=sFileExt.toString().toLowerCase().contains("jpeg");
		boolean sFileExt_png=sFileExt.toString().toLowerCase().contains("png");
		boolean sFileExt_bmp=sFileExt.toString().toLowerCase().contains("bmp");
		
		if (sFileExt_gif || sFileExt_jpg || sFileExt_jpeg || sFileExt_png || sFileExt_bmp) {
			return true;
		} else {
			return false;
		}
	}
	
	public static JSONObject getRecIdFromHy(String sWcmDocId, String sHyChnlId) {
		Connection connection = null;
		Statement statement = null;
		ResultSet rs = null;
		JSONObject result = new JSONObject();
		String sql = "select wc.docid,wc.recid from wcmchnldoc wc,wcmdocument wd where wc.modal=1 and wc.docid=wd.docid and wd.olddocid="+sWcmDocId+" and wc.chnlid="+sHyChnlId;
		try {
			connection = ConnectionUtil2.getConnection();
			statement = connection.createStatement();
			rs = statement.executeQuery(sql);
			if (rs.next()) {
				result.put("DOCID", rs.getString("docid"));
				result.put("RECID", rs.getString("recid"));
			}
		} catch(Exception e) {
			e.printStackTrace();
			Log_Exception.Error_txt("getRecIdFromHy()方法异常！"+e.getMessage());
			return result;
		} finally {
			ConnectionUtil2.closeConnection(statement,connection);
		}
		return result;
	}

}