package com.softwarebr.nunu.util;

import com.brframework.common.utils.DateTimeUtils;
import com.softwarebr.nunu.domain.VisualizationDate;

import java.time.Duration;
import java.time.LocalDateTime;

/**
 * 可视化数据时间区间处理工具类
 * @author lilin
 * @date 2020/1/9
 * @time 11:53
 */
public class VisualizationUtil {


    /**
     * 时间区间处理
     * @param minDateTime 区间最小时间
     * @param maxDateTime 区间最大时间
     * @return
     */
    public static VisualizationDate visualDateHandle(String minDateTime, String maxDateTime, Long days) {
        LocalDateTime maxDate = LocalDateTime.now();
        LocalDateTime minDate = maxDate.minusDays(days - 1);
        VisualizationDate visualDate = new VisualizationDate();
        if (null != minDateTime) {
            minDate = LocalDateTime.parse(minDateTime, DateTimeUtils.FORMATTER_DEFAULT_YMDHMS);
        }
        if (null != maxDateTime) {
            maxDate = LocalDateTime.parse(maxDateTime, DateTimeUtils.FORMATTER_DEFAULT_YMDHMS);
        }
        //计算时间区间差
        days = Duration.between(minDate, maxDate).toDays();
        visualDate.setDays(days < 0 ? 0 : days);
        visualDate.setMaxDate(maxDate);
        visualDate.setMinDate(minDate);
        return visualDate;
    }


}
