|
@@ -0,0 +1,273 @@
|
|
|
|
+/**
|
|
|
|
+ * @包名称 com.sagacloud.common
|
|
|
|
+ * @文件名 ToolsUtil.java
|
|
|
|
+ * @创建者 wanghailong
|
|
|
|
+ * @邮箱 wanghailong@persagy.com
|
|
|
|
+ * @修改描述
|
|
|
|
+ */
|
|
|
|
+
|
|
|
|
+package com.persagy.fm.common.old.utils;
|
|
|
|
+
|
|
|
|
+import com.alibaba.fastjson.JSON;
|
|
|
|
+import com.alibaba.fastjson.JSONArray;
|
|
|
|
+import com.alibaba.fastjson.JSONObject;
|
|
|
|
+import com.persagy.fm.common.constant.enums.ResultEnum;
|
|
|
|
+import sun.misc.BASE64Encoder;
|
|
|
|
+
|
|
|
|
+import java.io.UnsupportedEncodingException;
|
|
|
|
+import java.security.MessageDigest;
|
|
|
|
+import java.security.NoSuchAlgorithmException;
|
|
|
|
+import java.util.*;
|
|
|
|
+import java.util.stream.Stream;
|
|
|
|
+
|
|
|
|
+
|
|
|
|
+/**
|
|
|
|
+ * 功能描述: 常用工具方法
|
|
|
|
+ * @类型名称 ToolsUtil
|
|
|
|
+ * @创建者 wanghailong
|
|
|
|
+ * @邮箱 wanghailong@persagy.com
|
|
|
|
+ * @修改描述
|
|
|
|
+ */
|
|
|
|
+public class ToolsUtil {
|
|
|
|
+ //返回项-缺少必填项
|
|
|
|
+ public final static String return_error_json = "{\"Result\":\"failure\",\"ResultMsg\":\"缺少必填项\"}";
|
|
|
|
+ public static String errorJsonMsg(String msg) {
|
|
|
|
+ JSONObject jsonRes = new JSONObject();
|
|
|
|
+ jsonRes.put("Result", "failure");
|
|
|
|
+ jsonRes.put("ResultMsg", msg);
|
|
|
|
+ return jsonRes.toString();
|
|
|
|
+ }
|
|
|
|
+ public static String errorJsonMsg(JSONObject item){
|
|
|
|
+ JSONObject jsonRes = new JSONObject();
|
|
|
|
+ jsonRes.put("Result", "failure");
|
|
|
|
+ jsonRes.put("Item", item);
|
|
|
|
+ return jsonRes.toJSONString();
|
|
|
|
+ }
|
|
|
|
+
|
|
|
|
+ public static String successJsonMsg(JSONArray array){
|
|
|
|
+ JSONObject jsonRes = new JSONObject();
|
|
|
|
+ jsonRes.put("Result", "success");
|
|
|
|
+ jsonRes.put("Content", array);
|
|
|
|
+ jsonRes.put("Count", array.size());
|
|
|
|
+ return jsonRes.toJSONString();
|
|
|
|
+ }
|
|
|
|
+ public static <T> String successJsonMsg(List<T> array){
|
|
|
|
+ JSONObject jsonRes = new JSONObject();
|
|
|
|
+ jsonRes.put("Result", "success");
|
|
|
|
+ jsonRes.put("Content", array);
|
|
|
|
+ jsonRes.put("Count", array.size());
|
|
|
|
+ return jsonRes.toJSONString();
|
|
|
|
+ }
|
|
|
|
+
|
|
|
|
+ public static String successJsonMsg(JSONObject item){
|
|
|
|
+ JSONObject jsonRes = new JSONObject();
|
|
|
|
+ jsonRes.put("Result", "success");
|
|
|
|
+ jsonRes.put("Item", item);
|
|
|
|
+ return jsonRes.toJSONString();
|
|
|
|
+ }
|
|
|
|
+
|
|
|
|
+ public static String successJsonMsg(String successMsg){
|
|
|
|
+ JSONObject jsonRes = new JSONObject();
|
|
|
|
+ jsonRes.put("Result", "success");
|
|
|
|
+ jsonRes.put("ResultMsg", successMsg);
|
|
|
|
+ return jsonRes.toJSONString();
|
|
|
|
+ }
|
|
|
|
+
|
|
|
|
+
|
|
|
|
+ public static String getUuid() {
|
|
|
|
+ String uuid = UUID.randomUUID().toString().replace("-","");
|
|
|
|
+ return uuid;
|
|
|
|
+ }
|
|
|
|
+
|
|
|
|
+ /**
|
|
|
|
+ * 生成前缀+uuid的字符串
|
|
|
|
+ * @param prefix --前缀字符串
|
|
|
|
+ * @return recordId --prefix+uuid(去除-)
|
|
|
|
+ */
|
|
|
|
+ public static String getRecordId(String prefix) {
|
|
|
|
+ String recordId = "";
|
|
|
|
+ if (!StringUtil.isNull(prefix)) {
|
|
|
|
+ recordId = prefix;
|
|
|
|
+ }
|
|
|
|
+ recordId = recordId + UUID.randomUUID().toString().replace("-", "");
|
|
|
|
+ return recordId;
|
|
|
|
+ }
|
|
|
|
+ /**
|
|
|
|
+ * 倒序排序
|
|
|
|
+ * @param array
|
|
|
|
+ * @return
|
|
|
|
+ */
|
|
|
|
+ public static JSONArray sortDesc(JSONArray array){
|
|
|
|
+ JSONArray newArray = new JSONArray();
|
|
|
|
+ for(int i=array.size()-1; i>-1; i--){
|
|
|
|
+ newArray.add(array.get(i));
|
|
|
|
+ }
|
|
|
|
+ return newArray;
|
|
|
|
+ }
|
|
|
|
+
|
|
|
|
+ /**
|
|
|
|
+ * 根据条件过滤数组字段
|
|
|
|
+ * @param array
|
|
|
|
+ * @param filterCondition
|
|
|
|
+ * @return
|
|
|
|
+ */
|
|
|
|
+ public static JSONArray filterRemind(JSONArray array, JSONArray filterCondition){
|
|
|
|
+ JSONArray newArray = new JSONArray();
|
|
|
|
+ JSONObject item, newItem;
|
|
|
|
+ if(array != null){
|
|
|
|
+ for(int i=0; i<array.size(); i++){
|
|
|
|
+ item = array.getJSONObject(i);
|
|
|
|
+ newItem = new JSONObject();
|
|
|
|
+ for(int j=0; j<filterCondition.size(); j++){
|
|
|
|
+ newItem.put(filterCondition.getString(j), item.get(filterCondition.getString(j)));
|
|
|
|
+ }
|
|
|
|
+ newArray.add(newItem);
|
|
|
|
+ }
|
|
|
|
+ }
|
|
|
|
+ return newArray;
|
|
|
|
+ }
|
|
|
|
+ /**
|
|
|
|
+ * 根据条件过滤数组字段
|
|
|
|
+ * @param filterCondition
|
|
|
|
+ * @return
|
|
|
|
+ */
|
|
|
|
+ public static JSONObject filterRemind(JSONObject jsonObject, JSONArray filterCondition){
|
|
|
|
+ JSONObject newItem = new JSONObject();
|
|
|
|
+ if(jsonObject != null){
|
|
|
|
+ for(int j=0; j<filterCondition.size(); j++){
|
|
|
|
+ newItem.put(filterCondition.getString(j), jsonObject.get(filterCondition.getString(j)));
|
|
|
|
+ }
|
|
|
|
+ }
|
|
|
|
+ return newItem;
|
|
|
|
+ }
|
|
|
|
+ /**
|
|
|
|
+ * 根据条件过滤数组字段
|
|
|
|
+ * @param array
|
|
|
|
+ * @param filterConditions
|
|
|
|
+ * @return
|
|
|
|
+ */
|
|
|
|
+ public static JSONArray filterRemind(JSONArray array, String... filterConditions){
|
|
|
|
+ JSONArray newArray = new JSONArray();
|
|
|
|
+ JSONObject item, newItem;
|
|
|
|
+ if(array != null){
|
|
|
|
+ for(int i=0; i<array.size(); i++){
|
|
|
|
+ item = array.getJSONObject(i);
|
|
|
|
+ newItem = new JSONObject();
|
|
|
|
+ for(String filterCondition : filterConditions){
|
|
|
|
+ newItem.put(filterCondition, item.get(filterCondition));
|
|
|
|
+ }
|
|
|
|
+ newArray.add(newItem);
|
|
|
|
+ }
|
|
|
|
+ }
|
|
|
|
+ return newArray;
|
|
|
|
+ }
|
|
|
|
+ /**
|
|
|
|
+ * 根据条件过滤数组字段
|
|
|
|
+ * @param queryResult
|
|
|
|
+ * @param filterConditions
|
|
|
|
+ * @return
|
|
|
|
+ */
|
|
|
|
+ public static String filterRemind(String queryResult, String... filterConditions){
|
|
|
|
+ if(queryResult.contains(ResultEnum.RESULT.getType()) && queryResult.contains(ResultEnum.CONTENT.getType())) {
|
|
|
|
+ JSONObject resultJson = JSONObject.parseObject(queryResult);
|
|
|
|
+ JSONArray contents = resultJson.getJSONArray(ResultEnum.CONTENT.getType());
|
|
|
|
+ contents = filterRemind(contents, filterConditions);
|
|
|
|
+ resultJson.put(ResultEnum.CONTENT.getType(), contents);
|
|
|
|
+ queryResult = JSON.toJSONString(resultJson);
|
|
|
|
+ }
|
|
|
|
+ return queryResult;
|
|
|
|
+ }
|
|
|
|
+ /**
|
|
|
|
+ * 根据条件过滤数组字段
|
|
|
|
+ * @param queryResult
|
|
|
|
+ * @param filterCondition
|
|
|
|
+ * @return
|
|
|
|
+ */
|
|
|
|
+ public static String filterRemind(String queryResult, JSONArray filterCondition){
|
|
|
|
+ if(queryResult.contains(ResultEnum.RESULT.getType()) && queryResult.contains(ResultEnum.CONTENT.getType())) {
|
|
|
|
+ JSONObject resultJson = JSONObject.parseObject(queryResult);
|
|
|
|
+ JSONArray contents = resultJson.getJSONArray(ResultEnum.CONTENT.getType());
|
|
|
|
+ contents = filterRemind(contents, filterCondition);
|
|
|
|
+ resultJson.put(ResultEnum.CONTENT.getType(), contents);
|
|
|
|
+ queryResult = JSON.toJSONString(resultJson);
|
|
|
|
+ }
|
|
|
|
+ return queryResult;
|
|
|
|
+ }
|
|
|
|
+
|
|
|
|
+ /**
|
|
|
|
+ * MD5加密
|
|
|
|
+ * @param str
|
|
|
|
+ * @return
|
|
|
|
+ * @throws NoSuchAlgorithmException
|
|
|
|
+ * @throws UnsupportedEncodingException
|
|
|
|
+ */
|
|
|
|
+ public static String encodeByMd5(String str) throws NoSuchAlgorithmException, UnsupportedEncodingException {
|
|
|
|
+ //确定计算方法
|
|
|
|
+ MessageDigest md5= MessageDigest.getInstance("MD5");
|
|
|
|
+ BASE64Encoder base64en = new BASE64Encoder();
|
|
|
|
+ //加密后的字符串
|
|
|
|
+ String newstr=base64en.encode(md5.digest(str.getBytes("utf-8")));
|
|
|
|
+ return newstr;
|
|
|
|
+ }
|
|
|
|
+
|
|
|
|
+ /**
|
|
|
|
+ * 根据map的value排序 倒序
|
|
|
|
+ * @param map
|
|
|
|
+ * @param <K>
|
|
|
|
+ * @return
|
|
|
|
+ */
|
|
|
|
+ public static <K> JSONArray sortMapByValue(Map<K, Integer> map){
|
|
|
|
+ JSONArray array = new JSONArray();
|
|
|
|
+ Stream<Map.Entry<K, Integer>> stream = map.entrySet().stream();
|
|
|
|
+ stream.sorted(Comparator.comparing(e -> Integer.valueOf(e.getValue()) * -1)).forEach(e -> array.add(e.getKey()));
|
|
|
|
+ return array;
|
|
|
|
+ }
|
|
|
|
+
|
|
|
|
+
|
|
|
|
+ /**
|
|
|
|
+ *
|
|
|
|
+ * Description: ZillionAgent返回结果是否成功
|
|
|
|
+ * @param resultStr:结果字符串
|
|
|
|
+ * @return boolean
|
|
|
|
+ * @author yuecaipu
|
|
|
|
+ * @since 2018年7月9日: 下午12:20:22
|
|
|
|
+ * Update By yuecaipu 2018年7月9日: 下午12:20:22
|
|
|
|
+ */
|
|
|
|
+ public static boolean isSuccessForResult(String resultStr) {
|
|
|
|
+ JSONObject resultJson = JSONObject.parseObject(resultStr);
|
|
|
|
+ return isSuccessForResult(resultJson);
|
|
|
|
+ }
|
|
|
|
+ /***
|
|
|
|
+ * Description: 结果是否成功
|
|
|
|
+ * @param resultJson : 结果对象
|
|
|
|
+ * @return : boolean
|
|
|
|
+ * @author : lijie
|
|
|
|
+ * @date :2021/4/26 18:50
|
|
|
|
+ * Update By lijie 2021/4/26 18:50
|
|
|
|
+ */
|
|
|
|
+ public static boolean isSuccessForResult(JSONObject resultJson) {
|
|
|
|
+ String result = resultJson.getString(ResultEnum.RESULT.getType());
|
|
|
|
+ if (ResultEnum.SUCCESS.getType().equals(result)) {
|
|
|
|
+ return true;
|
|
|
|
+ }
|
|
|
|
+ return false;
|
|
|
|
+ }
|
|
|
|
+
|
|
|
|
+ /**
|
|
|
|
+ * Description: 获取一个key升序排序的map
|
|
|
|
+ * @return Map<String,JSONArray>
|
|
|
|
+ * @author yuecaipu
|
|
|
|
+ * @since 2018年7月26日: 下午1:35:01
|
|
|
|
+ * Update By yuecaipu 2018年7月26日: 下午1:35:01
|
|
|
|
+ */
|
|
|
|
+ public static Map<String, JSONArray> getAscMap(){
|
|
|
|
+ Map<String, JSONArray> map = new TreeMap<String, JSONArray>(
|
|
|
|
+ (obj1, obj2) -> {
|
|
|
|
+ // 升序排序
|
|
|
|
+ Integer order1 = Integer.valueOf(obj1);
|
|
|
|
+ Integer order2 = Integer.valueOf(obj2);
|
|
|
|
+ return order1.compareTo(order2);
|
|
|
|
+ });
|
|
|
|
+ return map;
|
|
|
|
+ }
|
|
|
|
+ }
|