package org.springblade.flow.engine.util;

import java.util.Map;

import liquibase.pro.packaged.S;
import org.springblade.flow.engine.model.flowengine.FlowModel;
import org.springblade.flow.engine.service.FlowTaskNewService;
import org.springblade.flow.exception.WorkFlowException;
import org.springblade.flow.util.JsonUtil;
import org.springblade.flow.util.context.SpringContext;

/**
 * 工作流
 *
 * @author JNPF开发平台组
 * @version V3.1.0
 * @copyright 引迈信息技术有限公司
 * @date 2021/5/24 9:19
 */
public class ModelUtil {

    public static void save(String id, String flowId, String processId, String flowTitle, int flowUrgent, String billNo, Object formEntity) throws WorkFlowException {
        FlowTaskNewService flowTaskNewService = SpringContext.getBean(FlowTaskNewService.class);
        FlowModel flowModel = new FlowModel();
        flowModel.setId(id);
        flowModel.setFlowId(flowId);
        flowModel.setProcessId(processId);
        flowModel.setFlowTitle(flowTitle);
        flowModel.setFlowUrgent(flowUrgent);
        flowModel.setBillNo(billNo);
        Map<String, Object> data = JsonUtil.entityToMap(formEntity);
        flowModel.setFormData(data);
        flowModel.setParentId("0");
        flowTaskNewService.saveIsAdmin(flowModel);
    }

    public static void submit(String id, String flowId, String processId, String flowTitle, int flowUrgent, String billNo, Object formEntity, String freeApproverUserId) throws WorkFlowException {
        submit(id, flowId, processId, flowTitle, flowUrgent, billNo, formEntity, freeApproverUserId, null, null);
    }

    public static void submit(String id, String flowId, String processId, String flowTitle, int flowUrgent, String billNo,Object formEntity, String freeApproverUserId, String creatorId, String creatorName) throws WorkFlowException {
        FlowTaskNewService flowTaskNewService = SpringContext.getBean(FlowTaskNewService.class);
        FlowModel flowModel = new FlowModel();
        flowModel.setId(id);
        flowModel.setFlowId(flowId);
        flowModel.setProcessId(processId);
        flowModel.setFlowTitle(flowTitle);
        flowModel.setFlowUrgent(flowUrgent);
        flowModel.setBillNo(billNo);
        Map<String, Object> data = JsonUtil.entityToMap(formEntity);
        flowModel.setFormData(data);
        flowModel.setFormData(data);
        flowModel.setFreeApproverUserId(freeApproverUserId);
        flowModel.setParentId("0");
        flowModel.setUserId(creatorId);
        flowModel.setUserName(creatorName);
        flowTaskNewService.submit(flowModel);
    }

}
