|
@@ -0,0 +1,324 @@
|
|
|
+package com.persagy.apm.common.configuration;
|
|
|
+
|
|
|
+import com.alibaba.fastjson.JSONObject;
|
|
|
+import org.apache.commons.lang.StringUtils;
|
|
|
+import org.springframework.util.CollectionUtils;
|
|
|
+
|
|
|
+import java.lang.reflect.Field;
|
|
|
+import java.lang.reflect.Modifier;
|
|
|
+import java.math.BigDecimal;
|
|
|
+import java.text.DecimalFormat;
|
|
|
+import java.util.*;
|
|
|
+
|
|
|
+/**
|
|
|
+ * 数值、集合处理工具类
|
|
|
+ *
|
|
|
+ * @author feng
|
|
|
+ */
|
|
|
+public class DataUtils {
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 对象转Double类型
|
|
|
+ *
|
|
|
+ * @param object
|
|
|
+ * @return
|
|
|
+ */
|
|
|
+ public static Double parseDouble(Object object) {
|
|
|
+ if (object == null) {
|
|
|
+ return null;
|
|
|
+ }
|
|
|
+ if (object instanceof String) {
|
|
|
+ if (StringUtils.isBlank((String) object)) {
|
|
|
+ return null;
|
|
|
+ }
|
|
|
+ return Double.valueOf(object.toString());
|
|
|
+ }
|
|
|
+ if (object instanceof Integer) {
|
|
|
+ return ((Integer) object).doubleValue();
|
|
|
+ }
|
|
|
+ if (object instanceof Long) {
|
|
|
+ return ((Long) object).doubleValue();
|
|
|
+ }
|
|
|
+ if (object instanceof Double) {
|
|
|
+ return (Double) object;
|
|
|
+ }
|
|
|
+ if (object instanceof BigDecimal){
|
|
|
+ return ((BigDecimal) object).doubleValue();
|
|
|
+ }
|
|
|
+ return (Double) object;
|
|
|
+ }
|
|
|
+
|
|
|
+ public static String parseString(Object object) {
|
|
|
+ if (object == null) {
|
|
|
+ return "";
|
|
|
+ }
|
|
|
+ if(object instanceof Double){
|
|
|
+ Long objectLong = ((Double) object).longValue();
|
|
|
+ if(objectLong.doubleValue()== (Double) object){
|
|
|
+ return objectLong.toString();
|
|
|
+ }
|
|
|
+ BigDecimal bigDecimal = BigDecimal.valueOf((double) object);
|
|
|
+ return bigDecimal.toString();
|
|
|
+ }
|
|
|
+ return object.toString();
|
|
|
+ }
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 设置小数精度
|
|
|
+ *
|
|
|
+ * @param obj
|
|
|
+ * @param count 精确的小数位数
|
|
|
+ * @return
|
|
|
+ */
|
|
|
+ public static Double setDecimalScale(Double obj, int count) {
|
|
|
+ if (obj == null) {
|
|
|
+ return null;
|
|
|
+ }
|
|
|
+ BigDecimal bd = new BigDecimal(obj);
|
|
|
+ return bd.setScale(count, BigDecimal.ROUND_HALF_UP).doubleValue();
|
|
|
+ }
|
|
|
+
|
|
|
+ public static String getUUID(){
|
|
|
+ return UUID.randomUUID().toString().replace("-", "");
|
|
|
+ }
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 将Double类型的数字向下取整
|
|
|
+ *
|
|
|
+ * @param num
|
|
|
+ * @return
|
|
|
+ */
|
|
|
+ public static String getInt(Object num) {
|
|
|
+ if (num == null) {
|
|
|
+ return null;
|
|
|
+ }
|
|
|
+ try {
|
|
|
+ Double dob = Double.valueOf(num + "");
|
|
|
+ DecimalFormat df = new DecimalFormat("#.#");
|
|
|
+ return df.format(dob);
|
|
|
+ } catch (Exception e) {
|
|
|
+ return num + "";
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ /**
|
|
|
+ * BigDecimal精确计算
|
|
|
+ *
|
|
|
+ * @param dividend
|
|
|
+ * @param divisor
|
|
|
+ * @param decimalScale 小数点精确范围
|
|
|
+ * @return
|
|
|
+ * @throws Exception
|
|
|
+ */
|
|
|
+ public static <T extends Number> Double divide(T dividend, T divisor, Integer decimalScale) throws Exception {
|
|
|
+ if (dividend == null || divisor == null) {
|
|
|
+ return null;
|
|
|
+ }
|
|
|
+ if (divisor.doubleValue() == 0) {
|
|
|
+ return null;
|
|
|
+ }
|
|
|
+ if (decimalScale == null) {
|
|
|
+ decimalScale = DECIMAL_SCALE;
|
|
|
+ }
|
|
|
+ BigDecimal dividendDecimal = new BigDecimal(dividend.toString());
|
|
|
+ BigDecimal divisorDecimal = new BigDecimal(divisor.toString());
|
|
|
+ return dividendDecimal.divide(divisorDecimal, decimalScale, BigDecimal.ROUND_HALF_UP).doubleValue();
|
|
|
+ }
|
|
|
+
|
|
|
+ private static final int DECIMAL_SCALE = 12;
|
|
|
+ public static <T extends Number> Double divide(T dividend, T divisor) throws Exception {
|
|
|
+ return divide(dividend,divisor,DECIMAL_SCALE);
|
|
|
+ }
|
|
|
+
|
|
|
+ public static Double subtract(Double minuend, Double subtrahend) {
|
|
|
+ if (minuend == null && subtrahend == null) {
|
|
|
+ return null;
|
|
|
+ }
|
|
|
+ if (minuend == null) {
|
|
|
+ return -subtrahend;
|
|
|
+ }
|
|
|
+ if (subtrahend == null) {
|
|
|
+ return minuend;
|
|
|
+ }
|
|
|
+ return minuend - subtrahend;
|
|
|
+ }
|
|
|
+
|
|
|
+ public static Double plus(Double data1, Double data2) {
|
|
|
+ if (data1 == null && data2 == null) {
|
|
|
+ return null;
|
|
|
+ }
|
|
|
+ if (data1 == null) {
|
|
|
+ return data2;
|
|
|
+ }
|
|
|
+ if (data2 == null) {
|
|
|
+ return data1;
|
|
|
+ }
|
|
|
+ return data1 + data2;
|
|
|
+ }
|
|
|
+
|
|
|
+ public static Double plus(Double... dataList) {
|
|
|
+ Double sum = null;
|
|
|
+ for (Double data : dataList) {
|
|
|
+ sum = plus(sum, data);
|
|
|
+ }
|
|
|
+ return sum;
|
|
|
+ }
|
|
|
+
|
|
|
+ public static Integer plus(Integer... dataList) {
|
|
|
+ Integer sum = null;
|
|
|
+ for (Integer data : dataList) {
|
|
|
+ sum = plus(sum, data);
|
|
|
+ }
|
|
|
+ return sum;
|
|
|
+ }
|
|
|
+ public static Integer plus(Integer data1, Integer data2) {
|
|
|
+ if (data1 == null && data2 == null) {
|
|
|
+ return null;
|
|
|
+ }
|
|
|
+ if (data1 == null) {
|
|
|
+ return data2;
|
|
|
+ }
|
|
|
+ if (data2 == null) {
|
|
|
+ return data1;
|
|
|
+ }
|
|
|
+ return data1 + data2;
|
|
|
+ }
|
|
|
+
|
|
|
+ public static Integer getInt(Double data) {
|
|
|
+ if (data == null) {
|
|
|
+ return null;
|
|
|
+ }
|
|
|
+ return (int) (data + 0.5);
|
|
|
+ }
|
|
|
+
|
|
|
+ public static Double formatDataNumber(Double energyData) {
|
|
|
+ if (energyData == null) {
|
|
|
+ return null;
|
|
|
+ }
|
|
|
+ if (energyData < 1) {
|
|
|
+ return DataUtils.setDecimalScale(energyData, 3);
|
|
|
+ } else if (energyData < 1000) {
|
|
|
+ return DataUtils.setDecimalScale(energyData, 1);
|
|
|
+ } else {
|
|
|
+ return Math.round(energyData) * 1.0;
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ @SuppressWarnings({ "unchecked", "rawtypes" })
|
|
|
+ public static void sortList(List srcList, String sortField, boolean isAsc) throws Exception{
|
|
|
+ if(CollectionUtils.isEmpty(srcList)){
|
|
|
+ return;
|
|
|
+ }
|
|
|
+ Collections.sort(srcList, (o1, o2) -> {
|
|
|
+ JSONObject object1 = JSONObject.parseObject(JSONObject.toJSONString(o1));
|
|
|
+ JSONObject object2 = JSONObject.parseObject(JSONObject.toJSONString(o2));
|
|
|
+ Object name1 = object1.get(sortField);
|
|
|
+ Object name2 = object2.get(sortField);
|
|
|
+ int coefficient;
|
|
|
+ if (isAsc) {
|
|
|
+ coefficient = 1;
|
|
|
+ } else {
|
|
|
+ coefficient = -1;
|
|
|
+ }
|
|
|
+ if (name1 != null && name2 != null) {
|
|
|
+ if (name1 instanceof Double) {
|
|
|
+ return ((Double) name1).compareTo((Double) name2) * coefficient;
|
|
|
+ } else if (name1 instanceof Integer) {
|
|
|
+ return ((Integer) name1).compareTo((Integer) name2) * coefficient;
|
|
|
+ } else if (name1 instanceof Long) {
|
|
|
+ return ((Long) name1).compareTo((Long) name2) * coefficient;
|
|
|
+ } else if (name1 instanceof BigDecimal) {
|
|
|
+ return ((BigDecimal) name1).compareTo((BigDecimal) name2) * coefficient;
|
|
|
+ } else if(name1 instanceof Date){
|
|
|
+ return ((Date) name1).compareTo((Date) name2) * coefficient;
|
|
|
+ } else {
|
|
|
+ return ((String) name1).compareTo(((String) name2)) * coefficient;
|
|
|
+ }
|
|
|
+ } else if (name1 != null) {
|
|
|
+ return coefficient;
|
|
|
+ } else if (name2 != null) {
|
|
|
+ return -coefficient;
|
|
|
+ } else {
|
|
|
+ return 0;
|
|
|
+ }
|
|
|
+
|
|
|
+ });
|
|
|
+ }
|
|
|
+
|
|
|
+
|
|
|
+ public static void sortList(List<Object> energyList) {
|
|
|
+ Collections.sort(energyList, (o1, o2) -> {
|
|
|
+ if (o1 == null && o2 == null) {
|
|
|
+ return 0;
|
|
|
+ } else if (o1 == null) {
|
|
|
+ return -1;
|
|
|
+ } else if (o2 == null) {
|
|
|
+ return 1;
|
|
|
+ } else {
|
|
|
+ if (o1 instanceof Double) {
|
|
|
+ return ((Double) o1).compareTo((Double) o2);
|
|
|
+ } else if (o1 instanceof Integer) {
|
|
|
+ return ((Integer) o1).compareTo((Integer) o2);
|
|
|
+ } else if (o1 instanceof Long) {
|
|
|
+ return ((Long) o1).compareTo((Long) o2);
|
|
|
+ } else if (o1 instanceof BigDecimal) {
|
|
|
+ return ((BigDecimal) o1).compareTo((BigDecimal) o2);
|
|
|
+ } else if (o1 instanceof Date) {
|
|
|
+ return ((Date) o1).compareTo((Date) o2);
|
|
|
+ } else {
|
|
|
+ return ((String) o1).compareTo(((String) o2));
|
|
|
+ }
|
|
|
+ }
|
|
|
+ });
|
|
|
+ }
|
|
|
+
|
|
|
+ public static List<String> getPagingObjectList(List<String> allObjectList, Integer pageNum, Integer pageSize) {
|
|
|
+ if (CollectionUtils.isEmpty(allObjectList)) {
|
|
|
+ return new ArrayList<>();
|
|
|
+ }
|
|
|
+
|
|
|
+ int skipCount = (pageNum - 1) * pageSize;
|
|
|
+ int totalCount = allObjectList.size();
|
|
|
+ if (skipCount >= totalCount) {
|
|
|
+ return new ArrayList<>();
|
|
|
+ }
|
|
|
+ List<String> resultList = new ArrayList<>();
|
|
|
+ for (int i = 0; i < pageSize; i++) {
|
|
|
+ int index = skipCount + i;
|
|
|
+ if (index >= totalCount) {
|
|
|
+ break;
|
|
|
+ }
|
|
|
+ resultList.add(allObjectList.get(index));
|
|
|
+ }
|
|
|
+ return resultList;
|
|
|
+ }
|
|
|
+
|
|
|
+ /**
|
|
|
+ * description 筛选originMapList中包含keyWord的对象
|
|
|
+ * @param originMapList
|
|
|
+ * @param keyWord
|
|
|
+ * @return java.util.List<java.util.Map<java.lang.String,java.lang.Object>>
|
|
|
+ * @author feng
|
|
|
+ * @since 19:47 2020/6/4
|
|
|
+ * @version 1.0
|
|
|
+ */
|
|
|
+ public static List<Map<String, Object>> queryByKeyWord(List<Map<String, Object>> originMapList, String keyWord) {
|
|
|
+ if (StringUtils.isBlank(keyWord)) {
|
|
|
+ return originMapList;
|
|
|
+ }
|
|
|
+ if (CollectionUtils.isEmpty(originMapList)) {
|
|
|
+ return new ArrayList<>();
|
|
|
+ }
|
|
|
+ List<Map<String, Object>> resultMapList = new ArrayList<>();
|
|
|
+ for (Map<String, Object> originMap : originMapList) {
|
|
|
+ for (String key : originMap.keySet()) {
|
|
|
+ Object data = originMap.get(key);
|
|
|
+ if (data != null && data.toString().contains(keyWord)) {
|
|
|
+ resultMapList.add(originMap);
|
|
|
+ break;
|
|
|
+ }
|
|
|
+ }
|
|
|
+ }
|
|
|
+ return resultMapList;
|
|
|
+ }
|
|
|
+}
|