package com.engine.htsc.seal.service;


import com.alibaba.fastjson.JSONObject;
import com.engine.common.util.ServiceUtil;
import com.engine.core.impl.Service;
import org.apache.commons.lang.StringUtils;
import weaver.conn.RecordSet;
import weaver.general.BaseBean;
import weaver.general.Util;
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 SubSealApplicationService extends Service {

    /**
     * 获得不经过总部的文件类别类型
     * @return
     */
    public Map<String,Object> getNotThroughType(){
        BaseBean bs = new BaseBean();
        RecordSet rs = new RecordSet();
        Map<String,Object> resultMap = new HashMap<>();

        try{
            List<Map<String,String>> typeMapList = new ArrayList<>();
            String sql = "select wjlb,wjlx from uf_fzjgyysyfw where sfzjszbds = '0'";
            bs.writeLog(sql);
            rs.executeQuery(sql);
            while(rs.next()){
                Map<String,String> typeMap = new HashMap<>();
                String wjlb = Util.null2String(rs.getString("wjlb"));
                String wjlx = Util.null2String(rs.getString("wjlx"));
                typeMap.put("wjlb",wjlb);
                typeMap.put("wjlx",wjlx);
                typeMapList.add(typeMap);
            }
            resultMap.put("result",typeMapList);
        }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 = 'fzjgyy' 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 = 'fzjgyy' 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 id,yzmc,yzlx,pxzd from uf_yinzhang where lcxx like '%fzjgyy%' and 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);
            }

            //上级分公司章
            String sjfgs = "";
            sql = "select t1.id from hrmdepartment t1,hrmdepartmentdefined t2 where (t1.id = '"+szbm+"' or t1.id = " +
                    "(select supdepid from hrmdepartment where id = '"+szbm+"')) and t2.deptid = t1.id and t2.deptScaleLevelCode = '002'";
            bs.writeLog(sql);
            rs.executeQuery(sql);
            if(rs.next()){
                sjfgs = Util.null2String(rs.getString("id"));
            }
            if(StringUtils.isNotEmpty(sjfgs)&&!sjfgs.equals(szbm)){
                sql = "select id,yzmc,yzlx,pxzd from uf_yinzhang where lcxx like '%fzjgyy%' and ssjg = '"+sjfgs+"'";
                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("sjfgs",seal);
                }
            }


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

        return resultMap;
    }

    /**
     * 获得岗位人员
     * @param szbm
     * @return
     */
    public Map<String,Object> getMember(String szbm){
        BaseBean bs = new BaseBean();
        RecordSet rs = new RecordSet();
        Map<String,Object> resultMap = new HashMap<>();

        try{
            ResourceComInfo resourceComInfo = new ResourceComInfo();
            String sfsxzx = "";
            String sql = "select sfsxzx from hrmdepartmentdefined where deptid ='"+szbm+"'";
            bs.writeLog(sql);
            rs.executeQuery(sql);
            if(rs.next()){
                sfsxzx = Util.null2String(rs.getString("sfsxzx"));
            }

            String supdepid = "";
            if("0".equals(sfsxzx)){
                supdepid = szbm;
            }else{
                sql = "select t1.id from hrmdepartment t1,hrmdepartmentdefined t2 where (t1.id = '"+szbm+"' or t1.id = (select supdepid from hrmdepartment where id = '"+szbm+"')) and t2.deptid = t1.id and t2.deptScaleLevelCode = '002'";
                bs.writeLog(sql);
                rs.executeQuery(sql);
                if(rs.next()){
                    supdepid = Util.null2String(rs.getString("id"));
                }
            }

            String cwhjid = "";
            String cwhj = "";
            sql = "select id from uf_gw where gwmc = '财务会计'";
            bs.writeLog(sql);
            rs.executeQuery(sql);
            if(rs.next()){
                cwhjid = Util.null2String(rs.getString("id"));
            }
            if(StringUtils.isNotEmpty(cwhjid)){
                sql = "select ry from uf_lcgwry where xzlc = 'fzjgyy' and jg = '"+supdepid+"' and xzgw = '"+cwhjid+"'";
                bs.writeLog(sql);
                rs.executeQuery(sql);
                if(rs.next()){
                    cwhj = Util.null2String(rs.getString("ry"));
                }
            }
            List<String> cwhjNameList = new ArrayList<>();
            for(String id:cwhj.split(",")){
                String name = resourceComInfo.getLastname(id);
                cwhjNameList.add(name);
            }
            resultMap.put("cwhj", cwhj);
            resultMap.put("cwhjName", StringUtils.join(cwhjNameList,","));

            String hgglgid = "";
            String hgglg = "";
            sql = "select id from uf_gw where gwmc = '合规管理岗'";
            bs.writeLog(sql);
            rs.executeQuery(sql);
            if(rs.next()){
                hgglgid = Util.null2String(rs.getString("id"));
            }
            if(StringUtils.isNotEmpty(hgglgid)){
                sql = "select ry from uf_lcgwry where xzlc = 'fzjgyy' and jg = '"+supdepid+"' and xzgw = '"+hgglgid+"'";
                bs.writeLog(sql);
                rs.executeQuery(sql);
                if(rs.next()){
                    hgglg = Util.null2String(rs.getString("ry"));
                }
            }
            List<String> hgglgNameList = new ArrayList<>();
            for(String id:hgglg.split(",")){
                String name = resourceComInfo.getLastname(id);
                hgglgNameList.add(name);
            }
            resultMap.put("hgglg",hgglg);
            resultMap.put("hgglgName",StringUtils.join(hgglgNameList,","));

            String hgfktdfzrid = "";
            String hgfktdfzr = "";
            sql = "select id from uf_gw where gwmc = '合规风控团队负责人'";
            bs.writeLog(sql);
            rs.executeQuery(sql);
            if(rs.next()){
                hgfktdfzrid = Util.null2String(rs.getString("id"));
            }
            if(StringUtils.isNotEmpty(hgfktdfzrid)){
                sql = "select ry from uf_lcgwry where xzlc = 'fzjgyy' and jg = '"+supdepid+"' and xzgw = '"+hgfktdfzrid+"'";
                bs.writeLog(sql);
                rs.executeQuery(sql);
                if(rs.next()){
                    hgfktdfzr = Util.null2String(rs.getString("ry"));
                }
            }
            List<String> hgfktdfzrNameList = new ArrayList<>();
            for(String id:hgfktdfzr.split(",")){
                String name = resourceComInfo.getLastname(id);
                hgfktdfzrNameList.add(name);
            }
            resultMap.put("hgfktdfzr",hgfktdfzr);
            resultMap.put("hgfktdfzrName",StringUtils.join(hgfktdfzrNameList,","));

            String fxqfkgid = "";
            String fxqfkg = "";
            String fgsfxqfkg = "";
            sql = "select id from uf_gw where gwmc = '反洗钱风控岗'";
            bs.writeLog(sql);
            rs.executeQuery(sql);
            if(rs.next()){
                fxqfkgid = Util.null2String(rs.getString("id"));
            }
            if(StringUtils.isNotEmpty(fxqfkgid)){
                sql = "select ry from uf_lcgwry where xzlc = 'fzjgyy' and jg = '"+szbm+"' and xzgw = '"+fxqfkgid+"'";
                bs.writeLog(sql);
                rs.executeQuery(sql);
                if(rs.next()){
                    fxqfkg = Util.null2String(rs.getString("ry"));
                }
                sql = "select ry from uf_lcgwry where xzlc = 'fzjgyy' and jg = '"+supdepid+"' and xzgw = '"+fxqfkgid+"'";
                bs.writeLog(sql);
                rs.executeQuery(sql);
                if(rs.next()){
                    fgsfxqfkg = Util.null2String(rs.getString("ry"));
                }
            }
            List<String> fxqfkgNameList = new ArrayList<>();
            for(String id:fxqfkg.split(",")){
                String name = resourceComInfo.getLastname(id);
                fxqfkgNameList.add(name);
            }
            List<String> fgsfxqfkgNameList = new ArrayList<>();
            for(String id:fgsfxqfkg.split(",")){
                String name = resourceComInfo.getLastname(id);
                fgsfxqfkgNameList.add(name);
            }
            resultMap.put("fxqfkg",fxqfkg);
            resultMap.put("fxqfkgName",StringUtils.join(fxqfkgNameList,","));
            resultMap.put("fgsfxqfkg",fgsfxqfkg);
            resultMap.put("fgsfxqfkgName",StringUtils.join(fgsfxqfkgNameList,","));

            resultMap.put("api_status",true);
        }catch (Exception e){
            e.printStackTrace();
            resultMap.put("api_status",false);
            resultMap.put("api_errormsg", "catch exception : " + e.getMessage());
        }
        return resultMap;
    }

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

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

            List<Map<String,String>> yzgsbmMapList = new ArrayList<>();//印章归属部门-明细9
            List<Map<String,String>> fzjgyzgsbmMapList = new ArrayList<>();//分支机构印章归属部门
            Set<String> bjldSet = new HashSet<>();//必经领导
            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 = "";
                    String deptscalelevelcode = "";
                    sql = "select t1.departmentname,t2.deptdirectors,t2.deptscalelevelcode 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"));
                        deptscalelevelcode = Util.null2String(rs.getString("deptscalelevelcode"));
                    }

                    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);
                    if("001".equals(deptscalelevelcode)){//总部部门
                        yzgsbmMapList.add(yzgsbmMap);
                    }else{
                        fzjgyzgsbmMapList.add(yzgsbmMap);
                    }

                }

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

            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>> yzglyMapList = new ArrayList<>();//印章管理员
            String dwzzb = "";
            sql = "select yz from uf_tsld where lcxx = 'fzjgyy' and qt = 'dwzzb'";
            bs.writeLog(sql);
            rs.executeQuery(sql);
            if(rs.next()){
                dwzzb = Util.null2String(rs.getString("yz"));
            }
            if(StringUtils.isNotEmpty(dwzzb)&&dwzzb.equals(yz)){//仅选择党委组织部印章，印章管理员为冯春妍
                Map<String,String> leaderMap = new HashMap<>();
                sql = "select id,lastname from hrmresource where loginid = '002501'";
                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 supdeptid = "";
                String supdeptcode = "";

                String sfsxzx = "";
                sql = "select sfsxzx from hrmdepartmentdefined where deptid ='"+szbm+"'";
                bs.writeLog(sql);
                rs.executeQuery(sql);
                if(rs.next()){
                    sfsxzx = Util.null2String(rs.getString("sfsxzx"));
                }

                if("0".equals(sfsxzx)){//私行中心
                    supdeptid = szbm;
                }else{
                    sql = "select t1.id,t1.departmentcode from hrmdepartment t1,hrmdepartmentdefined t2 where (t1.id = '"+szbm+"' or t1.id = " +
                            "(select supdepid from hrmdepartment where id = '"+szbm+"')) and t2.deptid = t1.id and t2.deptScaleLevelCode = '002'";
                    bs.writeLog(sql);
                    rs.executeQuery(sql);
                    if(rs.next()){
                        supdeptid = Util.null2String(rs.getString("id"));
                        supdeptcode = Util.null2String(rs.getString("departmentcode"));
                    }
                }

                String yzgly = "";
                sql = "select yzgly from uf_fzjgyyyzgly where jg = '"+supdeptid+"'";
                bs.writeLog(sql);
                rs.executeQuery(sql);
                if(rs.next()){
                    yzgly = Util.null2String(rs.getString("yzgly"));
                }

//                String yzglyLoginid = "";
//                String deptcode = Util.null2String(pb.getPropName("subSeal-yzgly1-deptcodde"));
//                if(Arrays.asList(deptcode.split(",")).contains(supdeptcode)){
//                    yzglyLoginid = Util.null2String(pb.getPropName("subSeal-yzgly1"));
//                }else{
//                    yzglyLoginid = Util.null2String(pb.getPropName("subSeal-yzgly-other"));
//                }
//
//                String yzgly = "";
//                sql = "select id from hrmresource where loginid = '"+yzglyLoginid+"'";
//                bs.writeLog(sql);
//                rs.executeQuery(sql);
//                if(rs.next()){
//                    yzgly = Util.null2String(rs.getString("id"));
//                }
                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> ywzgbmglgList = new ArrayList<>();
            List<String> sealList = Arrays.asList(yz.split(","));
            for(String sealid:sealList){
                String bshq = "";
                sql = "select bshq from uf_tsld where lcxx = 'fzjgyy' 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);
                    }
                }
            }

            String hrbp = "";
            String fxq = "";
            sql = "select bjhqbm,ywzgbmglg,hrbp,fxq from uf_fzjgyysyfw where wjlb = '"+wjlb+"' and wjlx = '"+wjlx+"'";
            bs.writeLog(sql);
            rs.executeQuery(sql);
            if(rs.next()){
                String bjhqbm = Util.null2String(rs.getString("bjhqbm"));
                String ywzgbmglg = Util.null2String(rs.getString("ywzgbmglg"));
                hrbp = Util.null2String(rs.getString("hrbp"));
                fxq = Util.null2String(rs.getString("fxq"));
                deptSet.addAll(Arrays.asList(bjhqbm.split(",")));
                ywzgbmglgList = Arrays.asList(ywzgbmglg.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);

            if("HRBP".equals(hrbp)){
                String depthrbp = "";
                sql = "select depthrbp from hrmdepartmentdefined where deptid = '"+szbm+"'";
                bs.writeLog(sql);
                rs.executeQuery(sql);
                if(rs.next()){
                    depthrbp = Util.null2String(rs.getString("depthrbp"));
                }
                if(StringUtils.isEmpty(depthrbp)){
                    sql = "select t1.depthrbp from hrmdepartmentdefined t1, hrmdepartment t2 where t1.deptid = t2.supdepid and t2.id = '"+szbm+"'";
                    bs.writeLog(sql);
                    rs.executeQuery(sql);
                    if(rs.next()){
                        depthrbp = Util.null2String(rs.getString("depthrbp"));
                    }
                }
                ywzgbmglgList = Arrays.asList(depthrbp.split(","));
            }
            if("FXQ".equals(fxq)){//反洗钱类型，业务主管部门管理岗单独取值
                String sjfgs = "";
                String sfsxzx = "";
                sql = "select sfsxzx from hrmdepartmentdefined where deptid ='"+szbm+"'";
                bs.writeLog(sql);
                rs.executeQuery(sql);
                if(rs.next()){
                    sfsxzx = Util.null2String(rs.getString("sfsxzx"));
                }
                if("0".equals(sfsxzx)){//私行中心
                    sjfgs = szbm;
                }else{
                    sql = "select t1.id from hrmdepartment t1,hrmdepartmentdefined t2 where (t1.id = '"+szbm+"' or t1.id = " +
                            "(select supdepid from hrmdepartment where id = '"+szbm+"')) and t2.deptid = t1.id and t2.deptScaleLevelCode = '002'";
                    bs.writeLog(sql);
                    rs.executeQuery(sql);
                    if(rs.next()){
                        sjfgs = Util.null2String(rs.getString("id"));
                    }
                }

                sql = "select ywzgbmglgry,gs from uf_ywzgbmglgrypp where lcid = 'fzjgyy'";
                bs.writeLog(sql);
                rs.executeQuery(sql);
                while(rs.next()){
                    String ywzgbmglgry = Util.null2String(rs.getString("ywzgbmglgry"));
                    String gs = Util.null2String(rs.getString("gs"));
                    for(String gsid:gs.split(",")){
                        if(gsid.equals(sjfgs)){
                            ywzgbmglgList = Arrays.asList(ywzgbmglgry.split(","));
                            break;
                        }
                    }
                }
            }
            List<Map<String,String>> ywzgbmglgMapList = new ArrayList<>();
            for(String id:ywzgbmglgList){
                if(StringUtils.isNotEmpty(id)){
                    Map<String,String> leaderMap = new HashMap<>();
                    leaderMap.put("id",id);
                    leaderMap.put("name",resourceComInfo.getLastname(id));
                    ywzgbmglgMapList.add(leaderMap);
                }
            }
            resultMap.put("ywzgbmglg",ywzgbmglgMapList);

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

        return resultMap;
    }

    /**
     *业务主管部门分送节点，获取下一步骤展示选项
     * @param requestid
     */
    public Map<String,Object> getNextValue(String requestid, String yz, String wjlb, String wjlx){
        BaseBean bs = new BaseBean();
        RecordSet rs = new RecordSet();
        Map<String,Object> resultMap = new HashMap<>();

        String xybz = "12,13,0,15,1,16";
//        String xybz = "12,13,0,1";
        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")){//风险管理部
            xybz += ",2";
        }
        if("ZZ323311".equals(deptCode)||deptList.contains("ZZ323311")){//合规法律部
            xybz += ",3";
        }
        if("ZZ323314".equals(deptCode)||"ZZ001016".equals(deptCode)||deptList.contains("ZZ323314")||deptList.contains("ZZ001016")){//资金运营部或计划财务部
            xybz += ",4";
        }

        //根据印章判断
        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 = 'fzjgyy' 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;
                }else if("htgsgz".equals(qt)||"fddbr".equals(qt)){
                    resultMap.put("bs",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)){//党群工作部
            sghzx = true;
        }

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

        return resultMap;
    }

    /**
     * 获取下级营业部id
     * @param deptid
     */
    public Map<String,Object> getSubordinateDeptid(String deptid){
        RecordSet rs = new RecordSet();
        BaseBean bs = new BaseBean();
        Map<String,Object> resultMap = new HashMap<>();

        Set<String> deptidSet = new HashSet<>();
        String sql = "select deptScaleLevelCode from hrmdepartmentdefined where deptid = '"+deptid+"'";
        bs.writeLog(sql);
        rs.executeQuery(sql);
        if(rs.next()){
            String deptScaleLevelCode = Util.null2String(rs.getString("deptScaleLevelCode"));
            if("010".equals(deptScaleLevelCode)||"006".equals(deptScaleLevelCode)){//本身是营业部，添加自己
                deptidSet.add(deptid);
            }else{
                sql = "select id from hrmdepartment where supdepid = '"+deptid+"'";
                bs.writeLog(sql);
                rs.executeQuery(sql);
                while(rs.next()){
                    deptidSet.add(Util.null2String(rs.getString("id")));
                }
            }
        }

//        sql = "select id from hrmdepartment where supdepid = '"+deptid+"'";
//        bs.writeLog(sql);
//        rs.executeQuery(sql);
//        while(rs.next()){
//            deptidSet.add(Util.null2String(rs.getString("id")));
//        }
//
//        sql = "select t2.deptid from hrmdepartment t1,hrmdepartmentdefined t2 where t2.parentsalecode = t1.departmentcode and t1.id = '"+deptid+"'";
//        bs.writeLog(sql);
//        rs.executeQuery(sql);
//        while(rs.next()){
//            deptidSet.add(Util.null2String(rs.getString("deptid")));
//        }

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

        return resultMap;
    }

    /**
     * 根据证照类型和所属机构（非必填）获取证照列表
     * @param zzlx
     * @return
     */
    public Map<String,Object> getLicenseList(String zzlx, String deptid){
        BaseBean bs = new BaseBean();
        RecordSet rs = new RecordSet();
        Map<String,Object> resultMap = new HashMap<>();

        List<Map<String,String>> licenseList = new ArrayList<>();
        String sql = "";
        if(StringUtils.isNotEmpty(deptid)){
            sql = "select id,zz,wjmc from uf_zzwj where zzlx = '"+zzlx+"' and ssbm = '"+deptid+"'";
        }else {
            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 requestid
     */
    public Map<String,Object> checkMailLabel(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 wjsdfs = "";
        String yzgly = "";
        String sql = "select wjsdfs,yzgly from "+tableName+" where requestid = "+requestid;
        bs.writeLog(sql);
        rs.executeQuery(sql);
        if(rs.next()){
            wjsdfs = Util.null2String(rs.getString("wjsdfs"));
            yzgly = Util.null2String(rs.getString("yzgly"));
        }
        if("2".equals(wjsdfs)){
            flag = true;
        }else {
            flag = false;
        }

        Set<String> useridSet = new HashSet<>();
        List<String> useridList = new ArrayList<>();
        useridList.addAll(Arrays.asList(yzgly.split(",")));
        sql = "select t1.resourceid from hrmrolemembers t1,hrmroles t2 where t1.roleid = t2.id and t2.rolesmark = '总部递送'";
        bs.writeLog(sql);
        rs.executeQuery(sql);
        while(rs.next()){
            useridList.add(Util.null2String(rs.getString("resourceid")));
        }
        useridSet.addAll(useridList);

        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 id:useridList){
                Map<String,String> map =  wfAgentCondition.getAgentInfoByResouce(workflowid,id,requestid);
                if(map.size()>0){
                    useridSet.add(map.get(id));
                }
            }
        }

        SealApplicationService sealApplicationService = ServiceUtil.getService(SealApplicationService.class);
        for(String id:useridSet){
            if(StringUtils.isNotEmpty(id)){
                if(flag){
                    sealApplicationService.updateLabel(requestid,id,"add","邮寄","#FAE257");
                }else {
                    sealApplicationService.updateLabel(requestid,id,"delete","邮寄","#FAE257");
                }
            }

        }

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

    /**
     * 业务主管部门管理岗，文件类别类型范围
     * @param ywzgbm
     * @return
     */
    public Map<String,Object> getOptionRange(String ywzgbm){
        RecordSet rs = new RecordSet();
        BaseBean bs = new BaseBean();
        Map<String,Object> resultMap = new HashMap<>();

        List<String> rangeList = new ArrayList<>();
        Map<String,Set<String>> rangeMap = new HashMap<>();
        String sql = "select wjlb,wjlx from uf_fzjgyysyfw where ywzgbm = '"+ywzgbm+"'";
        bs.writeLog(sql);
        rs.executeQuery(sql);
        while(rs.next()){
            String wjlb = Util.null2String(rs.getString("wjlb"));
            String wjlx = Util.null2String(rs.getString("wjlx"));

            if(StringUtils.isNotEmpty(wjlb)){
                Set<String> sblxSet = rangeMap.get(wjlb);
                if(sblxSet!=null){
                    sblxSet.add(wjlx);
                }else{
                    rangeList.add(wjlb);
                    sblxSet = new HashSet<>();
                    sblxSet.add(wjlx);
                    rangeMap.put(wjlb,sblxSet);
                }
            }
        }

        resultMap.put("wjlb",String.join(",",rangeList));
        resultMap.put("wjlx",rangeMap);

        return resultMap;
    }
}
