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

import cc.smtweb.framework.core.common.R;
import cc.smtweb.framework.core.common.SwMap;
import cc.smtweb.framework.core.db.DbEngine;
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.AbstractDynPageHandler;
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-09-01 15:03
 * 流程流转
 */
public class FlowTransHandler<T extends DefaultEntity> extends AbstractDynPageHandler implements FlowListener {
    //流程id
    protected long billId;
    //任务id
    protected long taskId;
    protected FlowInstance flowInstance;

    protected T bean;

    @Override
    public void setExprParam(SwMap param) {
        param.putAll(bean.getData());
    }

    private SwMap buildRetData() {
        SwMap ret = new SwMap();
        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));
        return ret;
    }

    protected void load() {
        if (flowInstance == null) {
            billId = params.readLong("id");
            taskId = params.readLong("taskId");
            flowInstance = FlowInstance.createById(us, this, billId, taskId);

            loadBean();
        }
    }

    protected void loadBean() {
        PageDataset cardDataset = findCardDataset(ModelHelper.DATASET_NAME_CARD);
        bean = provider.loadBean(cardDataset, billId);
    }

    public R submit() {
        load();
        DbEngine.getInstance().doTrans(() -> flowInstance.submit(params.readString("comment")));

        return R.success(buildRetData());
    }

    public R retake() {
        load();
        DbEngine.getInstance().doTrans(() -> flowInstance.retake());

        return R.success(buildRetData());
    }

    public R reject() {
        load();
        DbEngine.getInstance().doTrans(() -> flowInstance.reject(params.readString("comment"), params.readString("toTaskId")));

        return R.success(buildRetData());
    }

    public R disuse() {
        load();
        DbEngine.getInstance().doTrans(() -> flowInstance.disuse(params.readString("comment")));

        return R.success(buildRetData());
    }

    public R handle() {
        load();
        DbEngine.getInstance().doTrans(() -> flowInstance.handle());

        return R.success(buildRetData());
    }

    public R interrupt() {
        load();
        DbEngine.getInstance().doTrans(() -> flowInstance.interrupt(params.readString("comment")));

        return R.success(buildRetData());
    }
}
