package com.engine.secret.util;

import com.alibaba.fastjson.JSON;
import weaver.conn.RecordSet;
import weaver.general.BaseBean;
import weaver.general.Util;
import weaver.workflow.webservices.WorkflowRequestInfo;
import weaver.workflow.webservices.WorkflowServiceImpl;

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

/**
 * @author:dxfeng
 * @createTime: 2025/04/09
 * @version: 1.0
 */
public class FlowUtil {

    /**
     * 根据流程请求ID，获取对应流程的表名
     *
     * @param requestId
     * @return
     */
    public static String getTableNameByRequestId(String requestId) {
        RecordSet rs = new RecordSet();
        rs.executeQuery("select a.tablename from workflow_bill a join workflow_base b on a.id = b.formid where b.id = ( select workflowid from workflow_requestbase where requestid = ?)", requestId);
        if (rs.next()) {
            return rs.getString("tablename");
        }
        return "";
    }


    /**
     * 获取流程的当前处理人
     *
     * @param requestId
     * @return
     */
    public static List<Integer> getCurrentUserIds(String requestId) {
        RecordSet rs = new RecordSet();
        String sql = "select * from workflow_currentoperator where isremark = '0' and usertype = 0 and requestid=? order by id asc";
        List<Integer> currentUserIds = new ArrayList();
        rs.executeQuery(sql, requestId);
        while (rs.next()) {
            currentUserIds.add(Util.getIntValue(Util.null2String(rs.getString("userid"))));
        }
        return currentUserIds;
    }


    /**
     * 提交流程到下一节点
     *
     * @param requestId
     * @param creator
     * @param opinions
     * @return
     */
    public static String submitWorkflowRequest(String requestId, int creator, String opinions) {
        BaseBean bean = new BaseBean();
        WorkflowServiceImpl workflowService = new WorkflowServiceImpl();
        WorkflowRequestInfo wri = workflowService.getWorkflowRequest(Util.getIntValue(requestId, 0), creator, 0);
        bean.writeLog("wri==" + JSON.toJSONString(wri));
        return workflowService.submitWorkflowRequest(wri, Util.getIntValue(requestId, 0), creator, "submit", opinions);
    }

    /**
     * 提交流程到下一节点(机器人节点)
     *
     * @param requestId
     * @param opinions
     * @return
     */
    public static String submitWorkflowRequest(String requestId, String opinions) {
        WorkflowServiceImpl workflowService = new WorkflowServiceImpl();
        return workflowService.submitWorkflowRequest(null, Util.getIntValue(requestId, 0), 0, "submit", opinions);
    }
}
