package com.sky.ioc.entity.params;

import lombok.Data;

import java.time.Duration;
import java.time.LocalDate;
import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;
import java.time.temporal.ChronoUnit;

@Data
public class IocTimeRange {
    /** 开始时间 */
    private String startDate;

    /** 结束时间 */
    private String endDate;

    private Integer range;

    public Integer getRange() {
        DateTimeFormatter pattern = DateTimeFormatter.ofPattern("yyyy/MM/dd");
        if (this.startDate.contains(":")) {
            pattern = DateTimeFormatter.ofPattern("yyyy/MM/dd HH:mm:ss");
            LocalDateTime sDate = LocalDateTime.parse(this.startDate, pattern);
            LocalDateTime eDate = LocalDateTime.parse(this.endDate, pattern);
            this.range = Math.toIntExact(Duration.between(sDate, eDate).toDays());
        } else {
            LocalDate sDate = LocalDate.parse(this.startDate, pattern);
            LocalDate eDate = LocalDate.parse(this.endDate, pattern);
            this.range = Math.toIntExact(sDate.until(eDate, ChronoUnit.DAYS));
        }
        return this.range;
    }

}
