package weaver.interfaces.htsc.workflow;

import com.weaver.general.Util;
import weaver.conn.RecordSet;
import weaver.general.BaseBean;
import weaver.general.TimeUtil;
import weaver.hrm.resource.ResourceComInfo;

import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

/**
 * Created by K1810001 on 2020/9/24.
 * 计划任务 恢复搁置状态
 */

public class ReadRecoverJob extends weaver.interfaces.schedule.BaseCronJob {

    @Override
    public void execute() {

        RecordSet  rs = new RecordSet();
        BaseBean bs = new BaseBean();
        ResourceComInfo resourceComInfo = null;

        try {
            resourceComInfo = new ResourceComInfo();
            String recoverSql = "select * from uf_lcgzb where LCDQZT = 1";
            rs.execute(recoverSql);
            while (rs.next()){
                String lcid = Util.null2String(rs.getString("LCID"));
                String belongUser = Util.null2String(rs.getString("GZR"));
                String gzrq = Util.null2String(rs.getString("GZRQ"));
                String gzsj = Util.null2String(rs.getString("GZSJ"));
                String workflowid = Util.null2String(rs.getString("WORKFLOWID"));
                String nodeid = Util.null2String(rs.getString("NODEID"));
                String lastname = Util.null2String(rs.getString("LASTNAME"));
                String departmentid = Util.null2String(rs.getString("DEPARTMENTID"));
                String remark = "自选搁置时间到期,取消搁置";
                String showname = Util.null2String(rs.getString("SHOWNAME"));

                //获取代理人id代理人
                String agentSql = "select agentorbyagentid from workflow_currentoperator where requestid = ? and agenttype = 2 and userid = ?";
                rs.executeQuery(agentSql,lcid,belongUser);
                int agentorbyagentid = -1;
                while (rs.next()){
                    agentorbyagentid = Util.getIntValue(rs.getString("agentorbyagentid"));
                }

                String receivedPersons = "";
                String receiveName = resourceComInfo.getLastname(belongUser);
                String agentor = resourceComInfo.getLastname(String.valueOf(agentorbyagentid));
                if (agentorbyagentid != -1){
                    receivedPersons = agentor +"->"+receiveName;
                }else {
                    receivedPersons = receiveName;
                }

                if (!gzrq.equals("null") && !gzsj.equals("null")){
                    String gzDate = gzrq +" "+ gzsj;

                    SimpleDateFormat sf = new SimpleDateFormat("yyyy-MM-dd HH:mm");
                    //1.搁置日期时间戳
                    long gzTime = sf.parse(gzDate).getTime();
                    //2.当前日期时间戳
                    long currentTime = new Date().getTime();
                    if (currentTime >= gzTime) {
                        RecordSet updateRs = new RecordSet();
                        //更新搁置表
                        String updateSql = "update uf_lcgzb set gzrq= '', gzsj= '' ,remark= '"+remark+"',lcdqzt = 0 where gzr = "+belongUser+" and WORKFLOWID = "+workflowid+" and  lcid = "+lcid+" and NODEID = "+nodeid;
                        updateRs.executeUpdate(updateSql);
                        //插入签字意见 requestid workflowid nodeid logtype operatedate operatetime userid
                        RecordSet logRs = new RecordSet();
                        String operatedate = TimeUtil.getCurrentDateString();
                        String operatetime = TimeUtil.getOnlyCurrentTimeString();

                        List<Object> logList = new ArrayList<>();
                        String logtype = "u";
                        logList.add(lcid);
                        logList.add(workflowid);
                        logList.add(nodeid);
                        logList.add(logtype);
                        logList.add(operatedate);
                        logList.add(operatetime);
                        logList.add(agentorbyagentid);
                        logList.add(belongUser);
                        logList.add(receivedPersons);
                        logList.add(remark);
                        logList.add(departmentid);
                        String logSql = "insert into workflow_requestlog(requestid,workflowid,nodeid,logtype,operatedate,operatetime,agentorbyagentid,operator,receivedPersons,remark,operatorDept) values(?,?,?,?,?,?,?,?,?,?,?)";
                        logRs.executeUpdate(logSql, logList);

                        //删除主账号和次账号的标签
                        attentionUtil(belongUser,showname,lcid);
                        //是否有主账号
                        RecordSet accountRs = new RecordSet();
                        String accountSql = "select belongto from hrmresource where id = "+belongUser;
                        accountRs.execute(accountSql);
                        if (accountRs.next()){
                            String belongto = Util.null2String(accountRs.getString("belongto"));
                            if (!belongto.equals("")){
                                attentionUtil(belongto,showname,lcid);
                            }
                        }
                    }

                }

            }
        }catch (Exception e){
            e.printStackTrace();
        }
    }

    /**
     * 取消搁置标签
     */
    public  void attentionUtil(String userId,String showName,String requestid){
        RecordSet attRs = new RecordSet();
        String attSql = "select id from workflow_attentionTypeSet where userid = "+userId+" and SHOWNAME ='"+showName+"'";
        attRs.execute(attSql);
        if (attRs.next()){
            Integer ids = Util.getIntValue(attRs.getString("id"));
            RecordSet selRs = new RecordSet();
            String delsql = "delete from workflow_attention where userid= "+userId+" and usertype= 0 and requestid = " + requestid + " and atypeid = " + ids;
            selRs.executeUpdate(delsql);
        }
    }
}
