package com.gci.boot.common.util;

import com.fasterxml.jackson.databind.JsonNode;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.fasterxml.jackson.databind.node.ObjectNode;

import java.util.Date;

/**
 * @author lyz
 * @date 2023/10/10 10:09
 */
public class FormatUtil {

    public static void main(String[] args) {
        String jsonString = "[{\"controllerTemperature\":\"39\",\"dcBusCurrent\":\"1001.2000122070312\",\"inputVoltage\":\"408.0\",\"number\":1,\"rpm\":40531,\"status\":\"1\",\"temperature\":\"34\",\"torque\":\"10.0\"},{\"controllerTemperature\":\"39\",\"dcBusCurrent\":\"1001.2000122070312\",\"inputVoltage\":\"408.0\",\"number\":1,\"rpm\":40531,\"status\":\"3\",\"temperature\":\"34\",\"torque\":\"10.0\"}]";

        String updatedJsonString = replaceStatusValues("status",jsonString);

        System.out.println(updatedJsonString);
    }

    public static String replaceStatusValues(String key,String jsonString) {
        try {
            ObjectMapper objectMapper = new ObjectMapper();
            JsonNode arrayNode = objectMapper.readTree(jsonString);

            for (JsonNode motorNode : arrayNode) {
                String currentStatus = motorNode.get(key).asText();
                String newStatus = calculateNewStatus(currentStatus);
                ((ObjectNode) motorNode).put(key, newStatus);
            }
            return objectMapper.writeValueAsString(arrayNode);
        } catch (Exception e) {
            e.printStackTrace();
            return jsonString;
        }
    }

    private static String calculateNewStatus(String currentStatus) {
        // 根据实际情况计算新的status值
        switch (currentStatus) {
            case "1":
                return "正常";
            case "0":
                return "异常";
            default:
                return "未知";
        }
    }

    /**
     * 格式化两个时间之间的差值
     * @param startTime
     * @param endTime
     * @return
     */
    public static String formattedTimeDifference(Date startTime, Date endTime){
        if(startTime == null){
            return "未知";
        }
        // 如果endTime为空，则使用当前时间作为endTime
        if (endTime == null) {
            endTime = new Date();
        }
        long timeDifferenceInMillis = endTime.getTime() - startTime.getTime();
        long seconds = timeDifferenceInMillis / 1000;
        long minutes = seconds / 60;
        long hours = minutes / 60;
        long remainingMinutes = minutes % 60;
        long remainingSeconds = seconds % 60;
        String formattedTimeDifference = String.format("%d时%d分%d秒", hours, remainingMinutes, remainingSeconds);
        return formattedTimeDifference;
    }

    public static String convertToMonthDescription(String input) {
        try {
            int monthNumber = Integer.parseInt(input);
            String[] monthDescriptions = {
                    "1月", "2月", "3月", "4月", "5月", "6月", "7月", "8月", "9月", "10月", "11月", "12月"
            };
            // 检查输入的月份是否在有效范围内
            if (monthNumber >= 1 && monthNumber <= 12) {
                return monthDescriptions[monthNumber - 1];
            } else {
                return input;
            }
        } catch (NumberFormatException e) {
            return input;
        }
    }
}
