package com.app.hxqh.util;

import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import psdi.mbo.MboRemote;
import psdi.mbo.MboSetRemote;
import psdi.server.MXServer;
import psdi.workflow.*;

/**
 * 工作流启停工具类
 * 2023-06-07 edit
 */
public class WorkFlowUtil {

    /**
     * 启动工作流
     * @param processname 工作流程名
     * @param mbo 表名
     * @param keyValue 表ID值
     * @param key 表ID
     * @return
     */
    public static String startWF(String processname,String mbo,String keyValue,String key){
        com.alibaba.fastjson.JSONObject result = new com.alibaba.fastjson.JSONObject();
        try {
            MboSetRemote msr = MXServer.getMXServer().getMboSet(mbo, MXServer.getMXServer().getSystemUserInfo());
            msr.setWhere(key+"='"+keyValue+"'");
            msr.reset();
            if(null!=msr && !msr.isEmpty()){
                ((WorkFlowServiceRemote) MXServer.getMXServer().lookup("WORKFLOW")).initiateWorkflow(processname, msr.getMbo(0));
                result.put("code", "10000");
                result.put("msg", "success 工作流启动成功");
                result.put(key, keyValue);
            }else{
                result.put("code", "20001");
                result.put("errorMsg", "数据不存在，不能启动工作流");
                result.put(key, keyValue);
            }
        }catch (Exception e){
            result.put("code", "20001");
            result.put("errorMsg", e.getMessage());
            result.put(key, keyValue);
            e.getMessage();
            e.printStackTrace();
            e.getCause();
        }
        return result.toString();
    }

    /**
     * 停止工作流
     * @param processname 工作流程名
     * @param mbo 表名
     * @param keyValue 表ID值
     * @return
     */
    public static String stopWF(String processname,String mbo,String keyValue){
        com.alibaba.fastjson.JSONObject result = new com.alibaba.fastjson.JSONObject();
        try {
            MboSetRemote msr = MXServer.getMXServer().getMboSet("WFINSTANCE", MXServer.getMXServer().getSystemUserInfo());
            String sql="OWNERID='"+keyValue+"' and OWNERTABLE='"+mbo+"' and ACTIVE=1 ";
            msr.setWhere(sql);
            msr.reset();
            if(null!=msr && !msr.isEmpty()){
//                WFInstanceSetRemote mSet = (WFInstanceSetRemote)msr.getMbo(0).getMboSet("WFINSTANCE");
//                WFInstanceRemote wf = (WFInstanceRemote) msr.getMbo(0);
//                wf.stopWorkflow("STOP");
                msr.getMbo(0).setValue("ACTIVE", 0, 2L);
                msr.save();
                msr.close();
                result.put("code", "10000");
                result.put("msg", "success 工作流已停止");
            }else{
                result.put("code", "20001");
                result.put("errorMsg", "数据不存在，不能停止工作流");
            }
        }catch (Exception e){
            result.put("code", "20001");
            result.put("errorMsg", e.getMessage());
            e.getMessage();
            e.printStackTrace();
            e.getCause();
        }
        return result.toString();
    }

    /**
     * 发送工作流
     * @param processname 工作流程名
     * @param mboName 表名
     * @param keyValue 表ID值
     * @param key 表ID
     * @param zx
     * @param desc
     * @param loginid 登录人
     * @return
     */
    public static String wfGoOn(String processname,String mboName,String keyValue,String key ,int zx,String desc,String loginid){
        com.alibaba.fastjson.JSONObject result = new com.alibaba.fastjson.JSONObject();
        try {
            MboSetRemote wf = MXServer.getMXServer().getMboSet("wfassignment", MXServer.getMXServer().getSystemUserInfo());
            String sql="ASSIGNSTATUS='ACTIVE' and OWNERTABLE = '" + mboName + "' and OWNERID = '"+keyValue.replace(",", "") + "' and ASSIGNCODE = '" + loginid + "'";
            wf.setWhere(sql);
            wf.reset();
            if (wf.isEmpty()) {
                result.put("code", "20001");
                result.put("errorMsg","您不是当前流程任务分配人，无权限操作");
                result.put(key, keyValue);
                return result.toString();
            }

            WFAssignmentSet wfas = (WFAssignmentSet) MXServer.getMXServer().getMboSet("WFASSIGNMENT",MXServer.getMXServer().getSystemUserInfo());
            String sql2="ASSIGNCODE is not null and upper(ASSIGNCODE)= upper('"+ loginid+ "')  AND ASSIGNSTATUS = 'ACTIVE' AND upper(OWNERTABLE) = upper('" + mboName + "') AND OWNERID = " + keyValue;
            wfas.setWhere(sql2);
            wfas.reset();
            if (!wfas.isEmpty()) {
                boolean b;
                if(zx==1){
                    b=true;
                }else{
                    b=false;
                }
                int wfid = wfas.getMbo(0).getInt("wfid");
                WFAssignment wsfaa = (WFAssignment) wfas.getMbo(0);
                WorkFlowServiceRemote wfInst = (WorkFlowServiceRemote) MXServer.getMXServer().lookup("WORKFLOW");
                wfInst.completeAssignment(wsfaa, desc,b);

                MboSetRemote WFTRANSACTION = MXServer.getMXServer().getMboSet("WFTRANSACTION",MXServer.getMXServer().getSystemUserInfo());
                WFTRANSACTION.setWhere("wfid='"+ wfid+ "' and transtype='WFASSIGNCOMP' and assignid='"+wfas.getMbo(0).getString("assignid").replace(",", "")+"' ");
                WFTRANSACTION.reset();
                WFTRANSACTION.getMbo(0).setValue("personid", loginid);
                wfas.save();
                WFTRANSACTION.save();

                result.put("code", "10000");
                result.put("msg", "success 流程发送成功");
                result.put(key, keyValue);
            }else{
                result.put("code", "20002");
                result.put("errorMsg","无流程发送");
                result.put(key, keyValue);
            }

        }catch (Exception e){
            result.put("code", "20001");
            result.put("errorMsg",e.getMessage());
            result.put(key, keyValue);
            e.getMessage();
            e.printStackTrace();
            e.getCause();
        }
        return result.toString();
    }

    /**
     * 查看已分配任务的人员代码
     * @param keyValue 表ID值
     * @param key 表名
     * @return
     */
    public static com.alibaba.fastjson.JSONObject wfAssignMent(String keyValue, String key ){
        com.alibaba.fastjson.JSONArray jsonArray = new JSONArray() ;
        com.alibaba.fastjson.JSONObject result = new com.alibaba.fastjson.JSONObject();
        com.alibaba.fastjson.JSONObject person = new com.alibaba.fastjson.JSONObject();
        try {
            MboSetRemote wfAssign = MXServer.getMXServer().getMboSet("WFASSIGNMENT", MXServer.getMXServer().getSystemUserInfo());
            String sql="ownertable = '"+key+"' and wfassignment.ownerid = "+keyValue+" and assignstatus in (select value from synonymdomain where domainid='WFASGNSTATUS' and maxvalue='ACTIVE')";
            wfAssign.setWhere(sql);
            wfAssign.reset();
            if(null!=wfAssign && !wfAssign.isEmpty()){
                for(int i=0;i<wfAssign.count();i++){
                    com.alibaba.fastjson.JSONObject jsonObject  = new JSONObject();
                    String assigncode=wfAssign.getMbo(i).getString("ASSIGNCODE");
                    String displayname=wfAssign.getMbo(i).getString("ASSIGNEE.DISPLAYNAME");
                    jsonObject.put("ASSIGNCODE", assigncode);
                    jsonObject.put("DISPLAYNAME", displayname);
                    jsonArray.add(jsonObject);
                }
                person.put("person",jsonArray);
                person.put("code","10000");
                return person;
            }else{
                result.put("code", "20001");
                result.put("errorMsg","无代办任务");
                result.put(key, keyValue);
            }
        }catch (Exception e){
            result.put("code", "20001");
            result.put("errorMsg",e.getMessage());
        }
        return result;
    }

    /**
     * 查看首页收件箱任务代办
     * @param keyValue 当前登录人用户名ID
     * @return
     */
    public static com.alibaba.fastjson.JSONObject wfTaskAgent(String keyValue){
        com.alibaba.fastjson.JSONArray jsonArray = new JSONArray() ;
        com.alibaba.fastjson.JSONObject result = new com.alibaba.fastjson.JSONObject();
        com.alibaba.fastjson.JSONObject person = new com.alibaba.fastjson.JSONObject();
        try {
            MboSetRemote wfAssign = MXServer.getMXServer().getMboSet("WFASSIGNMENT", MXServer.getMXServer().getSystemUserInfo());
            String sql="WFID != 0 and ASSIGNCODE='"+keyValue+"' and ASSIGNSTATUS='ACTIVE' ";
            wfAssign.setWhere(sql);
            wfAssign.reset();
            if(null!=wfAssign && !wfAssign.isEmpty()){
                for(int i=0;i<wfAssign.count();i++){
                    com.alibaba.fastjson.JSONObject jsonObject  = new JSONObject();
                    MboRemote wfAssignMbo=wfAssign.getMbo(i);
                    jsonObject.put("WFASSIGNMENTID", wfAssignMbo.getInt("WFASSIGNMENTID")+"");
                    jsonObject.put("ASSIGNID", wfAssignMbo.getInt("ASSIGNID")+"");
                    jsonObject.put("DESCRIPTION", wfAssignMbo.getString("DESCRIPTION"));
                    jsonObject.put("ASSIGNSTATUS", wfAssignMbo.getString("ASSIGNSTATUS"));
                    jsonObject.put("OWNERTABLE", wfAssignMbo.getString("OWNERTABLE"));
                    jsonObject.put("ASSIGNCODE", wfAssignMbo.getString("ASSIGNCODE"));
                    jsonObject.put("APP", wfAssignMbo.getString("APP"));
                    jsonArray.add(jsonObject);
                }
                person.put("taskAgent",jsonArray);
                result.put("keyValue", keyValue);
                person.put("code","10000");
                return person;
            }else{
                result.put("code", "20001");
                result.put("errorMsg","无代办任务");
                result.put("keyValue", keyValue);
            }
        }catch (Exception e){
            result.put("code", "20001");
            result.put("keyValue", keyValue);
            result.put("errorMsg",e.getMessage());
        }
        return result;
    }
}
