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

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.SwEnum;
import cc.smtweb.framework.core.common.SwMap;
import cc.smtweb.framework.core.db.cache.ModelTableCache;
import cc.smtweb.framework.core.db.vo.ModelField;
import cc.smtweb.framework.core.db.vo.ModelTable;
import cc.smtweb.framework.core.mvc.service.AbstractCompService;
import cc.smtweb.framework.core.mvc.service.AbstractHandler;
import cc.smtweb.framework.core.mvc.service.DefaultDelHandler;
import cc.smtweb.framework.core.mvc.service.DefaultListHandler;
import cc.smtweb.framework.core.session.UserSession;
import cc.smtweb.system.bpm.web.design.form.ModelFormHelper;
import cc.smtweb.system.bpm.web.engine.flow.entity.Comment;
import cc.smtweb.system.bpm.web.engine.flow.entity.ProcInst;

import java.util.ArrayList;
import java.util.List;

/**
 * Created by Akmm at 2022/3/22 9:12
 */
@SwService
public class ModelProcService extends AbstractCompService {
    @Override
    protected AbstractHandler createHandler(String type) {
        switch (type) {
            case TYPE_LOAD:
                return new ModelProcLoadHandler();
            case TYPE_SAVE:
                return new ModelProcSaveHandler();
            case TYPE_DEL:
                return new DefaultDelHandler<ModelProc>(ModelProc.ENTITY_NAME);
            case TYPE_LIST:
                return new DefaultListHandler<ModelProc>(ModelProc.ENTITY_NAME);

        }
        return null;
    }

    //保存页面模型
    public R saveModel(@SwBody SwMap params, UserSession us) {
        return pageHandler(params, us, TYPE_SAVE, handler -> ((ModelProcSaveHandler) handler).saveModel());
    }

    //加载页面模型
    public R loadModel(@SwBody SwMap params, UserSession us) {
        return pageHandler(params, us, TYPE_LOAD, handler -> ((ModelProcLoadHandler) handler).loadModel());
    }

    //加载工作流的列表字段
    public R loadFlowListFields  (@SwBody SwMap params, UserSession us) {
        List<SwMap> ret = new ArrayList<>();
        ModelFormHelper.buildTableFields(ret, ProcInst.ENTITY_NAME);
        return R.success(ret);
    }
    
    public R loadFlowCardFields(@SwBody SwMap params, UserSession us) {
        List<SwMap> ret = new ArrayList<>();
        ModelFormHelper.buildTableFields(ret, ProcInst.ENTITY_NAME);
        ret.add(buildField("selTaskId", SwEnum.DataType.ID.value, "选中任务"));

        return R.success(ret);
    }

    //加载审批意见的列表字段
    public R loadCommentFields(@SwBody SwMap params, UserSession us) {
        List<SwMap> ret = new ArrayList<>();
        ModelFormHelper.buildTableFields(ret, Comment.ENTITY_NAME);
        ret.add(buildField("cmt_act_code", SwEnum.DataType.CODE.value, "活动编号"));
        ret.add(buildField("cmt_act_text", SwEnum.DataType.NAME.value, "活动名称"));
        return R.success(ret);
    }

    private SwMap buildField(String name, String dataType, String title) {
        SwMap col = new SwMap();
        col.put("name", name);
        col.put("dataType", dataType);
        col.put("title", title);
        col.put("editor", SwEnum.EditorType.INPUT.value);
        return col;
    }
}
