package com.docus.server.collect.web.utils;

import com.alibaba.fastjson.JSONArray;
import com.docus.server.collect.web.common.dto.HoliDay;

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

public class HoliDayUtil {

    /**
     * 取得周六日
     *
     * @param startTime
     * @param endTime
     * @return
     */
    public List<HoliDay> calLeaveDays(Date startTime, Date endTime) {
        List<HoliDay> holiDays = new ArrayList<>();
        //从startTime开始循环，
        //设置循环开始日期
        Date flag = startTime;
        Calendar cal = Calendar.getInstance();
        //循环遍历每个日期
        while (flag.compareTo(endTime) <= 0) {
            cal.setTime(flag);
            //判断是否为周六日
            int week = cal.get(Calendar.DAY_OF_WEEK) - 1;
            if (week == 0 || week == 6) {
                HoliDay holiDay = new HoliDay();
                holiDay.setIsOffDay(true);
                holiDay.setDate(new SimpleDateFormat("yyyy-MM-dd").format(cal.getTime()));
//                holiDay.setName("周末");
                holiDays.add(holiDay);
                //0为周日，6为周六
            }
            cal.add(Calendar.DAY_OF_MONTH, +1);
            flag = cal.getTime();
        }
        return holiDays;
    }

    /**
     * 取得节假日
     *
     * @param startTime
     * @param endTime
     * @return
     */
    public List<HoliDay> Getholidays(Date startTime, Date endTime) {
        TableJsonRead tableJsonRead = new TableJsonRead();
        String content = tableJsonRead.ReadContent("dataformat", "holiday");
        List<HoliDay> holiDays = JSONArray.parseArray(content, HoliDay.class);
        String startdate = DateUtils.toString(startTime, DateUtils.DEFAULT_DATE_FORMAT);
        String enddate = DateUtils.toString(endTime, DateUtils.DEFAULT_DATE_FORMAT);
        List<HoliDay> reutnholiDays = new ArrayList<>();
        if (null == holiDays || holiDays.size() <= 0) {
            return reutnholiDays;
        }
        holiDays.sort(Comparator.comparing(t -> t.getDate()));
        for (HoliDay o : holiDays) {
            if (o.getDate().compareTo(startdate) > 0 && o.getDate().compareTo(enddate) <= 0) {
                reutnholiDays.add(o);
            }
        }
        return reutnholiDays;
    }

}

