package com.engine.htsc.seal.service;

import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.engine.core.impl.Service;
import org.apache.commons.lang.StringUtils;
import weaver.conn.RecordSet;
import weaver.general.BaseBean;
import weaver.general.GCONST;
import weaver.general.TimeUtil;
import weaver.general.Util;
import weaver.hrm.company.DepartmentComInfo;
import weaver.hrm.resource.ResourceComInfo;
import weaver.interfaces.htsc.GlobatTools.JAVATools;
import weaver.interfaces.htsc.comInfo.PropBean;
import weaver.workflow.request.wfAgentCondition;

import java.util.*;

public class SealApplicationService extends Service {

    /**
     * 根据印章获取印章归属部门、必经领导、分管领导、用印管理员
     * @param yz
     * @return
     */
    public Map<String,Object> getLeader(String yz, String szbm){
        BaseBean bs = new BaseBean();
        RecordSet rs = new RecordSet();
        Map<String,Object> resultMap = new HashMap<>();

        try{
            String sql = "";
            ResourceComInfo resourceComInfo = new ResourceComInfo();
            DepartmentComInfo departmentComInfo = new DepartmentComInfo();

            List<Map<String,String>> yzgsbmMapList = new ArrayList<>();//印章归属部门-明细9
            Set<String> bjldSet = new HashSet<>();//必经领导
            boolean fg = false;//是否加载分管领导
            boolean htgsgz = false;
            List<String> sealList = Arrays.asList(yz.split(","));
            for(String sealid:sealList){
                String yzmc = "";
                String ssjg = "";
                String yzlx = "";
                sql = "select yzmc,yzbh,ssjg,yzlx from uf_yinzhang where id = '"+sealid+"'";
                bs.writeLog(sql);
                rs.executeQuery(sql);
                if(rs.next()){
                    yzmc = Util.null2String(rs.getString("yzmc"));
                    ssjg = Util.null2String(rs.getString("ssjg"));
                    yzlx = Util.null2String(rs.getString("yzlx"));
                }

                if(StringUtils.isNotEmpty(ssjg)){
                    String ssjgName = "";
                    String bmfzr = "";
                    String bmfzrName = "";
                    sql = "select t1.departmentname,t2.deptdirectors from hrmdepartment t1, hrmdepartmentdefined t2 where t2.deptid = t1.id and t1.id = '"+ssjg+"'";
                    bs.writeLog(sql);
                    rs.executeQuery(sql);
                    if(rs.next()){
                        ssjgName = Util.null2String(rs.getString("departmentname"));
                        bmfzr = Util.null2String(rs.getString("deptdirectors"));
                    }

                    if(StringUtils.isNotEmpty(bmfzr)&&1==bmfzr.split(",").length){
                        bmfzrName = resourceComInfo.getLastname(bmfzr);
                    }else{
                        bmfzr = "";
                    }

                    Map<String,String> yzgsbmMap = new HashMap<>();
                    yzgsbmMap.put("id",sealid);
                    yzgsbmMap.put("yzmc",yzmc);
                    yzgsbmMap.put("ssjg",ssjg);
                    yzgsbmMap.put("ssjgName",ssjgName);
                    yzgsbmMap.put("bmfzr",bmfzr);
                    yzgsbmMap.put("bmfzrName",bmfzrName);
                    yzgsbmMapList.add(yzgsbmMap);
                }

                String ld = "";
                String qt = "";
                sql = "select ld,qt from uf_tsld where lcxx = 'zbyy' and yz = '"+sealid+"'";
                bs.writeLog(sql);
                rs.executeQuery(sql);
                if(rs.next()){
                    ld = Util.null2String(rs.getString("ld"));
                    qt = Util.null2String(rs.getString("qt"));
                    bjldSet.addAll(Arrays.asList(ld.split(",")));
                }

                if("fddbr".equals(qt)){
                    fg = true;
                }else if("htgsgz".equals(qt)){
                    fg = true;
                    htgsgz = true;
                }
            }
            resultMap.put("yzgsbm",yzgsbmMapList);

            List<Map<String,String>> bjldMapList = new ArrayList<>();//必经领导
            for(String id:bjldSet){
                if(StringUtils.isNotEmpty(id)){
                    Map<String,String> leaderMap = new HashMap<>();
                    leaderMap.put("id",id);
                    leaderMap.put("name",resourceComInfo.getLastname(id));
                    bjldMapList.add(leaderMap);
                }
            }
            resultMap.put("bjld",bjldMapList);

            List<Map<String,String>> fgldMapList = new ArrayList<>();//分管领导
            if(fg){
                String fgld = "";
                sql = "select deptleaders from hrmdepartmentdefined where deptid = '"+szbm+"'";
                bs.writeLog(sql);
                rs.executeQuery(sql);
                if(rs.next()){
                    fgld = Util.null2String(rs.getString("deptleaders"));
                }
                if(htgsgz){
                    sql = "select bm,ld2 from uf_tsld where lcxx = 'zbyy' and qt = 'htgsgz'";
                    bs.writeLog(sql);
                    rs.executeQuery(sql);
                    if(rs.next()){
                        String bm = Util.null2String(rs.getString("bm"));
                        if(StringUtils.isNotEmpty(bm)&&Arrays.asList(bm.split(",")).contains(szbm)){
                            fgld = Util.null2String(rs.getString("ld2"));
                        }
                    }
                }

                //HTO-13736
                String deptcode = departmentComInfo.getDepartmentCode(szbm);
                String excludeId = "zz001129,ZZ001011" ;//销售交易部、研究所
                if(Arrays.asList(excludeId.split(",")).contains(deptcode)){
                    String lhid = resourceComInfo.getUserIdByLoginId("020363");//梁红
                    if(Arrays.asList(fgld.split(",")).contains(lhid)){
                        String newFgld = lhid;
                        for(String id:fgld.split(",")){
                            if(StringUtils.isNotBlank(id)&&!id.equals(lhid)){
                                newFgld += ","+id;
                            }
                        }
                        fgld = newFgld;
                    }
                }


                for(String id:fgld.split(",")){
                    if(StringUtils.isNotEmpty(id)){
                        Map<String,String> leaderMap = new HashMap<>();
                        leaderMap.put("id",id);
                        leaderMap.put("name",resourceComInfo.getLastname(id));
                        fgldMapList.add(leaderMap);
                    }
                }
            }
            resultMap.put("fgld",fgldMapList);

            List<Map<String,String>> yzglyMapList = new ArrayList<>();//印章管理员
            String dwzzb = "";
            String dwzzbGly = "";
            sql = "select yz,yzgly from uf_tsld where lcxx = 'zbyy' and qt = 'dwzzb'";
            bs.writeLog(sql);
            rs.executeQuery(sql);
            if(rs.next()){
                dwzzb = Util.null2String(rs.getString("yz"));
                dwzzbGly = Util.null2String(rs.getString("yzgly"));
            }
            String gcgl = "";
            String gcglGly = "";
            sql = "select yz,yzgly from uf_tsld where lcxx = 'zbyy' and qt = 'gcgl'";
            bs.writeLog(sql);
            rs.executeQuery(sql);
            if(rs.next()){
                gcgl = Util.null2String(rs.getString("yz"));
                gcglGly = Util.null2String(rs.getString("yzgly"));
            }
            if(StringUtils.isNotEmpty(dwzzb)&&dwzzb.equals(yz)){//仅选择党委组织部印章，印章管理员为冯春妍
                Map<String,String> leaderMap = new HashMap<>();
                sql = "select id,lastname from hrmresource where id = '"+dwzzbGly+"'";
                bs.writeLog(sql);
                rs.executeQuery(sql);
                if(rs.next()){
                    leaderMap.put("id",Util.null2String(rs.getString("id")));
                    leaderMap.put("name",Util.null2String(rs.getString("lastname")));
                    yzglyMapList.add(leaderMap);
                }
            }else if(StringUtils.isNotEmpty(gcgl)&&gcgl.equals(yz)){//仅选择工程专用章，印章管理员为曹文
                Map<String,String> leaderMap = new HashMap<>();
                sql = "select id,lastname from hrmresource where id = '"+gcglGly+"'";
                bs.writeLog(sql);
                rs.executeQuery(sql);
                if(rs.next()){
                    leaderMap.put("id",Util.null2String(rs.getString("id")));
                    leaderMap.put("name",Util.null2String(rs.getString("lastname")));
                    yzglyMapList.add(leaderMap);
                }
            }else{
                String yzgly = "";
                sql = "select yzgly from uf_yzgly where bm = '"+szbm+"'";
                bs.writeLog(sql);
                rs.executeQuery(sql);
                if(rs.next()){
                    yzgly = Util.null2String(rs.getString("yzgly"));
                }
                for(String id:yzgly.split(",")){
                    if(StringUtils.isNotEmpty(id)){
                        Map<String,String> leaderMap = new HashMap<>();
                        leaderMap.put("id",id);
                        leaderMap.put("name",resourceComInfo.getLastname(id));
                        yzglyMapList.add(leaderMap);
                    }
                }
            }
            resultMap.put("yzgly",yzglyMapList);

        }catch (Exception e){
            e.printStackTrace();
        }

        return resultMap;
    }

    /**
     * 根据印章和文件类别类型获取必经会签部门
     * @param yz
     * @param wjlb
     * @param wjlx
     * @return
     */
    public Map<String,Object> getDeptBySealAndFileType(String yz, String wjlb, String wjlx, String szbm){
        BaseBean bs = new BaseBean();
        RecordSet rs = new RecordSet();
        Map<String,Object> resultMap = new HashMap<>();

        try{
            String sql;
            ResourceComInfo resourceComInfo = new ResourceComInfo();

            Set<String> deptSet = new HashSet<>();
            List<String> sealList = Arrays.asList(yz.split(","));
            for(String sealid:sealList){
                String bshq = "";
                sql = "select bshq from uf_tsld where lcxx = 'zbyy' and yz = '"+sealid+"'";
                bs.writeLog(sql);
                rs.executeQuery(sql);
                if(rs.next()){
                    bshq = Util.null2String(rs.getString("bshq"));
                }
                for(String bshqid:bshq.split(",")){
                    if(StringUtils.isNotEmpty(bshqid)&&!bshqid.equals(szbm)){
                        deptSet.add(bshqid);
                    }
                }
            }

            sql = "select bjhqbm from uf_zbyysyfw where wjlb = '"+wjlb+"' and wjlx = '"+wjlx+"'";
            bs.writeLog(sql);
            rs.executeQuery(sql);
            if(rs.next()){
                String bjhqbm = Util.null2String(rs.getString("bjhqbm"));
                deptSet.addAll(Arrays.asList(bjhqbm.split(",")));
            }

            List<Map<String,String>> deptMapList = new ArrayList<>();
            for(String deptid:deptSet){
                if(StringUtils.isNotEmpty(deptid)){
                    Map<String,String> deptMap = new HashMap<>();
                    String deptName = "";
                    sql = "select departmentname from hrmdepartment where id = '"+deptid+"'";
                    bs.writeLog(sql);
                    rs.executeQuery(sql);
                    if(rs.next()){
                        deptName = Util.null2String(rs.getString("departmentname"));
                    }
                    deptMap.put("id",deptid);
                    deptMap.put("name",(deptName));
                    deptMapList.add(deptMap);
                }
            }
            resultMap.put("dept",deptMapList);

            JSONObject jsonObject = getDefaultNotice(wjlb,wjlx,szbm);
            resultMap.put("qztzyd",jsonObject);
        }catch (Exception e){
            e.printStackTrace();
        }

        return resultMap;
    }

    /**
     * 获得常用章
     * @param szbm
     * @return
     */
    public Map<String,Object> getCommonSeal(String szbm){
        BaseBean bs = new BaseBean();
        RecordSet rs = new RecordSet();
        Map<String,Object> resultMap = new HashMap<>();

        try{
            String sql;

            //公司公章
            sql = "select t1.id,t1.yzmc,t1.yzlx,t1.pxzd from uf_yinzhang t1,uf_tsld t2 where t2.yz = t1.id and t2.lcxx = 'zbyy' and t2.qt = 'htgsgz'";
            bs.writeLog(sql);
            rs.executeQuery(sql);
            if(rs.next()){
                Map<String,String> seal = new HashMap<>();
                seal.put("id",Util.null2String(rs.getString("id")));
                seal.put("yzmc",Util.null2String(rs.getString("yzmc")));
                seal.put("yzlx",Util.null2String(rs.getString("yzlx")));
                seal.put("pxzd",Util.null2String(rs.getString("pxzd")));
                resultMap.put("gsgz",seal);
            }

            //法人章
            sql = "select t1.id,t1.yzmc,t1.yzlx,t1.pxzd from uf_yinzhang t1,uf_tsld t2 where t2.yz = t1.id and t2.lcxx = 'zbyy' and t2.qt = 'fddbr'";
            bs.writeLog(sql);
            rs.executeQuery(sql);
            if(rs.next()){
                Map<String,String> seal = new HashMap<>();
                seal.put("id",Util.null2String(rs.getString("id")));
                seal.put("yzmc",Util.null2String(rs.getString("yzmc")));
                seal.put("yzlx",Util.null2String(rs.getString("yzlx")));
                seal.put("pxzd",Util.null2String(rs.getString("pxzd")));
                resultMap.put("frz",seal);
            }

            //所在部门公章
            sql = "select t1.id,t1.yzmc,t1.yzlx,t1.pxzd from uf_yinzhang t1,uf_tsld t2 where t2.yz = t1.yzlx and t2.qt = 'bmgz' and t1.lcxx like '%zbyy%' and t1.ssjg = '"+szbm+"'";
            bs.writeLog(sql);
            rs.executeQuery(sql);
            if(rs.next()){
                Map<String,String> seal = new HashMap<>();
                seal.put("id",Util.null2String(rs.getString("id")));
                seal.put("yzmc",Util.null2String(rs.getString("yzmc")));
                seal.put("yzlx",Util.null2String(rs.getString("yzlx")));
                seal.put("pxzd",Util.null2String(rs.getString("pxzd")));
                resultMap.put("szbmz",seal);
            }


        }catch (Exception e){
            e.printStackTrace();
        }

        return resultMap;
    }

    /**
     *分送节点，获取下一步骤展示选项
     * @param requestid
     */
    public String getNextValue(String requestid, String yz, String wjlb, String wjlx){
        BaseBean bs = new BaseBean();
        RecordSet rs = new RecordSet();

        String result = "0,12,1,10,11";
//        String result = "0,1,10,11";
        String tableName = JAVATools.getMaintable(requestid);
        String sql = "";

        //根据部门判断
        String mainid = "";
        String szbm = "";
        String deptCode = "";
        List<String> deptList = new ArrayList<>();
        sql = "select t2.id,t2.szbm,t1.departmentcode from hrmdepartment t1, "+tableName+" t2 where t2.szbm = t1.id and t2.requestid = '"+requestid+"'";
        bs.writeLog(sql);
        rs.executeQuery(sql);
        if(rs.next()){
            mainid = Util.null2String(rs.getString("id"));
            szbm = Util.null2String(rs.getString("szbm"));
            deptCode = Util.null2String(rs.getString("departmentcode"));
        }
        sql = "select t1.departmentcode from hrmdepartment t1, "+tableName+"_dt2 t2 where t2.hqbm = t1.id and t2.mainid = '"+mainid+"' and t2.yhq = '1'";
        bs.writeLog(sql);
        rs.executeQuery(sql);
        while(rs.next()){
            deptList.add(Util.null2String(rs.getString("departmentcode")));
        }

        if("ZZ323310".equals(deptCode)||deptList.contains("ZZ323310")){//风险管理部
            result += ",2";
        }
        if("ZZ323311".equals(deptCode)||deptList.contains("ZZ323311")){//合规法律部
            result += ",3";
        }
        if("ZZ323314".equals(deptCode)||"ZZ001016".equals(deptCode)||deptList.contains("ZZ323314")||deptList.contains("ZZ001016")){//资金运营部或计划财务部
            result += ",4";
        }
        if("ZZ323445".equals(deptCode)&&"1".equals(wjlb)&&"2".equals(wjlx)){//债务融资部发起“业务管理-发行与承销”
            result += ",5";
        }

        //根据印章判断
        boolean syzgsdw = false;
        boolean sdwzzb = false;
        boolean stwsj = false;
        boolean sghzx = false;
        List<String> sealList = Arrays.asList(yz.split(","));
        for(String sealid:sealList){
            String ssjg = "";
            String yzlx = "";
            sql = "select ssjg,yzlx from uf_yinzhang where id = '"+sealid+"'";
            bs.writeLog(sql);
            rs.executeQuery(sql);
            if(rs.next()){
                ssjg = Util.null2String(rs.getString("ssjg"));
                yzlx = Util.null2String(rs.getString("yzlx"));
            }
            if(!szbm.equals(ssjg)&&StringUtils.isNotEmpty(yzlx)){
                String qt = "";
                sql = "select qt from uf_tsld where yz = '"+yzlx+"'";
                bs.writeLog(sql);
                rs.executeQuery(sql);
                if(rs.next()){
                    qt = Util.null2String(rs.getString("qt"));
                }
                if("bmgz".equals(qt)||"yybgz".equals(qt)||"fgsgz".equals(qt)){
                    syzgsdw = true;
                }
            }


            String qt = "";
            sql = "select qt from uf_tsld where lcxx = 'zbyy' and yz = '"+sealid+"'";
            bs.writeLog(sql);
            rs.executeQuery(sql);
            if(rs.next()){
                qt = Util.null2String(rs.getString("qt"));
            }
            if(StringUtils.isNotEmpty(qt)){
                if("dwzzb".equals(qt)){
                    sdwzzb = true;
                }else if("twsj".equals(qt)){
                    stwsj = true;
                }else if("ghzx".equals(qt)){
                    sghzx = true;
                }
            }
        }

        //党群发起 或者 含有党群会签部门
        List<String> allDeptList = new ArrayList<>();
        sql = "select t1.departmentcode from hrmdepartment t1, "+tableName+"_dt2 t2 where t2.hqbm = t1.id and t2.mainid = '"+mainid+"'";
        bs.writeLog(sql);
        rs.executeQuery(sql);
        while(rs.next()){
            allDeptList.add(Util.null2String(rs.getString("departmentcode")));
        }
        if("ZZ001023".equals(deptCode)||allDeptList.contains("ZZ001023")){//党群工作部
            sghzx = true;
        }

        if(syzgsdw){//送印章归属单位
            result += ",6";
        }
        if(sdwzzb){//送党委组织部
            result += ",7";
        }
        if(stwsj){//送团委书记
            result += ",8";
        }
        if(sghzx){//送工会主席
            result += ",9";
        }

        return result;
    }

    /**
     * 根据证照类型获取证照列表
     * @param zzlx
     * @return
     */
    public Map<String,Object> getLicenseList(String zzlx){
        BaseBean bs = new BaseBean();
        RecordSet rs = new RecordSet();
        Map<String,Object> resultMap = new HashMap<>();

        List<Map<String,String>> licenseList = new ArrayList<>();
        String sql = "select id,zz,wjmc from uf_zzwj where zzlx = '"+zzlx+"'";
        bs.writeLog(sql);
        rs.executeQuery(sql);
        while(rs.next()){
            Map<String,String> licenseMap = new HashMap<>();
            licenseMap.put("id",Util.null2String(rs.getString("id")));
            licenseMap.put("zz",Util.null2String(rs.getString("zz")));
            licenseMap.put("wjmc",Util.null2String(rs.getString("wjmc")));
            licenseList.add(licenseMap);
        }

        resultMap.put("licenseList",licenseList);
        return resultMap;
    }

    /**
     * 获得强制通知阅读对象
     * @param wjlb
     * @param wjlx
     * @return
     */
    public JSONObject getDefaultNotice(String wjlb, String wjlx, String szbm){
        BaseBean bs = new BaseBean();
        PropBean pb = new PropBean();
        RecordSet rs = new RecordSet();
        JSONObject jsonData = new JSONObject();

        try{
            ResourceComInfo resourceInfo = new ResourceComInfo();
            JSONArray jsonArray2 = new JSONArray();

            String qzsydxs = "";  //强制会签部门
            String qzsyrys = ""; //强制送阅人员
            String hrbp = "";//是否加载hrbp
            String sql = " select qztzyddx,qztzydbm,hrbp from uf_zbyysyfw where wjlb ='" + wjlb + "' and wjlx='" + wjlx + "'";
            bs.writeLog( sql );
            rs.executeQuery( sql );
            if(rs.next()){
                qzsydxs = Util.null2String(rs.getString("qztzydbm"));//强制送阅部门
                qzsyrys = Util.null2String(rs.getString("qztzyddx"));//强制送阅人员
                hrbp = Util.null2String(rs.getString("hrbp"));
            }
            if("HRBP".equals(hrbp)){
                sql = "select depthrbp from hrmdepartmentdefined where deptid = '"+szbm+"'";
                bs.writeLog(sql);
                rs.executeQuery(sql);
                if(rs.next()){
                    qzsyrys = Util.null2String(rs.getString("depthrbp"));
                }
            }

            List<String> arr3 = new ArrayList<>();
            String ids = "";
            if(!"".equals(qzsydxs)){
                List<String> qzsydxList = Arrays.asList(qzsydxs.split(","));
                for(String qzsydx : qzsydxList){
                    String sql1 = "select id,lastname,departmentid from hrmresource where departmentid = "+qzsydx+" and status =1 order by dsporder,lastname";
                    rs.executeQuery( sql1 );
                    while (rs.next()) {
                        JSONObject json2 = new JSONObject();
                        String id = Util.null2String( rs.getString( "id" ) );
                        ids += ids == "" ? id : "," + id;
                        String lastname = Util.null2String(rs.getString( "lastname" ));
                        json2.put( "id", id );
                        json2.put( "name", lastname );
                        jsonArray2.add( json2 );

                        String departmentid =  Util.null2String(rs.getString("departmentid"));
                        if(arr3 !=null && arr3.size() > 0){
                            if(!arr3.contains(departmentid)){
                                arr3.add(departmentid);
                            }
                        }else{
                            arr3.add(departmentid);
                        }
                    }
                }
            }

            String str1 = "";
            if(arr3 !=null && arr3.size() >0){
                for (String str : arr3)
                {
                    str1 += str1 == "" ? "dept_"+str : "," + "dept_"+str;
                }
            }

            if(!"".equals(qzsyrys)){
                List<String> qzsyryList = Arrays.asList(qzsyrys.split(","));
                for(String qzsyry : qzsyryList){
                    String qzsyryname = resourceInfo.getLastname(qzsyry);//强制送阅人员名称
                    JSONObject json3 = new JSONObject();
                    json3.put("id",qzsyry);
                    json3.put("name",qzsyryname);
                    jsonArray2.add(json3);
                }
            }
            jsonData.put( "qzsyryid", qzsyrys );

            JSONArray replaceObject  = getReplaceDatas(wjlb,wjlx);
            jsonData.put( "tzyddx", replaceObject );
            jsonData.put( "tzydgd", jsonArray2 );

            String strs = "";
            if(!"".equals(qzsyrys)){
                List<String> arr2 = Arrays.asList(qzsyrys.split(","));
                if(arr2!=null && arr2.size() > 0){
                    for (String str : arr2) {
                        strs += strs == "" ? "hrm_"+str : "," + "hrm_"+str;
                    }
                }
            }


            jsonData.put( "qzsydxid", ids ); // 人员汇总ID
            jsonData.put( "qzsyrys", strs ); //人员
            jsonData.put( "qzsydxs", str1 );  //部门
            bs.writeLog( "getDefaultNotice=================jsonData:" + jsonData.toJSONString() );
        }catch (Exception e){
            e.printStackTrace();
        }
        return jsonData;
    }

    public JSONArray getReplaceDatas(String wjlb, String wjlx)
    {
        String ecologyContentPath = GCONST.getContextPath();
        BaseBean bs = new BaseBean();
        RecordSet rs = new RecordSet();
        RecordSet rs1 = new RecordSet();
        RecordSet rs2 = new RecordSet();
        JSONArray replaceDatas = new JSONArray();
        DepartmentComInfo departInfo = null;
        try{
            departInfo = new DepartmentComInfo();

            String qzsydxs = "";
            String qzsyrys = "";
            String sql = "select qztzyddx,qztzydbm from uf_zbyysyfw where wjlb ='" + wjlb + "' and wjlx='" + wjlx + "'";
            rs.executeQuery( sql );
            if (rs.next()) {
                qzsydxs = Util.null2String(rs.getString("qztzydbm"));//强制送阅部门
                qzsyrys = Util.null2String(rs.getString("qztzyddx"));//强制送阅人员
            }

            bs.writeLog("qzsydxs:"+qzsydxs);
            bs.writeLog("qzsyrys:"+qzsyrys);

            if (!"".equals(qzsydxs))
            {
                String[] children = new String[0];
                List<String> qzsydxList = Arrays.asList(qzsydxs.split(","));
                for(String qzsydx : qzsydxList)
                {
                    JSONObject deptJson = new JSONObject();
                    bs.writeLog("qzsydx:"+qzsydx);
                    String qzsydxname = departInfo.getDepartmentName( qzsydx );//强制送阅部门名称
                    String sql1 = " select h.id,h.lastname,h.workcode,h.pinyinlastname,h.subcompanyid1,t.supdepid from hrmresource h,hrmdepartment t where h.departmentid=t.id and h.departmentid= " + qzsydx + " and h.status='1' order by dsporder,h.lastname";
                    bs.writeLog("sql1:"+sql1);

                    rs1.executeQuery( sql1 );
                    JSONArray users = new JSONArray();
                    String ids = "";
                    String subcompanyid1 = "";
                    String supdepid = "";
                    while (rs1.next()) {
                        String[] displayKeys = {"lastnamespan", "workcodespan", "departmentidspan", "jobtitlespan"};
                        String id = Util.null2String( rs1.getString( "id" ) );
                        String lastname = Util.null2String( rs1.getString( "lastname" ) );
                        String workcode = Util.null2String( rs1.getString( "workcode" ) );
                        subcompanyid1 = Util.null2String( rs1.getString( "subcompanyid1" ) );
                        String pinyinlastname = Util.null2String( rs1.getString( "pinyinlastname" ) );
                        supdepid = Util.null2String( rs1.getString( "supdepid" ) );
                        ids += ids == "" ? id+ "" : "," + id;
                        JSONObject userJson = new JSONObject();
                        userJson.put( "departmentid", qzsydx );
                        userJson.put( "departmentidspan", qzsydx );
                        userJson.put( "departmentname", qzsydx );
                        userJson.put( "displayKeys", displayKeys );
                        userJson.put( "icon", ecologyContentPath+"/messager/usericon/" + workcode + "/" + workcode + ".jpg" );
                        userJson.put( "id", id );
                        userJson.put( "jobtitlename", getJob( id ) );
                        userJson.put( "jobtitlespan", getJob( id ) );
                        userJson.put( "lastname", lastname );
                        userJson.put( "lastnamespan", lastname );
                        userJson.put( "nodeid", "resource_" + id + "x" );
                        userJson.put( "pinyinlastname", pinyinlastname );
                        userJson.put( "subcompanyid", subcompanyid1 );
                        userJson.put( "subcompanyname", "华泰集团" );
                        userJson.put( "supsubcompanyid", "0" );
                        userJson.put( "supsubcompanyname", "" );
                        userJson.put( "type", "resource" );
                        userJson.put( "workcodespan", workcode );
                        users.add( userJson );
                    }
                    deptJson.put( "children", children );
                    deptJson.put( "crmNames", "" );
                    deptJson.put( "icon", "/images/treeimages/subCopany_Colse_wev8.gif" );
                    deptJson.put( "id", qzsydx );
                    deptJson.put( "isLeaf", false );
                    deptJson.put( "isParent", "true" );
                    deptJson.put( "lastname", qzsydxname );
                    deptJson.put( "name", qzsydxname );
                    deptJson.put( "nocheck", "Y" );
                    deptJson.put( "nodeid", "dept_" + qzsydx + "x" );
                    deptJson.put( "treeKey", "wea0-com_1x-subcom_" + subcompanyid1 + "x-dept_" + supdepid + "x-dept_" + qzsydx + "x" );
                    deptJson.put( "type", "dept" );
                    deptJson.put( "users", users );
                    replaceDatas.add( deptJson );
                }

                if(!"".equals(qzsyrys))
                {
                    String sql2 = "select id,lastname,workcode,departmentid,jobtitle,mobile,pinyinlastname,messagerurl,subcompanyid1 from hrmresource where id in (" + qzsyrys + ") and status='1' order by dsporder,lastname";
                    bs.writeLog("sql2:"+sql2);
                    rs2.executeQuery( sql2 );
                    while (rs2.next()) {
                        JSONObject hrmJson = new JSONObject();
                        String id = Util.null2String( rs2.getString( "id" ) );
                        String lastname = Util.null2String( rs2.getString( "lastname" ) );
                        String workcode = Util.null2String( rs2.getString( "workcode" ) );
                        String jobtitle = Util.null2String( rs2.getString( "jobtitle" ) );
                        String mobile = Util.null2String( rs2.getString( "mobile" ) );
                        String pinyinlastname = Util.null2String( rs2.getString( "pinyinlastname" ) );
                        String departmentid = Util.null2String( rs2.getString( "departmentid" ) );
                        String messagerurl = Util.null2String( rs2.getString( "messagerurl" ) );
                        String subcompanyid = Util.null2String( rs2.getString( "subcompanyid1" ) );
                        String departmentname = departInfo.getDepartmentName( departmentid );
                        hrmJson.put( "crmNames", "" + departmentname + "／华泰集团" );
                        hrmJson.put( "departmentid", departmentid );
                        hrmJson.put( "departmentidspan", departmentname );
                        hrmJson.put( "departmentname", departmentname );
                        hrmJson.put( "icon", ecologyContentPath+"/messager/usericon/" + workcode + "/" + workcode + ".jpg" );
                        hrmJson.put( "id", id );
                        hrmJson.put( "img", ecologyContentPath+"/messager/usericon/" + workcode + "/" + workcode + ".jpg" );
                        hrmJson.put( "jobtitle", jobtitle );
                        hrmJson.put( "jobtitlename", getJob( id ) );
                        hrmJson.put( "jobtitlespan", getJob( id ) );
                        hrmJson.put( "lastname", lastname );
                        hrmJson.put( "lastnamespan", lastname );
                        hrmJson.put( "messagerurl", messagerurl );
                        hrmJson.put( "mobile", mobile );
                        hrmJson.put( "name", lastname );
                        hrmJson.put( "nodeid", "resource_" + id + "x" );
                        hrmJson.put( "pinyinlastname", pinyinlastname );
                        hrmJson.put( "subcompanyid", subcompanyid );
                        hrmJson.put( "subcompanyid1", subcompanyid );
                        hrmJson.put( "subcompanyname", "华泰集团" );
                        hrmJson.put( "supsubcompanyid", "0" );
                        hrmJson.put( "supsubcompanyname", "" );
                        hrmJson.put( "type", "resource" );
                        hrmJson.put( "workcode", workcode );
                        hrmJson.put( "workcodespan", workcode );
                        replaceDatas.add( hrmJson );
                    }
                }
            }

        }catch (Exception e){
            e.printStackTrace();
            bs.writeLog("-----error-------"+e.getMessage());
        }
        return replaceDatas;
    }

    public static String getJob(String id) {
        RecordSet rs = new RecordSet();
        rs.executeQuery( "select s.jobtitlename from hrmresource h,hrmjobtitles s where h.jobtitle =s.id and h.id='" + id + "'" );
        String jobtitlename = "";
        if (rs.next()) {
            jobtitlename = Util.null2String( rs.getString( "jobtitlename" ) );
        }
        return jobtitlename;
    }

    /**
     * 根据部门id返回前端User
     * @param deptids
     */
    public JSONArray getUserByDeptids(String deptids){
        BaseBean bs = new BaseBean();
        RecordSet rs1 = new RecordSet();
        String ecologyContentPath = GCONST.getContextPath();
        List<String> deptidList = Arrays.asList(deptids.split(","));
        JSONArray result = new JSONArray();

        for(String deptid:deptidList){

            JSONObject deptJson = new JSONObject();
            deptJson.put( "deptid", deptid );

            String sql1 = " select h.id,h.lastname,h.workcode,h.pinyinlastname,h.subcompanyid1,t.supdepid from hrmresource h,hrmdepartment t where h.departmentid=t.id and h.departmentid= " + deptid + " and h.status='1' order by dsporder,h.lastname";
            bs.writeLog("sql1:"+sql1);
            rs1.executeQuery( sql1 );

            JSONArray users = new JSONArray();
            String ids = "";
            String subcompanyid1 = "";
            String supdepid = "";
            while (rs1.next()) {
                String[] displayKeys = {"lastnamespan", "workcodespan", "departmentidspan", "jobtitlespan"};
                String id = Util.null2String( rs1.getString( "id" ) );
                String lastname = Util.null2String( rs1.getString( "lastname" ) );
                String workcode = Util.null2String( rs1.getString( "workcode" ) );
                subcompanyid1 = Util.null2String( rs1.getString( "subcompanyid1" ) );
                String pinyinlastname = Util.null2String( rs1.getString( "pinyinlastname" ) );
                supdepid = Util.null2String( rs1.getString( "supdepid" ) );
                ids += ids == "" ? id+ "" : "," + id;
                JSONObject userJson = new JSONObject();
                userJson.put( "departmentid", deptid );
                userJson.put( "departmentidspan", deptid );
                userJson.put( "departmentname", deptid );
                userJson.put( "displayKeys", displayKeys );
                userJson.put( "icon", ecologyContentPath+"/messager/usericon/" + workcode + "/" + workcode + ".jpg" );
                userJson.put( "id", id );
                userJson.put( "jobtitlename", getJob( id ) );
                userJson.put( "jobtitlespan", getJob( id ) );
                userJson.put( "lastname", lastname );
                userJson.put( "lastnamespan", lastname );
                userJson.put( "nodeid", "resource_" + id + "x" );
                userJson.put( "pinyinlastname", pinyinlastname );
                userJson.put( "subcompanyid", subcompanyid1 );
                userJson.put( "subcompanyname", "华泰集团" );
                userJson.put( "supsubcompanyid", "0" );
                userJson.put( "supsubcompanyname", "" );
                userJson.put( "type", "resource" );
                userJson.put( "workcodespan", workcode );
                users.add( userJson );
            }
            deptJson.put( "users", users );
            result.add( deptJson );
        }
        return result;
    }

    /**
     * 查询是否需要添加电子用印标签
     * @param requestid
     */
    public Map<String,Object> checkElectronicSealLabel(String requestid,String userid){
        BaseBean bs = new BaseBean();
        RecordSet rs = new RecordSet();
        Map<String,Object> resultMap = new HashMap<>();

        boolean flag = false;
        String tableName = JAVATools.getMaintable(requestid);

        String mainid = "";
        String yzgly = "";
        String sql = "select id,yzgly from "+tableName+" where requestid = "+requestid;
        bs.writeLog(sql);
        rs.executeQuery(sql);
        if(rs.next()){
            mainid = Util.null2String(rs.getString("id"));
            yzgly = Util.null2String(rs.getString("yzgly"));
        }

        sql = "select id from "+tableName+"_dt7 where mainid = '"+mainid+"' and yyfs in (0,2)";
        bs.writeLog(sql);
        rs.executeQuery(sql);
        while(rs.next()){
            flag = true;
        }

        sql = "select id from "+tableName+"_dt8 where mainid = '"+mainid+"' and yyfs in (0,2)";
        bs.writeLog(sql);
        rs.executeQuery(sql);
        while(rs.next()){
            flag = true;
        }

        Set<String> useridSet = new HashSet<>();
        useridSet.addAll(Arrays.asList(yzgly.split(",")));
        if(StringUtils.isNotEmpty(userid)){
            useridSet.add(userid);
        }else{
            String workflowid = "";
            sql = "select workflowid from workflow_requestbase where requestid = '"+requestid+"'";
            bs.writeLog(sql);
            rs.executeQuery(sql);
            if(rs.next()){
                workflowid = Util.null2String(rs.getString("workflowid"));
            }

            wfAgentCondition wfAgentCondition = new wfAgentCondition();
            for(String yzglyid:yzgly.split(",")){
                Map<String,String> map =  wfAgentCondition.getAgentInfoByResouce(workflowid,yzglyid,requestid);
                if(map.size()>0){
                    useridSet.add(map.get(yzglyid));
                }
            }
        }

        for(String id:useridSet){
            if(StringUtils.isNotEmpty(id)){
                if(flag){
                    updateLabel(requestid,id,"add","电子用印","#6D5BFA");
                }else {
                    updateLabel(requestid,id,"delete","电子用印","#6D5BFA");
                }
            }

        }

        resultMap.put("result",flag);
        return resultMap;
    }

    /**
     * 更新标签状态
     * @param requestid
     * @param userid
     * @param type
     * @return
     */
    public Map<String, Object> updateLabel(String requestid, String userid, String type, String showName, String colorval){
        BaseBean bs = new BaseBean();
        RecordSet rs = new RecordSet();
        Map<String, Object> apidatas = new HashMap<String, Object>();

//        String showName = "电子用印";
//        String colorval = "#6D5BFA";
        String operatedate = TimeUtil.getCurrentDateString();
        String operatetime = TimeUtil.getOnlyCurrentTimeString();
        String id = "";
        String sql = "select id from workflow_attentionTypeSet where userid = "+userid+" and showname ='"+showName+"'";
        bs.writeLog(sql);
        rs.executeQuery(sql);
        if(rs.next()){
            id = Util.null2String(rs.getString("id"));
        }else{
            String insertSql = "insert into WORKFLOW_ATTENTIONTYPESET(userid,usertype,showname,colorval,showorder,operatedate,operatetime) values(?,?,?,?,?,?,?)";
            rs.executeUpdate(insertSql,new Object[]{userid,0,showName,colorval,-1,operatedate,operatetime});
            sql = "select id from workflow_attentionTypeSet where userid = "+userid+" and SHOWNAME ='"+showName+"'";
            bs.writeLog(sql);
            rs.executeQuery(sql);
            if(rs.next()){
                id = Util.null2String(rs.getString("id"));
            }
        }
        if("add".equals(type)){
            sql = "select * from workflow_attention where userid= "+userid+" and usertype= 0 and requestid = "+requestid+" and atypeid = "+id;
            bs.writeLog(sql);
            rs.execute(sql);
            if (rs.next()){
            }else {
                String addSql = "insert into workflow_attention(userid,usertype,requestid,atypeid,operatedate,operatetime) values(?,?,?,?,?,?)";
                rs.executeUpdate(addSql,new Object[]{userid,0,requestid,id,operatedate,operatetime});
            }
        }else if("delete".equals(type)){
            String delsql = "delete from workflow_attention where userid= "+userid+" and usertype= 0 and requestid = "+requestid+" and atypeid = "+id;
            bs.writeLog(delsql);
            rs.executeUpdate(delsql);
        }
        return apidatas;
    }
}
