package com.engine.htxc.jzxtpqs.cmd;

import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.engine.common.biz.AbstractCommonCommand;
import com.engine.common.entity.BizLogContext;
import com.engine.common.util.ServiceUtil;
import com.engine.core.interceptor.CommandContext;
import com.engine.workflow.service.RequestFreeFlowService;
import com.engine.workflow.service.impl.RequestFreeFlowServiceImpl;
import weaver.conn.RecordSet;
import weaver.formmode.setup.ModeRightInfo;
import weaver.general.BaseBean;
import weaver.general.Util;
import weaver.hrm.User;
import weaver.hrm.company.DepartmentComInfo;
import weaver.interfaces.htsc.comInfo.PropBean;

import java.util.HashMap;
import java.util.Map;

public class JzxtpqsFreeNodeCmd extends AbstractCommonCommand<Map<String, Object>> {

    public JzxtpqsFreeNodeCmd(User user, Map<String, Object> params) {
        this.params = params;
        this.user = user;
    }

    @Override
    public BizLogContext getLogContext() {
        return null;
    }

    @Override
    public Map<String, Object> execute(CommandContext commandContext) {
        DepartmentComInfo departmentComInfo = null;

        RecordSet rs = new RecordSet();
        BaseBean bb = new BaseBean();
        bb.writeLog( "in=================ItlxlcFreeNodeCmd" );
        Map<String, Object> apimap = new HashMap<String, Object>();
        try{
            PropBean pb = new PropBean();
            departmentComInfo = new DepartmentComInfo();
            int requestid = Util.getIntValue(Util.null2String(params.get("requestid")));
            int workflowid = Util.getIntValue(Util.null2String(params.get("workflowid")));
            int nodeid = Util.getIntValue(Util.null2String(params.get("nodeid")));

            int xybzvalue = Util.getIntValue(Util.null2String(params.get("xybzvalue")));
            String jdfl = "";
            if (xybzvalue == 0) {
                jdfl = "6";
            } else if (xybzvalue == 1) {
                jdfl = "11";
            }

            String jsonArr = Util.null2String(Util.null2String(params.get("jsonArr")));
            bb.writeLog("jsonArr:"+jsonArr);
            JSONArray jsonArray = JSONArray.parseArray( jsonArr );
            JSONObject freeNode = new JSONObject();
            JSONArray nodeData = new JSONArray();
            String bmfzrs = "";
            String zdrys = "";
            for (int i = 0; i < jsonArray.size(); i++) {
                String hqbmvalue = Util.null2String( jsonArray.getJSONObject( i ).get( "hqbmvalue" ) );
                String zdry = Util.null2String( jsonArray.getJSONObject( i ).get( "zdry" ) );
                String bmfzr = Util.null2String( jsonArray.getJSONObject( i ).get( "bmfzr" ) );
                bb.writeLog("zdry:"+zdry);
                bb.writeLog("bmfzr:"+bmfzr);
                String sfhq = Util.null2String( jsonArray.getJSONObject( i ).get( "sfhq" ) );
                if ("1".equals( sfhq )) {
                    JSONArray nodeArray = new JSONArray();
                    if (!"".equals( zdry ) && !"".equals( bmfzr )) {
                        JSONObject nodeDataJson1 = new JSONObject();
                        if(xybzvalue==0){
                            nodeDataJson1.put( "nodename", "指定人员" );
                            nodeDataJson1.put( "operators", zdry );
                            nodeDataJson1.put( "signType", 1 );  //会签方式,(0:非会签、1:会签、2:依次逐个签)	//20201226
                        }
                        if(xybzvalue==1){
                            nodeDataJson1.put( "nodename", "跨公司会签" );
                            nodeDataJson1.put( "operators", zdry );
                            nodeDataJson1.put( "signType", Util.getIntValue( sfhq ) );  //会签方式,(0:非会签、1:会签、2:依次逐个签)
                        }

                        nodeArray.add( nodeDataJson1 );
                        JSONObject nodeDataJson2 = new JSONObject();
                        if(xybzvalue == 0){
                            nodeDataJson2.put( "nodename", "会签部门负责人" );
                            String deptcode = departmentComInfo.getDepartmentCode(hqbmvalue);
                            bb.writeLog("deptcode:"+deptcode);
                            // ZZ001014
                            if("ZZ001014".equals(deptcode)){
                                nodeDataJson2.put( "signType", "0" );  //20201226
                            }else{
                                nodeDataJson2.put( "signType", "1" );  //20201226
                            }
                        }
                        if(xybzvalue==1){
                            nodeDataJson2.put( "nodename", "跨公司会签" );
                            nodeDataJson2.put( "signType", Util.getIntValue( sfhq ) );
                        }

                        nodeDataJson2.put( "operators", bmfzr );
                        nodeArray.add( nodeDataJson2 );

                    } else if (!"".equals( zdry ) && "".equals( bmfzr )) {
                        JSONObject nodeDataJson1 = new JSONObject();
                        if(xybzvalue==0){
                            nodeDataJson1.put( "nodename", "指定人员" );
                            nodeDataJson1.put( "operators", zdry );
                            nodeDataJson1.put( "signType", 1 );
                        }
                        if(xybzvalue==1){
                            nodeDataJson1.put( "nodename", "跨公司会签" );
                            nodeDataJson1.put( "operators", zdry );
                            nodeDataJson1.put( "signType", Util.getIntValue( sfhq ) );
                        }
                        nodeArray.add( nodeDataJson1 );

                    } else if ("".equals( zdry ) && !"".equals( bmfzr )) {
                        JSONObject nodeDataJson2 = new JSONObject();
                        if(xybzvalue==0){
                            nodeDataJson2.put( "nodename", "会签部门负责人" );
                            String deptcode = departmentComInfo.getDepartmentCode(hqbmvalue);
                            bb.writeLog("deptcode:"+deptcode);
                            // ZZ001014
                            if("ZZ001014".equals(deptcode)){
                                nodeDataJson2.put( "signType", "0" );  //20201226
                            }else{
                                nodeDataJson2.put( "signType", "1" );  //20201226
                            }
                        }

                        if(xybzvalue==1){
                            nodeDataJson2.put( "nodename", "跨公司会签" );
                            nodeDataJson2.put( "signType", Util.getIntValue( sfhq ) );
                        }
                        nodeDataJson2.put( "operators", bmfzr );
                        nodeArray.add( nodeDataJson2 );
                    }
                    nodeData.add( nodeArray );
                    freeNode.put( "nodeData", nodeData );
                    freeNode.put( "mergeType", 1 );   //合并方式，（1:按比例方式通过，2:按分支数通过）
                    freeNode.put( "mergeValue", 100 );
                    bmfzrs += bmfzrs == "" ? "'" + bmfzr + "'" + "" : "," + "'" + bmfzr + "'";
                    zdrys += zdrys == "" ? "'" + zdry + "'" + "" : "," + "'" + zdry + "'";
                }
            }

            int userid = user.getUID();
            // 自由节点参数
            Map<String, Object> params = new HashMap<String, Object>();
            params.put( "requestid", requestid );
            params.put( "nodeid", nodeid );
            params.put( "workflowid", workflowid );
            params.put( "nodetype", 2 );
            params.put( "nextNodeEditPurview", 2 );
            params.put( "allowModify", 1 );
            params.put( "isFlowToNext", 0 );
            params.put( "isAddToParentGroup", 1 );
            params.put( "isParallel", 0 );
            params.put( "freeNodeData", freeNode );
            if (freeNode.size() > 0) {
                bb.writeLog( "=================params:" + params.toString() );

                //生产自由节点
                RequestFreeFlowService freeFlowService = ServiceUtil.getService( RequestFreeFlowServiceImpl.class, user );
                freeFlowService.generateFreeNode( params );
                int formmodeid = 0 ;
                rs.executeQuery( "select m.id from modeinfo m left join WORKFLOW_BILL b on m.formid = b.id where b.tablename = 'uf_qzyjhqb'" );
                if(rs.next()){
                    formmodeid = Util.getIntValue( rs.getString( "id" ) );
                }


                String freenode = "";
                rs.executeQuery( "select id from workflow_freenode where requestid ='" + requestid + "'" );
                while (rs.next()) {
                    String id = Util.null2String(rs.getString("id"));
                    freenode += freenode==""? id : ","+id ;//自由节点
                }

                String ufFreenode = "";
                rs.executeQuery( " select freenode as id from uf_qzyjhqb where lcid ='" + requestid + "'" );
                while(rs.next()) {
                    String id = Util.null2String(rs.getString("id"));
                    ufFreenode += ufFreenode==""? id : ","+id ;//自由节点
                }

                String bmfzrNode= "";
                String zdryNode= "";
                if(!"".equals(bmfzrs)){
                    rs.executeQuery( " select id from workflow_freenode where requestid ='" + requestid + "' and to_char(operatorids) in ("+bmfzrs+")" );
                    while(rs.next()) {
                        String id = Util.null2String(rs.getString("id"));
                        bmfzrNode += bmfzrNode ==""? id : ","+id ;//自由节点
                    }
                }
                if(!"".equals(zdrys))
                {
                    rs.executeQuery( " select id from workflow_freenode where requestid ='" + requestid + "' and to_char(operatorids) in ("+zdrys+")" );
                    while (rs.next()) {
                        String id = Util.null2String(rs.getString("id"));
                        zdryNode += zdryNode ==""? id : ","+id ;//自由节点
                    }
                }
                bb.writeLog( "=================freenode:" + freenode + ",freenode1:" + ufFreenode );
                String str = equestStr( freenode, ufFreenode );
                boolean flag =  rs.executeUpdate( "insert into uf_qzyjhqb(lcid,jdfl,freenode,formmodeid,bmfzr,zdry) values('" + requestid + "','" + jdfl + "','" + str + "','" + formmodeid + "','"+bmfzrNode+"','"+zdryNode+"')" );
                if(flag){
                    rs.executeQuery( "select id from uf_qzyjhqb where requestid='" + requestid + "'" );
                    while (rs.next())
                    {
                        int id = Util.getIntValue(rs.getString( "id" ) );
                        ModeRightInfo moderight = new ModeRightInfo();
                        moderight.editModeDataShare( userid, formmodeid, id );
                    }
                    apimap.put("flag",true);
                }
            }else {
                apimap.put("flag",false);
            }

        }catch (Exception e){

        }
        return apimap;
    }

    /***
     *
     * @param str1
     * @param str2
     * @return
     */
    public static String equestStr(String str1, String str2)
    {
        StringBuilder sb = new StringBuilder();
        String[] strings = str1.split( "," );
        for (String s : strings) {
            if (!str2.contains( s )) {
                sb.append( s + "," );
            }
        }
        String str = sb.substring( 0, sb.length() - 1 ).toString();
        return str;
    }

}
