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

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 cc.smtweb.system.bpm.web.sys.user.party.Party;
import cc.smtweb.system.bpm.web.sys.user.party.PartyCache;

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

/**
 * Created by 1 at 2022-07-27 09:35:05
 * 实体【[部门表](SYS_DEPT)】的缓存类
 */
@SwCache(ident = "SYS_DEPT", title = "部门表")
public class DeptCache extends AbstractEntityCache<Dept> {
    //缓存key：按编码
    public final static String mk_code = "code";
    //缓存key：按机构
    public final static String mk_p = "p";

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

    public DeptCache() {
        //缓存key：按编码
        regMap(mk_code, "dpt_code");
        //缓存key：按机构
        regList(mk_p, "dpt_pt_id");
    }

    //缓存key：按编码
    public final Dept getByCode(String key) {
        return getByKey(mk_code, key);
    }

    //缓存key：按机构
    public final Set<Dept> getByP(String key) {
        return getListByKey(mk_p, key);
    }

    public final String getNameById(Long id) {
        Dept dept = get(id);
        if (dept == null) return "";
        return dept.getName();
    }

    /**
     * @Author yaoq
     * @Date 2022/9/22 17:08
     * @Params
     * @Return java.util.Set<cc.smtweb.system.bpm.web.sys.user.dept.Dept>
     * @Description 根据单位获取，该单位级下级单位所有部门集合
     */
    public final Set<Dept> getAllByPartyEx(long party_id) {
        Set<Dept> set = getByP(String.valueOf(party_id));
        if (set == null) set = new HashSet<>();
        Set<Party> child = PartyCache.getInstance().getAllChildren(party_id);
        for (Party party : child) {
            Set<Dept> depts = getByP(String.valueOf(party.getId()));
            if (CommUtil.isEmpty(depts)) continue;
            set.addAll(depts);
        }
        return set;
    }
}
