package com.ruoyi.common.core.domain;

import java.io.Serializable;
import java.util.Date;
import java.util.HashMap;
import java.util.LinkedHashMap;
import java.util.Map;

import cn.hutool.core.bean.BeanUtil;
import com.fasterxml.jackson.annotation.JsonFormat;
import com.fasterxml.jackson.annotation.JsonIgnore;
import com.fasterxml.jackson.annotation.JsonInclude;
import com.ruoyi.common.utils.translation.TranslateUtils;

/**
 * Entity基类
 * 
 * @author ruoyi
 */
public class BaseEntity implements Serializable
{
    private static final long serialVersionUID = 1L;

    /** 搜索值 */
    @JsonIgnore
    private String searchValue;

    /** 创建者 */
    private String createBy;

    /** 创建时间 */
    @JsonFormat(pattern = "yyyy-MM-dd HH:mm:ss")
    private Date createTime;

    /** 更新者 */
    private String updateBy;

    /** 更新时间 */
    @JsonFormat(pattern = "yyyy-MM-dd HH:mm:ss")
    private Date updateTime;

    /** 备注 */
    private String remark;

    /** 请求参数 */
    @JsonInclude(JsonInclude.Include.NON_EMPTY)
    private Map<String, Object> params;

    public String getSearchValue()
    {
        return searchValue;
    }

    public void setSearchValue(String searchValue)
    {
        this.searchValue = searchValue;
    }

    public String getCreateBy()
    {
        return createBy;
    }

    public void setCreateBy(String createBy)
    {
        this.createBy = createBy;
    }

    public Date getCreateTime()
    {
        return createTime;
    }

    public void setCreateTime(Date createTime)
    {
        this.createTime = createTime;
    }

    public String getUpdateBy()
    {
        return updateBy;
    }

    public void setUpdateBy(String updateBy)
    {
        this.updateBy = updateBy;
    }

    public Date getUpdateTime()
    {
        return updateTime;
    }

    public void setUpdateTime(Date updateTime)
    {
        this.updateTime = updateTime;
    }

    public String getRemark()
    {
        return remark;
    }

    public void setRemark(String remark)
    {
        this.remark = remark;
    }

    public Map<String, Object> getParams()
    {
        if (params == null)
        {
            params = new HashMap<>();
        }
        return params;
    }

    public void setParams(Map<String, Object> params)
    {
        this.params = params;
    }

    /*
     * keepRawField为true, 保存原值到params
     * */
    public void translate(boolean... keepRawField) {
        boolean b = null != keepRawField && keepRawField.length > 0 && keepRawField[0];
        TranslateUtils.translateEntity(this, b);
    }

    @SuppressWarnings("unchecked")
    public <T extends BaseEntity> T putParam(String name, Object value) {
        getParams().put(name, value);
        return (T) this;
    }

    @SuppressWarnings("unchecked")
    public <T extends BaseEntity> T putParams(Object... args) {
        for (int i = 0; i < args.length; i += 2) {
            String name = (String) args[i];
            Object value = args[i + 1];
            getParams().put(name, value);
        }
        return (T) this;
    }

    @SuppressWarnings("unchecked")
    public <T extends BaseEntity> T removeParam(String... name) {
        if (params != null && null != name && name.length > 0) {
            for (String s : name) {
                params.remove(s);
            }
        }
        return (T) this;
    }

    @SuppressWarnings("unchecked")
    public <T extends BaseEntity> T removeParams() {
        if (params != null)
            params.clear();
        return (T) this;
    }

    public Object obtainParam(String name) {
        if (params == null)
            return null;
        return params.get(name);
    }

    public Object obtainParam(String name, Object defValue) {
        if (params == null)
            return defValue;
        return params.getOrDefault(name, defValue);
    }

    @SuppressWarnings("unchecked")
    public <T> T obtainParamT(String name) {
        if (params == null)
            return null;
        return (T) params.get(name);
    }

    @SuppressWarnings("unchecked")
    public <T> T obtainParamT(String name, T defValue) {
        if (params == null)
            return defValue;
        return (T) params.getOrDefault(name, defValue);
    }

    public boolean containsParam(String name) {
        if (params == null)
            return false;
        return params.containsKey(name);
    }

    @JsonIgnore
    private Map<String, Object> sqlParams; // SQL额外参数, 用于mybatis, 例如用来强制更新某字段为null, 值没有防注入
    private Map<String, Object> SQLPARAMS()
    {
        if (sqlParams == null)
            sqlParams = new LinkedHashMap<>();
        return sqlParams;
    }

    @SuppressWarnings("unchecked")
    public <T extends BaseEntity> T sqlParam(String name, Object value) {
        SQLPARAMS().put(name, value);
        return (T) this;
    }

    public Map<String, Object> toMap(boolean...combineParams)
    {
        Map<String, Object> map = BeanUtil.beanToMap(this);
        if(null != combineParams && combineParams.length > 0 && combineParams[0])
        {
            Object params = map.remove("params");
            if(null != params)
            {
                Map<String, Object> m = (Map<String, Object>) params;
                m.forEach((k, v) -> {
                    if(!map.containsKey(k))
                        map.put(k, v);
                });
            }
        }
        return map;
    }
}
