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

import cc.smtweb.framework.core.annotation.SwCache;
import cc.smtweb.framework.core.cache.AbstractCache;
import cc.smtweb.framework.core.cache.CacheManager;
import cc.smtweb.framework.core.db.DbEngine;
import cc.smtweb.framework.core.db.EntityDao;

import java.util.ArrayList;
import java.util.Comparator;
import java.util.List;
import java.util.Set;

/**
 * Created by Akmm at 2022/1/12 18:34
 */
@SwCache(ident = "ASP_MODEL_PROC", title = "流程定义")
public class ModelProcCache extends AbstractCache<ModelProc> {
    private final static String mk = "k";
    private final static String mp = "prj";
    private final static String mc = "c";

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

    public ModelProcCache() {
        regMap(mk, k -> k.getName().toUpperCase());
        regList(mp, k -> String.valueOf(k.getPrjId()));
        regList(mc, k -> String.valueOf(k.getMcId()));
    }

    @Override
    protected String getId(ModelProc bean) {
        return String.valueOf(bean.getId());
    }

    @Override
    protected List<ModelProc> loadAll() {
        EntityDao<ModelProc> dao = DbEngine.getInstance().findDao(ModelProc.class);
        return dao.query();
    }

    public final ModelProc getByName(String key) {
        return getByKey(mk, key.toUpperCase());
    }

    public final Set<ModelProc> getProcByMc(long mcId) {
        return getListByKey(mc, String.valueOf(mcId));
    }

    public final List<ModelProc> getProcByMc(long mcId, Comparator<ModelProc> comparator) {
        Set<ModelProc> set = getListByKey(mc, String.valueOf(mcId));
        if (set == null || set.isEmpty()) return null;
        List<ModelProc> list = new ArrayList<>(set);
        list.sort(comparator);
        return list;
    }
}
