yangys
2024-05-30 a3686cfa49bf53fb91a2ceb960cf15b3ebdac641
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
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
package com.qianwen.smartman.common.utils;
 
import cn.hutool.crypto.Mode;
import cn.hutool.crypto.Padding;
import cn.hutool.crypto.symmetric.DES;
import com.google.common.collect.Lists;
import java.util.ArrayList;
import java.util.List;
import org.apache.rocketmq.client.consumer.DefaultMQPushConsumer;
import org.apache.rocketmq.common.UtilAll;
import com.qianwen.smartman.common.constant.CommonConstant;
import com.qianwen.smartman.common.constant.TenantConstant;
import com.qianwen.core.tool.node.INode;
import com.qianwen.core.tool.utils.Func;
import com.qianwen.core.tool.utils.StringUtil;
 
 
public class CommonUtil {
    public static String changeInstanceNameToPID(DefaultMQPushConsumer consumer) {
        String instanceName = consumer.getInstanceName();
        if ("DEFAULT".equals(consumer.getInstanceName())) {
            instanceName = UtilAll.getPid() + "#" + System.nanoTime();
        }
        return instanceName;
    }
 
    public static int initCapacity(int size) {
        return (int) ((size / 0.75d) + 1.0d);
    }
 
    /**
     * 拼接redisKey,[cacheName]::[keyPrefix][key]
     * @param cacheName 缓存名称
     * @param keyPrefix key前缀
     * @param key 最末端的key
     * @return
     */
    public static String getReallyCacheName(String cacheName, String keyPrefix, String key) {
        return cacheName.concat("::").concat(keyPrefix).concat(key);
    }
 
    public static String getReallyCacheName(String cacheName, String keyPrefix, String key, Object... args) {
        return StringUtil.format(cacheName.concat("::").concat(keyPrefix).concat(key), args);
    }
 
    public static String getDefaultPass() {
        DES des = new DES(Mode.ECB, Padding.ZeroPadding, "yw8W8jud0CoJUm6Q".getBytes());
        return des.decryptStr(TenantConstant.DEFAULT_ENCRYPTED_PASS);
    }
 
    public static String secToTime(Integer time) {
        String timeStr;
        
        int hour = 0;
        int minute = 0;
        int second = 0;
        
        if (Func.isEmpty(time) || time.intValue() <= 0) {
            return "00:00:00";
        }
        
        time = Integer.valueOf(time.intValue() / 1000);
        minute = time.intValue() / CommonConstant.SECONDS_OF_MINUTE.intValue();
        
        if (minute < CommonConstant.SECONDS_OF_MINUTE.intValue()) {   
            second = time.intValue() % CommonConstant.SECONDS_OF_MINUTE.intValue();
            timeStr = "00:" + unitFormat(minute) + ":" + unitFormat(second);
        } else {
            hour = minute / CommonConstant.SECONDS_OF_MINUTE.intValue();
            minute %= CommonConstant.SECONDS_OF_MINUTE.intValue();
            second = time.intValue() - hour * CommonConstant.SECONDS_OF_HOUR.intValue() - minute * CommonConstant.SECONDS_OF_MINUTE.intValue();
            timeStr = unitFormat(hour) + ":" + unitFormat(minute) + ":" + unitFormat(second);
            
        }
        return timeStr;
    }
 
    private static String unitFormat(int i) {
        String retStr;
        if (i >= 0 && i < CommonConstant.NUMBER_ABOVE_TWO.intValue()) {
            retStr = "0" + i;
        } else {
            retStr = "" + i;
        }
        return retStr;
    }
 
    public static <T extends INode<T>> List<T> treeToList(List<T> items) {
        List<T> list = new ArrayList<>();
        items.forEach(forestNode -> {
            list.add(forestNode);
            recursion(forestNode, list);
        });
        return list;
    }
 
    private static <T extends INode<T>> void recursion(T forestNode, List<T> list) {
        if (Func.isNotEmpty(forestNode.getChildren())) {
            forestNode.getChildren().forEach(childForestNode -> {
                list.add(childForestNode);
                recursion(childForestNode, list);
            });
        }
    }
 
    public static <T> List<List<T>> groupList(List<T> list, int batchSize) {
        if (Func.isEmpty(list)) {
              return Lists.newArrayList(); 
        }
        int size = list.size();
        List<List<T>> res = Lists.newArrayList();
        int i = 0;
        while (i <= size) {
          if (i + batchSize >= size) {
            List<T> list1 = list.subList(i, size);
            res.add(list1);
            break;
          } 
          int j = i + batchSize;
          List<T> newList = list.subList(i, j);
          i = j;
          res.add(newList);
        } 
        return res;
    }
}