package jnpf.model.common;

import lombok.Data;
import lombok.NoArgsConstructor;
import lombok.extern.slf4j.Slf4j;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
import java.util.Objects;

@Data
@Slf4j
@NoArgsConstructor
public class DateRangeDto {

    /**
     * 开始日期
     */
    private String startDate;

    /**
     * 结束日期
     */
    private String endDate;

    public DateRangeDto(String startDate, String endDate) {
        this.startDate = startDate + " 00:00:00";
        this.endDate = endDate + " 23:59:59";
    }

    public DateRangeDto(String month, Boolean aFalse) {
        SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM");
        Date date = null;
        try {
            date = dateFormat.parse(month);
        } catch (ParseException e) {
            log.error("日期解析异常，月份格式不正确: {}", month, e);
        }
        Calendar calendar = Calendar.getInstance();
        calendar.setTime(Objects.requireNonNull(date));
        calendar.set(Calendar.DAY_OF_MONTH, 1);
        SimpleDateFormat dateFormat1 = new SimpleDateFormat("yyyy-MM-dd");
        String startDate = dateFormat1.format(calendar.getTime());
        calendar.set(Calendar.DAY_OF_MONTH, calendar.getActualMaximum(Calendar.DAY_OF_MONTH));
        String endDate = dateFormat1.format(calendar.getTime());
        this.startDate = startDate;
        this.endDate = aFalse ? Calendar.getInstance().getTime().compareTo(calendar.getTime()) < 0 ? dateFormat1.format(Calendar.getInstance().getTime()) : endDate : endDate;
    }
}
