package com.haobang.util;


import com.common.entity.DeviceDevice;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.haobang.config.AppConfig;

import java.util.LinkedHashMap;
import java.util.Map;
import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;
public class DeviceInfoUtil {

    /**
     * 补充设备配置信息 key value值
     * @param strSyslog
     * @return
     */
    public   static  String  getFullLogString(String strSyslog)
    {
        ObjectMapper objectMapper = new ObjectMapper();
        // 创建设备配置信息MAP
        // 获取当前时间（精度到毫秒）
        LocalDateTime now = LocalDateTime.now();
        // 定义格式化器，包含毫秒
        DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyyMMddHHmmssSSS");
        // 转换为字符串
        String timeString = now.format(formatter);

        Map<String, String> map = new LinkedHashMap<>();
        map.put("receive_time",timeString);
        map.put("device_id", AppConfig.getAppServieDeviceId());
        map.put("device_name", AppConfig.getAppServieDeviceName());
        map.put("vendor", AppConfig.getAppServieVendor());
        map.put("data_type", AppConfig.getAppServieDataType());
        map.put("device_collect_id", Long.toString(AppConfig.getDeviceCollectId()));

        String formattedString = formatDeviceInfo(map);
        //return  formattedString + "" + strSyslog.substring(34);

        //测试环境截取34位之后字符串
        return  formattedString + strSyslog;
    }
    /**
     * 补充设备配置信息 key value值
     * @param strSyslog
     * @return
     */
    public   static  String  getFullLogString(DeviceDevice deviceDevice,String strSyslog)
    {
        ObjectMapper objectMapper = new ObjectMapper();
        // 创建设备配置信息MAP
        // 获取当前时间（精度到毫秒）
        LocalDateTime now = LocalDateTime.now();
        // 定义格式化器，包含毫秒
        DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyyMMddHHmmssSSS");
        // 转换为字符串
        String timeString = now.format(formatter);
        Map<String, String> map = new LinkedHashMap<>();
        map.put("receive_time",timeString);
        map.put("device_id",deviceDevice.getId().toString());
        map.put("device_name", deviceDevice.getName());
        map.put("vendor", deviceDevice.getVendor());
        map.put("data_type", AppConfig.getAppServieDataType());
        map.put("device_collect_id", Long.toString(AppConfig.getDeviceCollectId()));
        String formattedString = formatDeviceInfo(map);
        //return  formattedString + "" + strSyslog.substring(34);
        //测试环境截取34位之后字符串
        return  formattedString + strSyslog;
    }

    public   static  String  getFullLogString(DeviceDevice deviceDevice,String strSyslog,String receive_time)
    {
        ObjectMapper objectMapper = new ObjectMapper();
        // 创建设备配置信息MAP
        // 获取当前时间（精度到毫秒）
        LocalDateTime now = LocalDateTime.now();
        // 定义格式化器，包含毫秒
        DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyyMMddHHmmssSSS");
        // 转换为字符串
        String timeString = now.format(formatter);
        Map<String, String> map = new LinkedHashMap<>();
        map.put("receive_time",receive_time);
        map.put("device_id",deviceDevice.getId().toString());
        map.put("device_name", deviceDevice.getName());
        map.put("vendor", deviceDevice.getVendor());
        map.put("data_type", AppConfig.getAppServieDataType());
        map.put("device_collect_id", Long.toString(AppConfig.getDeviceCollectId()));
        String formattedString = formatDeviceInfo(map);
        //return  formattedString + "" + strSyslog.substring(34);
        //测试环境截取34位之后字符串
        return  formattedString + strSyslog;
    }


    public static String formatDeviceInfo(Map<String, String> deviceData) {
        if (deviceData == null || deviceData.isEmpty()) {
            return "[]"; // 返回空括号表示无数据
        }
        StringBuilder sb = new StringBuilder();
        sb.append("["); // 开始字符

        // 使用 LinkedHashMap 保持插入顺序
        boolean firstEntry = true;
        for (Map.Entry<String, String> entry : deviceData.entrySet()) {
            if (!firstEntry) {
                sb.append(" "); // 键值对之间用空格分隔
            }
            sb.append(entry.getKey())
                    .append("=")
                    .append(entry.getValue());
            firstEntry = false;
        }
        sb.append("]"); // 结束字符
        return sb.toString();
    }

}
