package weaver.interfaces.htxc.cgty;

import com.engine.integration.util.StringUtils;
import weaver.conn.RecordSet;
import weaver.general.BaseBean;
import weaver.interfaces.htsc.comInfo.PropBean;
import weaver.interfaces.workflow.action.Action;
import weaver.soa.workflow.request.RequestInfo;

import java.time.Duration;
import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;
import java.util.ArrayList;
/**
 * @Title: ecology-9
 * @Company: 泛微软件
 * @author: K1810027
 * @version: 1.0
 * @CreateDate: 2022-08-22 14:00
 * @Description:这个节点前的action是用来做采购的7条流程更新明细表1；
 *  * 当前处理节点
 *  * 当前处理人
 *  * 当前处理时长
 *  * 总时长
 *  * 这个四个信息的
 * @ModifyLog:
 **/


public class UpdateDt1TyAction extends BaseBean implements Action {
    @Override
    public String execute(RequestInfo requestinfo) {
        String requestId = requestinfo.getRequestid();//请求ID
//        String workflowid = requestinfo.getWorkflowid();//流程ID
        String tableName = requestinfo.getRequestManager().getBillTableName();//表单名称
//        int nodeId = requestinfo.getRequestManager().getNodeid();//节点id
        int nextNodeId = requestinfo.getRequestManager().getNextNodeid();
        int lastNodeId = requestinfo.getRequestManager().getLastNodeid();
        Thread thread1 = new Thread(() -> {
            try {
                Thread.sleep(10000);
                PropBean pb = new PropBean();
                RecordSet rs = new RecordSet();
                RecordSet nodeRs = new RecordSet();
                String nodeSql = "select * from workflow_nodebase where id = "+nextNodeId;
                nodeRs.execute(nodeSql);
                nodeRs.next();
                String nodename = nodeRs.getString("nodename");
                String sql = "select * from  "+tableName +" where requestid = "+requestId;
                BaseBean bb = new BaseBean();
                bb.writeLog("7条流程通用sql"+sql);
                bb.writeLog("7条流程通用nextNodeid"+nextNodeId);
                bb.writeLog("7条流程通用lastNodeid"+lastNodeId);
                rs.execute(sql);
                String ngrq = null;
                if(rs.next()){
                    ngrq = rs.getString("ngrq");
                }
                //通过你搞日期计算总时长
                String zsc = getHour(ngrq);
                bb.writeLog("7条流程通用zsc"+zsc);
                //保存当前的节点的操作者
                ArrayList<String> userList = new ArrayList<String>();

                String currentoperatorSql = "select * from workflow_currentoperator where requestid = '"+requestId+"' and nodeid = '"+nextNodeId+"' and (agenttype =0 or agenttype =2)";
                rs.execute(currentoperatorSql);
                bb.writeLog("7条流程通用currentoperatorSql"+currentoperatorSql);
                String curTime = "";
                while(rs.next()){
                    userList.add(rs.getString("userid"));
                    curTime = rs.getString("receivedate") +" "+rs.getString("receivetime");
                    bb.writeLog("无数据curTime"+curTime);
                }
                bb.writeLog("有数据curTime "+curTime);
                String users = StringUtils.join(userList, ",");
                //当前处理时长，通过
                String dqclsc = getdqclsc(curTime);
                bb.writeLog("7条流程通用dqclsc"+dqclsc);
                String dt1Sql = "select * from  uf_cgxq_dt1 where lcmc = "+requestId;
                bb.writeLog("7条流程通用dt1Sql"+dt1Sql);
                rs.execute(dt1Sql);
                if(rs.next()){

                    String updateDt1Sql = "update uf_cgxq_dt1 set dqcljd ='"+nodename+"',dqclr= '"+users+"',dqclsc='"+dqclsc+"',zsc='"+zsc+"',dqjdqssj='"+curTime+"' where lcmc = "+requestId;
                    bb.writeLog("7条流程通用updateDt1Sql"+updateDt1Sql);
                    rs.execute(updateDt1Sql);
                }
            } catch (Exception e) {
                e.printStackTrace();
            }

        });

        try{

            thread1.start();
            return Action.SUCCESS;
        }catch (Exception e){
            e.printStackTrace();
            return Action.FAILURE_AND_CONTINUE;
        }

    }
    public String getHour(String ngrq){
        //计算总时长

        LocalDateTime now = LocalDateTime.now();
        LocalDateTime newngrq = LocalDateTime.parse(ngrq, DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm"));
        Duration between = Duration.between( newngrq,now);
        long hours = between.toHours();
        String zsc = "";
        long hour = hours % 24;
        if(hours>=24){
            long day = between.toDays();
            zsc = day + "天"+hour+"小时";
        }else{
            zsc = hour+"小时";
        }
        return zsc;
    }
    public String getdqclsc(String ngrq){
        //计算总时长

        LocalDateTime now = LocalDateTime.now();
        LocalDateTime newngrq = LocalDateTime.parse(ngrq, DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss"));
        Duration between = Duration.between( newngrq,now);
        long hours = between.toHours();
        String zsc = "";
        long hour = hours % 24;
        if(hours>=24){
            long day = between.toDays();
            zsc = day + "天"+hour+"小时";
        }else{
            zsc = hour+"小时";
        }
        return zsc;
    }

    public static void main(String[] args) {
        UpdateDt1TyAction a = new UpdateDt1TyAction();
        String hour = a.getHour("2022-08-25 18:44");
        System.out.println(hour);
    }
}
