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

import cc.smtweb.framework.core.annotation.SwBody;
import cc.smtweb.framework.core.annotation.SwService;
import cc.smtweb.framework.core.common.R;
import cc.smtweb.framework.core.common.SwMap;
import cc.smtweb.framework.core.mvc.service.AbstractHandler;
import cc.smtweb.framework.core.mvc.service.IWorker;
import cc.smtweb.framework.core.session.UserSession;
import cc.smtweb.system.bpm.web.engine.model.listcard.single.LCSingleService;

/**
 * Created by Akmm at 2022-08-26 16:35
 * 带工作流的简单列表+卡片
 */
@SwService
public class FlowModelService extends LCSingleService {
    public static final String TYPE_FLOW = "flow";

    @Override
    protected AbstractHandler createHandler(String type) {
        switch (type) {
            case TYPE_MODEL_LIST:
                return new FlowModelListHandler();
            case TYPE_MODEL_LOAD:
                return new FlowModelLoadHandler<>();
            case TYPE_MODEL_SAVE:
                return new FlowModelSaveHandler<>();
            case TYPE_MODEL_DEL:
                return new FlowModelDelHandler();
            case TYPE_FLOW:
                return new FlowTransHandler();
        }
        return super.createHandler(type);
    }
    protected R flowHandler(SwMap params, UserSession us, IWorker<FlowTransHandler> worker) {
        try {
            FlowTransHandler handler = (FlowTransHandler) getHandler(params, us, TYPE_FLOW);
            return worker.doWork(handler);
        } catch (Exception e) {
            return R.error("操作失败！", e);
        }
    }

    public R submit(@SwBody SwMap params, UserSession us) {
        return flowHandler(params, us, FlowTransHandler::submit);
    }

    public R retake(@SwBody SwMap params, UserSession us) {
        return flowHandler(params, us, FlowTransHandler::retake);
    }

    public R reject(@SwBody SwMap params, UserSession us) {
        return flowHandler(params, us, FlowTransHandler::reject);
    }

    public R disuse(@SwBody SwMap params, UserSession us) {
        return flowHandler(params, us, FlowTransHandler::disuse);
    }

    public R handle(@SwBody SwMap params, UserSession us) {
        return flowHandler(params, us, FlowTransHandler::handle);
    }

    public R interrupt(@SwBody SwMap params, UserSession us) {
        return flowHandler(params, us, FlowTransHandler::interrupt);
    }
}
