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_PARTY)】的缓存类
 */
@SwCache(ident = "SYS_USER_PARTY", title = "用户机构关系")
public class UserPartyCache extends AbstractEntityCache<UserParty> {
    //缓存key：按用户缓存
    public final static String mk_u = "u";
    //缓存key：按部门缓存
    public final static String mk_d = "d";
    //缓存key：按机构缓存
    public final static String mk_p = "p";

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

    public UserPartyCache() {
        //缓存key：按用户缓存
        regList(mk_u, "sup_user_id");
        regList(mk_d, "sup_dept_id");
        regList(mk_p, "sup_party_id");
    }

    //缓存key：按用户缓存
    public final Set<UserParty> getByUser(String key) {
        return getListByKey(mk_u, key);
    }

    public final Set<UserParty> getByDept(String key) {
        return getListByKey(mk_d, key);
    }

    public final Set<UserParty> getByParty(String key) {
        return getListByKey(mk_p, key);
    }

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


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

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