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

import cc.smtweb.framework.core.common.SwMap;
import cc.smtweb.framework.core.exception.BizException;
import cc.smtweb.framework.core.util.CommUtil;
import cc.smtweb.framework.core.util.JsonUtil;
import cc.smtweb.framework.core.util.StringUtil;

import java.util.*;

/**
 * @Author: tanghp
 * @Date: 2022-08-02 13:16
 * @Desc: 菜单方案content字段处理类
 */
public class RoleRightContent {

    private Map<Long, RoleRight> map = new HashMap<>();

    public RoleRightContent(String content) {
        List<RoleRight> tempList = JsonUtil.parseList(content, RoleRight.class);
        if (StringUtil.isNotEmpty(content) && tempList == null) {
            throw new BizException("角色权限转换失败！");
        }
        if (CommUtil.isEmpty(tempList)) return;
        tempList.forEach(bean -> {
            map.put(bean.getId(), bean);
        });
    }

    public void saveItem(SwMap map) {
        RoleRight right = new RoleRight();
    }

    public void resetItem() {
        map.values().forEach(roleRight -> {
            roleRight.setMenu(0);
        });
    }

    // 保存节点
    public void saveItem(RoleRight item) {
        map.put(item.getId(), item);
    }

    // 删除节点
    public void delItem(long id) {
        map.remove(id);
    }

    // 转换成字符串
    public String toContentStr() {
        return JsonUtil.encodeString(new ArrayList<>(map.values()));
    }


    public RoleRight getRoleRight(Long key) {
        return map.get(key);
    }

    public String getFuncRight(long key) {
        RoleRight right = getRoleRight(key);
        if (right == null) return "";
        return StringUtil.join(right.getRightFunc(), ",");
    }

    public String getMenuRight() {
        if (map.size() == 0) return "";
        Set<String> menus = new HashSet<>();
        map.values().forEach(roleRight -> {
            if (roleRight.getMenu() == 1) menus.add(roleRight.getId() + "");
        });
        return StringUtil.join(menus, ",");
    }

    public Set<Long> getMenuRightIds() {
        Set<Long> ids = new HashSet<>();
        if (map.size() == 0) return ids;
        map.values().forEach(roleRight -> {
            if (roleRight.getMenu() == 1) ids.add(roleRight.getId());
        });
        return ids;
    }

    public String getFieldRight(long key, int type) {
        RoleRight right = getRoleRight(key);
        if (right == null) return "";
        Map<String, Integer> cache = right.getRightField();
        if (cache == null || cache.size() == 0) return "";
        Set<String> fields = new HashSet<>();
        cache.forEach((k, v) -> {
            if (v == type) fields.add(k);
        });
        return StringUtil.join(fields, ",");
    }

    /**
     * @Author yaoq
     * @Date 2022/9/23 17:53
     * @Params
     * @Return java.util.Map<java.lang.String, java.util.Map < java.lang.String, java.lang.Object>>
     * @Description 根据菜单方案明细ID 获取 数据权限值：Map<权限类型，<Key,Value>>
     */
    public Map<String, Map<String, Object>> getDataRight(long key) {
        RoleRight right = getRoleRight(key);
        if (right == null) return new HashMap<>();
        List<Map<String, Object>> list = right.getRightData();
        if (list == null || list.size() == 0) return new HashMap<>();
        Map<String, Map<String, Object>> cache = new HashMap<>();
        list.forEach(k -> {
            cache.put(k.get("key").toString(), k);
        });
        return cache;
    }

    /**
     * 获取菜单的数据权限组ID
     * @param menuId 菜单明细id
     * @param dataRightId 数据权限定义id
     * @return 数据权限组ID
     */
    public long getDataRightGroupId(long menuId, long dataRightId){
        RoleRight right = getRoleRight(menuId);
        if(right ==null) return 0L;
        List<Map<String, Object>> list = right.getRightData();
        if(CommUtil.isEmpty(list))return 0L;
        for(Map<String,Object> map :list){
            SwMap swMap = new SwMap();
            swMap.putAll(map);
            long key = swMap.readLong("key");
            if(key ==dataRightId){
              return  swMap.readLong("value");
            }
        }
        return 0L;
    }
}
