package com.engine.htsc.smsd.util;

import cn.hutool.core.date.DateUtil;
import weaver.conn.RecordSet;
import weaver.general.BaseBean;
import weaver.general.Util;

import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.HashMap;
import java.util.Map;

public class AddDataUtil {

    /**
     * @param type  通过type获取主表名
     * @param param type的value
     * @Describe 根据流程编号获取主表名
     */
    public static String getMaintableName(String type, String param) {
        RecordSet rs = new RecordSet();
        if ("requestid".equals(type)) {
            rs.executeQuery("select t1.tablename from workflow_bill t1,workflow_base t2,workflow_requestbase t3 where t1.id =t2.formid and t2.id = t3.workflowid and t3.requestid = '" + param + "'");
            if (rs.next()) {
                return Util.null2String(rs.getString("tablename"));
            } else {
                return "";
            }
        } else if ("lcbh".equals(type)) {
            rs.executeQuery("select t1.tablename from workflow_bill t1,workflow_base t2,workflow_requestbase t3 where t1.id =t2.formid and t2.id = t3.workflowid and t3.REQUESTMARK = '" + param + "'");
            if (rs.next()) {
                return Util.null2String(rs.getString("tablename"));
            } else {
                return "";
            }
        } else {
            return "";
        }
    }

    /**
     * @Describe 判断流程是否已办结
     */
    public static String isFinished(String lcbh, String tableName) {
        BaseBean bb = new BaseBean();
        bb.writeLog("==================判断流程是否已办结，接收到的参数是：lcbh:" + lcbh + ",tableName:" + tableName + "===============");
        RecordSet rs = new RecordSet();
        String sql = "select currentnodetype,isstopped,concat(concat(LASTOPERATEDATE,' '),LASTOPERATETIME) as lcbjsj from workflow_requestbase where requestid=(select requestid from " + tableName + " where lcbh = '" + lcbh + "')";
        bb.writeLog("即将执行的获取是否已办结的sql语句是：" + sql);
        rs.executeQuery(sql);
        if (rs.next()) {
            String currentnodetype = Util.null2String(rs.getString("currentnodetype"));
            String isstopped = Util.null2String(rs.getString("isstopped"));
            if ((!"1".equals(isstopped)) && ("3".equals(currentnodetype))) {
                return Util.null2String(rs.getString("lcbjsj"));
            } else {
                return "";
            }
        } else {
            return "";
        }
    }

    /**
     * @Describe 根据流程唯一标识获取流程指定相关字段
     */
    public static Map<String, Object> getMainInfo(String[] fields, String lcbh, String tableName) {
        BaseBean bb = new BaseBean();
        bb.writeLog("===============进入getMainInfo方法，准备获取主表的相关参数==============");
        Map<String, Object> mainInfo = new HashMap<>();
        RecordSet rs = new RecordSet();

        String sql;
        sql = "select f.lcbh,f.hjxxjezfmx,f.ngr,f.szbm,b.unnumber as lclx from "+tableName+" f," +
                "workflow_requestbase a,workflow_base b " +
                "where f.REQUESTID = a.REQUESTID and a.WORKFLOWID = b.id and f.lcbh = '"+lcbh+"'";
        bb.writeLog("即将执行获取主表信息的sql是：" + sql);
        rs.executeQuery(sql);
        if (rs.next()) {
            String[] columnNames = rs.getColumnName();
            for (String columnName : columnNames) {
                mainInfo.put(columnName.toLowerCase(), rs.getString(columnName));
            }
        }
        return mainInfo;
    }

    /**
     * @Describe 根据流程唯一表示获取流程指定明细表的一个字段的所有值并用；分隔
     */
    public static String getDtFieldValue(String fieldName, String tableNum, String lcbh, String tableName) {
        RecordSet rs = new RecordSet();
        String value = "";
        String sql = "select listagg(" + fieldName + ",'；') as fieldName from " + tableName + "_dt" + tableNum + " where mainid = (select id from " + tableName + " where lcbh = '" + lcbh + "')";
        new BaseBean().writeLog("=============即将执行获取明细表指定字段值的sql是：" + sql);
        rs.executeQuery(sql);
        if (rs.next()) {
            value = Util.null2String(rs.getString(rs.getColumnName()[0]));
        }
        return value;
    }

    /**
     * @Describe 是否逾期以及逾期天数
     */
    public static String getSfyq(String date) {
        try {
            SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
            Date date1 = simpleDateFormat.parse(date);
            long days = DateUtil.betweenDay(date1, new Date(), true);
            return days > 30 ? String.valueOf(days - 30) : "";
        } catch (Exception e) {
            new BaseBean().writeLog("是否逾期判断发生错误：" + e.getMessage());
            e.getMessage();
            return "";
        }
    }

    public static int getDataIdByUUID(String uuid, String uf_tableName) {
        RecordSet rs = new RecordSet();
        int dataid = -1;
        String sql = "select id from " + uf_tableName + " where modeuuid = ?";
        if (rs.executeQuery(sql, uuid) && rs.next()) {
            dataid = Util.getIntValue(rs.getString(1));
        }
        return dataid;
    }

    /**
     * @Describe 根据流程编号获取流程的requestId
     * */
    public static String getRequestIdByLcbh(String lcbh, String mainTableName) {
        RecordSet rs = new RecordSet();
        rs.executeQuery("select requestid from "+mainTableName+" where lcbh = '"+lcbh+"'");
        if (rs.next()){
            return Util.null2String(rs.getString("requestid"));
        }else {
            return "";
        }
    }
}