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

import cc.smtweb.framework.core.annotation.SwCache;
import cc.smtweb.framework.core.cache.AbstractEntityCache;
import cc.smtweb.framework.core.cache.CacheManager;
import cc.smtweb.framework.core.util.CommUtil;

import java.util.HashSet;
import java.util.Set;

/**
 * Created by 1 at 2022-07-29 09:25:43
 * 实体【[用户角色关系](SYS_USER_ROLE)】的缓存类
 */
@SwCache(ident = "SYS_USER_ROLE", title = "用户角色关系")
public class UserRoleCache extends AbstractEntityCache<UserRole> {
    //缓存key：按机构关系缓存
    public final static String mk_s = "s";

    public static UserRoleCache getInstance() {
        return CacheManager.getIntance().getCache(UserRoleCache.class);
    }

    public UserRoleCache() {
        //缓存key：按机构关系缓存
        regList(mk_s, "sur_sup_id");
    }

    //缓存key：按机构关系缓存
    public final Set<UserRole> getByParty(String key) {
        return getListByKey(mk_s, key);
    }

    public final Set<Long> getByParty(Long key) {
        Set<Long> ids = new HashSet<>();
        Set<UserRole> set = getByParty(String.valueOf(key));
        if (CommUtil.isEmpty(set)) return ids;
        set.forEach(bean -> {
            ids.add(bean.getEntityId());
        });
        return ids;
    }

    public final Set<Long> getByParty(Set<Long> key) {
        Set<Long> ids = new HashSet<>();
        for (Long k : key) ids.addAll(getByParty(k));
        return ids;
    }

    public final Set<Long> getRoleByParty(Long key) {
        Set<Long> ids = new HashSet<>();
        Set<UserRole> set = getByParty(String.valueOf(key));
        if (CommUtil.isEmpty(set)) return ids;
        set.forEach(bean -> {
            ids.add(bean.getRoleId());
        });
        return ids;
    }

    public final Set<Long> getRoleByParty(Set<Long> key) {
        Set<Long> ids = new HashSet<>();
        for (Long k : key) ids.addAll(getRoleByParty(k));
        return ids;
    }

    public final Set<Long> getRoleIdByUser(Long key) {
        Set<Long> roleSet = new HashSet<>();
        Set<UserRole> userRoles = getSetByUser(String.valueOf(key));
        for (UserRole userRole : userRoles) {
            roleSet.add(userRole.getRoleId());
        }
        return roleSet;
    }

    public final Set<Long> getRoleIdByUP(Long user_id, Long party_id) {
        Set<Long> ids = new HashSet<>();
        Set<UserParty> upSet = UserPartyCache.getInstance().getListByKey(UserPartyCache.mk_u, String.valueOf(user_id));
        if (CommUtil.isEmpty(upSet)) return ids;
        upSet.forEach(up -> {
            if (up.getPartyId() != party_id) return;
            Set<UserRole> urSet = getListByKey(mk_s, String.valueOf(up.getId()));
            if (CommUtil.isEmpty(urSet)) return;
            for (UserRole userRole : urSet) {
                ids.add(userRole.getRoleId());
            }
        });
        return ids;
    }

    public final Set<Long> getIdByUser(Long key) {
        return getIdByUser(String.valueOf(key));
    }

    public final Set<Long> getIdByUser(String key) {
        Set<Long> ids = new HashSet<>();
        Set<Long> upSet = UserPartyCache.getInstance().getListIdByKey(UserPartyCache.mk_u, key);
        if (CommUtil.isEmpty(upSet)) return ids;
        upSet.forEach(k -> {
            Set<UserRole> urSet = getListByKey(mk_s, String.valueOf(k));
            if (CommUtil.isEmpty(urSet)) return;
            for (UserRole userRole : urSet) {
                ids.add(userRole.getEntityId());
            }
        });
        return ids;
    }

    public final Set<UserRole> getSetByUser(String key) {
        Set<UserRole> set = new HashSet<>();
        getIdByUser(key).forEach(k -> {
            set.add(get(k));
        });
        return set;
    }
}
