From 38f29e38fcc668171dc05c53d40a36b895c86102 Mon Sep 17 00:00:00 2001 From: liusuyi <1951119284@qq.com> Date: 星期四, 10 十月 2024 13:34:28 +0800 Subject: [PATCH] init --- ard-work/src/main/java/com/ruoyi/utils/tools/ArdTool.java | 144 ++++++++++++++++++++++++++++++++++++++++++++--- 1 files changed, 134 insertions(+), 10 deletions(-) diff --git a/ard-work/src/main/java/com/ruoyi/utils/tools/ArdTool.java b/ard-work/src/main/java/com/ruoyi/utils/tools/ArdTool.java index 70172c5..24e2b43 100644 --- a/ard-work/src/main/java/com/ruoyi/utils/tools/ArdTool.java +++ b/ard-work/src/main/java/com/ruoyi/utils/tools/ArdTool.java @@ -1,10 +1,10 @@ package com.ruoyi.utils.tools; -import com.ruoyi.common.core.redis.RedisCache; import lombok.extern.slf4j.Slf4j; -import javax.annotation.Resource; +import java.io.*; import java.lang.reflect.Field; +import java.text.DecimalFormat; import java.time.LocalTime; import java.util.*; @@ -21,7 +21,7 @@ /** * @鎻忚堪 瀹炰綋瀵硅薄杞琈ap * @鍙傛暟 [entity] - * @杩斿洖鍊� java.util.Map<java.lang.String,java.lang.Object> + * @杩斿洖鍊� java.util.Map<java.lang.String, java.lang.Object> * @鍒涘缓浜� 鍒樿嫃涔� * @鍒涘缓鏃堕棿 2023/6/2 14:35 * @淇敼浜哄拰鍏跺畠淇℃伅 @@ -40,6 +40,7 @@ } return map; } + /** * 姹侻ap<K,V>涓渶灏� Value 瀵瑰簲鐨凨ey鍊� * @@ -47,13 +48,14 @@ * @return */ public static String getKeyByMinValue(Map<String, Double> map) { - if (map == null) return null; + if (map == null || map.size() == 0) 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; } + /** * @鎻忚堪 閫氳繃鏃ュ鍒囨崲鏃堕棿鍒ゆ柇褰撳墠浣跨敤閫氶亾鍙� * @鍙傛暟 [] @@ -62,8 +64,7 @@ * @鍒涘缓鏃堕棿 2023/6/17 13:50 * @淇敼浜哄拰鍏跺畠淇℃伅 */ - public static Integer getChannelBydayNightTime(String dayNightTime) - { + public static Integer getChannelBydayNightTime(String dayNightTime) { try { // 鑾峰彇褰撳墠鏃堕棿 LocalTime currentTime = LocalTime.now(); @@ -80,11 +81,134 @@ } else { return 2; } - } - catch (Exception ex) - { - log.error("鍏夌數鍒囨崲鑾峰彇閫氶亾寮傚父锛�"+ex.getMessage()); + } catch (Exception ex) { + log.error("鍏夌數鍒囨崲鑾峰彇閫氶亾寮傚父锛�" + ex.getMessage()); return 1; } } + + /** + * 鏂囦欢澶у皬鏅鸿兘杞崲 + * 浼氬皢鏂囦欢澶у皬杞崲涓烘渶澶ф弧瓒冲崟浣� + * + * @param size锛堟枃浠跺ぇ灏忥紝鍗曚綅涓築锛� + * @return 鏂囦欢澶у皬 + */ + public static String formatFileSize(Long size) { + String sizeName = null; + if (1024 * 1024 > size && size >= 1024) { + sizeName = String.format("%.2f", size.doubleValue() / 1024) + "KB"; + } else if (1024 * 1024 * 1024 > size && size >= 1024 * 1024) { + sizeName = String.format("%.2f", size.doubleValue() / (1024 * 1024)) + "MB"; + } else if (size >= 1024 * 1024 * 1024) { + sizeName = String.format("%.2f", size.doubleValue() / (1024 * 1024 * 1024)) + "GB"; + } else { + sizeName = size.toString() + "B"; + } + return sizeName; + } + + /** + * 鏂囦欢澶у皬鏅鸿兘杞崲 + * 浼氬皢鏂囦欢澶у皬杞崲涓烘渶澶ф弧瓒冲崟浣� + * + * @param size锛堟枃浠跺ぇ灏忥紝鍗曚綅涓築锛� + * @return 鏂囦欢澶у皬 + */ + public static String readableFileSize(long size) { + if (size <= 0) { + return "0"; + } + final String[] units = new String[]{"B", "KB", "MB", "GB", "TB"}; + int digitGroups = (int) (Math.log10(size) / Math.log10(1024)); + return new DecimalFormat("#,###.##").format(size / Math.pow(1024, digitGroups)) + " " + units[digitGroups]; + } + + /** + * byte鏁扮粍杞瓧绗︿覆 + * 鍒樿嫃涔� + * 2023/10/18 8:42:59 + * + * @param byteArray + */ + public static String byteArrayToString(byte[] byteArray) { + StringBuilder stringBuilder = new StringBuilder(); + for (byte b : byteArray) { + stringBuilder.append(String.valueOf(b)); + } + return stringBuilder.toString(); + } + + /** + * 灏嗘暟鎹啓鍏xt鏂囦欢 + * 鍒樿嫃涔� + * 2024/3/8 12:58:39 + */ + public static void writeStringToFile(String data, String fileName) { + + try { + File file = new File(fileName); + File parentDir = file.getParentFile(); + if (!parentDir.exists()) { + parentDir.mkdirs(); // 鍒涘缓鏂囦欢鎵�鍦ㄧ殑鐩綍锛屽寘鎷埗鐩綍 + } + boolean fileExists = file.exists(); + boolean dataExists = false; + if(fileExists) { + // 璇诲彇鏂囦欢鍐呭 + BufferedReader reader = new BufferedReader(new FileReader(fileName)); + String line; + while ((line = reader.readLine()) != null) { + if (line.equals(data)) { + dataExists = true; + break; + } + } + reader.close(); + } + + // 濡傛灉鏂囦欢涓嶅瓨鍦ㄦ垨鏁版嵁涓嶅瓨鍦紝鍒欒拷鍔犲埌鏂囦欢 + if (!fileExists || !dataExists) { + FileWriter writer = new FileWriter(fileName, true); + writer.write(data + System.lineSeparator()); + writer.close(); + if (!fileExists) { + log.debug("File created and data has been appended."); + } else { + log.debug("Data has been appended to the file."); + } + } else { + log.debug("Data already exists in the file. Not appending."); + } + } catch (IOException e) { + log.error("An error occurred."); + e.printStackTrace(); + } + } + + /** + * 閫氳繃鍙嶅皠瀵硅薄濉厖 + * 鍒樿嫃涔� + * 2024/4/2 11:50:57 + */ + public static void fillNullFields(Object source, Object target) { + if (source == null || target == null) { + return; + } + Field[] fields = source.getClass().getDeclaredFields(); + for (Field field : fields) { + field.setAccessible(true); + try { + Object sourceValue = field.get(source); + Object targetValue = field.get(target); + if (targetValue == null && sourceValue != null) { + field.set(target, sourceValue); + } + } catch (IllegalAccessException e) { + e.printStackTrace(); + } + } + } + + } -- Gitblit v1.9.3