package com.engine.htsc.seal.service;


import com.engine.core.impl.Service;
import com.weaver.general.TimeUtil;
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.comInfo.PropBean;

import java.util.*;

public class UnionSealApplicationService extends Service {

    /**
     * 根据印章和用印类型获取用印管理员
     * @param yz
     * @return
     */
    public Map<String,Object> getYzgly(String yz, String dzyy){
        BaseBean bs = new BaseBean();
        RecordSet rs = new RecordSet();
        Map<String,Object> resultMap = new HashMap<>();

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

            List<Map<String,String>> yzglyMapList = new ArrayList<>();//印章管理员
            Set<String> yzglySet = new HashSet<>();
            sql = "select sfdzyz,yygly,yyglyhdzyyqx from uf_lhyz where id in ("+yz+")";
            bs.writeLog(sql);
            rs.executeQuery(sql);
            while(rs.next()){
                if("1".equals(dzyy)&&"0".equals(Util.null2String(rs.getString("sfdzyz")))){
                    yzglySet.addAll(Arrays.asList(Util.null2String(rs.getString("yyglyhdzyyqx")).split(",")));
                }else {
                    yzglySet.addAll(Arrays.asList(Util.null2String(rs.getString("yygly")).split(",")));
                    yzglySet.addAll(Arrays.asList(Util.null2String(rs.getString("yyglyhdzyyqx")).split(",")));
                }
            }

            for(String id:yzglySet){
                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 requestid
     * @return
     */
    public Map<String,Object> getPassUser(String requestid){
        PropBean pb = new PropBean();
        RecordSet rs = new RecordSet();
        BaseBean bs = new BaseBean();
        Map<String,Object> resultMap = new HashMap<>();

        String lhyy_xgrysh_nodeid = pb.getNodeIdsByUnNumber("lhyy_xgrysh");
        String lhyy_hq_nodeid = pb.getNodeIdsByUnNumber("lhyy_hq");
        String lhyy_kgshq_nodeid = pb.getNodeIdsByUnNumber("lhyy_kgshq");
        String lhyy_gsld_nodeid = pb.getNodeIdsByUnNumber("lhyy_gsld");

        List<Map<String,String>> xgryshMap = getPassUserByNodeid(requestid,lhyy_xgrysh_nodeid);
        resultMap.put("xgrysh",xgryshMap);
        List<Map<String,String>> hqMap = getPassUserByNodeid(requestid,lhyy_hq_nodeid);
        resultMap.put("hq",hqMap);
        List<Map<String,String>> kgshqMap = getPassUserByNodeid(requestid,lhyy_kgshq_nodeid);
        resultMap.put("kgshq",kgshqMap);
        List<Map<String,String>> gsldMap = getPassUserByNodeid(requestid,lhyy_gsld_nodeid);
        resultMap.put("gsld",gsldMap);


        return resultMap;
    }

    public List<Map<String,String>> getPassUserByNodeid(String requestid, String nodeid){
        RecordSet rs = new RecordSet();
        BaseBean bs = new BaseBean();
        List<Map<String,String>> userMapList = new ArrayList<>();

        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.add(operator);
            }else{
                useridSet.add(agentorbyagentid);
            }
        }

        for(String userid:useridSet){
            if(StringUtils.isNotEmpty(userid)){
                Map<String,String> userMap = new HashMap<>();
                sql = "select t1.id as userid,t1.lastname,t2.id as deptid,t2.departmentname from hrmresource t1,hrmdepartment t2 where t1.departmentid = t2.id and t1.id = '"+userid+"'";
                bs.writeLog(sql);
                rs.executeQuery(sql);
                if(rs.next()){
                    userMap.put("id",Util.null2String(rs.getString("userid")));
                    userMap.put("name",Util.null2String(rs.getString("lastname")));
                    userMap.put("deptid",Util.null2String(rs.getString("deptid")));
                    userMap.put("deptname",Util.null2String(rs.getString("departmentname")));
                }
                userMapList.add(userMap);
            }
        }

        return userMapList;
    }

    /**
     * 会签部门联席负责人抄送
     * @param requestid
     * @param nodeid
     * @param deptid
     */
    public Map<String,Object> noticeSignDeptCoDirector(String requestid, String nodeid, String deptid){
        RecordSet rs = new RecordSet();
        BaseBean bs = new BaseBean();
        Map<String,Object> resultMap = new HashMap<>();

        //联席负责人
        String cohead = "";
        String sql = "select cohead from hrmdepartmentdefined where deptid = '"+deptid+"'";
        bs.writeLog(sql);
        rs.executeQuery(sql);
        if(rs.next()){
            cohead = Util.null2String(rs.getString("cohead"));
        }

        if(StringUtils.isNotEmpty(cohead)){
            for(String id:cohead.split(",")){
                String workflowid = "";
                String workflowtype = "";
                sql = "select t1.id,t1.workflowtype from workflow_base t1,workflow_requestbase t2 where t2.workflowid = t1.id and t2.requestid = '"+requestid+"'";
                bs.writeLog(sql);
                rs.executeQuery(sql);
                if(rs.next()){
                    workflowid = Util.null2String(rs.getString("id"));
                    workflowtype = Util.null2String(rs.getString("workflowtype"));
                }

                char flag = com.weaver.general.Util.getSeparator();
                String operatorgroup = "0";
                String creatertype = "0";
                String currentdate = TimeUtil.getCurrentDateString();
                String currenttime =  TimeUtil.getCurrentTimeString().substring(11,19);
                String isremarktemp = "9";
                String lastnodeid = ""+nodeid;
                String groupdetailid = "-1";
                String useridtemp = id;//抄送人
                String Procparatemp = "" + requestid + flag + useridtemp + flag + operatorgroup + flag + workflowid + flag
                        + workflowtype + flag + creatertype + flag + isremarktemp + flag + lastnodeid + flag + -1
                        + flag + "0" + flag + -1 + flag + groupdetailid+flag+currentdate+flag+currenttime;
                bs.writeLog("chaosong Procparatemp====="+Procparatemp);
                boolean executesuccess = rs.executeProc("workflow_CurrentOperator_I2", Procparatemp);
                resultMap.put("result",executesuccess);

                if(executesuccess){
                    bs.writeLog("CC SUCCESS:"+executesuccess);
                }else{
                    bs.writeLog("CC FAIL:"+executesuccess);
                }
            }
        }
        return resultMap;
    }

    /**
     * 校验联席负责人是否阅签
     * @param requestid
     * @param nodename
     * @return
     */
    public Map<String,Object> checkCoDirectorNotice(String requestid, String nodename){
        RecordSet rs = new RecordSet();
        BaseBean bs = new BaseBean();
        PropBean pb = new PropBean();
        Map<String,Object> resultMap = new HashMap<>();

        try{
            ResourceComInfo resourceComInfo = new ResourceComInfo();

            String nodeid = "";
            if("bmfzr".equals(nodename)){
                nodeid = pb.getNodeIdsByUnNumber("lhyy_bmfzr");
            }else if("hq".equals(nodename)){
                nodeid = pb.getNodeIdsByUnNumber("lhyy_hq");
            }

            List<Map<String,String>> userMapList = new ArrayList<>();
            String sql = "select userid from workflow_currentoperator where requestid = '"+requestid+"' and nodeid = '"+nodeid+"' and isremark = '9'";
            bs.writeLog(sql);
            rs.executeQuery(sql);
            while(rs.next()){
                Map<String,String> userMap = new HashMap<>();
                String userid = Util.null2String(rs.getString("userid"));
                userMap.put("id",userid);
                userMap.put("name",resourceComInfo.getLastname(userid));
                userMapList.add(userMap);
            }

            resultMap.put("result",userMapList);
        }catch (Exception e){
            e.printStackTrace();
        }

        return resultMap;
    }
}
