package cc.smtweb.system.bpm.web.engine.model.common;

import cc.smtweb.framework.core.common.R;
import cc.smtweb.framework.core.common.SwConsts;
import cc.smtweb.framework.core.common.SwMap;
import cc.smtweb.framework.core.exception.BizException;
import cc.smtweb.framework.core.session.UserSession;
import cc.smtweb.system.bpm.web.common.BpmConst;
import cc.smtweb.system.bpm.web.design.form.ModelForm;
import cc.smtweb.system.bpm.web.design.form.ModelFormHelper;
import cc.smtweb.system.bpm.web.design.form.define.PageDataset;
import cc.smtweb.system.bpm.web.design.form.define.PageDatasets;
import cc.smtweb.system.bpm.web.engine.dynPage.DynPageListHandler;
import cc.smtweb.system.bpm.web.engine.dynPage.DynRetBean;

import static cc.smtweb.system.bpm.web.common.BpmConst.KEY_HEADER_MENU;

/**
 * Created by Akmm at 2022-09-15 15:01
 * 模型列表handler
 */
public class ModelListHandler extends DynPageListHandler {
    protected String dsName = ModelHelper.DATASET_NAME_LIST;
    private ModelListHandler(long pageId, SwMap filter, PageDataset pageDataSet) {
        super(pageId, filter, pageDataSet);
    }

    public ModelListHandler(String dsName) {
        this.dsName = dsName;
    }
    public ModelListHandler() {

    }
    @Override
    public void init(SwMap params, UserSession us) {
        super.init(params, us);
        String pId = params.readString("pageId");
        ModelForm form = ModelFormHelper.getFromCache(pId);
        if (form == null) throw new BizException("没有找到页面定义数据(" + pageId + ")！");
        pageId = form.getId();
        PageDatasets datasets = form.getDatasets();
        if (datasets == null || datasets.list == null) throw new BizException("没有找到页面定义的数据集数据(" + pageId + ")！");

        filter = params.readMap("filter");
        SwMap header = params.readMap(SwConsts.PARAMS_HEADER_KEY);
        if(header!=null){
            filter.put(BpmConst.MENU_KEY,header.get(KEY_HEADER_MENU));
        }
        pageDataSet = datasets.findByName(dsName);
    }

    @Override
    public R data() {
        DynRetBean bean = DynRetBean.createList(buildListData());
        return R.success(bean);
    }

}
