package cc.smtweb.system.bpm.web.sys.user.role;

import cc.smtweb.framework.core.db.impl.BaseBean;

import java.util.*;
import java.util.stream.Collectors;

/**
 * @Author: tanghp
 * @Date: 2022-08-02 12:52
 * @Desc:
 */
public class RoleRight extends BaseBean {
    /**
     * 主键
     */
    public long getId() {
        return getLong("id");
    }

    /**
     * 主键
     */
    public void setId(long id) {
        put("id", id);
    }


    /**
     * 菜单ID
     */
    public long getMenuId() {
        return getLong("menuId");
    }

    /**
     * 菜单ID
     */
    public void setMenuId(long menuId) {
        put("menuId", menuId);
    }

    /**
     * 菜单权限
     */
    public int getMenu() {
        return getInt("menu");
    }

    /**
     * 菜单权限
     */
    public void setMenu(int menu) {
        put("menu", menu);
    }

    /**
     * 功能权限
     */
    public Set<String> getRightFunc() {
        return (Set<String>) get("func");
    }

    /**
     * 功能权限
     */
    public void setFunc(Set<String> func) {
        put("func", func);
    }

    /**
     * [{"key": "权限定义id","value": "权限组id"}]
     * 数据权限
     */
    public List<Map<String, Object>> getRightData() {
        return (List<Map<String, Object>>) get("data");
    }

    /**
     * [{"key": "权限定义id","value": "权限组id"}]
     * 数据权限
     */
    public void setData(List<Map<String, Object>> data) {
        put("data", data.stream().filter(row -> data.contains("value")).collect(Collectors.toList()));
    }

    /**
     * "0-无/1-读/2-写"
     * 字段权限  {field1:0,field2:2}
     */
    public Map<String, Integer> getRightField() {
        return (Map<String, Integer>) get("field");
    }

    /**
     * "0-无/1-读/2-写"
     * 字段权限  {field1:0,field2:2}
     */
    public void setField(Map<String, Integer> field) {
        put("field", field);
    }


    public void setField(Set<String> as, Set<String> ws, Set<String> rs) {
        Map<String, Integer> field = new HashMap<>();
        if (ws == null) ws = new HashSet<>();
        if (rs == null) rs = new HashSet<>();
        as.removeAll(ws);
        as.removeAll(rs);
        rs.removeAll(ws);
        ws.forEach(k -> {
            field.put(k, 2);
        });
        rs.forEach(k -> {
            field.put(k, 1);
        });
        as.forEach(k -> {
            field.put(k, 0);
        });
        setField(field);
    }
}
