package com.engine.htsc.lhfxhgtysp.service.impl;

import com.engine.core.impl.Service;
import com.engine.htsc.lhfxhgtysp.service.LhfxhgtyspService;
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.*;

/**
 * @ClassName LhqssqServiceImpl
 * @Author K1810015
 * @Date 2022/3/1 10:36
 * Version 1.0
 */
public class LhfxhgtyspServiceImpl extends Service implements LhfxhgtyspService {


    /**
     * 校验联席负责人是否阅签
     * @param requestid
     * @param nodename
     * @return
     */
    @Override
    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(pb.getPropName("lhfxhg.nodeunnumber.lhfxhg_bmfzr"));

            }else if("hq".equals(nodename)){
                nodeid = pb.getNodeIdsByUnNumber(pb.getPropName("lhfxhg.nodeunnumber.lhfxhg_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;
    }

    /**
     * 会签部门联席负责人抄送
     * @param requestid
     * @param nodeid
     * @param deptid
     */
    @Override
    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;
    }

    @Override
    public Map<String, Object> workflowdone(Map<String, Object> map) {
        PropBean pb = new PropBean();
        String requestid=Util.null2String(map.get("requestid"));
        Map<String,Object> resultMap = new HashMap<>();
        String lhfxhg_hgfkg_unnumber = pb.getPropName("lhfxhg.nodeunnumber.lhfxhg_hgfkg");
        String lhfxhg_fxglbfzr_unnumber = pb.getPropName("lhfxhg.nodeunnumber.lhfxhg_fxglbfzr");
        String lhfxhg_hgzj_unnumber = pb.getPropName("lhfxhg.nodeunnumber.lhfxhg_hgzj");
        String lhfxhg_xgrysh_unnumber = pb.getPropName("lhfxhg.nodeunnumber.lhfxhg_xgrysh");
        String lhfxhg_hq_unnumber = pb.getPropName("lhfxhg.nodeunnumber.lhfxhg_hq");
        String lhfxhg_kgshq_unnumber = pb.getPropName("lhfxhg.nodeunnumber.lhfxhg_kgshq");
        String lhfxhg_gsld_unnumber = pb.getPropName("lhfxhg.nodeunnumber.lhfxhg_gsld");
        List<Map<String,String>> hgfkg = getPassUserByNodeid(requestid,lhfxhg_hgfkg_unnumber);
        resultMap.put("hgfkg",hgfkg);
        List<Map<String,String>> fxglbfzr = getPassUserByNodeid(requestid,lhfxhg_fxglbfzr_unnumber);
        resultMap.put("fxglbfzr",fxglbfzr);
        List<Map<String,String>> hgzj = getPassUserByNodeid(requestid,lhfxhg_hgzj_unnumber);
        resultMap.put("hgzj",hgzj);
        List<Map<String,String>> xgryshMap = getPassUserByNodeid(requestid,lhfxhg_xgrysh_unnumber);
        resultMap.put("xgrysh",xgryshMap);
        List<Map<String,String>> hqMap = getPassUserByNodeid(requestid,lhfxhg_hq_unnumber);
        resultMap.put("hq",hqMap);
        List<Map<String,String>> kgshqMap = getPassUserByNodeid(requestid,lhfxhg_kgshq_unnumber);
        resultMap.put("kgshq",kgshqMap);
        List<Map<String,String>> gsldMap = getPassUserByNodeid(requestid,lhfxhg_gsld_unnumber);
        resultMap.put("gsld",gsldMap);


        return resultMap;

    }

    @Override
    public List<Map<String, String>>JudgmentAudit(String requestid, String nodename) {
        RecordSet rs = new RecordSet();
        BaseBean bs = new BaseBean();
        PropBean pb = new PropBean();
        String lhfxhg_fxglbfzr_nodeid =  pb.getPropName("lhfxhg_fxglbfzr_nodeid");
        String lhfxhg_hgzj_nodeid =  pb.getPropName("lhfxhg_hgzj_nodeid");

        ArrayList<Map<String, String>> list = new ArrayList<>();
        ResourceComInfo resourceComInfo = null;
        try {
            resourceComInfo = new ResourceComInfo();
            Map<String,Object> resultMap = new HashMap<>();
            String sql = "select operator,nodeid from workflow_requestlog where requestid = '"+requestid+"' and (nodeid = '"+lhfxhg_fxglbfzr_nodeid+"' or nodeid = '"+lhfxhg_hgzj_nodeid+"') ";
            
            System.out.println(sql);
            bs.writeLog(sql);
            rs.executeQuery(sql);
            while(rs.next()){
                Map<String,String> userMap = new HashMap<>();
                String userid = Util.null2String(rs.getString("operator"));
                userMap.put("id",userid);
                userMap.put("name",resourceComInfo.getLastname(userid));
                list.add(userMap);
            }
        } catch (Exception e) {
            e.printStackTrace();
        }


        return list;

    }


    private List<Map<String,String>>  getPassUserByNodeid(String requestid, String nodenumbber){
        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 in ( select id from workflow_nodebase where unnumber='"+nodenumbber+"')   order by  to_date((operatedate || ' ' || OPERATETIME),'yyyy-MM-dd hh24:mi:ss')  desc";
        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;
    }




}

