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

import com.engine.core.impl.Service;
import com.engine.htsc.itxqglpt.cmd.itxqglptXxjsbCmd;
import com.engine.htsc.itxqglpt.service.itxqglptService;
import com.engine.htsc.itxqglpt.cmd.ItxqlcLdspCmd;
import org.apache.commons.lang.StringUtils;
import weaver.conn.RecordSet;
import weaver.general.BaseBean;
import weaver.general.Util;
import weaver.hrm.User;
import weaver.interfaces.htsc.comInfo.PropBean;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @Title: ecology-9
 * @Company: 泛微软件
 * @author: K1810039
 * @version: 1.0
 * @CreateDate: 2022-08-19
 * @Description: IT需求管理平台流程
 * @ModifyLog:
 **/
public class itxqglptServiceImpl extends Service implements itxqglptService {


    @Override
    public Map<String, Object> getXxjsbInfo(Map<String, Object> params) {
        return commandExecutor.execute(new itxqglptXxjsbCmd(params));
    }

    @Override
    public Map<String, Object> checkJw(String requestid) {
        RecordSet rs = new RecordSet();
        RecordSet rs2 = new RecordSet();
        BaseBean bs = new BaseBean();
        PropBean pb = new PropBean();
        String sql ="";
        String jwId ="";
        bs.writeLog("----checkJw-----");

        Map<String, Object> map = new HashMap<>();
        Map<String, Object> operatorsMap = new HashMap<>();

        String jwLoginid = pb.getPropName("jwLoginid");
        sql = "select id from HRMRESOURCE where LOGINID ='"+jwLoginid+"'";
        bs.writeLog(sql);
        rs.execute(sql);
        if (rs.next()){
            jwId = rs.getString("id");
            bs.writeLog("jwId-->"+jwId);
        }

        // 拿江旺的最新意见，若无则为false
        //sql = "select t.operator,t.agentorbyagentid,t.AGENTTYPE,t.logtype,t.REMARK,t.RECEIVEDPERSONIDS from workflow_requestlog t,(\n" +
        //        "select operator,max(to_date((operatedate || ' ' || OPERATETIME),'yyyy-MM-dd hh24:mi:ss'))as time\n" +
        //        "from workflow_requestlog\n" +
        //        "GROUP BY operator\n" +
        //        "ORDER BY count(*)DESC) a\n" +
        //        "where t.operator=a.operator\n" +
        //        "  and to_date((t.operatedate || ' ' || t.OPERATETIME),'yyyy-MM-dd hh24:mi:ss')=a.time\n" +
        //        "  and t.requestid = ? and t.OPERATOR = ?";
        sql = "select t.operator,t.agentorbyagentid,t.AGENTTYPE,t.logtype,t.REMARK,t.RECEIVEDPERSONIDS,t.operatedate,t.OPERATETIME from workflow_requestlog t\n" +
                "where t.requestid = ? and OPERATOR = ?\n" +
                "order by  to_date((t.operatedate || ' ' || t.OPERATETIME),'yyyy-MM-dd hh24:mi:ss') asc ";
        bs.writeLog(sql);
        rs.executeQuery(sql,requestid,jwId);
        while (rs.next()){
            String agentorbyagentid = Util.null2String(rs.getString("agentorbyagentid"));
            String agenttype = Util.null2String(rs.getString("AGENTTYPE"));
            String operator = Util.null2String(rs.getString("operator"));
            String logtype = Util.null2String(rs.getString("logtype"));
            String receivedpersonids = Util.null2String(rs.getString("RECEIVEDPERSONIDS"));
            receivedpersonids = receivedpersonids.substring(0,receivedpersonids.length()-1);
            if ("0".equals(agenttype)){
                // 没有代理时
                while ("h".equals(logtype)){
                    // 如果是转办，则取接收人的最新的意见
                    //String receiver = receivedpersonids;
                    String sql2 = "select t.operator,t.agentorbyagentid,t.AGENTTYPE,t.logtype,t.REMARK,t.RECEIVEDPERSONIDS,t.operatedate,t.OPERATETIME from workflow_requestlog t\n" +
                            "where t.requestid = ? and OPERATOR = ?\n" +
                            "order by  to_date((t.operatedate || ' ' || t.OPERATETIME),'yyyy-MM-dd hh24:mi:ss') asc ";
                    rs2.executeQuery(sql2,requestid,receivedpersonids);
                    while (rs2.next()) {
                        logtype = Util.null2String(rs2.getString("logtype"));
                        receivedpersonids = Util.null2String(rs2.getString("RECEIVEDPERSONIDS"));
                        receivedpersonids = receivedpersonids.substring(0, receivedpersonids.length() - 1);
                    }
                    //}else{
                    //    break;
                    //}

                }
                operatorsMap.put(operator,logtype);
            }else if ("2".equals(agenttype)){
                // 当前记录是代理人记录时，agentorbyagentid是被代理人的id
                while ("h".equals(logtype)){
                    // 如果是转办，则取接收人的意见
                    //String sql2 = "select t.operator,t.agentorbyagentid,t.AGENTTYPE,t.logtype,t.REMARK,t.RECEIVEDPERSONIDS from workflow_requestlog t,(\n" +
                    //        "select operator,max(to_date((operatedate || ' ' || OPERATETIME),'yyyy-MM-dd hh24:mi:ss'))as time\n" +
                    //        "from workflow_requestlog\n" +
                    //        "GROUP BY operator\n" +
                    //        "ORDER BY count(*)DESC) a\n" +
                    //        "where t.operator=a.operator\n" +
                    //        "  and to_date((t.operatedate || ' ' || t.OPERATETIME),'yyyy-MM-dd hh24:mi:ss')=a.time\n" +
                    //        "  and t.requestid = ? and t.OPERATOR = ?";
                    String sql2 = "select t.operator,t.agentorbyagentid,t.AGENTTYPE,t.logtype,t.REMARK,t.RECEIVEDPERSONIDS,t.operatedate,t.OPERATETIME from workflow_requestlog t\n" +
                            "where t.requestid = ? and OPERATOR = ?\n" +
                            "order by  to_date((t.operatedate || ' ' || t.OPERATETIME),'yyyy-MM-dd hh24:mi:ss') asc ";
                    rs2.executeQuery(sql2,requestid,receivedpersonids);
                    //if(rs2.next()){
                    //    logtype = Util.null2String(rs2.getString("logtype"));
                    //    receivedpersonids = Util.null2String(rs2.getString("RECEIVEDPERSONIDS"));
                    //    receivedpersonids = receivedpersonids.substring(0,receivedpersonids.length()-1);
                    //}else{
                    //    break;
                    //}
                    while (rs2.next()){
                        logtype = Util.null2String(rs2.getString("logtype"));
                        receivedpersonids = Util.null2String(rs2.getString("RECEIVEDPERSONIDS"));
                        receivedpersonids = receivedpersonids.substring(0,receivedpersonids.length()-1);
                    }

                }
                operatorsMap.put(agentorbyagentid,logtype);
            }
        }

        // 获取已送，但尚未操作的人，这里把他的意见置为空
        sql = "select USERID,ISREMARK,AGENTORBYAGENTID,AGENTTYPE\n" +
                "from workflow_currentoperator\n" +
                "where REQUESTID = ? and ISREMARK = 0 ";

        bs.writeLog(sql);
        rs.executeQuery(sql,requestid);
        while(rs.next()){
            String userid = Util.null2String(rs.getString("USERID"));
            operatorsMap.put(userid,"");
        }
        bs.writeLog("operatorsMap-->"+operatorsMap);

        // 从map中获取江旺的意见
        String hsJw = Util.null2String(operatorsMap.get(jwId));
        bs.writeLog("hsJw-->"+hsJw);

        if ("0".equals(hsJw) || "2".equals(hsJw)){
            // 如果意见为批准，或者提交，则代表已送且同意
            map.put("data","true");
        }else {
            // 否则不满足已送且同意，返回false
            map.put("data","false");
        }
        bs.writeLog("map-->"+map);
        return map;
    }

    @Override
    public Map<String, Object> checkHasJw(String requestid,String currentUserId) {
        RecordSet rs = new RecordSet();
        BaseBean bs = new BaseBean();
        PropBean pb = new PropBean();
        String sql ="";
        String unnumber ="";
        String nodeName ="";
        String groupidJw ="";
        String groupidCurrentUser ="";
        bs.writeLog("----checkHasJw-----");
        Map<String, Object> map = new HashMap<>();

        // 获取江旺 id
        String loginidJw = pb.getPropName("jwLoginid");

        // 根据requestid判断是IT需求流程  还是  IT需求管理平台流程
        sql = "select UNNUMBER from WORKFLOW_REQUESTBASE a join WORKFLOW_BASE b on b.ID = a.WORKFLOWID where a.REQUESTID = "+requestid;
        bs.writeLog(sql);
        rs.execute(sql);
        if (rs.next()){
            unnumber = rs.getString("UNNUMBER");
        }
        if ("itxqglpt".equals(unnumber)){
            nodeName = "itxqglpt_hqbm";
        }else if ("itxqsq".equals(unnumber)){
            nodeName = "itxqsq_hqbm";
        }

        // 查询会签节点 获取江旺的GROUPID，江旺转发/转办/内审/授权给其他人时，其他人的GROUPID与江旺的是相同的。
        sql = "select a.USERID,a.GROUPID from workflow_currentoperator a\n" +
                "    join workflow_nodebase b on a.NODEID = b.ID\n" +
                "    join HRMRESOURCE h on a.USERID = h.ID\n" +
                " where a.REQUESTID = "+requestid+" and b.UNNUMBER = '"+nodeName+"' and h.LOGINID = '"+loginidJw+"' and a.processuser is null " +
                " order by  to_date((a.RECEIVEDATE || ' ' || a.RECEIVETIME),'yyyy-MM-dd hh24:mi:ss') desc ";
        bs.writeLog(sql);
        rs.execute(sql);
        if (rs.next()){
            groupidJw = rs.getString("GROUPID");
        }

        // 获取当前操作人的 groupid
        sql = "select a.USERID,a.GROUPID from workflow_currentoperator a join workflow_nodebase b\n" +
                "    on a.NODEID = b.ID where a.REQUESTID = "+requestid+" and b.UNNUMBER = '"+nodeName+"' and a.USERID = "+currentUserId+" and a.processuser is null" +
                " order by  to_date((a.RECEIVEDATE || ' ' || a.RECEIVETIME),'yyyy-MM-dd hh24:mi:ss') desc ";
        bs.writeLog(sql);
        rs.execute(sql);
        if (rs.next()){
            groupidCurrentUser = rs.getString("GROUPID");
        }

        // 通过判断groupid是否相等，来判断当前操作者分支的第一操作人是不是江旺
        if (StringUtils.isNotBlank(groupidJw) && StringUtils.isNotBlank(groupidCurrentUser)){
            if (groupidCurrentUser.equals(groupidJw)){
                map.put("data","true");
            }else {
                map.put("data","false");
            }
        }else {
            map.put("data","false");
            bs.writeLog("groupidJw or groupidCurrentUser is null");
        }
        bs.writeLog("map-->"+map);
        return map;
    }


    @Override
    public Map<String, Object> getLsdpInfo(Map<String, Object> params, User user) {
        return commandExecutor.execute(new ItxqlcLdspCmd(params,user));
    }



}
