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

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.common.SwConsts;
import cc.smtweb.framework.core.db.DbEngine;
import cc.smtweb.framework.core.db.EntityDao;

import java.util.*;

/**
 * Created by Akmm at 2022/1/12 18:34
 */
@SwCache(ident = "ASP_MODEL_FORM", title = "页面定义")
public class ModelFormCache extends AbstractEntityCache<ModelForm> {
    private final static String mk = "k";
    private final static String mp = "prj";
    private final static String mc = "c";
    private final static String mt = "t";
    private final static String ms = "s";
    private final static String mn = "mn";

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

    public ModelFormCache() {
        regMap(mk, k -> k.getName().toUpperCase());
        regMap(mn, k -> ModelFormHelper.getPageName(k).toUpperCase());
        regList(mp, k -> String.valueOf(k.getPrjId()));
        regList(mc, k -> String.valueOf(k.getMcId()));
        regList(ms, k -> String.valueOf(k.getParent()));
        regList(mt, k -> k.getMasterTableId() + SwConsts.SPLIT_CHAR + k.getType());
//        regList(mf, k-> k.get);
    }

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

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

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

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

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

    public final Set<ModelForm> getFormsByPrj(long mcId) {
        return getListByKey(mp, String.valueOf(mcId));
    }

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

    //根据表id，找对应的页面设计
    public Set<ModelForm> getListByTable(long tableId, int type) {
        return getListByKey(mt, tableId + SwConsts.SPLIT_CHAR + type);
    }

    //根据模块id，找对应的页面设计
    public Set<ModelForm> getListByModule(long parent) {
        return getListByKey(ms, String.valueOf(parent));
    }

    public String getText(long id) {
        ModelForm form = get(id);
        return form != null ? form.getTitle(): String.valueOf(id);
    }

    /**
     * 根据末模块名称和页面名称获取 页面模型
     *
     * @param module
     * @param pageName
     * @return ModelForm
     */
    public ModelForm getByPageName(String pageName) {
        return getByKey(mn, pageName.toUpperCase());
    }
    // 获取包含本页面的模块下的所有页面
    public Set<ModelForm> getIncPage(long pageId){
        ModelForm form = get(pageId);
        if(form==null || form.getParent()<=0L){
            return  null;
        }
        return getListByModule(form.getParent());
    }
}
