package com.ozs.appshow.util;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.Collections;
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;

/**
 * @ClassName: ParamUtil
 * @Description: 接口参数转换类
 * @author: IF
 * @date: 2018年7月2日 下午3:25:59
 */
public class ParamUtil {

	/**
	 * @Title: getParamString
	 * @Description: 用于模糊搜索
	 * @param param
	 * @return: String
	 */
	public static String getParamString(String param) {
		String result = null;
		if (param != null) {
			result = param.replaceAll("%", "\\\\%").replaceAll("_", "\\\\_");
		}
		return result;
	}

	public static Boolean isFloat(String number) {
		Boolean strResult = number.matches("-[0-9]+(.[0-9]+)?|[0-9]+(.[0-9]+)?");
		return strResult;
	}

	public static boolean isValidDate(String str) {
		boolean convertSuccess = true;
		SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd");
		try {
			format.setLenient(false);
			format.parse(str);
		} catch (ParseException e) {
			convertSuccess = false;
		}
		return convertSuccess;
	}

	public static boolean isNumeric(String str) {
		try {
			Integer.parseInt(str);
			return true;
		} catch (NumberFormatException e) {
			return false;
		}
	}

	public static boolean isNullString(String str) {
		if (str == null || removeBlank(str).equals("")) {
			return true;
		}
		return false;
	}

	public static String removeBlank(String str) {
		String result = str.replaceAll("\\s*", "");
		return result;
	}

	// 驼峰转下划线
	public static final char UNDERLINE = '_';

	public static String camelToUnderline(String param) {
		if (param == null || "".equals(param.trim())) {
			return "total";
		}
		int len = param.length();
		StringBuilder sb = new StringBuilder(len);
		for (int i = 0; i < len; i++) {
			char c = param.charAt(i);
			if (Character.isUpperCase(c)) {
				sb.append(UNDERLINE);
				sb.append(Character.toLowerCase(c));
			} else {
				sb.append(c);
			}
		}
		return sb.toString();
	}

	private static Pattern linePattern = Pattern.compile("_(\\w)");

	/** 下划线转驼峰 */
	public static String lineToHump(String str) {
		str = str.toLowerCase();
		Matcher matcher = linePattern.matcher(str);
		StringBuffer sb = new StringBuffer();
		while (matcher.find()) {
			matcher.appendReplacement(sb, matcher.group(1).toUpperCase());
		}
		matcher.appendTail(sb);
		return sb.toString();
	}

	public static String getDefaultOrder(String order) {
		if (order == null || "".equals(order.trim())) {
			return "asc";
		}
		return order;
	}

	public static String getDefaultEndTime(String endTime) {
		if (endTime == null || endTime.equals("")) {
			SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd");
			// Calendar calendar = Calendar.getInstance();
			Date todayDate = new Date();
			String resultDay = df.format(todayDate);
			// calendar.setTime(todayDate);
			// calendar.add(Calendar.DAY_OF_MONTH, -1);
			// Date resultDate = calendar.getTime();
			// String resultDay = df.format(resultDate);
			return resultDay;
		} else {
			return endTime;
		}
	}

	/**
	 * 
	 * @Title: getDefaultCurrentTime
	 * @Description:当前时间
	 * @param endTime
	 * @return
	 * @return: String
	 */
	public static String getDefaultCurrentTime(String endTime) {
		if (endTime == null || endTime.equals("")) {
			SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd");
			Date todayDate = new Date();
			String today = df.format(todayDate);
			return today;
		} else {
			return endTime;
		}

	}

	public static String getDefaultBeginTime(String beginTime) {
		// if (beginTime == null || beginTime.equals("")) {
		// SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd");
		// Calendar calendar = Calendar.getInstance();
		// Date todayDate = new Date();
		// calendar.setTime(todayDate);
		// calendar.add(Calendar.DAY_OF_MONTH, -1);
		// Date resultDate = calendar.getTime();
		// String resultDay = df.format(resultDate);
		// return resultDay;
		// } else {
		return beginTime;
		// }
	}

	// 输入一个月份，判断周数
	public static void main(String args[]) throws Exception {
		System.out.println(getDefaultDataMonth("2018-12", -1));

	}

	public static String getDefaultDataMonth(String dataMonth, Integer MonthNum) throws ParseException {
		Date todayDate = null;
		SimpleDateFormat df = new SimpleDateFormat("yyyy-MM");
		Calendar calendar = Calendar.getInstance();
		if (MonthNum == null) {
			MonthNum = 12;
		}
		if (dataMonth == null || dataMonth.equals("")) {
			todayDate = new Date();
			calendar.setTime(todayDate);
			calendar.set(Calendar.MONDAY, calendar.get(Calendar.MONTH) - 1 - MonthNum);
			Date resultDate = calendar.getTime();
			String resultDay = df.format(resultDate);
			return resultDay;
		} else {
			todayDate = df.parse(dataMonth);
			calendar.setTime(todayDate);
			calendar.set(Calendar.MONDAY, calendar.get(Calendar.MONTH) - 1 - MonthNum);
			Date resultDate = calendar.getTime();
			return df.format(resultDate);
		}
	}

	public static String getDefaultBeginTime(String beginTime, Integer dayNum) throws ParseException {
		Date todayDate = null;
		SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd");
		Calendar calendar = Calendar.getInstance();
		if (dayNum == null) {
			dayNum = 7;
		}
		if (beginTime == null || beginTime.equals("")) {
			todayDate = new Date();
			calendar.setTime(todayDate);
			calendar.add(Calendar.DAY_OF_MONTH, -dayNum);
			Date resultDate = calendar.getTime();
			String resultDay = df.format(resultDate);
			return resultDay;
		} else {
			todayDate = df.parse(beginTime);
			calendar.setTime(todayDate);
			calendar.add(Calendar.DAY_OF_MONTH, -dayNum);
			Date resultDate = calendar.getTime();
			return df.format(resultDate);
		}
	}

	/**
	 * 
	 * @Title: getDaysOfMonth --废掉
	 * @Description: 获取月天数
	 * @param date
	 * @return
	 * @return: int
	 */
	public static int getDaysOfMonth(Date date) {
		Calendar calendar = Calendar.getInstance();
		calendar.setTime(date);
		return calendar.getActualMaximum(Calendar.DAY_OF_MONTH);
	}

	private static final String[] ONE_TO_TEN = { "一", "二", "三", "四", "五", "六", "七", "八", "九", "十" };

	/**
	 * 
	 * @Title: getWeeks --废掉
	 * @Description: 获取周列表
	 * @param date
	 * @return
	 * @return: List<Map<String,String>>
	 */
	public static List<Map<String, String>> getWeeks(Date date) {
		List<Map<String, String>> months = new ArrayList<>();
		Map<String, String> week = new HashMap<>();

		SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd");
		Calendar calendar = Calendar.getInstance();
		calendar.setTime(date);
		int month = calendar.get(Calendar.MONTH);
		int count = 0;
		while (calendar.get(Calendar.MONTH) == month) {
			if (calendar.get(Calendar.DAY_OF_WEEK) == Calendar.MONDAY) {
				week = new HashMap<>();
				String time = format.format(calendar.getTime());
				calendar.add(Calendar.DATE, 6);
				String time2 = format.format(calendar.getTime());
				week.put("value",
						"第" + ONE_TO_TEN[count] + "周（"
								+ time.substring(time.length() - 5, time.length()).replace("-", "月") + "日-"
								+ time2.substring(time2.length() - 5, time2.length()).replace("-", "月") + "日）");
				week.put("key", time);
				months.add(week);
				count++;
			}
			calendar.add(Calendar.DATE, 1);
		}
		return months;
	}

	public static String getWeekEndTime(String weekBeginTime) throws Exception {
		SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd");
		Date weekBeginDate = format.parse(weekBeginTime);
		Calendar calendar = Calendar.getInstance();
		calendar.setTime(weekBeginDate);
		calendar.add(Calendar.DATE, 6);
		String weekEndTime = format.format(calendar.getTime());
		return weekEndTime;

	}

	/**
	 * 
	 * @Title: listWeeks
	 * @Description: 获取数据最小时间到当前时间所有周信息
	 * @param minDataTime
	 *            一年内数据最小时间
	 * @return
	 * @throws Exception
	 * @return: List<Map<String,String>>
	 */
	public static List<Map<String, String>> listWeeks(String minDataTime) throws Exception {
		List<Map<String, String>> months = new ArrayList<>();
		Map<String, String> week = new HashMap<>();
		SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd");
		Date date = format.parse(minDataTime);
		Calendar calendar = Calendar.getInstance();
		calendar.setTime(date);
		int count = 1;
		boolean fristSunDay = false;
		String startTime = null;
		String endTime = null;
		// 获取第一周第一天，即数据开始时间并赋值
		startTime = format.format(calendar.getTime());
		week.put("weekTime", startTime);
		Date nowDate = new Date();
		while (calendar.getTime().compareTo(nowDate) < 1) {
			if (!fristSunDay && calendar.get(Calendar.DAY_OF_WEEK) == Calendar.SUNDAY) {
				fristSunDay = true;
				endTime = format.format(calendar.getTime());
				week.put("weekTime", startTime + "," + endTime);
				week.put("value", "第" + count + "周（" + formatTime(startTime) + "-" + formatTime(endTime) + "）");
				months.add(week);
				count++;
			}
			if (calendar.get(Calendar.DAY_OF_WEEK) == Calendar.MONDAY) {
				week = new HashMap<>();
				startTime = format.format(calendar.getTime());
				calendar.add(Calendar.DATE, 6);
				endTime = format.format(calendar.getTime());
				week.put("value", "第" + count + "周（" + formatTime(startTime) + "-" + formatTime(endTime) + "）");
				week.put("weekTime", startTime + "," + endTime);

				months.add(week);
				count++;
			}
			calendar.add(Calendar.DATE, 1);
		}
		// 默认选中当前时间前一周
		Map<String, String> map = months.get(months.size() - 2);
		map.put("selected", "true");
		months.set(months.size() - 2, map);
		// 覆盖最后一周最后一天为当天
		calendar.add(Calendar.DATE, -7);
		months.get(months.size() - 1).put("value", "第" + (count - 1) + "周（"
				+ formatTime(format.format(calendar.getTime())) + "-" + formatTime(format.format(nowDate)) + "）");
		months.get(months.size() - 1).put("weekTime", format.format(calendar.getTime()) + "," + format.format(nowDate));
		Collections.reverse(months); // 倒序排列

		return months;
	}

	/**
	 * 
	 * @Title: formatTime
	 * @Description:格式化时间字符串 yyyy-MM-dd 为yyyy年MM月dd日
	 * @param time
	 * @return
	 * @return: String
	 */
	public static String formatTime(String time) {
		time = time.replaceFirst("-", "年");
		time = time.replaceFirst("-", "月");
		time = time + "日";
		return time;
	}

	/**
	 * 
	 * @Title: getDefaultDate
	 * @Description: 获取默认时间
	 * @param week
	 *            是否为周报
	 * @return
	 * @return: String
	 * @throws Exception
	 */
	public static String getDefaultDate(Boolean week) throws Exception {
		SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd");
		String defaultDate = null;
		Date date = null;
		Calendar calendar = Calendar.getInstance();
		date = new Date();
		calendar.setTime(date);
		if (week) {
			String startDate = null;
			String endDate = null;
			while (week) {
				if (calendar.get(Calendar.DAY_OF_WEEK) == Calendar.MONDAY) {
					calendar.add(Calendar.DATE, -1);
					endDate = format.format(calendar.getTime());
					calendar.add(Calendar.DATE, -6);
					startDate = format.format(calendar.getTime());
					break;
				}
				calendar.add(Calendar.DATE, -1);
			}
			defaultDate = startDate + "," + endDate;
		} else {

			calendar.add(Calendar.DATE, -1);
			defaultDate = format.format(calendar.getTime());
		}
		return defaultDate;
	}
}
