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

import cc.smtweb.framework.core.common.SwConsts;
import cc.smtweb.framework.core.common.SwMap;
import cc.smtweb.framework.core.db.impl.DefaultEntity;
import cc.smtweb.framework.core.mvc.service.SwListData;
import cc.smtweb.system.bpm.web.design.form.define.PageDataset;
import cc.smtweb.system.bpm.web.engine.dynPage.DynRetBean;
import cc.smtweb.system.bpm.web.engine.flow.FlowHelper;
import cc.smtweb.system.bpm.web.engine.flow.FlowInstance;
import cc.smtweb.system.bpm.web.engine.flow.FlowListener;

/**
 * Created by Akmm at 2022-08-26 17:15
 * 流程加载
 */
public class FlowModelLoadHandler<T extends DefaultEntity> extends ModelLoadHandler<T> implements FlowListener {
    protected FlowInstance flowInstance;
    @Override
    protected void afterAdd(SwMap ret) {
        super.afterAdd(ret);
        flowInstance = FlowInstance.createByBillType(us, this, form.getBillType());

        bean.setEntityId(flowInstance.getProcInst().getId());
        ret.put(ModelHelper.DATASET_NAME_CARD, DynRetBean.createBean(bean));

        flowInstance.setPageRetExtra(ret);
        ret.put(FlowHelper.DATASET_NAME_FLOW, DynRetBean.createBean(flowInstance.getProcInst()));
        ret.put(FlowHelper.DATASET_NAME_COMMENT, DynRetBean.createList(SwListData.EMPTY));
    }

    @Override
    protected void load(SwMap ret, SwMap filter) {

        //过滤条件
        long id = params.readLong("id");
        if (id <= 0) id = filter.readLong(SwConsts.NAME_ID);
        long taskId = params.readLong("taskId");

        PageDataset cardDataset = findCardDataset(ModelHelper.DATASET_NAME_CARD);
        T bean = provider.loadBean(cardDataset, id);
        ret.put(cardDataset.name, DynRetBean.createBean(bean));

        //加载流程信息
        flowInstance = FlowInstance.createById(us, this, id, taskId);

        flowInstance.setPageRetExtra(ret);
        ret.put(FlowHelper.DATASET_NAME_FLOW, DynRetBean.createBean(flowInstance.getProcInst()));
        ret.put(FlowHelper.DATASET_NAME_COMMENT, DynRetBean.createList(SwListData.create(flowInstance.loadComment(), 0)));
    }
}
