package com.fjhx.base;

import cn.hutool.core.date.DateUtil;
import cn.hutool.core.util.ObjectUtil;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import lombok.Getter;
import lombok.Setter;
import org.springblade.core.log.exception.ServiceException;

import java.io.Serializable;
import java.util.Date;

@Getter
@Setter
public class BaseSelectDto implements Serializable {

    private static final long serialVersionUID = 1L;

    /**
     * 页数
     */
    private Integer pageNum;

    /**
     * 每页条数
     */
    private Integer pageSize;

    /**
     * 关键字查询
     */
    private String keyword;

    /**
     * 通用开始时间
     */
    private String beginTime;

    /**
     * 通用结束时间
     */
    private String endTime;

    /**
     * 返回分页实体
     */
    public <T> Page<T> getPage() {
        return new Page<>(getPageNum(), getPageSize());
    }

    public Integer getPageNum() {
        return ObjectUtil.defaultIfNull(pageNum, 1);
    }

    public Integer getPageSize() {
        return ObjectUtil.defaultIfNull(pageSize, 10);
    }

    public Date getBeginTime() {
        if (ObjectUtil.isEmpty(beginTime)) {
            return null;
        }

        try {
            return DateUtil.beginOfDay(DateUtil.parse(beginTime));
        } catch (Exception e) {
            throw new ServiceException("开始时间格式错误");
        }

    }

    public Date getEndTime() {

        if (ObjectUtil.isEmpty(endTime)) {
            return null;
        }

        try {
            return DateUtil.endOfDay(DateUtil.parse(endTime));
        } catch (Exception e) {
            throw new ServiceException("结束时间格式错误");
        }

    }

}
