王董 před 1 rokem
rodič
revize
2e5e74cd92

+ 18 - 0
ruoyi-common/pom.xml

@@ -103,6 +103,24 @@
             <artifactId>jjwt</artifactId>
         </dependency>
 
+        <!-- HttpClient工具类 -->
+        <dependency>
+            <groupId>org.apache.httpcomponents</groupId>
+            <artifactId>httpclient</artifactId>
+            <version>4.5.13</version>
+        </dependency>
+        <dependency>
+            <groupId>org.apache.httpcomponents</groupId>
+            <artifactId>httpcore</artifactId>
+            <version>4.4.14</version>
+        </dependency>
+        <!-- nacos joiner拼接 -->
+        <dependency>
+            <groupId>com.alibaba.nacos</groupId>
+            <artifactId>nacos-client</artifactId>
+            <version>2.2.2</version>
+        </dependency>
+
         <!-- Jaxb -->
         <dependency>
             <groupId>javax.xml.bind</groupId>

+ 239 - 0
ruoyi-common/src/main/java/com/ruoyi/common/utils/MapUtil.java

@@ -0,0 +1,239 @@
+package com.ruoyi.common.utils;
+
+
+import cn.hutool.core.util.ObjectUtil;
+
+import java.beans.BeanInfo;
+import java.beans.Introspector;
+import java.beans.PropertyDescriptor;
+import java.io.UnsupportedEncodingException;
+import java.lang.reflect.Method;
+import java.math.BigDecimal;
+import java.net.URLEncoder;
+import java.sql.Timestamp;
+import java.util.*;
+
+public class MapUtil {
+
+
+    //根据map的value获取map的key
+    public static String getKeyByString(Map<String, String> map, String value) {
+        String key = "";
+        for (Map.Entry<String, String> entry : map.entrySet()) {
+            if (value.equals(entry.getValue())) {
+                key = entry.getKey();
+            }
+        }
+        return key;
+    }
+
+    //根据map的value获取map的key
+    public static String getKeyByLong(Map<Long, String> map, String value) {
+        Long key = null;
+        for (Map.Entry<Long, String> entry : map.entrySet()) {
+            if (value.equals(entry.getValue())) {
+                key = entry.getKey();
+            }
+        }
+        return String.valueOf(key);
+    }
+
+    //根据map的value模糊匹配map的key
+    public static String getKeyByContainsString(Map<String, String> map, String value) {
+        String key = "";
+        for (Map.Entry<String, String> entry : map.entrySet()) {
+            List<String> list = Arrays.asList(entry.getValue().split(","));
+            if (list.contains(value)) {
+                key = entry.getKey();
+            }
+        }
+        return key;
+    }
+
+    //根据map的value模糊匹配map的key
+    public static BigDecimal getDecKeyByContainsString(Map<BigDecimal, String> map, String value) {
+        BigDecimal key = BigDecimal.ZERO;
+        for (Map.Entry<BigDecimal, String> entry : map.entrySet()) {
+            List<String> list = Arrays.asList(entry.getValue().split(","));
+            if (list.contains(value)) {
+                key = entry.getKey();
+            }
+        }
+        return key;
+    }
+
+    /**
+     * 根据map的value模糊匹配map的key  key是list
+     *
+     * @param map
+     * @param value
+     * @return
+     */
+    public static List<String> getListKeyByContainsString(Map<String, String> map, String value) {
+        List<String> list = new ArrayList<>();
+        for (Map.Entry<String, String> entry : map.entrySet()) {
+            if (StringUtils.isEmpty(value)) {
+                return list;
+            }
+            List<String> l = Arrays.asList(entry.getValue().split(","));
+            if (l.contains(value)) {
+                list.add(entry.getKey());
+            }
+        }
+        return list;
+    }
+
+    //根据map的key模糊匹配map的value
+    public static BigDecimal getDecValueByContainsString(Map<String, BigDecimal> map, String value) {
+        BigDecimal key = BigDecimal.ZERO;
+        for (Map.Entry<String, BigDecimal> entry : map.entrySet()) {
+            List<String> list = Arrays.asList(entry.getKey().split(","));
+            if (list.contains(value)) {
+                key = entry.getValue();
+            }
+        }
+        return key;
+    }
+
+    /**
+     * 实体类转map
+     *
+     * @param obj 实体类
+     * @return
+     */
+    public static Map<String, Object> beanToMap(Object obj) {
+        if (obj == null) {
+            return null;
+        }
+        Map<String, Object> map = new HashMap<>();
+        try {
+            BeanInfo beanInfo = Introspector.getBeanInfo(obj.getClass());
+            PropertyDescriptor[] propertyDescriptors = beanInfo.getPropertyDescriptors();
+            for (PropertyDescriptor property : propertyDescriptors) {
+                String key = property.getName();
+                if (!key.equals("class")) {
+                    //得到getter方法
+                    Method getter = property.getReadMethod();
+                    Object value = getter.invoke(obj);
+                    if (ObjectUtil.isNotEmpty(value)) {
+                        map.put(key, value);
+                    }
+                }
+            }
+        } catch (Exception e) {
+            System.err.println("反射错误");
+        }
+        return map;
+    }
+
+    /**
+     * 拼接 &
+     *
+     * @param params
+     * @return
+     */
+    public static String createLinkString(Map<String, Object> params) {
+        List<String> keys = new ArrayList<String>(params.keySet());
+        //进行首字母排序
+        Collections.sort(keys);
+        StringBuilder prestrSB = new StringBuilder();
+        for (int i = 0; i < keys.size(); i++) {
+            String key = keys.get(i);
+            Object value = params.get(key);
+            try {
+                value = URLEncoder.encode(value.toString(), "UTF-8");
+            } catch (UnsupportedEncodingException e) {
+                e.printStackTrace();
+            }
+            if (i == keys.size() - 1) {// 拼接时,不包括最后一个&字符
+                prestrSB.append(key).append("=").append(value);
+            } else {
+                prestrSB.append(key).append("=").append(value).append("&");
+            }
+        }
+        return prestrSB.toString();
+    }
+
+    /**
+     * 实体类对比
+     *
+     * @param newEntity
+     * @param oldEntity
+     * @param filed
+     * @return
+     */
+    public static Map<String, List<Object>> compareFields(Object newEntity, Object oldEntity, String filed) {
+        Map<String, String> filedInfo = new HashMap<>();
+        if (StringUtils.isNotEmpty(filed)) {
+            List<String> list = Arrays.asList(filed.split(","));
+            for (String s : list) {
+                filedInfo.put(s, s);
+            }
+        }
+        try {
+            Map<String, List<Object>> map = new HashMap<>();
+            // 只有两个对象都是同一类型的才有可比性
+            if (newEntity.getClass() == oldEntity.getClass()) {
+                Class clazz = newEntity.getClass();
+                // 获取object的属性描述
+                PropertyDescriptor[] pds = Introspector.getBeanInfo(clazz,
+                        Object.class).getPropertyDescriptors();
+                // 属性对比
+                for (PropertyDescriptor pd : pds) {
+                    // 属性名
+                    String name = pd.getName();
+                    // 是否当前属性选择进行比较,否跳到下一次循环
+                    if (filedInfo != null && filedInfo.containsKey(name)) {
+                        // get方法
+                        Method readMethod = pd.getReadMethod();
+                        // 在objBefore上调用get方法等同于获得objBefore的属性值
+                        Object objBefore = readMethod.invoke(oldEntity);
+                        // 在objAfter上调用get方法等同于获得objAfter的属性值
+                        Object objAfter = readMethod.invoke(newEntity);
+
+                        //如果是日期类型需要转化成时间戳
+                        if (objBefore instanceof Timestamp) {
+                            objBefore = new Date(((Timestamp) objBefore).getTime());
+                        }
+                        if (objAfter instanceof Timestamp) {
+                            objAfter = new Date(((Timestamp) objAfter).getTime());
+                        }
+                        //判断开始
+                        if (objBefore == null && objAfter == null) {
+                            continue;
+                        } else if (objBefore == null && objAfter != null) {
+                            List<Object> list = new ArrayList<Object>();
+                            list.add(objBefore);
+                            list.add(objAfter);
+                            map.put(name, list);
+                            continue;
+                        }
+                        // 比较这两个值是否相等,不等则放入map
+                        //如果是金额类型需要转成BigDecimal
+                        if (objBefore instanceof BigDecimal && objAfter instanceof BigDecimal) {
+                            BigDecimal b1 = new BigDecimal(objBefore.toString());
+                            BigDecimal b2 = new BigDecimal(objAfter.toString());
+                            if (b1.compareTo(b2) != 0) {
+                                List<Object> list = new ArrayList<Object>();
+                                list.add(objBefore);
+                                list.add(objAfter);
+                                map.put(name, list);
+                            }
+                        } else {
+                            if (!objBefore.equals(objAfter)) {
+                                List<Object> list = new ArrayList<Object>();
+                                list.add(objBefore);
+                                list.add(objAfter);
+                                map.put(name, list);
+                            }
+                        }
+
+                    }
+                }
+            }
+            return map;
+        } catch (Exception e) {
+            return null;
+        }
+    }
+}

+ 1 - 1
ruoyi-common/src/main/java/com/ruoyi/common/utils/http/HttpUtils.java

@@ -96,7 +96,7 @@ public class HttpUtils {
      * @param param 请求参数,请求参数应该是 name1=value1&name2=value2 的形式。
      * @return 所代表远程资源的响应结果
      */
-    public static String sendPost(String url, String param) {
+    public static String sendPost(String url, Object param) {
         PrintWriter out = null;
         BufferedReader in = null;
         StringBuilder result = new StringBuilder();