package com.customization.htsc.autoremind;

import weaver.conn.RecordSet;
import weaver.email.EmailWorkRunnable;
import weaver.general.BaseBean;
import weaver.general.Util;
import weaver.hrm.User;
import weaver.system.SysRemindWorkflow;
import java.util.HashSet;
import java.util.Set;


public class RequestSubmitErrorRemindThread implements Runnable {
    private String requestid;
    private User user;
    private String newremark;

    public RequestSubmitErrorRemindThread(String requestid,User user,String newremark) {
        this.requestid = requestid;
        this.user = user;
        this.newremark = newremark;
    }

    @Override
    public void run() {
        BaseBean bb = new BaseBean();
        bb.writeLog("RequestSubmitErrorRemindThread--requestid:"+requestid);
        try{
            RecordSet rs = new RecordSet();
            String requestname = "" ;
            String requestmark = "" ;
            String sqltable = " select abs(t.formid) as formid,t.id,l.requestname,l.requestmark " +
                    " from workflow_base t " +
                    " inner join workflow_requestbase l on l.workflowid = t.id " +
                    " where l.requestid=" + requestid + "";
            rs.execute(sqltable);
            if (rs.next()) {
                requestname = Util.null2String(rs.getString("requestname"));
                requestmark = Util.null2String(rs.getString("requestmark"));
            }

            if(!"".equals(requestid))
            {
                String type = "";
                String objids = "";
                String sql = " select * from workflow_errorremind ";
                rs.executeQuery(sql);
                if (rs.next()) {
                    type = Util.null2String(rs.getString("type"));
                    objids = Util.null2String(rs.getString("objids"));
                }
                String operators = "";
                //String operatormails = "" ;
                Set<String> setMail = new HashSet<String>();

                if("0".equals(type)){ //管理人员账号
                    operators = objids;
                }else if("1".equals(type)){//人力资源
                    operators = objids;
                    if(!"".equals(operators)){
                        sql = " select email  from hrmresource where id in("+operators+") ";
                        rs.execute(sql);
                        while (rs.next()){
                            String email =Util.null2String(rs.getString("email")) ;
                            if(!"".equals(email)){
                                //operatormails += operatormails == "" ? email : ","+email;
                                setMail.add(email);
                            }
                        }
                    }
                }else if("2".equals(type)){ //角色
                    sql = " select t.resourceid,k.email from hrmrolemembers t \n" +
                            " inner join hrmroles l on l.id = t.roleid \n" +
                            " inner join hrmresource k on k.id = t.resourceid \n" +
                            " where l.id in ("+objids+")";

                    rs.execute(sql);
                    while (rs.next()){
                        String resourceid =Util.null2String(rs.getString("resourceid")) ;
                        String email =Util.null2String(rs.getString("email")) ;
                        operators += operators == "" ? resourceid : ","+resourceid ;

                        if(!"".equals(email)){
                            //operatormails += operatormails == "" ? email : ","+email;
                            setMail.add(email);
                        }

                    }
                }else if(("".equals(type) || type.equals("3")) && this.isAdmin(user)){
                    operators = user.getUID()+"";
                    String email = user.getEmail();

                    if(!"".equals(email)){
                        //operatormails += operatormails == "" ? email : ","+email;
                        setMail.add(email);
                    }
                }
                bb.writeLog("operators:"+operators);
                bb.writeLog("setMail:"+setMail.toString());
                String remark = this.newremark;
                if(!"".equals(operators))
                {
                    int resource = 1;
                    SysRemindWorkflow sysRemindWorkflow = new SysRemindWorkflow();
                    sysRemindWorkflow.make("流程-"+requestname+"提交报错",0,0,0,0,resource,operators,remark,Util.getIntValue(requestid));
                }

                String operatormails = String.join(",", setMail);
                if(!"".equals(operatormails))
                {
                    String sendTo = operatormails ;
                    String subject = "流程--"+requestname+"提交报错";
                    String sendCc = "" ;
                    String sendBcc = "" ;

                    bb.writeLog("sendTo:" + sendTo);
                    bb.writeLog("subject:" + subject);

                    //EmailService EmailService = new EmailService();

                    String content = " 流程请求ID："+requestid + "<br>"+
                            " 流程请求名称："+requestname+ "<br>"+
                            " 流程编号:" +requestmark+"<br>"+
                            " 当前操作者:" +user.getLastname()+"<br>"+
                            " 异常原因:<br>"+remark;

                    EmailWorkRunnable ewr = new EmailWorkRunnable(sendTo,sendCc,sendBcc,subject, content);
                    boolean result = ewr.emailCommonRemind();
                    bb.writeLog("result:" + result);
                }
            }
        }catch (Exception e){
            e.printStackTrace();
            bb.writeLog("RequestSubmitErrorRemindThread---e:"+e);
        }
    }
    /***
     *
     * @param user
     * @return
     */
    public boolean isAdmin(User user){
        boolean flag = false;
        RecordSet rs = new RecordSet();
        rs.execute("select count(*) from hrmresourcemanager where id=" + user.getUID());
        if(rs.next()){
            if(rs.getInt(1) > 0){
                flag = true;
            }
        }
        return flag;
    }

}
