#
Junjie
2024-08-19 18147c6baeb59f3427eba5904614e924a6a72785
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
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
package com.zy.asrs.wms.utils;
 
 
import com.zy.asrs.framework.common.Cools;
import com.zy.asrs.wms.common.constant.Constants;
 
import javax.servlet.http.HttpServletResponse;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;
import java.util.Optional;
import java.util.function.BiConsumer;
import java.util.function.Function;
 
/**
 * Created by vincent on 2023/3/14
 */
public class Utils {
 
    public static final String _LINK = "-";
 
    /**
     * 通过库位号获取 排
     */
    public static int getRow(String locNo) {
        if (!Cools.isEmpty(locNo)) {
            String[] split = locNo.split(_LINK);
            return Integer.parseInt(split[0]);
        }
        throw new RuntimeException("库位解析异常");
    }
 
    /**
     * 通过库位号获取 列
     */
    public static int getBay(String locNo) {
        if (!Cools.isEmpty(locNo)) {
            String[] split = locNo.split(_LINK);
            return Integer.parseInt(split[1]);
        }
        throw new RuntimeException("库位解析异常");
    }
 
    /**
     * 通过库位号获取 层
     */
    public static int getLev(String locNo) {
        if (!Cools.isEmpty(locNo)) {
            String[] split = locNo.split(_LINK);
            return Integer.parseInt(split[2]);
        }
        throw new RuntimeException("库位解析异常");
    }
 
    public static String getLocNo(Number row, Number bay, Number lev) {
        return row + _LINK + bay + _LINK + lev;
    }
 
    /**
     * List转为树形结构
     *
     * @param data           List
     * @param parentId       顶级的parentId
     * @param parentIdMapper 获取parentId的Function
     * @param idMapper       获取id的Function
     * @param consumer       赋值children的Consumer
     * @param <T>            数据的类型
     * @param <R>            parentId的类型
     * @return List<T>
     */
    public static <T, R> List<T> toTreeData(List<T> data,
                                            R parentId,
                                            Function<? super T, ? extends R> parentIdMapper,
                                            Function<? super T, ? extends R> idMapper,
                                            BiConsumer<T, List<T>> consumer) {
        List<T> result = new ArrayList<>();
        for (T d : data) {
            R dParentId = parentIdMapper.apply(d);
            if (parentId.equals(dParentId)) {
                R dId = idMapper.apply(d);
                List<T> children = toTreeData(data, dId, parentIdMapper, idMapper, consumer);
                if(children.size() > 0) {
                    consumer.accept(d, children);
                }
                result.add(d);
            }
        }
        return result;
    }
 
//    public static <T> List<T> treeRemove(List<T> data, String condition,
//                                         Function<? super T, ? extends String> fetcher,
//                                         Function<T, List<T>> childrenGetter,
//                                         BiConsumer<T, List<T>> childrenSetter) {
//        List<T> result = new ArrayList<>();
//        for (T node : data) {
//            List<T> children = childrenGetter.apply(node);
//
//            if (children != null && !children.isEmpty()) {
//                List<T> newChildren = treeRemove(children, condition, fetcher, childrenGetter, childrenSetter);
//                childrenSetter.accept(node, newChildren);
//            }
//
//            if (fetcher.apply(node).contains(condition)) {
//                result.add(node);
//            }
//        }
//        return result;
//    }
 
    public static <T> void treeRemove(List<T> list, String condition, Function<? super T, ? extends String> fetcher, Function<T, List<T>> childrenGetter) {
        Iterator<T> iterator = list.iterator();
        while (iterator.hasNext()) {
            T next = iterator.next();
            List<T> children = childrenGetter.apply(next);
            if (children != null && !children.isEmpty()) {
                treeRemove(children, condition, fetcher, childrenGetter);
            } else {
                if (!fetcher.apply(next).contains(condition)) {
                    iterator.remove();
                }
            }
        }
    }
 
    /**
     * 数组倒序
     * @param bytes
     * @param <T>
     */
    public static <T> byte[] reverse(byte[] bytes) {
        if (bytes == null) return null;
        for (int start = 0, end = bytes.length - 1; start < end; start++, end--) {
            byte temp = bytes[end];
            bytes[end] = bytes[start];
            bytes[start] = temp;
        }
        return bytes;
    }
 
    /**
     * 截取数组
     * @param bytes 原数组
     * @param pos   定位(截取后包含定位点数据)
     * @param len   长度
     * @return new arr
     */
    public static byte[] slice(byte[] bytes, int pos, int len) {
        if (bytes == null || bytes.length == 0 || len == 0) {
            return new byte[0];
        }
        if (pos + len > bytes.length) {
            throw new RuntimeException("com.zy.acs.common.utils ArrayIndexOutOfBoundsException\n" +
                    "原数组 bytes 长度为 " + bytes.length + ",截取长度超过原数组!");
        }
        byte[] arr = new byte[len];
        System.arraycopy(bytes, pos, arr, 0, len);
        return arr;
    }
 
    public static byte[] sliceWithReverse(byte[] bytes, int pos, int len) {
        byte[] slice = slice(bytes, pos, len);
        reverse(slice);
        return slice;
    }
 
    public static byte[] merge(Object... objects) {
        int len = 0;
        for (Object object : objects) {
            if (object instanceof byte[]) {
                byte[] bytes = (byte[]) object;
                len += bytes.length;
            }
            if (object instanceof Byte) {
                len++;
            }
        }
        byte[] arr = new byte[len];
        int idx = 0;
        for (Object object : objects) {
            if (object instanceof byte[]) {
                byte[] bytes = (byte[]) object;
                System.arraycopy(bytes, 0, arr, idx, bytes.length);
                idx += bytes.length;
            }
            if (object instanceof Byte) {
                byte[] bytes = new byte[] {(Byte) object};
                System.arraycopy(bytes, 0, arr, idx, bytes.length);
                idx += bytes.length;
            }
        }
        return arr;
    }
 
    public static <T> String join(T[] array, String seq) {
        StringBuilder sb = new StringBuilder();
        if (array != null) {
            for (int i = 0; i < array.length; i++) {
                sb.append(array[i]);
                if (i < array.length - 1) {
                    sb.append(seq);
                }
            }
        }
        return sb.toString();
    }
 
    public static String zeroFill(String msg, Integer len) {
        len = Optional.ofNullable(len).orElse(16);
        if (msg.length() == len){
            return msg;
        } else if (msg.length() > len){
            return msg.substring(0, 16);
        } else {
            StringBuilder msgBuilder = new StringBuilder(msg);
            for (int i = 0; i<len-msg.length(); i++){
                msgBuilder.insert(0,"0");
            }
            return msgBuilder.toString();
        }
    }
 
    public static String removePrefix(String str, String prefix) {
        if (!Cools.isEmpty(str) && !Cools.isEmpty(prefix)) {
            if (str.startsWith(prefix)) {
                return str.substring(prefix.length());
            } else {
                return str;
            }
        } else {
            return str;
        }
    }
 
    public static String removeSuffix(String str, String suffix) {
        if (!Cools.isEmpty(str) && !Cools.isEmpty(suffix)) {
            if (str.endsWith(suffix)) {
                return str.substring(0, str.indexOf(suffix));
            } else {
                return str;
            }
        } else {
            return str;
        }
    }
 
    /**
     * 大驼峰 转 symbol小驼峰
     */
    public static String toSymbolCase(String str, char symbol) {
        if (str == null) {
            return null;
        } else {
            int length = str.length();
            StringBuilder sb = new StringBuilder();
 
            for(int i = 0; i < length; ++i) {
                char c = str.charAt(i);
                if (Character.isUpperCase(c)) {
                    Character preChar = i > 0 ? str.charAt(i - 1) : null;
                    Character nextChar = i < str.length() - 1 ? str.charAt(i + 1) : null;
                    if (null != preChar) {
                        if (symbol == preChar) {
                            if (null == nextChar || Character.isLowerCase(nextChar)) {
                                c = Character.toLowerCase(c);
                            }
                        } else if (Character.isLowerCase(preChar)) {
                            sb.append(symbol);
                            if (null == nextChar || Character.isLowerCase(nextChar)) {
                                c = Character.toLowerCase(c);
                            }
                        } else if (null == nextChar || Character.isLowerCase(nextChar)) {
                            sb.append(symbol);
                            c = Character.toLowerCase(c);
                        }
                    } else if (null == nextChar || Character.isLowerCase(nextChar)) {
                        c = Character.toLowerCase(c);
                    }
                }
 
                sb.append(c);
            }
 
            return sb.toString();
        }
    }
 
    public static String toCamelCase(CharSequence name) {
        if (null == name) {
            return null;
        } else {
            String name2 = name.toString();
            if (name2.contains("_")) {
                int length = name2.length();
                StringBuilder sb = new StringBuilder(length);
                boolean upperCase = false;
 
                for(int i = 0; i < length; ++i) {
                    char c = name2.charAt(i);
                    if (c == '_') {
                        upperCase = true;
                    } else if (upperCase) {
                        sb.append(Character.toUpperCase(c));
                        upperCase = false;
                    } else {
                        sb.append(Character.toLowerCase(c));
                    }
                }
 
                return sb.toString();
            } else {
                return name2;
            }
        }
    }
 
    public static String sub(String str, Integer maxLen) {
        if (str.length() > maxLen) {
            return str.substring(0, maxLen);
        }
        return str;
    }
 
    public static String generateSeqNum(String lastSeqNum) {
        if (Cools.isEmpty(lastSeqNum)) {
            return zeroFill("1", 4);
        } else {
            int i = Integer.parseInt(lastSeqNum);
            if (i >= 9999) {
                return zeroFill("1", 4);
            } else {
                return zeroFill(String.valueOf(i+1), 4);
            }
        }
    }
 
    public static void cors(HttpServletResponse response){
        // 跨域设置
        response.setHeader("Access-Control-Max-Age", "3600");
        response.setHeader("Access-Control-Allow-Origin", "*");
        response.setHeader("Access-Control-Allow-Methods", "*");
        response.setHeader("Access-Control-Allow-Headers", "*");
        response.setHeader("Access-Control-Expose-Headers", Constants.TOKEN_HEADER_NAME);
    }
 
}