package weaver.interfaces.shuzhi.util;

import weaver.general.BaseBean;
import weaver.general.Util;
import weaver.hrm.resource.ResourceComInfo;
import weaver.soa.workflow.request.MainTableInfo;
import weaver.soa.workflow.request.Property;
import weaver.soa.workflow.request.RequestInfo;
import weaver.soa.workflow.request.RequestService;
import weaver.workflow.request.RequestComInfo;
import weaver.workflow.workflow.WorkflowComInfo;

import java.util.Map;

/**
 * 类的详细说明
 *
 * @author wangj
 * @version 1.00版本
 * @Date 2023/7/3
 */

public class WorkflowUtil extends BaseBean {
    /**
     * @param @param  creatorId 创建⼈Id
     * @param @param  workflowId 流程Id
     * @param @param  requestName 请求标题
     * @param @param  IsNextFlow 是否提交到下⼀节点
     * @param @param  requestMap Map格式的主表数据<fieldname,fieldvalue>=<字段名称,字段值>
     * @param @return 设定⽂件
     * @return String 返回类型
     * @throws
     * @Title: WorkflowCreateByMainTableMap
     */
    public String WorkflowCreateByMainTableMap(String creatorId, String workflowId, String requestName, String IsNextFlow, Map<String, String> requestMainMap) {
        writeLog("-----创建流程传递的参数个数--------");
        writeLog("creatorId:" + creatorId);
        writeLog("workflowId:" + workflowId);
        writeLog("requestName:" + requestName);
        writeLog("IsNextFlow:" + IsNextFlow);
        String requestid = "";
        RequestInfo requestInfo = new RequestInfo();
        requestInfo.setCreatorid(creatorId);//创建⼈Id
        requestInfo.setWorkflowid(workflowId);//⼯作流Id
        requestInfo.setDescription(requestName);//请求标题
        if (!"".equals(IsNextFlow)) {
            requestInfo.setIsNextFlow(IsNextFlow);
        }
        //主表字段
        MainTableInfo mainTableInfo = new MainTableInfo();
        Property[] propertyArray = new Property[requestMainMap.size()];
        int p = 0;
        for (Map.Entry<String, String> entry : requestMainMap.entrySet()) {
            propertyArray[p] = new Property();
            propertyArray[p].setName(Util.null2String(entry.getKey()));
            //2、其他接⼝
            propertyArray[p].setValue(Util.null2String(entry.getValue()));
            writeLog("\r\n---p=" + p + "---Key=" + entry.getKey() + "---Value = " + entry.getValue());
            p++;
        }
        writeLog("-----创建流程传递的参数个数p=" + p);
        mainTableInfo.setProperty(propertyArray);
        requestInfo.setMainTableInfo(mainTableInfo);
        RequestService service = new RequestService();
        try {
//流程
            try {
                requestid = service.createRequest(requestInfo);//创建请求id
            } catch (Exception e) {
                writeLog("=======" + e.getMessage());
            }
//String userId = requestInfo.getLastoperator();//请求最后的操作者
//记录⽇志
            StringBuffer sbf = new StringBuffer("\r\n-----xwd创建⼯作流记录⽇志开始");
            WorkflowComInfo wfcif = new WorkflowComInfo();
            RequestComInfo rcif = new RequestComInfo();
            ResourceComInfo rscif = new ResourceComInfo();
            sbf.append("\r\n-----姓名:" + rscif.getLastname(rcif.getRequestCreater(requestid)));
            sbf.append("\r\n-----时间:" + rcif.getRequestCreateTime(requestid));
            sbf.append("\r\n-----创建流程:" + wfcif.getWorkflowname(workflowId));
            sbf.append("\r\n-----请求:" + rcif.getRequestname(requestid));
            sbf.append("\r\n-----请求:" + requestid);
            sbf.append("\r\n-----创建⼯作流记录⽇志结束");
            writeLog(sbf.toString());
            System.out.println("" + sbf.toString());
        } catch (Exception e) {
            writeLog("错误：" + e);
            System.out.println("错误：" + e);
        }
        return requestid;
    }
}
