package com.engine.htsc.seal.service;

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.Util;
import weaver.hrm.resource.ResourceComInfo;
import weaver.interfaces.htsc.GlobatTools.JAVATools;
import weaver.interfaces.htsc.comInfo.PropBean;

import java.util.*;

public class ZgSealApplicationService extends Service {

    /**
     * 根据印章和文件类别类型获取默认内部审核、会签部门、跨公司会签、公司领导
     * @param yz
     * @param wjlb
     * @param wjlx
     * @param szbm
     * @return
     */
    public Map<String,Object> getDeptBySealAndFileType(String yz, String wjlb, String wjlx, String szbm){
        PropBean pb = new PropBean();
        BaseBean bs = new BaseBean();
        RecordSet rs = new RecordSet();
        Map<String,Object> resultMap = new HashMap<>();

        try{
            String sql;
            ResourceComInfo resourceComInfo = new ResourceComInfo();
            List<String> sealList = new ArrayList<>();
            if(StringUtils.isNotEmpty(yz)){
                sealList = Arrays.asList(yz.split(","));
            }

            String qt = "";
            String bjhqbm = "";
            String bjkgshqbm = "";
            sql = "select qt,bjhqbm,bjkgshqbm from uf_zgyysyfw where wjlb = '"+wjlb+"' and wjlx = '"+wjlx+"'";
            bs.writeLog(sql);
            rs.executeQuery(sql);
            if(rs.next()){
                qt = Util.null2String(rs.getString("qt"));
                bjhqbm = Util.null2String(rs.getString("bjhqbm"));
                bjkgshqbm = Util.null2String(rs.getString("bjkgshqbm"));
            }

            List<Map<String,String>> nbshMapList = new ArrayList<>();
            //当文件类别为合同时，自动加载拟稿部门的合规管理专员反洗钱岗为内部审核
            if("ht".equals(qt)){
                String postid = "";
                String postUser = "";
                sql = "select id from uf_gw where gwmc = '合规管理专员反洗钱岗'";
                bs.writeLog(sql);
                rs.executeQuery(sql);
                if(rs.next()){
                    postid = Util.null2String(rs.getString("id"));
                }
                if(StringUtils.isNotEmpty(postid)){
                    sql = "select ry from uf_lcgwry where xzlc = 'zgyy' and jg = '"+szbm+"' and xzgw = '"+postid+"'";
                    bs.writeLog(sql);
                    rs.executeQuery(sql);
                    if(rs.next()){
                        postUser = Util.null2String(rs.getString("ry"));
                    }
                }
                for(String id:postUser.split(",")){
                    Map<String,String> userMap = new HashMap<>();
                    userMap.put("id",id);
                    userMap.put("name",resourceComInfo.getLastname(id));
                    nbshMapList.add(userMap);
                }
            }
            resultMap.put("nbsh",nbshMapList);//内部审核

            Set<String> hqbmSet = new HashSet<>();
            if(StringUtils.isNotBlank(bjhqbm)){
                hqbmSet.addAll(Arrays.asList(bjhqbm.split(",")));
            }
            //印章包含财务专用章时，会签部门默认加载运营部
            resultMap.put("cwz",false);
            String cwzyz = "";
            sql = "select yz,bshq from uf_tsld where lcxx = 'zgyy' and qt = 'cwzyz'";
            bs.writeLog(sql);
            rs.executeQuery(sql);
            if(rs.next()){
                cwzyz = Util.null2String(rs.getString("yz"));
                if(sealList.contains(cwzyz)){
                    hqbmSet.add(Util.null2String(rs.getString("bshq")));
                    resultMap.put("cwz",true);
                }
            }
            List<Map<String,String>> hqbmMapList = new ArrayList<>();
            for(String deptid:hqbmSet){
                if(StringUtils.isNotEmpty(deptid)){
                    sql = "select departmentname from hrmdepartment where id = '"+deptid+"'";
                    bs.writeLog(sql);
                    rs.executeQuery(sql);
                    if(rs.next()){
                        Map<String,String> deptMap = new HashMap<>();
                        deptMap.put("id",deptid);
                        deptMap.put("name",Util.null2String(rs.getString("departmentname")));
                        hqbmMapList.add(deptMap);
                    }
                }
            }
            resultMap.put("hqbm",hqbmMapList);//会签部门

            Set<String> kgshqSet = new HashSet<>();
            if(StringUtils.isNotBlank(bjkgshqbm)){
                kgshqSet.addAll(Arrays.asList(bjkgshqbm.split(",")));
            }
            List<Map<String,String>> kgshqMapList = new ArrayList<>();
            for(String deptid:kgshqSet){
                sql = "select deptdirectors from hrmdepartmentdefined where deptid = '"+deptid+"'";
                bs.writeLog(sql);
                rs.executeQuery(sql);
                if(rs.next()){
                    String deptdirectors = Util.null2String(rs.getString("deptdirectors"));
                    if(StringUtils.isNotBlank(deptdirectors)){
                        for(String userid:deptdirectors.split(",")){
                            Map<String,String> deptMap = new HashMap<>();
                            deptMap.put("id",userid);
                            deptMap.put("name",resourceComInfo.getLastname(userid));
                            kgshqMapList.add(deptMap);
                        }
                    }
                }
            }
            resultMap.put("kgshq",kgshqMapList);//跨公司会签

            List<Map<String,String>> gsldMapList = new ArrayList<>();
            List<Map<String,String>> fgldMapList = new ArrayList<>();
            String gsgz = "";
            String fddbrz = "";
            sql = "select yz from uf_tsld where lcxx = 'zgyy' and qt = 'zggsgz'";
            bs.writeLog(sql);
            rs.executeQuery(sql);
            if(rs.next()){
                gsgz = Util.null2String(rs.getString("yz"));
            }
            sql = "select yz from uf_tsld where lcxx = 'zgyy' and qt = 'fddbr'";
            bs.writeLog(sql);
            rs.executeQuery(sql);
            if(rs.next()){
                fddbrz = Util.null2String(rs.getString("yz"));
            }
            //文件类型不属于银证通道业务、公募业务存款、可转债网下一级申购、自有资金投资，并印章选择包含公司公章和法人章
            if(StringUtils.isNotBlank(wjlx) && !Arrays.asList("9","10","15","13").contains(wjlx) && (sealList.contains(gsgz)||sealList.contains(fddbrz))){
                List<String> gsldidList = new ArrayList<>();
                List<String> fgldidList = new ArrayList<>();

                //分管领导
                String deptleaders = "";
                sql = "select deptleaders from hrmdepartmentdefined where deptid = '"+szbm+"'";
                bs.writeLog(sql);
                rs.executeQuery(sql);
                if(rs.next()){
                    deptleaders = Util.null2String(rs.getString("deptleaders"));
                }

                if(Arrays.asList("18","20","22").contains(wjlx)){//ABS-已授权文件
                    gsldidList.addAll(Arrays.asList(pb.getPropName("zg-hgzj").split(",")));
                    gsldidList.addAll(Arrays.asList(pb.getPropName("zg-ABS").split(",")));
                    if(StringUtils.isNotBlank(deptleaders)){
                        fgldidList.addAll(Arrays.asList(deptleaders.split(",")));
                    }
                }else if(Arrays.asList("25","26","27").contains(wjlx)){//运营管理-审计询证函
                    gsldidList.addAll(Arrays.asList(pb.getPropName("zg-hgzj").split(",")));
                    gsldidList.addAll(Arrays.asList(pb.getPropName("zg-yygl").split(",")));
                    if(StringUtils.isNotBlank(deptleaders)){
                        fgldidList.addAll(Arrays.asList(deptleaders.split(",")));
                    }
                }else{//其他情况加载公司领导，分管领导去重
                    gsldidList.addAll(Arrays.asList(pb.getPropName("zg-gsld").split(",")));
                    if(StringUtils.isNotBlank(deptleaders)){
                        for(String fgldid:deptleaders.split(",")){
                            boolean flag = false;
                            for(String gsldid:gsldidList){
                                if(compareLoginid(fgldid,gsldid)){
                                    flag = true;
                                    break;
                                }
                            }
                            if(!flag){
                                fgldidList.add(fgldid);
                            }
                        }
                    }
                }

                for(String id:gsldidList){
                    if(StringUtils.isNotEmpty(id)){
                        Map<String,String> userMap = new HashMap<>();
                        userMap.put("id",id);
                        userMap.put("name",resourceComInfo.getLastname(id));
                        gsldMapList.add(userMap);
                    }
                }
                for(String id:fgldidList){
                    if(StringUtils.isNotEmpty(id)){
                        Map<String,String> userMap = new HashMap<>();
                        userMap.put("id",id);
                        userMap.put("name",resourceComInfo.getLastname(id));
                        fgldMapList.add(userMap);
                    }
                }
            }
            resultMap.put("gsld",gsldMapList);//公司领导
            resultMap.put("fgld",fgldMapList);//分管领导

        }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 = 'zgyy' and t2.qt = 'zggsgz'";
            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 = 'zgyy' 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);
            }

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

        return resultMap;
    }

    /**
     * 获取审批过的审批人
     * @param requestid
     * @return
     */
    public Map<String,Object> getPassLeader(String requestid, String nodeUnNumber){
        PropBean pb = new PropBean();
        RecordSet rs = new RecordSet();
        BaseBean bs = new BaseBean();
        Map<String,Object> resultMap = new HashMap<>();

        String nodeid = pb.getNodeIdsByUnNumber(nodeUnNumber);

        Set<String> useridSet = new HashSet<>();
        String sql = "select operator,agentorbyagentid from workflow_requestlog where requestid = '"+requestid+"' and nodeid = '"+nodeid+"'";
        bs.writeLog(sql);
        rs.executeQuery(sql);
        while(rs.next()){
            String agentorbyagentid = Util.null2String(rs.getString("agentorbyagentid"));
            if("-1".equals(agentorbyagentid)){//授权，获取原处理人
                String operator = Util.null2String(rs.getString("operator"));
                useridSet.addAll(getAllUserid(operator));
            }else{
                useridSet.addAll(getAllUserid(agentorbyagentid));
            }
        }

        resultMap.put("passLeader",useridSet);
        return resultMap;
    }

    /**
     * 获取分送类型，是否强制过合规稽核部
     * @param yz
     * @param wjlb
     * @param wjlx
     * @return
     */
    public Map<String,Object> getSendType(String yz, String wjlb, String wjlx){
        PropBean pb = new PropBean();
        RecordSet rs = new RecordSet();
        BaseBean bs = new BaseBean();
        Map<String,Object> resultMap = new HashMap<>();

        String sfbqzghgjhb = "";//是否不强制过合规稽核部
        String sql = "select sfbqzghgjhb from uf_zgyysyfw where wjlb = '"+wjlb+"' and wjlx = '"+wjlx+"'";
        bs.writeLog(sql);
        rs.executeQuery(sql);
        if(rs.next()){
            sfbqzghgjhb = Util.null2String(rs.getString("sfbqzghgjhb"));
        }
        if("1".equals(sfbqzghgjhb)&&StringUtils.isNotBlank(yz)){
            String ywzid = "";
            sql = "select yz from uf_tsld where qt = 'ywz'";
            bs.writeLog(sql);
            rs.executeQuery(sql);
            if(rs.next()){
                ywzid = Util.null2String(rs.getString("yz"));
            }

            boolean only = true;
            sql = "select id from uf_yinzhang where id in ("+yz+") and yzlx <> "+ywzid;
            bs.writeLog(sql);
            rs.executeQuery(sql);
            while(rs.next()){
                only = false;
            }
            if(only){
                resultMap.put("sfqzshgjhb",false);
                return resultMap;
            }

        }

        resultMap.put("sfqzshgjhb",true);
        return resultMap;
    }

    /**
     * 获得全部工号
     * @param id
     * @return
     */
    private List<String> getAllUserid(String id){
        BaseBean bs = new BaseBean();
        RecordSet rs = new RecordSet();
        List<String> idList = new ArrayList<>();

        String mainUserid = JAVATools.getmainhrmid(id);
        idList.add(mainUserid);
        String sql="select id from hrmresource where belongto = '"+mainUserid+"'";
        bs.writeLog(sql);
        rs.executeQuery(sql);
        while(rs.next()){
            idList.add(Util.null2String(rs.getString("id")));
        }
        return idList;
    }


    /**
     * 根据id获得岗位
     * @param userid
     * @return
     */
    public Map<String,Object> getJob(String userid) {
        BaseBean bs = new BaseBean();
        RecordSet rs = new RecordSet();
        Map<String,Object> resultMap = new HashMap<>();

        List<Map<String,String>> userMapList = new ArrayList<>();
        for(String id:userid.split(",")){
            if(StringUtils.isNotEmpty(id)){
                String sql="select h.lastname,s.jobtitlename from hrmresource h,hrmjobtitles s where h.jobtitle =s.id and h.id='"+id+"'";
                bs.writeLog(sql);
                rs.executeQuery(sql);
                if (rs.next()) {
                    Map<String,String> userMap = new HashMap<>();
                    userMap.put("id",id);
                    userMap.put("name",Util.null2String(rs.getString("lastname")));
                    userMap.put("job",Util.null2String(rs.getString("jobtitlename")));
                    userMapList.add(userMap);
                }
            }
        }
        resultMap.put("job",userMapList);
        return resultMap;
    }

    /**
     * 比较工号
     * @param userid_1
     * @param userid_2
     * @return
     */
    private boolean compareLoginid(String userid_1,String userid_2){
        RecordSet rs = new RecordSet();
        BaseBean bs = new BaseBean();
        String loginid_1 = "";
        String loginid_2 = "";
        String sql = "select workcode from hrmresource where id = '"+userid_1+"'";
        rs.executeQuery(sql);
        if(rs.next()){
            loginid_1 = Util.null2String(rs.getString("workcode"));
        }
        sql = "select workcode from hrmresource where id = '"+userid_2+"'";
        rs.executeQuery(sql);
        if(rs.next()){
            loginid_2 = Util.null2String(rs.getString("workcode"));
        }
        if(StringUtils.isNotEmpty(loginid_1)&&StringUtils.isNotEmpty(loginid_2)&&loginid_1.equals(loginid_2)){
            return true;
        }else{
            return false;
        }
    }
}
