package com.engine.htsc.wdzx.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.wdzx.dao.WdzxTreeNodeDao;
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.HashMap;
import java.util.List;
import java.util.Map;

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

    public WdzxTreeNodecmd(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) {
        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 jsonArr=Util.null2String(params.get("jsonarr"));
        JAVATools.SetCmdwritelog("requestid："+requestid);
        JAVATools.SetCmdwritelog("workflowid："+workflowid);
        JAVATools.SetCmdwritelog("nodeid："+nodeid);
        JAVATools.SetCmdwritelog("xybzvalue："+xybzvalue);
        JAVATools.SetCmdwritelog("jsonArr："+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= WdzxTreeNodeDao.gettablemap(Util.null2String(requestid));
            if(tablemap==null || tablemap.size()<=0){
                returnmap.put("flag",false);
            }
            JSONObject freeNode = new JSONObject();
            JSONArray nodeData = new JSONArray();
            if("0".equals(xybzvalue)){
                jdfl=new PropBean().getPropName("wdzx.fs.xtfzryj");
                //会签节点
                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 ryvalue=Util.null2String(jsonObject.get("ry"));

                    if(!"".equals(ryvalue) ){

                        JSONObject jsonObject2 = new JSONObject();
                        jsonObject2.put( "operators", ryvalue );
                        jsonObject2.put( "nodename", "系统负责人审核");
                        jsonObject2.put( "signType", "1" );
                        jsonArray2.add(jsonObject2);

                    }
                    nodeData.add( jsonArray2 );
                }
            }else if("1".equals(xybzvalue)){
                jdfl=new PropBean().getPropName("wdzx.fs.xgbmyj");
             //会签节点
            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 hqbmvalue=Util.null2String(jsonObject.get("hqbm"));
                    if(!"".equals(hqbmfzr) && !"".equals(hqbmvalue)){
                        String deptcode = departInfo.getDepartmentCode(hqbmvalue);
                        JSONObject jsonObject2 = new JSONObject();
                        jsonObject2.put( "operators", hqbmfzr );

                        jsonObject2.put( "nodename", "相关部门审核");


                        if("ZZ001014".equals(deptcode)){
                            jsonObject2.put( "signType", "0" );  //20201226
                        }else{
                            jsonObject2.put( "signType", "1" );  //20201226
                        }
                        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 );
        //判断


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


            String freenode = WdzxTreeNodeDao.getfreenode(Util.null2String(requestid));
            String uf_freenode = WdzxTreeNodeDao.getmodelfreenode(Util.null2String(requestid));
            String bmfzrNode= "";
            String zdryNode= "";
            if(!"".equals(bmfzrs)){
                bmfzrNode=WdzxTreeNodeDao.getbmfzr(Util.null2String(requestid),bmfzrs);
            }
            if(!"".equals(zdrys))
            {
                zdryNode=WdzxTreeNodeDao.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=WdzxTreeNodeDao.Inertmodel(Paramsinert);
             if(flag){
               // rs.executeQuery();
                 List<Map<String,Object>> list=WdzxTreeNodeDao.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;
    }
    }
