liusuyi
2023-07-14 252e5230703e4355d25274d21b84f2d060907416
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
package com.ruoyi.utils.tools;
 
import com.ruoyi.common.core.redis.RedisCache;
import lombok.extern.slf4j.Slf4j;
 
import javax.annotation.Resource;
import java.lang.reflect.Field;
import java.time.LocalTime;
import java.util.*;
 
/**
 * @Description: 工具方法
 * @ClassName: ArdTool
 * @Author: 刘苏义
 * @Date: 2023年06月02日14:37
 * @Version: 1.0
 **/
@Slf4j
public class ArdTool {
 
    /**
     * @描述 实体对象转Map
     * @参数 [entity]
     * @返回值 java.util.Map<java.lang.String,java.lang.Object>
     * @创建人 刘苏义
     * @创建时间 2023/6/2 14:35
     * @修改人和其它信息
     */
    public static Map<String, Object> convertEntityToMap(Object entity) {
        Map<String, Object> map = new HashMap<>();
        Field[] fields = entity.getClass().getDeclaredFields();
        for (Field field : fields) {
            field.setAccessible(true);
            try {
                Object value = field.get(entity);
                map.put(field.getName(), value);
            } catch (IllegalAccessException e) {
                e.printStackTrace();
            }
        }
        return map;
    }
    /**
     * 求Map<K,V>中最小 Value 对应的Key值
     *
     * @param map
     * @return
     */
    public static String getKeyByMinValue(Map<String, Double> map) {
        if (map == null) return null;
        List<Map.Entry<String, Double>> list = new ArrayList(map.entrySet());
        Collections.sort(list, (o1, o2) -> (o1.getValue().intValue() - o2.getValue().intValue()));
        String min = list.get(0).getKey();
//        String max = list.get(list.size() - 1).getKey();
        return min;
    }
    /**
     * @描述 通过日夜切换时间判断当前使用通道号
     * @参数 []
     * @返回值 java.lang.Integer
     * @创建人 刘苏义
     * @创建时间 2023/6/17 13:50
     * @修改人和其它信息
     */
    public static Integer getChannelBydayNightTime(String dayNightTime)
    {
        try {
            // 获取当前时间
            LocalTime currentTime = LocalTime.now();
            String[] dayNight = dayNightTime.split("-");
            // 解析时间字符串
            LocalTime startTime = LocalTime.parse(dayNight[0]);
            LocalTime endTime = LocalTime.parse(dayNight[1]);
 
            // 检查当前时间是否在给定的时间范围内
            boolean isWithinRange = !currentTime.isBefore(startTime) && !currentTime.isAfter(endTime);
            // 输出结果
            if (isWithinRange) {
                return 1;
            } else {
                return 2;
            }
        }
        catch (Exception ex)
        {
            log.error("光电切换获取通道异常:"+ex.getMessage());
            return 1;
        }
    }
}