package com.customization.htsc.receipt;

import com.api.workflow.service.RequestAuthenticationService;
import com.cloudstore.dev.api.bean.MessageBean;
import com.cloudstore.dev.api.bean.MessageType;
import com.cloudstore.dev.api.util.Util_Message;
import com.engine.core.cfg.annotation.CommandDynamicProxy;
import com.engine.core.interceptor.AbstractCommandProxy;
import com.engine.core.interceptor.Command;
import com.engine.workflow.cmd.requestForm.RequestSubmitCmd;
import com.engine.workflow.entity.requestForm.RequestOperationResultBean;
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 javax.servlet.http.HttpServletRequest;
import java.io.IOException;
import java.text.SimpleDateFormat;
import java.util.*;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

@CommandDynamicProxy(target = RequestSubmitCmd.class, desc = "领导审阅意见通知")
public class LeaderNoticeCmd extends AbstractCommandProxy<Map<String, Object>> {

    @Override
    public Map<String, Object> execute(Command<Map<String, Object>> targetCommand) {
        //获取到被代理对象
        RequestSubmitCmd requestSubmitCmd = (RequestSubmitCmd) targetCommand;
        HttpServletRequest request = requestSubmitCmd.getRequest();//从request中可以取到参数，不用以前的方法了
        User user = requestSubmitCmd.getUser();
        String src = Util.null2String(request.getParameter("src"));
        //执行标准的业务处理
        Map<String, Object> result = nextExecute(targetCommand);
        RequestOperationResultBean resultBean = (RequestOperationResultBean) result.get("data");
        String type = Util.null2String(resultBean.getType());
        //加工
        if("submit".equalsIgnoreCase(src)&&"SUCCESS".equalsIgnoreCase(type)&&needNotice(request,user)){
            BaseBean bs = new BaseBean();
            try{
                String remark = Util.null2String(request.getParameter("remark"));
                remark = remark.replaceAll("[\\s\\t\\n\\r]", "");
                if(!("已阅".equals(remark)||"同意".equals(remark))){
                    bs.writeLog("need notice");
                    getMessageContent(request,user);
                }
            }catch (Exception e){
                e.printStackTrace();
                bs.writeLog("send leader notice error:",e);
            }
        }
        return result;
    }

    /**
     * 是否需要通知
     * @param request
     * @param user
     * @return
     */
    private boolean  needNotice(HttpServletRequest request,User user){
        BaseBean bs = new BaseBean();
        PropBean pb = new PropBean();
        try{
            String workflowid = Util.null2String(request.getParameter("workflowid"));
            String nodeid = Util.null2String(request.getParameter("nodeid"));

            String gsswldsy = pb.getPropName("gsswldsy");
            String gsswldsy_workflowid = pb.getActiveWorkflowIdByUnNumber(gsswldsy);

            String gsswldsy_ldsy = pb.getPropName("gsswldsy_ldsy");
            String gsswldsy_ldsy_nodeid = pb.getNodeIdsByUnNumber(gsswldsy_ldsy);

            if(workflowid.equals(gsswldsy_workflowid)&&nodeid.equals(gsswldsy_ldsy_nodeid)){
                bs.writeLog("true");
                return true;
            }
        }catch (Exception e){
            e.printStackTrace();
            bs.writeLog("check notice need error:",e);
        }
        return false;
    }

    /**
     * 发送消息提醒
     * @param sendIdSet
     * @param title
     * @param context
     * @param linkUrl
     * @param linkMobileUrl
     */
    private void sendMessage(Set<String> sendIdSet,String title,String context,String linkUrl,String linkMobileUrl){
        BaseBean bs = new BaseBean();
        PropBean pb = new PropBean();
        String messageCode = pb.getPropName("ldtzMsgTypeCode");
        MessageType messageType = MessageType.newInstance(Integer.parseInt(messageCode)); // 消息来源（见文档第四点补充 必填）
        try {
            MessageBean messageBean = Util_Message.createMessage(messageType, sendIdSet, title, context, linkUrl, linkMobileUrl);
            messageBean.setCreater(1);// 创建人id
            Util_Message.store(messageBean);
        } catch (IOException e) {
            e.printStackTrace();
            bs.writeLog("send message error:",e);
        }
    }

    /**
     * 获取消息详情
     * @param request
     * @param user
     */
    private void getMessageContent(HttpServletRequest request,User user){
        BaseBean bs = new BaseBean();
        PropBean pb = new PropBean();
        RecordSet rs = new RecordSet();
        String requestid = Util.null2String(request.getParameter("requestid"));
        String workflowid = Util.null2String(request.getParameter("workflowid"));
        String remark = Util.null2String(request.getParameter("remark"));
        remark = remark.replaceAll("[\\s\\t\\n\\r]", "");

        //获得消息内容
        String operatorName = user.getUsername();
        String operatetime = getTime();
        String requestName = cutString(getRequestName(requestid),20);
        String requestmark = getRequestMark(requestid);
        String requestRemark = cutString(remark,30);
        String title = "领导审阅意见通知";
        String context = operatorName+"于"+operatetime+"对“"+requestName+"”（"+requestmark+"）" +
                "签署意见“"+requestRemark+"”，请知悉。";

        Set<String> subNoticeUserSet = new HashSet<>();
        Set<String> adminNoticeUserSet = new HashSet<>();

        String sql = "";
        //获得主流程id
        String mainRequestid = "";
        sql = "select mainrequestid from WORKFLOW_SUBWFREQUEST where subrequestid = '"+requestid+"'";
        bs.writeLog(sql);
        rs.executeQuery(sql);
        if(rs.next()){
            mainRequestid = Util.null2String(rs.getString("mainrequestid"));
        }
        //获得主流程表名
        String mainTableName = "";
        sql = "select t3.tablename from WORKFLOW_REQUESTBASE t1,WORKFLOW_BASE t2,WORKFLOW_BILL t3 "
                +"where t2.id = t1.workflowid and t3.id = t2.formid and t1.requestid = '"+mainRequestid+"'";
        bs.writeLog(sql);
        rs.executeQuery(sql);
        if(rs.next()){
            mainTableName = Util.null2String(rs.getString("tablename"));
        }
//        //获取主流程表内Id
//        String mainId = "";
//        sql = "select id from "+mainTableName+" where requestid = '"+mainRequestid + "'";
//        bs.writeLog(sql);
//        rs.executeQuery(sql);
//        if(rs.next()){
//            mainId = Util.null2String(rs.getString("id"));
//        }

        //特殊领导:周易、张伟
        String gsswldsy = pb.getPropName("gsswldsy");
        List<String> specialLeaderList = new ArrayList<>();
        sql = "select tzry2 from UF_LCTZRY where lcxx = '"+gsswldsy+"'";
        bs.writeLog(sql);
        rs.executeQuery(sql);
        if(rs.next()){
            specialLeaderList = Arrays.asList(Util.null2String(rs.getString("tzry2")).split(","));
        }
        for(String specialLeader:specialLeaderList){
            bs.writeLog("specialLeader:"+specialLeader);
            if(compareLoginid(specialLeader,String.valueOf(user.getUID()))){
                bs.writeLog("user.getUID():"+user.getUID());
                //领导审阅已送
                List<String> leaderList = new ArrayList<>();

                String gsswldsy_ldsy = pb.getPropName("gsswldsy_ldsy");
                String gsswldsy_ldsy_nodeid = pb.getNodeIdsByUnNumber(gsswldsy_ldsy);
                bs.writeLog("gsswldsy_ldsy:"+gsswldsy_ldsy);
                bs.writeLog("gsswldsy_ldsy_nodeid:"+gsswldsy_ldsy_nodeid);

                sql = "select t1.userid from WORKFLOW_CURRENTOPERATOR t1, WORKFLOW_SUBWFREQUEST t2 "
                        +"where t2.subrequestid = t1.requestid and t1.isremark <> 0 and t2.mainrequestid = '"+mainRequestid+"' and t1.nodeid = '"+gsswldsy_ldsy_nodeid+"'";
                bs.writeLog(sql);
                rs.executeQuery(sql);
                while(rs.next()){
                    leaderList.add(Util.null2String(rs.getString("userid")));
                }
                //党委、董事会、经营管理层
                List<String> specialDeptList = new ArrayList<>();
                sql = "select tzbm from UF_LCTZRY where lcxx = '"+gsswldsy+"'";
                bs.writeLog(sql);
                rs.executeQuery(sql);
                if(rs.next()){
                    specialDeptList = Arrays.asList(Util.null2String(rs.getString("tzbm")).split(","));
                }

                for(String leader:leaderList){
                    String deptid = "";
                    sql = "select departmentid from HRMRESOURCE where id = '"+leader+"'";
                    bs.writeLog(sql);
                    rs.executeQuery(sql);
                    if(rs.next()){
                        deptid = Util.null2String(rs.getString("departmentid"));
                    }
                    for(String specialDept:specialDeptList){
                        if(specialDept.equals(deptid)&&!Integer.toString(user.getUID()).equals(leader)){
                            subNoticeUserSet.add(leader);
                            break;
                        }
                    }
                }
                break;
            }
        }

        //主办部门
        String gsswzbbm = pb.getPropName("gsswzbbm");
        String gsswzbbm_workflowid = pb.getActiveWorkflowIdByUnNumber(gsswzbbm);
        String gsswzbbm_zbbmfzrsh = pb.getPropName("gsswzbbm_zbbmfzrsh");
        String gsswzbbm_zbbmfzrsh_nodeid = pb.getNodeIdsByUnNumber(gsswzbbm_zbbmfzrsh);
        String gsswzbbm_zbrycl = pb.getPropName("gsswzbbm_zbrycl");
        String gsswzbbm_zbrycl_nodeid = pb.getNodeIdsByUnNumber(gsswzbbm_zbrycl);

        sql = "select distinct userid from WORKFLOW_CURRENTOPERATOR where isremark <> 0 and requestid in "
                +"(select * from "
                +"(select t1.subrequestid from WORKFLOW_SUBWFREQUEST t1,WORKFLOW_REQUESTBASE t2 "
                +"where t2.requestid = t1.subrequestid and t1.mainrequestid = '"+mainRequestid+"' and t2.workflowid = '"+gsswzbbm_workflowid+"' order by t2.createdate desc, t2.createtime desc） "
                +"where ROWNUM <= 1) and nodeid in ("+gsswzbbm_zbbmfzrsh_nodeid+","+gsswzbbm_zbrycl_nodeid+")";
        bs.writeLog(sql);
        rs.executeQuery(sql);
        while(rs.next()){
            String userid = Util.null2String(rs.getString("userid"));
            subNoticeUserSet.add(userid);
        }

//        String zbbmRequestid = "";
//        sql  = "select lcmc from "+mainTableName+"_DT11 where mainid = '"+mainId+"' and lclx = '"+gsswzbbm+"' and ROWNUM <= 1";
//        bs.writeLog(sql);
//        rs.executeQuery(sql);
//        if(rs.next()){
//            zbbmRequestid = Util.null2String(rs.getString("lcmc"));
//        }
//        if(StringUtils.isNotEmpty(zbbmRequestid)){
//            String zbbmTableName = "";
//            sql = "select t3.tablename from WORKFLOW_REQUESTBASE t1,WORKFLOW_BASE t2,WORKFLOW_BILL t3 "
//                    +"where t2.id = t1.workflowid and t3.id = t2.formid and t1.requestid = '"+zbbmRequestid+"'";
//            bs.writeLog(sql);
//            rs.executeQuery(sql);
//            if(rs.next()){
//                zbbmTableName = Util.null2String(rs.getString("tablename"));
//            }
//            sql = "select zbry,fzzbbmld from "+zbbmTableName+" where requestid = '"+zbbmRequestid+"'";
//            bs.writeLog(sql);
//            rs.executeQuery(sql);
//            if(rs.next()){
//                String zbry = Util.null2String(rs.getString("zbry"));
//                String fzzbbmld = Util.null2String(rs.getString("fzzbbmld"));
//                if(StringUtils.isNotEmpty(zbry)){
//                    subNoticeUserSet.add(zbry);
//                }
//                if(StringUtils.isNotEmpty(fzzbbmld)){
//                    subNoticeUserSet.addAll(Arrays.asList(fzzbbmld.split(",")));
//                }
//            }
//        }
//
//        //协办部门
////        sql = "select bmld from "+mainTableName+"_DT8 where ys = '1' and mainid = '"+mainId+"'";
////        bs.writeLog(sql);
////        rs.executeQuery(sql);
////        while(rs.next()){
////            String bmld = Util.null2String(rs.getString("bmld"));
////            if(StringUtils.isNotEmpty(bmld)){
////                subNoticeUserSet.add(bmld);
////            }
////        }
        //看阅部门+协办部门领导
        String gsswyz_swyz = pb.getPropName("gsswyz_swyz");
        String gsswyz_swyz_nodeid = pb.getNodeIdsByUnNumber(gsswyz_swyz);
        String gsswxbbm_xbbmfzrsh = pb.getPropName("gsswxbbm_xbbmfzrsh");
        String gsswxbbm_xbbmfzrsh_nodeid = pb.getNodeIdsByUnNumber(gsswxbbm_xbbmfzrsh);

        sql = "select t1.userid from WORKFLOW_CURRENTOPERATOR t1, WORKFLOW_SUBWFREQUEST t2 "
                +"where t2.subrequestid = t1.requestid and t1.isremark <> 0 "
                +"and t2.mainrequestid = '"+mainRequestid+"' and t1.nodeid in ("+gsswyz_swyz_nodeid+","+gsswxbbm_xbbmfzrsh_nodeid+")";
        bs.writeLog(sql);
        rs.executeQuery(sql);
        while(rs.next()){
            String userid = Util.null2String(rs.getString("userid"));
            subNoticeUserSet.add(userid);
        }

        //配置通知人员:办公室主任（配置成006064）、办公室相关人员（配置成黄叶轩001874、宋艳锴002550、张徐健002155）
        sql = "select tzry from UF_LCTZRY where lcxx = '"+gsswldsy+"'";
        bs.writeLog(sql);
        rs.executeQuery(sql);
        if(rs.next()){
            List<String> list = Arrays.asList(Util.null2String(rs.getString("tzry")).split(","));
            adminNoticeUserSet.addAll(list);
        }

        bs.writeLog("subNoticeUserSet:"+subNoticeUserSet.toString());
        bs.writeLog("adminNoticeUserSet:"+adminNoticeUserSet.toString());
        List<String> subNoticeUserList = new ArrayList<>();
        for(String userid:subNoticeUserSet){
            boolean flag = false;
            for(String adminid:adminNoticeUserSet){
                if(compareLoginid(adminid,userid)){
                    flag = true;
                    break;
                }
            }
            for(String currentid:subNoticeUserList){
                if(compareLoginid(currentid,userid)){
                    flag = true;
                    break;
                }
            }
            if(!flag){
                subNoticeUserList.add(userid);
            }
        }
        bs.writeLog("subNoticeUserList:"+subNoticeUserList.toString());

        //发送消息通知
        String receiptLeaderNotice = pb.getPropName("receipt_leader_notice");
        if("1".equals(receiptLeaderNotice)){
            sql = "select id from hrmresource where loginid = '011413'";
            bs.writeLog(sql);
            rs.executeQuery(sql);
            if(rs.next()){
                String id = Util.null2String(rs.getString("id"));
                sendNotice(id,mainRequestid,title,context,"2");
            }
        }else{
            for(String adminNoticeUser:adminNoticeUserSet){
                if(!compareLoginid(adminNoticeUser,String.valueOf(user.getUID()))){
                    sendNotice(adminNoticeUser,mainRequestid,title,context,"2");
                }
            }
            for(String subNoticeUser:subNoticeUserList){
                if(!compareLoginid(subNoticeUser,String.valueOf(user.getUID()))){
                    sendNotice(subNoticeUser,mainRequestid,title,context,"1");
                }
            }
        }
    }

    /**
     * 发送消息通知 type:1-发送子流程链接，2-发送主流程监控链接
     * @param userid
     * @param mainRequestid
     * @param title
     * @param context
     * @param type
     */
    private void sendNotice(String userid,String mainRequestid,String title,String context,String type){
        BaseBean bs = new BaseBean();
        RecordSet rs = new RecordSet();
        try{
            Set<String> sendIdSet = new HashSet<>();
            sendIdSet.add(userid);
            String linkUrl = "";
            String linkMobileUrl = "";

            if("1".equals(type)){
                String subRequestid = "";
                String sql = "select t1.requestid from WORKFLOW_CURRENTOPERATOR t1, WORKFLOW_SUBWFREQUEST t2 "
                        +"where t2.subrequestid = t1.requestid and t2.mainrequestid = '"+mainRequestid+"' and t1.userid = '"+userid+"' and t1.isremark <> 0 and ROWNUM <= 1";
                bs.writeLog(sql);
                rs.executeQuery(sql);
                if(rs.next()){
                    subRequestid = Util.null2String(rs.getString("requestid"));
                }
                linkUrl = "/spa/workflow/static4form/index.html#/main/workflow/req?requestid="+subRequestid+"&f_weaver_belongto_userid="+userid; // PC端链接
                linkMobileUrl = "/spa/workflow/static4mobileform/index.html#/req?requestid="+subRequestid+"&f_weaver_belongto_userid="+userid; // 移动端链接

            }else if("2".equals(type)){
                linkUrl = "/spa/workflow/static4form/index.html#/main/workflow/req?requestid="+mainRequestid+"&ismonitor=1"; // PC端链接
                linkMobileUrl = "/spa/workflow/static4mobileform/index.html#/req?requestid="+mainRequestid+"&ismonitor=1"; // 移动端链接
            }

            bs.writeLog("sendIdSet:"+sendIdSet.toString());
            bs.writeLog("title:"+title);
            bs.writeLog("context:"+context);
            bs.writeLog("linkUrl:"+linkUrl);
            bs.writeLog("linkMobileUrl:"+linkMobileUrl);
            sendMessage(sendIdSet,title,context,linkUrl,linkMobileUrl);
        }catch(Exception e){
            e.printStackTrace();
        }
    }

    /**
     * 获取消息内容--废弃
     * @param request
     * @param user
     */
    private void getMessageContext(HttpServletRequest request,User user){
        BaseBean bs = new BaseBean();
        PropBean pb = new PropBean();
        RecordSet rs = new RecordSet();
        String requestid = Util.null2String(request.getParameter("requestid"));
        String workflowid = Util.null2String(request.getParameter("workflowid"));
        String remark = Util.null2String(request.getParameter("remark"));

        String sql = "";
        //获得主流程id
        String mainRequestid = "";
        sql = "select mainrequestid from WORKFLOW_SUBWFREQUEST where subrequestid = '"+requestid+"'";
        bs.writeLog(sql);
        rs.executeQuery(sql);
        if(rs.next()){
            mainRequestid = Util.null2String(rs.getString("mainrequestid"));
        }

        Set<String> sendIdSet = new HashSet<String>(); //接收人id 必填
        String title = ""; //标题
        String context = ""; //内容
        String authStr = "";
        String authSignatureStr = "";
        String url = "requestid="+mainRequestid+"&relaterequest="+requestid+"&isrequest=3&isovertime=0&desrequestid="+requestid+
                "&authStr="+authStr+"&authSignatureStr="+authSignatureStr+"&f_weaver_belongto_userid="+user.getUID()+"&f_weaver_belongto_usertype=0";
        String linkUrl = "/workflow/request/ViewRequestForwardSPA.jsp?"+url; // PC端链接
        String linkMobileUrl = "/spa/workflow/forwardMobileForm.html?"+url; // 移动端链接

        sendIdSet = getSendUserId(request,user);

        String operatorName = user.getUsername();
        String operatetime = getTime();
        String requestName = cutString(getRequestName(requestid),20);
        String requestmark = getRequestMark(requestid);
        String requestRemark = cutString(remark,30);

        title = "领导审阅意见通知";
        context = operatorName+"于"+operatetime+"对“"+requestName+"”（"+requestmark+"）" +
                "签署意见“"+requestRemark+"”，请知悉。";

        bs.writeLog("sendIdSet:"+sendIdSet.toString());
        bs.writeLog("title:"+title);
        bs.writeLog("context:"+context);
        bs.writeLog("linkUrl:"+linkUrl);
        bs.writeLog("linkMobileUrl:"+linkMobileUrl);
        sendMessage(sendIdSet,title,context,linkUrl,linkMobileUrl);
    }

    /**
     * 获得接收人id--废弃
     * @param request
     * @return
     */
    private Set<String> getSendUserId(HttpServletRequest request, User user){
        BaseBean bs = new BaseBean();
        PropBean pb = new PropBean();
        RecordSet rs = new RecordSet();
        Set<String> sendIdSet = new HashSet<>();
        String requestid = Util.null2String(request.getParameter("requestid"));
        String workflowid = Util.null2String(request.getParameter("workflowid"));

        String sql = "";
        //获得主流程id
        String mainRequestid = "";
        sql = "select mainrequestid from WORKFLOW_SUBWFREQUEST where subrequestid = '"+requestid+"'";
        bs.writeLog(sql);
        rs.executeQuery(sql);
        if(rs.next()){
            mainRequestid = Util.null2String(rs.getString("mainrequestid"));
        }
        //获得主流程表名
        String mainTableName = "";
        sql = "select t3.tablename from WORKFLOW_REQUESTBASE t1,WORKFLOW_BASE t2,WORKFLOW_BILL t3 "
                +"where t2.id = t1.workflowid and t3.id = t2.formid and t1.requestid = '"+mainRequestid+"'";
        bs.writeLog(sql);
        rs.executeQuery(sql);
        if(rs.next()){
            mainTableName = Util.null2String(rs.getString("tablename"));
        }
        //获取主流程表内Id
        String mainId = "";
        sql = "select id from "+mainTableName+" where requestid = '"+mainRequestid + "'";
        bs.writeLog(sql);
        rs.executeQuery(sql);
        if(rs.next()){
            mainId = Util.null2String(rs.getString("id"));
        }

        //主办部门领导+人员
//        sql = "select bmld,zbclr from "+mainTableName+"_DT6 where ys = '1' and mainid = '"+mainId+"'";
//        bs.writeLog(sql);
//        rs.executeQuery(sql);
//        while(rs.next()){
//            String bmld = Util.null2String(rs.getString("bmld"));
//            String zbclr = Util.null2String(rs.getString("zbclr"));
//            if(StringUtils.isNotEmpty(bmld)){
//                sendIdSet.add(bmld);
//            }
//            if(StringUtils.isNotEmpty(zbclr)){
//                sendIdSet.add(zbclr);
//            }
//        }
        String zbbmRequestid = "";
        String gsswzbbm = pb.getPropName("gsswzbbm");
        sql  = "select lcmc from "+mainTableName+"_DT11 where mainid = '"+mainId+"' and lclx = '"+gsswzbbm+"' and ROWNUM <= 1";
        bs.writeLog(sql);
        rs.executeQuery(sql);
        if(rs.next()){
            zbbmRequestid = Util.null2String(rs.getString("lcmc"));
        }
        String zbbmTableName = "";
        sql = "select t3.tablename from WORKFLOW_REQUESTBASE t1,WORKFLOW_BASE t2,WORKFLOW_BILL t3 "
                +"where t2.id = t1.workflowid and t3.id = t2.formid and t1.requestid = '"+zbbmRequestid+"'";
        bs.writeLog(sql);
        rs.executeQuery(sql);
        if(rs.next()){
            zbbmTableName = Util.null2String(rs.getString("tablename"));
        }
        sql = "select zbry,fzzbbmld from "+zbbmTableName+" where requestid = '"+zbbmRequestid+"'";
        bs.writeLog(sql);
        rs.executeQuery(sql);
        if(rs.next()){
            String zbry = Util.null2String(rs.getString("zbry"));
            String fzzbbmld = Util.null2String(rs.getString("fzzbbmld"));
            if(StringUtils.isNotEmpty(zbry)){
                sendIdSet.add(zbry);
            }
            if(StringUtils.isNotEmpty(fzzbbmld)){
                sendIdSet.addAll(Arrays.asList(fzzbbmld.split(",")));
            }
        }

        //协办部门
        sql = "select bmld from "+mainTableName+"_DT8 where ys = '1' and mainid = '"+mainId+"'";
        bs.writeLog(sql);
        rs.executeQuery(sql);
        while(rs.next()){
            String bmld = Util.null2String(rs.getString("bmld"));
            if(StringUtils.isNotEmpty(bmld)){
                sendIdSet.add(bmld);
            }
        }
        //看阅部门
        String gsswyz_swyz = pb.getPropName("gsswyz_swyz");
        String gsswyz_swyz_nodeid = pb.getNodeIdsByUnNumber(gsswyz_swyz);

        sql = "select t1.userid from WORKFLOW_CURRENTOPERATOR t1, WORKFLOW_SUBWFREQUEST t2 "
                +"where t2.subrequestid = t1.requestid and t2.mainrequestid = '"+mainRequestid+"' and t1.nodeid = '"+gsswyz_swyz_nodeid+"'";
        bs.writeLog(sql);
        rs.executeQuery(sql);
        while(rs.next()){
            List<String> list = Arrays.asList(Util.null2String(rs.getString("userid")).split(","));
            sendIdSet.addAll(list);
        }

        //配置通知人员:办公室主任（配置成006064）、办公室相关人员（配置成黄叶轩001874、宋艳锴002550、张徐健002155）
        String gsswldsy = pb.getPropName("gsswldsy");
        sql = "select tzry from UF_LCTZRY where lcxx = '"+gsswldsy+"'";
        bs.writeLog(sql);
        rs.executeQuery(sql);
        if(rs.next()){
            List<String> list = Arrays.asList(Util.null2String(rs.getString("tzry")).split(","));
            sendIdSet.addAll(list);
        }

        //特殊领导:周易、张伟
        List<String> specialLeaderList = new ArrayList<>();
        sql = "select tzry2 from UF_LCTZRY where lcxx = '"+gsswldsy+"'";
        bs.writeLog(sql);
        rs.executeQuery(sql);
        if(rs.next()){
            specialLeaderList = Arrays.asList(Util.null2String(rs.getString("tzry2")).split(","));
        }
        for(String specialLeader:specialLeaderList){
            bs.writeLog("specialLeader:"+specialLeader);
            if(specialLeader.equals(Integer.toString(user.getUID()))){
                bs.writeLog("user.getUID():"+user.getUID());
                //领导审阅已送
                List<String> leaderList = new ArrayList<>();

                String gsswldsy_ldsy = pb.getPropName("gsswldsy_ldsy");
                String gsswldsy_ldsy_nodeid = pb.getNodeIdsByUnNumber(gsswldsy_ldsy);
                bs.writeLog("gsswldsy_ldsy:"+gsswldsy_ldsy);
                bs.writeLog("gsswldsy_ldsy_nodeid:"+gsswldsy_ldsy_nodeid);

                sql = "select t1.userid from WORKFLOW_CURRENTOPERATOR t1, WORKFLOW_SUBWFREQUEST t2 "
                        +"where t2.subrequestid = t1.requestid and t2.mainrequestid = '"+mainRequestid+"' and t1.nodeid = '"+gsswldsy_ldsy_nodeid+"'";
                bs.writeLog(sql);
                rs.executeQuery(sql);
                while(rs.next()){
                    leaderList.add(Util.null2String(rs.getString("userid")));
                }
                //党委、董事会、经营管理层
                List<String> specialDeptList = new ArrayList<>();
                sql = "select tzbm from UF_LCTZRY where lcxx = '"+gsswldsy+"'";
                bs.writeLog(sql);
                rs.executeQuery(sql);
                if(rs.next()){
                    specialDeptList = Arrays.asList(Util.null2String(rs.getString("tzbm")).split(","));
                }

                for(String leader:leaderList){
                    String deptid = "";
                    sql = "select departmentid from HRMRESOURCE where id = '"+leader+"'";
                    bs.writeLog(sql);
                    rs.executeQuery(sql);
                    if(rs.next()){
                        deptid = Util.null2String(rs.getString("departmentid"));
                    }
                    for(String specialDept:specialDeptList){
                        if(specialDept.equals(deptid)&&!Integer.toString(user.getUID()).equals(leader)){
                            sendIdSet.add(leader);
                            break;
                        }
                    }
                }
                break;
            }
        }

        return sendIdSet;
    }

    /**
     * 获取流程标题
     * @param requestid
     * @return
     */
    private String getRequestName(String requestid){
        String requestName = "";
        RecordSet rs = new RecordSet();
        rs.executeQuery("select requestname from workflow_requestbase where requestid=? ", new Object[]{requestid});
        if(rs.next()){
            requestName = Util.null2String(rs.getString("requestname"));
        }
        return requestName;
    }

    /**
     * 获得流程编号
     * @param requestid
     * @return
     */
    private String getRequestMark(String requestid){
        String requestmark = "";
        RecordSet rs = new RecordSet();
        rs.executeQuery("select requestmark from workflow_requestbase where requestid=? ", new Object[]{requestid});
        if(rs.next()){
            requestmark = Util.null2String(rs.getString("requestmark"));
        }
        return requestmark;
    }

    /**
     * 获得当前时间
     * @return
     */
    private String getTime(){
        Date now = new Date();
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy年MM月dd日HH时");
        return sdf.format(now);
    }

    /**
     * 截取String
     * @param str
     * @param index
     * @return
     */
    private String cutString(String str,int index){
        if(str.length()>index){
            return str.substring(0, index)+"…";
        }else{
            return str;
        }
    }

    /**
     * 比较工号
     * @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;
        }
    }
}
