package com.eversec.dynamic.entity;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * Created by liangxianyou at 2018/09/21
 */
public class DnyCate {

    private int cateId; //类别主键
    private int cate; //类别标志
    private int cateName;   //类别名称
    private Map attach = new HashMap();

    public int getCateId() {
        return cateId;
    }

    public void setCateId(int cateId) {
        this.cateId = cateId;
    }

    public int getCate() {
        return cate;
    }

    public void setCate(int cate) {
        this.cate = cate;
    }

    public int getCateName() {
        return cateName;
    }

    public void setCateName(int cateName) {
        this.cateName = cateName;
    }

    public Map<String, Object> getAttach() {
        return attach;
    }

    public void setAttach(Map attach) {
        this.attach = attach;
    }


    //---------- setAttach ---------------
    public DnyCate set(String k, Object v){
        attach.put(k, v);
        return this;
    }

    public String get(String k){
        return attach.get(k)+"";
    }


    //--------- 辅助处理 --------------
    private static Map<Integer, List<DnyAttr>> attrCache = new HashMap();//附加属性缓存
    //设置缓存数据
    public void setAttrCache(List<DnyAttr> attrs) {
        attrs.forEach(x->{
            List list = attrCache.get(x.getCate());
            if (list == null) {
                list = new ArrayList();
            }

            list.add(x);
            attrCache.put(x.getCate(), list);
        });
    }

    public List<DnyAttr> getAttrs(){
        List<DnyAttr> dnyAttrs = attrCache.get(cate);
        if (dnyAttrs == null){
            dnyAttrs = new ArrayList<>();
        }
        return dnyAttrs;
    }

    //-------------  getAttr --------------
    public DnyAttr getAttr(int attrId){
        List<DnyAttr> dnyAttrs = attrCache.get(cateId);
        if (dnyAttrs  == null) return null;

        return dnyAttrs.stream().filter(x->x.getAttrId() == attrId).findFirst().get();
    }

    public DnyAttr getAttr(String attr) {
        List<DnyAttr> dnyAttrs = attrCache.get(cateId);
        if (dnyAttrs  == null) return null;

        return dnyAttrs.stream().filter(x->x.getAttr().equals(attr)).findFirst().get();
    }

    //-------------  createDnyV --------------
    public DnyV createDnyV(String attr, String v){
        return createDnyV(getAttr(attr), v);
    }

    public DnyV createDnyV(int attrId, String v){
        return createDnyV(getAttr(attrId), v);
    }

    public DnyV createDnyV(DnyAttr attr, String v){
        DnyV dnyV = new DnyV();
        dnyV.setCateId(cateId);
        dnyV.setAttrId(attr.getAttrId());
        dnyV.setV(v);

        return dnyV;
    }


}
