package com.fjhx.base;

import cn.hutool.core.convert.Convert;
import cn.hutool.core.date.DateUtil;
import cn.hutool.core.util.ObjectUtil;
import com.alibaba.fastjson.JSONObject;
import com.alibaba.fastjson.parser.ParserConfig;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.fjhx.utils.Assert;
import com.fjhx.utils.PageUtil;
import org.springblade.core.log.exception.ServiceException;

import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.Date;
import java.util.HashMap;
import java.util.List;

/**
 * 条件实体
 */
public class Condition extends HashMap<String, Object> {

    @Override
    public Condition put(String key, Object value) {
        super.put(key, value);
        return this;
    }

    /**
     * 开始时间
     */
    public Date getBeginTime() {
        return getBeginTime("beginTime");
    }

    public Date getBeginTime(String str) {
        Object beginTimeObj = this.get(str);

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

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

    /**
     * 结束时间
     */
    public Date getEndTime() {
        return getEndTime("endTime");
    }

    public Date getEndTime(String str) {
        Object endTimeObj = this.get(str);

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

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

    public <T> Page<T> getPage() {
        return new Page<>(PageUtil.getCurrent(this), PageUtil.getSize(this));
    }

    /**
     * 关键字
     */
    public String getKeyword() {
        return getStr("keyword");
    }

    /**
     * 审批状态
     */
    public Integer getFlowStatus() {
        return getInt("flowStatus");
    }

    /**
     * 类型
     */
    public Integer getType() {
        return getInt("type");
    }

    /**
     * 状态
     */
    public Integer getStatus() {
        return getInt("status");
    }

    /**
     * 获取字符串
     */
    public String getStr(String str) {
        Object value = super.get(str);
        if (ObjectUtil.isEmpty(value)) {
            return null;
        }
        return value.toString();
    }

    /**
     * 获取字符串
     */
    public String getStr(String str, String error) {
        Object value = super.get(str);
        Assert.notEmpty(value, error);
        return value.toString();
    }

    /**
     * 获取int
     */
    public Integer getInt(String str) {
        return Convert.toInt(get(str));
    }

    public Integer getInt(String str, String error) {
        Object value = super.get(str);
        Assert.notEmpty(value, error);
        return Convert.toInt(get(str));
    }

    /**
     * 获取long
     */
    public Long getLong(String str) {
        return Convert.toLong(get(str));
    }

    public Long getLong(String str, String error) {
        Object value = super.get(str);
        Assert.notEmpty(value, error);
        return Convert.toLong(get(str));
    }

    /**
     * 获取BigDecimal
     */
    public BigDecimal getBigDecimal(String str) {
        return Convert.toBigDecimal(get(str));
    }

    /**
     * 获取List
     */
    public <T> List<T> getArray(String str, Class<T> clazz) {
        return ObjectUtil.defaultIfNull(JSONObject.parseArray(this.getStr(str), clazz, ParserConfig.global), new ArrayList<>());
    }

    public Date getDate(String str) {
        String dateStr = this.getStr(str);

        if (dateStr != null) {
            return DateUtil.parse(dateStr);
        }

        return null;

    }

    public Long getUserId() {
        return getLong("userId");
    }
}
