package com.gic.haoban.manage.web.utils.statistics;

import com.gic.commons.util.DateUtil;
import com.gic.haoban.manage.web.qo.hm.data.HmTrendQO;
import org.apache.commons.lang.StringUtils;

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

/**
 * @Author guojx
 * @Date 2022/7/27 13:58
 */
public class DateUtils {

    private static List<String> getMonthBetween(Date minDate, Date maxDate) {
        ArrayList<String> result = new ArrayList<>();
        try {
            SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM");// 格式化为年月

            Calendar min = Calendar.getInstance();
            Calendar max = Calendar.getInstance();

            min.setTime(minDate);
            min.set(min.get(Calendar.YEAR), min.get(Calendar.MONTH), 1);

            max.setTime(maxDate);
            max.set(max.get(Calendar.YEAR), max.get(Calendar.MONTH), 2);

            Calendar curr = min;
            while (curr.before(max)) {
                result.add(sdf.format(curr.getTime()));
                curr.add(Calendar.MONTH, 1);
            }
        } catch (Exception e) {

        }
        return result;
    }

    public static List<String> getDateList(String startTimeStr, String endTimeStr, Integer type) throws ParseException {
        // 日期填充
        List<String> dateList = new ArrayList<>();
        Date startTime = getStartTime(startTimeStr);
        Date endTime = getEndTime(endTimeStr);
        if (type == null) {
            //计算
            if (DateUtil.daysBetween(startTime, endTime) > 60) {
                //月
                type = 2;
            } else {
                type = 1;
            }
        }
        if (type == 1) {
            dateList = DateUtil.getBetweenDates(startTime, endTime);
        } else if (type == 2){
            dateList = DateUtils.getMonthBetween(startTime, endTime);
        }
        return dateList;
    }

    public static Date getStartTime(String startTimeStr) throws ParseException {
        SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        Date startTime = null;
        if (StringUtils.isNotBlank(startTimeStr)) {
            startTime = df.parse(startTimeStr);
        }
        if (startTime == null) {
            // 过去7天
            Calendar cl = Calendar.getInstance();
            cl.add(Calendar.DATE, -7);
            startTime = cl.getTime();
        }
        startTime = DateUtil.getStartTimeOfDay(startTime);
        return startTime;
    }

    public static Date getEndTime(String endTimeStr) throws ParseException {
        SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        Date endTime = null;
        if (StringUtils.isNotBlank(endTimeStr)) {
            endTime = df.parse(endTimeStr);
        }

        if (endTime == null) {
            endTime = new Date();
        }
        endTime = DateUtil.getEndTimeOfDay(endTime);
        return endTime;
    }
}
