package nc.hr.utils;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.Date;
import java.util.List;

import org.joda.time.LocalDate;

import com.alibaba.fastjson.JSONObject;

import nc.vo.pub.lang.UFBoolean;
import nc.vo.pub.lang.UFDate;
import nc.vo.pub.lang.UFDouble;
import nc.vo.pubapp.pattern.exception.ExceptionUtils;

public class SpecialDataUtil {

	private static String url = "https://autoaccount.thape.com.cn/nc/vacation/workAndLeaveDays";
	/**
	 *获取起始日期-结束日期的月份并拼接成sql
	 * @param start  起始日期
	 * @param stop	结束日期
	 * @return
	 */
	public static String getDateIntervalsql(String str,Date startDate,Date endDate){

		// 创建起始日期的 Calendar 实例  
        Calendar startCal = Calendar.getInstance();  
        startCal.setTime(startDate);  
  
        // 创建用于迭代的 Calendar 实例，从起始日期开始  
        Calendar currentCal = Calendar.getInstance();  
        currentCal.setTime(startDate);  
  
        // 用于构建 SQL IN 子句的 StringBuffer  
        StringBuffer sqlin = new StringBuffer();  
        sqlin.append(" in (");  
  
        // 循环直到当前日期超过结束日期  
        while (currentCal.getTime().before(endDate) || currentCal.getTime().equals(endDate)) {  
            // 获取年份和月份（月份需要 +1，因为 Calendar 月份是从 0 开始的）  
            int year = currentCal.get(Calendar.YEAR);  
            int month = currentCal.get(Calendar.MONTH) + 1;  
	            if(str.equals("N") && year != 2022) {
	            	  // 格式化年份和月份为字符串（YYYY-MM）  
		            String yearMonthStr = String.format("%04d%02d", year, month);  
		            sqlin.append("'" + yearMonthStr + "',");  
	            }
	            if(str.equals("Y") && year == 2022) {
	            	  // 格式化年份和月份为字符串（YYYY-MM）  
		            String yearMonthStr = String.format("%04d%02d", year, month);  
		            sqlin.append("'" + yearMonthStr + "',");  
	            }
	            // 将当前日期设置为下一个月的第一天  
	            currentCal.add(Calendar.MONTH, 1);  
	            currentCal.set(Calendar.DAY_OF_MONTH, 1);  
	            
        }  
        if(sqlin.substring(sqlin.length()-1).equals("(")) {
        	sqlin.append("'1111'");
        }else {
        	sqlin.deleteCharAt(sqlin.length() - 1); // 删除最后的逗号  
        }
       
        sqlin.append(")");  
		return sqlin.toString();
	}
	
	
	/**
	 *获取起始日期-结束日期中间的月份
	 * @param start  起始日期
	 * @param stop	结束日期
	 * @return
	 */
	public static List<String> getDateInterval(Date startDate,Date endDate){

	       Calendar startCal = Calendar.getInstance();  
	        startCal.setTime(startDate);  
	  
	        // 创建用于迭代的 Calendar 实例，从起始日期开始  
	        Calendar currentCal = Calendar.getInstance();  
	        currentCal.setTime(startDate);  
	  
	        List<String> list = new ArrayList<String>();
	        // 循环直到当前日期超过结束日期  
	        while (currentCal.getTime().before(endDate) || currentCal.getTime().equals(endDate)) {  
	            // 获取年份和月份（月份需要 +1，因为 Calendar 月份是从 0 开始的）  
	            int year = currentCal.get(Calendar.YEAR);  
	            int month = currentCal.get(Calendar.MONTH) + 1;
	  
	            // 格式化年份和月份为字符串（YYYYMM）  
	            String yearMonthStr = String.format("%04d%02d", year, month);  
	            String formattedStr = yearMonthStr.replaceFirst("^(\\d{4})(\\d{2})", "$1-$2");
	            list.add(formattedStr);  
	            // 将当前日期设置为下一个月的第一天  
	            currentCal.add(Calendar.MONTH, 1);  
	            currentCal.set(Calendar.DAY_OF_MONTH, 1);  
	        }   
			return list;
	}
	
	/**
	 *获取起始日期-结束日期和起始日期结束日期中间的年
	 * @param start  起始日期
	 * @param stop	结束日期
	 * @return
	 */
	public static String[] GetYears(Date startDate,Date endDate) {
		
		Calendar startCal = Calendar.getInstance();
        startCal.setTime(startDate);
        int startYear = startCal.get(Calendar.YEAR);

        Calendar endCal = Calendar.getInstance();
        endCal.setTime(endDate);
        int endYear = endCal.get(Calendar.YEAR);

        ArrayList<String> yearList = new ArrayList<String>();
        for (int year = startYear; year <= endYear; year++) {
            yearList.add(String.valueOf(year));
        }

        return yearList.toArray(new String[0]);
		
	}
	
	/**
	 * 
	 * @param begindate 起始日期
	 * @param enddate 结束日期
	 * @param pk_psndoc 人员id
	 * @return
	 * @throws Exception
	 */
	public static JSONObject getWorkDays(String begindate,String enddate,String pk_psndoc) throws Exception {
		return JSONObject.parseObject(new HttpHelper().doGet(url+"?begindate="+begindate.toString().subSequence(0, 10)+"&enddate="+enddate.toString().subSequence(0, 10)+"&pk_psndoc="+pk_psndoc+""));
	}
	
	/**
	 * 
	 * @param begindate 起始日期
	 * @param enddate 结束日期
	 * @param pk_psndoc 人员id
	 * @return
	 * @throws Exception
	 */
//	public static JSONObject getWorkDaysn(LocalDate begindate,LocalDate enddate,String pk_psndoc) throws Exception {
//		JSONObject jsonObject = JSONObject.parseObject(new HttpHelper().doGet(url+"?begindate="+begindate.toString().subSequence(0, 10)+"&enddate="+enddate.toString().subSequence(0, 10)+"&pk_psndoc="+pk_psndoc+""));
//		if(!"1".equals(jsonObject.getString("code"))) {
//        	ExceptionUtils.wrappBusinessException("获取工作天数失败:"+jsonObject.get("msg"));
//        }
//		return jsonObject;
//	}
	public static JSONObject getWorkDaysn(Date begindate,Date enddate,String pk_psndoc) throws Exception {
		JSONObject jsonObject = JSONObject.parseObject(new HttpHelper().doGet(url+"?begindate="+formatDate(begindate)+"&enddate="+formatDate(enddate)+"&pk_psndoc="+pk_psndoc+""));
		if(!"1".equals(jsonObject.getString("code"))) {
        	ExceptionUtils.wrappBusinessException("获取工作天数失败:"+jsonObject.get("msg"));
        }
		return jsonObject;
	}

	
	/**
	 * 
	 * @param workPeriodStart 工作周期开始日期
	 * @param workPeriodEnd 工作周期结束日期
	 * @param minimumPeriodStart 已发奖金工作开始日期
	 * @param minimumPeriodEnd 已发奖金工作结束日期
	 * @param pk_psndoc 人员id
	 * @return 工作周期和保底周期有交集的工作天数
	 * @throws Exception
	 */
//	public static String DateIntersection(Date workPeriodStart,Date workPeriodEnd,LocalDate minimumPeriodStart,LocalDate minimumPeriodEnd,String pk_psndoc)throws Exception {
//		if (minimumPeriodStart.isAfter(workPeriodEnd) || minimumPeriodEnd.isBefore(workPeriodStart)) {
//            return "0";
//        } else {
//            LocalDate intersectionStart = minimumPeriodStart.isAfter(workPeriodStart) ? minimumPeriodStart : workPeriodStart;
//            LocalDate intersectionEnd = minimumPeriodEnd.isBefore(workPeriodEnd) ? minimumPeriodEnd : workPeriodEnd;
//            JSONObject jsonObject =  JSONObject.parseObject(new HttpHelper().doGet(url+"?begindate="+intersectionStart+"&enddate="+intersectionEnd+"&pk_psndoc="+pk_psndoc+""));
//            if(!"1".equals(jsonObject.getString("code"))) {
//            	ExceptionUtils.wrappBusinessException("获取工作天数失败:"+jsonObject.get("msg"));
//            }
//			return jsonObject.getJSONObject("data").getString("workday");
//        }
//	}
	

	  
	public static String DateIntersection(Date workPeriodStart, Date workPeriodEnd, Date minimumPeriodStart, Date minimumPeriodEnd, String pk_psndoc) throws Exception {  
		if (minimumPeriodEnd.compareTo(workPeriodStart) == -1) {  
	        return "0";  
		} else {  
	        Date intersectionStart = minimumPeriodStart.after(workPeriodStart) ? minimumPeriodStart : workPeriodStart;  
	        Date intersectionEnd = minimumPeriodEnd.before(workPeriodEnd) ? minimumPeriodEnd : workPeriodEnd;  
	        JSONObject jsonObject = JSONObject.parseObject(new HttpHelper().doGet(url + "?begindate=" + formatDate(intersectionStart) + "&enddate=" + formatDate(intersectionEnd) + "&pk_psndoc=" + pk_psndoc + ""));  
	        if (!"1".equals(jsonObject.getString("code"))) {  
	            ExceptionUtils.wrappBusinessException("获取工作天数失败:" + jsonObject.get("msg"));  
	        }  
	        return jsonObject.getJSONObject("data").getString("workday");  
	    }  
	}  
	  
	private static Date getDate(String dateStr) throws Exception {  
	    SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
	    return sdf.parse(dateStr);  
	}  
	  
	private static String formatDate(Date date) {  
	    SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd"); 
	    return sdf.format(date);  
	}
	
	private static String formatDates(Date date) {  
	    SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM"); 
	    return sdf.format(date);  
	}
	
}
