package com.engine.htsc.jgxt.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.htsc.jgxt.dao.JgxtTreeNodeDao;
import com.engine.htsc.jgxt.dao.JgxtVDao;
import com.engine.workflow.service.RequestFreeFlowService;
import com.engine.workflow.service.impl.RequestFreeFlowServiceImpl;
import weaver.formmode.setup.ModeRightInfo;
import weaver.general.Util;
import weaver.hrm.User;
import weaver.hrm.company.DepartmentComInfo;
import weaver.interfaces.htsc.GlobatTools.JAVATools;
import weaver.interfaces.htsc.comInfo.PropBean;

import java.util.Arrays;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

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

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

    @Override
    public BizLogContext getLogContext() {
        return null;
    }
    /**
     * @title execute
     * @remark 监管数据自由节点
     * @author xf
     * @Params：
       * @param: commandContext
     * @updateTime 2021/9/16 13:35
     * @return: java.util.Map<java.lang.String,java.lang.Object>
     */
    @Override
    public Map<String, Object> execute(CommandContext commandContext) {
        PropBean propBean=new PropBean();
        Map<String,Object> returnmap=new HashMap<>();
        try{
        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")));
        String xybzvalue = Util.null2String(params.get("xybzvalue"));
       // String userids = Util.null2String(params.get("userids"));
        String jsonArr=Util.null2String(params.get("jsonarr"));
        String jdfl="6";
        String sfgssh="";
        String zdry="";
        String bmfzrs="";
        String zdrys="";
        if( "".equals(jsonArr)){
            returnmap.put("flag",false);
            return returnmap;
        }
            Map<String,Object> tablemap=JgxtTreeNodeDao.gettablemap(Util.null2String(requestid));
            if(tablemap==null || tablemap.size()<=0){
                returnmap.put("flag",false);
            }
            JSONObject freeNode = new JSONObject();
            JSONArray nodeData = new JSONArray();
            if("6".equals(xybzvalue) || "7".equals(xybzvalue) ){


                JSONArray jsonArrayparams=JSONArray.parseArray(jsonArr);

                for (int i=0;i<jsonArrayparams.size();i++) {
                    JSONObject  jsonObject= jsonArrayparams.getJSONObject(i);
                    JSONArray jsonArray=new JSONArray();
                    String ry=Util.null2String(jsonObject.get("ry"));
                    String deptid=Util.null2String(jsonObject.get("deptid"));
                    if(!"".equals(ry)){
                        String nodename="总部反馈";
                        jdfl=new PropBean().getPropName("jgxt.fs.zbfkyjhz");
                        if("6".equals(xybzvalue)){
                            nodename="营业部反馈意见";
                            jdfl=new PropBean().getPropName("jgxt.fs.yybfkyjhz");
                        }
                        JSONObject jsonObject3 = new JSONObject();
                        jsonObject3.put( "nodename", nodename );
                        jsonObject3.put( "operators", ry );
                        jsonObject3.put( "signType", 1 );  //会签方式,(0:非会签、1:会签、2:依次逐个签)
                        jsonArray.add(jsonObject3);

                        if("6".equals(xybzvalue)){

                            sfgssh=Util.null2String(tablemap.get("sfgssh"));
                            if("0".equals(sfgssh)){
                                String fgsfkgry= JgxtTreeNodeDao.getfgsfkg(ry);
                                if(!"".equals(fgsfkgry)){
                                    JSONObject jsonObject2 = new JSONObject();
                                    jsonObject2.put( "nodename", "分公司意见" );
                                    jsonObject2.put( "operators", fgsfkgry );
                                    jsonObject2.put( "signType", 0 );  //会签方式,(0:非会签、1:会签、2:依次逐个签)
                                    jsonArray.add(jsonObject2);
                                }
                            }
                        }
                        nodeData.add(jsonArray);
                        System.out.println("------------------nodeData--------------");
                        System.out.println(nodeData);
                    }

                }

            }else  if("0".equals(xybzvalue)){
                //会签节点
            JSONArray jsonArray=JSONArray.parseArray(jsonArr);
            DepartmentComInfo departInfo=new DepartmentComInfo();
                for (int i=0;i<jsonArray.size();i++) {
                   JSONObject  jsonObject= jsonArray.getJSONObject(i);
                    JSONArray jsonArray2=new JSONArray();
                   String hqbmfzr=Util.null2String(jsonObject.get("bmfzr"));
                   String hqzdry=Util.null2String(jsonObject.get("zdry"));
                   String hqbmvalue=Util.null2String(jsonObject.get("hqbm"));
                    String fkgryvalue=Util.null2String(jsonObject.get("fkgry"));
                    if(!"".equals(hqbmfzr) && !"".equals(hqbmvalue)){

                        if(!"".equals(hqzdry)){
                            JSONObject nodeDataJson1 = new JSONObject();
                            nodeDataJson1.put( "nodename", "指定人员" );
                            nodeDataJson1.put( "operators", hqzdry );
                            nodeDataJson1.put( "signType", 1 );
                            jsonArray2.add(nodeDataJson1);
                        }

                        String deptcode = departInfo.getDepartmentCode(hqbmvalue);
                        JSONObject jsonObject2 = new JSONObject();
                        jsonObject2.put( "operators", hqbmfzr );
                        if("1".equals(fkgryvalue)){
                            jsonObject2.put( "nodename", "合规风控人员" );
                        }else{
                            jsonObject2.put( "nodename", "会签部门负责人" );
                        }

                        if("ZZ001014".equals(deptcode)){
                            jsonObject2.put( "signType", "0" );  //20201226
                        }else{
                            jsonObject2.put( "signType", "1" );  //20201226
                        }
                        jsonArray2.add(jsonObject2);

                    }
                    nodeData.add( jsonArray2 );
                    /*if(!"".equals(hqbmfzr)){
                        Arrays.stream(hqbmfzr.split(",")).forEach((item)->{
                            JSONArray jsonArray2=new JSONArray();
                            JSONObject jsonObject2 = new JSONObject();
                         //   String hqbm = departInfo.getDepartmentName( hqbmvalue );
                            String deptcode = departInfo.getDepartmentCode(hqbmvalue);
                            jsonObject2.put( "nodename", "会签部门负责人" );
                            jsonObject2.put( "operators", item );
                            if("ZZ001014".equals(deptcode)){
                                jsonObject2.put( "signType", "0" );  //20201226
                            }else{
                                jsonObject2.put( "signType", "1" );  //20201226
                            }
                           // jsonObject2.put( "signType", 1 );  //会签方式,(0:非会签、1:会签、2:依次逐个签)
                            jsonArray2.add(jsonObject2);
                            nodeData.add( jsonArray2 );
                        });
                    }
                    if(!"".equals(hqzdry)){
                        Arrays.stream(hqzdry.split(",")).forEach((item)->{
                            JSONArray jsonArray2=new JSONArray();
                            JSONObject jsonObject2 = new JSONObject();
                            jsonObject2.put( "nodename", "指定人员" );
                            jsonObject2.put( "operators", item );
                            jsonObject2.put( "signType", 1 );  //会签方式,(0:非会签、1:会签、2:依次逐个签)
                            jsonArray2.add(jsonObject2);
                            nodeData.add( jsonArray2 );
                        });
                    }*/
                }

        }





        freeNode.put( "nodeData", nodeData );
        freeNode.put( "mergeType", 1 );   //合并方式，（1:按比例方式通过，2:按分支数通过）
        freeNode.put( "mergeValue", 100 );


            // 自由节点参数
        Map<String, Object> params = new HashMap<String, Object>();
        params.put( "requestid", requestid );
        params.put( "nodeid", nodeid );
        params.put( "workflowid", workflowid );
        //判断
        int nodetypevalue=2;
        if("6".equals(xybzvalue)){
            nodetypevalue=1;
        }
        params.put( "nodetype", nodetypevalue );

        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) {
            JAVATools.SetCmdwritelog( "=================params:" + params.toString() );
            //生产自由节点
            RequestFreeFlowService freeFlowService = ServiceUtil.getService( RequestFreeFlowServiceImpl.class, user );
            freeFlowService.generateFreeNode( params );
            int formmodeid = 0 ;
            JSONObject josobject= propBean.getModeInfoByUUID(propBean.getPropName("UF_qzyjhqb_CUBEUUID"));
            formmodeid=Util.getIntValue(Util.null2String(josobject.get("modeid")));


            String freenode = JgxtTreeNodeDao.getfreenode(Util.null2String(requestid));
            String uf_freenode = JgxtTreeNodeDao.getmodelfreenode(Util.null2String(requestid));
            String bmfzrNode= "";
            String zdryNode= "";
            if(!"".equals(bmfzrs)){
                bmfzrNode=JgxtTreeNodeDao.getbmfzr(Util.null2String(requestid),bmfzrs);
            }
            if(!"".equals(zdrys))
            {
                zdryNode=JgxtTreeNodeDao.getzdrys(Util.null2String(requestid),zdrys);
            }
            JAVATools.SetCmdwritelog( "=================freenode:" + freenode + ",freenode1:" + uf_freenode );
            String strnode = equestStr( freenode, uf_freenode );
            Map<String,String> Paramsinert =new HashMap<>();
            Paramsinert.put("lcid",Util.null2String(requestid));
            Paramsinert.put("jdfl",jdfl);
            Paramsinert.put("freenode",strnode);
            Paramsinert.put("formmodeid",Util.null2String(formmodeid));
            Paramsinert.put("bmfzr",bmfzrNode);
            Paramsinert.put("zdry",zdryNode);
            boolean flag=JgxtTreeNodeDao.Inertmodel(Paramsinert);
             if(flag){
               // rs.executeQuery();
                 List<Map<String,Object>> list=JgxtTreeNodeDao.getListStr(Util.null2String(requestid));
                 if(list!=null  && list.size()>=0){
                     for (Map<String,Object> model:list) {
                         int id = Util.getIntValue(Util.null2String(model.get( "id" )) );
                         ModeRightInfo moderight = new ModeRightInfo();
                         moderight.editModeDataShare( 1, formmodeid, id );
                     }
                 }


            }
            returnmap.put("flag",true);
            return returnmap;
        }
    }catch (Exception exo){
            returnmap.put("flag",false);
            return returnmap;
     }
        returnmap.put("flag",false);
        return returnmap;
    }
    /**
     * @title equestStr
     * @remark 过滤数据
     * @author xf
     * @Params：
       * @param: str1
     * @param: str2
     * @updateTime 2021/9/15
     * @return: java.lang.String
     */
    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;
    }
    }
