package cn.ibizlab.util.domain;

import com.alibaba.fastjson.annotation.JSONField;
import com.alibaba.fastjson.util.TypeUtils;
import com.fasterxml.jackson.annotation.JsonAnyGetter;
import com.fasterxml.jackson.annotation.JsonAnySetter;
import com.fasterxml.jackson.annotation.JsonIgnore;
import lombok.Data;
import java.io.Serializable;
import java.util.HashMap;
import java.util.Map;

@Data
public class DTOBase implements Serializable {

    @JsonIgnore
    @JSONField(serialize = false)
    private Map<String,Object> extensionparams=new HashMap<String,Object>();

    @JsonIgnore
    @JSONField(serialize = false)
    public Map<String, Object> getExtensionparams() {
        return extensionparams;
    }

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

    @JsonAnyGetter
    @JSONField(name = "_any", unwrapped = true, serialize = true, deserialize = false)
    public Map<String , Object> any() {
        return extensionparams;
    }

    public Object get(String field) {
        return this.extensionparams.get(field.toLowerCase());
    }

    @JsonAnySetter
    @JSONField(name = "_any", unwrapped = true, serialize = false, deserialize = true)
    public void set(String field, Object value) {
        this.extensionparams.put(field.toLowerCase(),value);
    }

    public <V> V getOrDefault(String field,V defaultValue) {
        V v;
        return ((v = (V)get(field)) != null)
                ? ((defaultValue!=null)? (V) TypeUtils.cast(v,defaultValue.getClass(),null):v)
                : defaultValue;
    }

}
