package com.lerx.v6.util;

import java.awt.image.BufferedImage;
import java.io.File;
import javax.imageio.ImageIO;

import org.apache.commons.lang.StringEscapeUtils;
import org.hibernate.HibernateException;
import org.hibernate.Session;
import org.hibernate.Transaction;
import org.hibernate.query.Query;
import org.springframework.orm.hibernate5.HibernateTemplate;
import com.google.gson.Gson;
import com.google.gson.GsonBuilder;
import com.google.gson.JsonObject;
import com.google.gson.JsonParser;
import com.lerx.sys.util.TimeUtil;

import cn.hutool.core.date.DateUtil;
import cn.hutool.core.date.Week;

public class TestUtil {

    
	/*
	 * public static void main(String[] args) {
	 * 
	 * 
	 * String
	 * str="AccessToken [accessToken=2.00CT2O_B0xYjee25cb994eeccbHATC, expireIn=157679999, refreshToken=,uid=1200302332]"
	 * ;
	 * 
	 * String rgex = "\\[(.*?)\\]"; Pattern pattern = Pattern.compile(rgex); Matcher
	 * matcher = pattern.matcher(str); String tmp1; while(matcher.find()) {
	 * tmp1=matcher.group(1); System.out.println("tmp1:"+tmp1);
	 * 
	 * }
	 * 
	 * }
	 */

	public static void main(String[] args) {
		String aaa="   &amp;amp;&ldquo;貌合神离的&amp;amp;amp;amp;amp;amp;amp;amp;amp;amp;amp;amp;amp;amp;amp;amp;amp;amp;amp;amp;amp;amp;amp;amp;amp;amp;amp;amp;amp;";
//		aaa=StringEscapeUtils.escapeHtml(aaa);
//		aaa=StringUtil.strReplace(aaa, "amp;amp;", "amp;");
		while (aaa.indexOf("amp;amp;")!= -1) {
			aaa=aaa.replaceAll("amp;amp;", "amp;");
		}
		aaa=StringEscapeUtils.unescapeHtml(aaa);
		
		System.out.println(aaa);
		int week=1;
		String ws;
		String returnStr = DateUtil.formatChineseDate(TimeUtil.getCurDate(), false);
		Week w;
		w = DateUtil.dayOfWeekEnum(TimeUtil.getCurDate());
		
		switch (week) {
		case 1:
			ws = w.toChinese();
			break;
		case 2:
			ws = w.toChinese("周");
			break;
		default:
			ws="";
		}

		returnStr += " "+ws;
		System.out.println(returnStr);
		
		
		java.text.SimpleDateFormat formatter = new java.text.SimpleDateFormat("yyyy年M月d日 星期六");
		System.out.println(formatter.format(System.currentTimeMillis()));
		
		/*
		 * String salt="smemdk"; String uuid="1c966224e1724c53a447649940d49fd5";
		 * 
		 * String
		 * saltmd5=StringUtil.md5(StringUtil.nullFilter(salt)+StringUtil.nullFilter(uuid
		 * )); int len = saltmd5.length(); char c; int nOfChar; int n=0; for (int i =
		 * len-6; i < len; i++) { c = saltmd5.charAt(i); nOfChar = c; n += nOfChar; } n=
		 * n%100; System.out.println("n:"+n); //n= n/188; String
		 * qrcStr=DesUtils.escapeStr(DesUtils.encode(saltmd5, n),false,true);
		 * System.out.println(n+" +密后："+qrcStr);
		 */
		
//		String str=" &amp;emsp;&amp;emsp;3月5日下午，姜堰区卫健委一行领";
		/*
		 * String str=" &am_rpage_s_@3月5日下午，姜堰区卫健委一行领@_rpage_e_aaa";
		 * 
		 * str=AnalyzeUtil.rpageConfuse("http://www.jysedu.com/aaa.jsp?id=888&name=lzh")
		 * ; System.out.println("str加密后："+str); str=AnalyzeUtil.rpageAnalyze(str);
		 * System.out.println("str解密后："+str);
		 */
//		Pattern pattern = Pattern.compile("(&(\\S+{1,6});)");
		/*
		 * Pattern pattern = Pattern.compile("(_rpage_s_@(\\S+)@_rpage_e_)"); Matcher
		 * matcher = pattern.matcher(str); if (matcher.find()) {
		 * System.out.println("group:"+matcher.group(2)); } str =
		 * matcher.replaceAll(" ");
		 * 
		 * System.out.println("str:"+str);
		 */
		
		/*
		 * String
		 * htmlStr="<div class=\"btimg\"><img SRC=\"http://www.lerx.com/template_receive_demo/portal/15.jpg\">\r\n"
		 * + "</div>"; String img = ""; Pattern p_image; Matcher m_image; // String
		 * regEx_img = "<img.*src=(.*?)[^>]*?>"; //图片链接地址 String regEx_img =
		 * "<img.*src\\s*=\\s*(.*?)[^>]*?>";
		 * 
		 * p_image = Pattern.compile (regEx_img, Pattern.CASE_INSENSITIVE); m_image =
		 * p_image.matcher(htmlStr);
		 * 
		 * 
		 * while (m_image.find()) { // 得到<img />数据 img = m_image.group(); //
		 * 匹配<img>中的src数据 Matcher m =
		 * Pattern.compile("src\\s*=\\s*\"?(.*?)(\"|>|\\s+)",Pattern.CASE_INSENSITIVE).
		 * matcher(img); while (m.find()) { System.out.println("cr:"+m.group(1)); } }
		 */
	    
		/*
		 * System.out.println("cr:"+System.currentTimeMillis());
		 * System.out.println("db:"+1532684240084L);
		 * System.out.println(TimeUtil.daysSubByTimestamp(TimeUtil.coverLongToTimestamp(
		 * System.currentTimeMillis()),TimeUtil.coverLongToTimestamp(1532684240084L)));
		 */
		
		/*long curr=System.currentTimeMillis();
		System.out.println("1:"+TimeUtil.coverLongToStr(curr, "yyyy-MM-dd HH:mm:ss") + " "+curr);
		String currStr=TimeUtil.coverLongToStr(curr, "yyyyMMddHHmmss");
		System.out.println("2:"+currStr);
		long zh=TimeUtil.coverStrToLong(currStr,"yyyyMMddHHmmss");
		System.out.println("3:"+TimeUtil.coverLongToStr(zh, "yyyy-MM-dd HH:mm:ss") + " "+zh);*/
		
		/*long curr=System.currentTimeMillis();
		long endT=curr-5*24*60*60*1000;
		
		System.out.println(TimeUtil.coverLongToStr(endT, "yyyy-MM-dd HH:mm:ss"));*/
		
		/*long tl=TimeUtil.unixLenAtCurr(7);
		System.out.println(TimeUtil.coverLongToStr(tl, "yyyy-MM-dd HH:mm:ss"));*/
		/*String url="jdbc:mysql://192.168.6.57:3306/portal?useSSL=false&characterEncoding=utf-8&serverTimezone=UTC";
		String regexp = "(jdbc:mysql://)?([^/]*)(/?.*)";
		
		 Pattern p = Pattern.compile(regexp);
		 Matcher m = p.matcher(url);
		if(m.matches()){
		System.out.println(m.group(2));
		}*/
		/*String htmlStr = "aaa<a href='www.sina.com.cn'>uuu</a>";
		htmlStr = HtmlUtils.htmlEscape(htmlStr);
		htmlStr = HtmlUtils.htmlUnescape(htmlStr);
		System.out.println(htmlStr);*/
		/*
		 * String src="D:/tmp/1.png";
		 * 
		 * System.out.println(src); InputStream in = null; try { in = new
		 * FileInputStream(src); } catch (FileNotFoundException e) { // TODO
		 * Auto-generated catch block e.printStackTrace(); } //文件与BufferedImage间的转换
		 * BufferedImage bi=file2img(src); //读取图片 BufferedImage
		 * bii=img_tailor(bi,50,50,550,550); img2file(bii,"jpg","d:/tmp/2.png"); //生成图片
		 */
	}

	public static long countHql(HibernateTemplate ht, String hql) {

		try {
			Session s = ht.getSessionFactory().openSession();
			Transaction t = s.beginTransaction();
			Query<?> query = s.createQuery(hql);
			Long count=(Long) query.list().get(0);
			
			t.commit();
			s.close();
			return count;
		} catch (HibernateException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
			return 0;
		}
	}

	public static BufferedImage img_tailor(BufferedImage src, int x, int y, int width, int height) {
		BufferedImage back = src.getSubimage(x, y, width, height);
		return back;
	}

	// 读取图片
	public static BufferedImage file2img(String imgpath) {
		try {
			BufferedImage bufferedImage = ImageIO.read(new File(imgpath));
			return bufferedImage;
		} catch (Exception e) {
			e.printStackTrace();
			return null;
		}
	}

	// 保存图片,extent为格式，"jpg"、"png"等
	public static void img2file(BufferedImage img, String extent, String newfile) {
		try {
			ImageIO.write(img, extent, new File(newfile));
		} catch (Exception e) {
			e.printStackTrace();
		}
	}

	public static String toPrettyFormat(String json) {
		JsonParser jsonParser = new JsonParser();
		JsonObject jsonObject = jsonParser.parse(json).getAsJsonObject();
		Gson gson = new GsonBuilder().setPrettyPrinting().create();
		return gson.toJson(jsonObject);
	}

}
