package com.zhiqiyun.open.core.models;

import lombok.Data;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.time.DateUtils;

import java.io.Serializable;
import java.util.Calendar;
import java.util.Date;
import java.util.GregorianCalendar;
import java.util.List;

@Data
@Slf4j
public class RangeDate implements Serializable {
    private static final long serialVersionUID = 8140835909075449716L;
    private Date statDate;
    private Date endDate;

    public static RangeDate build(List<String> listRangeDate) {

        if (listRangeDate != null && listRangeDate.size() > 1) {
            try {
                RangeDate rangeDate = new RangeDate();
                Calendar cal = new GregorianCalendar();
                Date statDate = DateUtils.parseDate(listRangeDate.get(0), "yyyy-MM-dd", "yyyy-MM-dd HH:mm:ss");
                cal.setTime(statDate);
                cal.set(Calendar.HOUR_OF_DAY, 0);
                cal.set(Calendar.MINUTE, 0);
                cal.set(Calendar.SECOND, 0);
                cal.set(Calendar.MILLISECOND, 0);
                rangeDate.setStatDate(cal.getTime());

                Date endDate = DateUtils.parseDate(listRangeDate.get(1), "yyyy-MM-dd", "yyyy-MM-dd HH:mm:ss");
                cal.setTime(endDate);
                cal.set(Calendar.HOUR_OF_DAY, 23);
                cal.set(Calendar.MINUTE, 59);
                cal.set(Calendar.SECOND, 59);
                cal.set(Calendar.MILLISECOND, 999);
                rangeDate.setEndDate(cal.getTime());
                return rangeDate;
            } catch (Exception e) {
                log.warn("", e);
            }
        }
        return null;
    }
}
