package com.customization.htxc.cgty;

import com.engine.core.cfg.annotation.CommandDynamicProxy;
import com.engine.core.interceptor.AbstractCommandProxy;
import com.engine.core.interceptor.Command;
import com.engine.integration.util.StringUtils;
import com.engine.workflow.cmd.requestForm.RequestSubmitCmd;
import com.engine.workflow.entity.requestForm.RequestOperationResultBean;
import weaver.conn.RecordSet;
import weaver.conn.RecordSetTrans;
import weaver.general.BaseBean;
import weaver.general.Util;
import weaver.interfaces.htsc.comInfo.PropBean;

import javax.servlet.http.HttpServletRequest;
import java.time.Duration;
import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;
import java.util.ArrayList;
import java.util.Map;
import java.util.Objects;

/**
 * @Title: ecology-9
 * @Company: 泛微软件
 * @author: K1810027
 * @version: 1.0
 * @CreateDate: 2022-08-18 16:55
 * @Description:这个是用来修改采购的7条流程分送出来的自由节点处理人更新到采购项目的明细表1
 * @ModifyLog:
 **/
@CommandDynamicProxy(target = RequestSubmitCmd.class, desc = "流程提交处理业务")
public class UpdateDt1Cmd extends AbstractCommandProxy<Map<String, Object>> {
    private static ArrayList<String> unnumberList = new ArrayList<String>();
    private static ArrayList<String> nodeList = new ArrayList<String>();
    static{
        unnumberList.add("zbhb");
        unnumberList.add("zbqs");
        unnumberList.add("swtp");
        unnumberList.add("jzxtpqs");
        unnumberList.add("jzxtphb");
        unnumberList.add("jzxcsqs");
        unnumberList.add("jzxcshb");
        nodeList.add("zbhb_fs");
        nodeList.add("zbqs_fs");
        nodeList.add("swtp_fs");
        nodeList.add("jzxtpqs_fs");
        nodeList.add("jzxtphb_fs");
        nodeList.add("jzxcsqs_fs");
        nodeList.add("jzxcshb_fs");
    }
    @Override
    public Map<String, Object> execute(Command<Map<String, Object>> targetCommand) {
        RequestSubmitCmd requestSubmitCmd = (RequestSubmitCmd) targetCommand;
        HttpServletRequest request = requestSubmitCmd.getRequest();//从request中可以取到参数，不用以前的方法了
        String requestId = Util.null2String(request.getParameter("requestid"));
        String workflowId = Util.null2String(request.getParameter("workflowid"));
        String nodeType = Util.null2String(request.getParameter("nodetype"));
        String nodeId = Util.null2String(request.getParameter("nodeid"));
        String src = Util.null2String(request.getParameter("src"));
        String remark = Util.HTMLtoTxt(Util.null2String(request.getParameter("remark"))).trim();
        String operator = Util.null2String(request.getParameter("f_weaver_belongto_userid"));
        String requestName = Util.null2String(request.getParameter("requestname"));
//        String requestId="5650648";
//       String workflowId = "56528";
//        String nodeId = "80561";
//        String src = "submit";
        BaseBean bb = new BaseBean();
        bb.writeLog("UpdateDt1Cmd-src"+src);
        bb.writeLog("UpdateDt1Cmd-requestname"+requestName);
        //执行标准的业务处理
        Map<String, Object> result = nextExecute(targetCommand);
        try{
            RequestOperationResultBean resultBean = (RequestOperationResultBean) result.get("data");
            String executeResult = resultBean.getType().name();
//            String executeResult = "SUCCESS";
            RecordSet rs = new RecordSet();
            String sql = "select * from workflow_base where id = "+workflowId;
            bb.writeLog("UpdateDt1Cmd-sql"+sql);
            rs.execute(sql);
            rs.next();
            String unnumber = rs.getString("unnumber");
            String nodeSql = "select nodename,unnumber from workflow_nodebase where id = "+nodeId;
            rs.execute(nodeSql);
            bb.writeLog("UpdateDt1Cmd-nodeSql"+nodeSql);
            rs.next();
//            String nodename = rs.getString("nodename");
            String nodename = "会签";
            String nodeUnnumber = rs.getString("unnumber");
            if("SUCCESS".equals(executeResult) && "submit".equals(src)){
                bb.writeLog("UpdateDt1Cmd-executeResult"+executeResult);
                bb.writeLog("UpdateDt1Cmd-src"+src);
                if(unnumberList.contains(unnumber)&&nodeList.contains(nodeUnnumber)){
                    bb.writeLog("UpdateDt1Cmd-unnumber"+unnumber);
                    bb.writeLog("UpdateDt1Cmd-nodename"+nodename);
                    ArrayList<String> userList = new ArrayList<String>();
                    String currentoperatorSql = "select * from workflow_currentoperator where requestid = '"+requestId+"' and nodeid < 0 and (agenttype =0 or agenttype =2) and isremark = 0";
                    rs.execute(currentoperatorSql);
                    bb.writeLog("UpdateDt1Cmd-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("UpdateDt1Cmd-dqclsc"+dqclsc);
                    String dt1Sql = "select * from  uf_cgxq_dt1 where lcmc = "+requestId;
                    bb.writeLog("UpdateDt1Cmd-dt1Sql"+dt1Sql);
                    rs.execute(dt1Sql);
                    if(rs.next()){
                        String updateDt1Sql = "update uf_cgxq_dt1 set dqcljd ='"+nodename+"',dqclr= '"+users+"',dqclsc='"+dqclsc+"',dqjdqssj='"+curTime+"' where lcmc = "+requestId;
                        bb.writeLog("UpdateDt1Cmd-updateDt1Sql"+updateDt1Sql);
                        rs.execute(updateDt1Sql);
                    }
                }
            }

        }catch (Exception e){
            e.printStackTrace();
        }
        return result;
    }
    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;
    }
}
