package org.jeecg.modules.bytedance.report.utils;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.*;

/**
 * Administrator
 * 2021/5/19
 **/
public class TimeStartAndEndUtil {


    /**
     * 根据本阶段日期 获取上阶段 开始 和 截至 日期
     * @param startTime 本阶段开始时间
     * @param endTime 本阶段截至时间
     * @return
     * @throws ParseException
     */
    public static Map<String,String> getStartEndTime(String startTime,String endTime) throws ParseException {
        Map<String,String> timeMap = new HashMap();
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
        Date start = sdf.parse(startTime);
        Date end = sdf.parse(endTime);
        //相差天数
        Long daysBetween = (end.getTime()-start.getTime())/(60*60*24*1000);
        String lastTimeStart = getSubtractTime(start, daysBetween.intValue());
        String lastTimeEnd = getSubtractTime(end, daysBetween.intValue());
        timeMap.put("lastTimeStart",lastTimeStart);
        timeMap.put("lastTimeEnd",lastTimeEnd);
        timeMap.put("daysBetween",daysBetween.toString());
        return timeMap;
    }

    /**
     * 返回 日期 - 天数 的string 日期
     * @param time 时间
     * @param daysBetween 天数
     * @return
     */
    public static String getSubtractTime(Date time,Integer daysBetween){
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
        Calendar rightNow  = Calendar.getInstance();
        rightNow .setTime(time);
        if (daysBetween.intValue() > 0){
            rightNow .add(Calendar.DAY_OF_YEAR,-daysBetween.intValue()-1);//日期加 相差的 天数 再 -1
        }else {
            rightNow .add(Calendar.DAY_OF_YEAR,-1);//日期 -1
        }
        Date rightNowTime = rightNow .getTime();
        String nowTime = sdf.format(rightNowTime);
        return nowTime;
    }

    /**
     * 获取 两个日期之间的所有 日期
     * @param startDate
     * @param endDate
     * @return
     */
    public static List<String> getAllDatesOfTwoTimes(String startDate, String endDate) {
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
        List<String> dateList = new ArrayList<String>();
        try{
            Date dateOne = sdf.parse(startDate);
            Date dateTwo = sdf.parse(endDate);

            Calendar calendar = Calendar.getInstance();
            calendar.setTime(dateOne);

            dateList.add(startDate);
            while(calendar.getTime().before(dateTwo)){ //倒序时间,顺序after改before其他相应的改动。
                calendar.add(Calendar.DAY_OF_MONTH, 1);
                dateList.add(sdf.format(calendar.getTime()));
            }
        } catch(Exception e){
            e.printStackTrace();
        }
        return dateList;
    }


    /**
     * 获取当前时间 -14 天的时间点
     * @param
     * @return
     */
    public static Map<String,String> getNowFourTeenTime(){
        Map map = new HashMap();
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
        Calendar cld = Calendar.getInstance();
        cld.setTime(new Date());
        cld.set(Calendar.DATE, cld.get(Calendar.DATE) - 14);
        map.put("startTime",sdf.format(cld.getTime()) +" 00:00:00");
        map.put("endTime",sdf.format(new Date()) +" 23:59:59");
        return map;
    }


    public static void main(String[] args) throws Exception {
//        Map<String,String> map = TimeStartAndEndUtil.getStartEndTime("2020-05-15","2020-05-18");
        //List<String> list = TimeStartAndEndUtil.getAllDatesOfTwoTimes("2021-05-19","2021-05-27");

        System.out.println(getNowFourTeenTime());
    }


}