package cc.smtweb.system.bpm.web.design.db;

import cc.smtweb.framework.core.common.SwConsts;
import cc.smtweb.framework.core.db.vo.ModelCatalog;
import cc.smtweb.framework.core.mvc.service.TreeHelper;

import java.util.*;

/**
 * Created by Akmm at 2022/3/25 10:47
 */
public class ModelCatalogTreeHelper extends TreeHelper<ModelCatalog> {
    //按项目的缓存key，确保此表缓存配置：prj：prjid_parentid
    private final static String CACHE_KEY = "prj";

    public ModelCatalogTreeHelper() {
        super(ModelCatalog.ENTITY_NAME);
    }

    public List<ModelCatalog> getChildren(long id, long prj_id, boolean exc_depend, Comparator<ModelCatalog> comparator) {
        if (id > 0L) {
            return getChildren(id, comparator);
        }
        Set<Long> setId = exc_depend ? new HashSet<>(Arrays.asList(prj_id)): ModelProjectCache.getInstance().getDependsPrjIds(prj_id);
        if (setId.isEmpty()) return new ArrayList<>();
        Set<ModelCatalog> set = new HashSet<>();

        for (long prjId : setId) {
            Collection<ModelCatalog> st = cache.getListByKey(CACHE_KEY, prjId + SwConsts.SPLIT_CHAR + SwConsts.DEF_ROOT_ID);
            if (st != null) set.addAll(st);
        }

        List<ModelCatalog> list = new ArrayList<>(set);
        if (comparator != null)
            list.sort(comparator);
        return list;
    }


}
