package weaver.interfaces.htsc.GlobatTools;

import redis.clients.jedis.BinaryClient;
import weaver.conn.RecordSet;
import weaver.general.BaseBean;
import weaver.general.Util;

import java.text.SimpleDateFormat;
import java.util.*;

public class MessageDetailLog {


    public String error="[error]";
    public String success="[success]";
    public String Other="[other]";
    public String watermark="[watermark]";
    public String refuse="[refuse]";
    public String fail="[fail]";
    public String tablename="";
    public MessageDetailLog(String tablename) {
        this.tablename = tablename;
    }

/*
*
*
*
 */
    public void writeinseertLogs(String reqestid,
                                 String message,
                                 String type,
                                 String status,
                                 String resstus,
                                 String remes
                                  ){
        String tablename = "uf_messagedetail";
        if(message.length()>4000){
            message = message.substring(0,4000);
        }
        Map<String,String> mapvalue=new HashMap<String,String>();
        mapvalue.put("requestid",reqestid);
        mapvalue.put("type",type);
        mapvalue.put("status",status);
        mapvalue.put("message",message);
        mapvalue.put("resstus",resstus);
        mapvalue.put("remes",remes);
        mapvalue.put("isnotreturn","1");

      boolean flag= excetesqlboolean(mapvalue,tablename);

    }


    public void writLogsnotretrun(String reqestid,
                                 String message,
                                 String type,
                                 String status){
        String tablename = "uf_messagedetail";
        if(message.length()>4000){
            message = message.substring(0,4000);
        }
        Map<String,String> mapvalue=new HashMap<String,String>();
        mapvalue.put("requestid",reqestid);
        mapvalue.put("type",type);
        mapvalue.put("status",status);
        mapvalue.put("message",message);

        mapvalue.put("isnotreturn","0");
        boolean flag= excetesqlboolean(mapvalue,tablename);

    }



    private boolean excetesqlboolean(  Map<String,String> mapvalue,String table){
        boolean flag=true;
        String dttablename="";
        String dttablenvalue="";
        mapvalue.put("tablename",this.tablename);
        mapvalue.put("createdatetime",new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").format(new Date()).toString());
        List<String> list=new ArrayList<>();
        for (Map.Entry<String, String> entry : mapvalue.entrySet())
        {
            String fieldname = Util.null2String(entry.getKey());
            String fieldvalue = Util.null2String(entry.getValue());
            dttablename += dttablename == "" ? fieldname : "," + fieldname;
            //dttablenvalue += dttablenvalue == "" ? "'" + fieldvalue + "'" : ",'" + fieldvalue + "'";
            dttablenvalue += dttablenvalue == "" ? "?" : ",?";
            list.add(lengthmax(fieldvalue.replace("\r\n","")));
        }

        String sql = "insert into "+table+"("+dttablename+") values("+dttablenvalue+")";
        RecordSet rs = new RecordSet();
        try {
            rs.executeUpdate(sql,new Object[]{list});
        }catch (Exception exo){
            flag=false;
            exo.printStackTrace();
            new BaseBean().writeLog("存入1uf_messagedetail数据报错:"+exo.getMessage());
        }
        return flag;
    }

    public String lengthmax(String value){
        if("".equals(value)) return "";
        if(value.length()>4000){
            value=value.substring(0,3999);
        }
        return  value;
    }

}
