package weaver.interfaces.workflow.action.javacode;

import com.api.formmode.page.util.Util;
import weaver.conn.RecordSet;
import weaver.interfaces.workflow.action.Action;
import weaver.general.BaseBean;
import weaver.soa.workflow.request.RequestInfo;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.HashSet;
import java.util.Set;

/**
 * Online custom action interface
 */
public class Action20231110054933 extends BaseBean implements Action{
    /**
     * After selecting aciton after the process path node, this method will be executed after the node is submitted.
     */
    @Override
    public String execute(RequestInfo request) {
        String requestId = request.getRequestid();
        String tablename = request.getRequestManager().getBillTableName();

        RecordSet rs = new RecordSet();
        try {
            rs.executeQuery("select MAINREQUESTID from workflow_requestbase where requestid = ?" , requestId);
            //查询主流程
            String mainrequestid = "";
            if(rs.next()){
                mainrequestid = Util.null2String(rs.getString("MAINREQUESTID"));
            }
            //查询平行流程request
            ArrayList<String> siblingRequestIds = new ArrayList<>();
            rs.executeQuery("select requestid from workflow_requestbase where MAINREQUESTID = ?" , mainrequestid);
            while(rs.next()){
                String siblingRequestId = Util.null2String(rs.getString("requestid"));
                siblingRequestIds.add(siblingRequestId);
            }
            writeLog("平行流程"+siblingRequestIds);
            //当前流程的协办部门
            rs.execute("select * from " + tablename + " where requestid =  " + requestId);
            rs.next();
            String xbbm = rs.getString("xbbm");

            //
            HashMap<String, String> xbbmMap = new HashMap<>();
            rs.execute("select * from " + tablename + " where requestid in (" + String.join(",",siblingRequestIds)+" )");
            while ( rs.next()){
                String mainid = rs.getString("id");
                String ycfxbbm = rs.getString("ycfxbbm");
                String newycfxbbm = mergeWithoutDuplicates(xbbm, ycfxbbm);
                xbbmMap.put(mainid,newycfxbbm);
            }
            writeLog("修改平行流程"+xbbmMap);
            xbbmMap.forEach((key,value) ->{
                rs.executeUpdate("update "+ tablename +" set ycfxbbm = ? where id = ?",value,key);
            });
        }catch (Exception e){
            boolean error = true;
            if (error) {
                request.getRequestManager().setMessageid("90001");
                request.getRequestManager().setMessagecontent("System Abnormal Termination Process Submission！");
            }
        }
        return Action.SUCCESS;

    }

    public static String mergeWithoutDuplicates(String a, String b) {
        Set<String> setB = new HashSet<>();
        StringBuilder result = new StringBuilder(b);

        if (b != null && !b.isEmpty()) {
            for (String id : b.split(",")) {
                setB.add(id.trim());
            }
        }

        for (String idA : a.split(",")) {
            idA = idA.trim();
            if (!setB.contains(idA)) {
                if (result.length() > 0) {
                    result.append(",");
                }
                result.append(idA);
            }
        }

        return result.toString();
    }
}
