tianyun 2 vuotta sitten
vanhempi
commit
0395c48c4f

+ 49 - 6
springboot-main/src/main/java/com/alvin/util/StopWatch.java

@@ -13,17 +13,60 @@ public class StopWatch {
         this.name = name;
     }
 
-    public String stop() {
+    /**
+     * 将毫秒转换为  时分秒,用于时间间隔的计算
+     *
+     * @param ms 毫秒数
+     * @return {@link String}
+     */
+    public static String formatTime(Long ms) {
+        Integer ss = 1000;
+        Integer mi = ss * 60;
+        Integer hh = mi * 60;
+        Integer dd = hh * 24;
+
+        Long day = ms / dd;
+        Long hour = (ms - day * dd) / hh;
+        Long minute = (ms - day * dd - hour * hh) / mi;
+        Long second = (ms - day * dd - hour * hh - minute * mi) / ss;
+        Long milliSecond = ms - day * dd - hour * hh - minute * mi - second * ss;
+
+        StringBuffer sb = new StringBuffer();
+        if (day > 0) {
+            sb.append(day + " 天 ");
+        }
+        if (hour > 0) {
+            sb.append(hour + " 小时 ");
+        }
+        if (minute > 0) {
+            sb.append(minute + " 分 ");
+        }
+        if (second > 0) {
+            sb.append(second + " 秒 ");
+        }
+        if (milliSecond > 0) {
+            sb.append(milliSecond + " 毫秒");
+        }
+        return sb.toString();
+    }
+
+    /**
+     * 停止并格式化输出
+     *
+     * @return {@link String}
+     */
+    public String stopAndPretty() {
         long endTime = System.currentTimeMillis();
         long spendTime = endTime - this.startTime;
-        String s = DateUtil.formatTime(spendTime);
-        System.out.println(this.name + " 用时:" + s);
-        return s;
+        String s = formatTime(spendTime);
+        String res = this.name + " 用时:" + s;
+        System.out.println(res);
+        return res;
     }
 
     public static void main(String[] args) throws InterruptedException {
         StopWatch stopWatch = new StopWatch("A");
-        Thread.sleep(100);
-        stopWatch.stop();
+        Thread.sleep(1230);
+        stopWatch.stopAndPretty();
     }
 }

+ 104 - 0
test1/src/main/java/JsonTransformer.java

@@ -0,0 +1,104 @@
+import com.alibaba.fastjson.JSONArray;
+import com.alibaba.fastjson.JSONObject;
+
+import java.util.UUID;
+
+public class JsonTransformer {
+
+    public static void main(String[] args) {
+        String json = "{\n" +
+                "    \"relationship\": \"and\",\n" +
+                "    \"list\": [\n" +
+                "        {\n" +
+                "            \"relationship\": \"or\",\n" +
+                "            \"list\": [\n" +
+                "                {\n" +
+                "                    \"key\": \"tag1\",\n" +
+                "                    \"op\": \"greater\",\n" +
+                "                    \"value\": 0\n" +
+                "                },\n" +
+                "                {\n" +
+                "                    \"key\": \"tag2\",\n" +
+                "                    \"op\": \"less\",\n" +
+                "                    \"value\": 2\n" +
+                "                },\n" +
+                "                {\n" +
+                "                    \"key\": \"tag5\",\n" +
+                "                    \"op\": \"equals\",\n" +
+                "                    \"value\": 7\n" +
+                "                }\n" +
+                "            ]\n" +
+                "        },\n" +
+                "        {\n" +
+                "            \"relationship\": \"and\",\n" +
+                "            \"list\": [\n" +
+                "                {\n" +
+                "                    \"key\": \"tag3\",\n" +
+                "                    \"op\": \"equals\",\n" +
+                "                    \"value\": 4\n" +
+                "                }\n" +
+                "            ]\n" +
+                "        },\n" +
+                "        {\n" +
+                "            \"relationship\": \"and\",\n" +
+                "            \"list\": [\n" +
+                "                {\n" +
+                "                    \"key\": \"tag3\",\n" +
+                "                    \"op\": \"equals\",\n" +
+                "                    \"value\": 4\n" +
+                "                },\n" +
+                "                {\n" +
+                "                    \"relationship\": \"or\",\n" +
+                "                    \"list\": [\n" +
+                "                        {\n" +
+                "                            \"key\": \"tag3\",\n" +
+                "                            \"op\": \"equals\",\n" +
+                "                            \"value\": 4\n" +
+                "                        },\n" +
+                "                        {\n" +
+                "                            \"key\": \"tag4\",\n" +
+                "                            \"op\": \"equals\",\n" +
+                "                            \"value\": 5\n" +
+                "                        }\n" +
+                "                    ]\n" +
+                "                }\n" +
+                "            ]\n" +
+                "        }\n" +
+                "    ]\n" +
+                "}\n";
+        JSONObject oldJson = JSONObject.parseObject(json);
+        JSONObject newJson = transformJson(oldJson);
+        System.out.println(newJson);
+    }
+
+    public static JSONObject transformJson(JSONObject oldJson) {
+        JSONObject newJson = new JSONObject();
+        JSONArray children = new JSONArray();
+        newJson.put("id", UUID.randomUUID().toString());
+        newJson.put("conjunction", oldJson.getString("relationship"));
+        JSONArray oldList = oldJson.getJSONArray("list");
+        for (int i = 0; i < oldList.size(); i++) {
+            JSONObject oldChild = oldList.getJSONObject(i);
+            if (oldChild.containsKey("relationship")) {
+                children.add(transformJson(oldChild));
+            } else {
+                JSONObject child = new JSONObject();
+                child.put("id", UUID.randomUUID().toString());
+                child.put("left", createFieldJson(oldChild.getString("key")));
+                child.put("op", oldChild.getString("op"));
+                child.put("right", oldChild.get("value"));
+                children.add(child);
+            }
+        }
+        newJson.put("children", children);
+        return newJson;
+    }
+
+    public static JSONObject createFieldJson(String field) {
+        JSONObject fieldJson = new JSONObject();
+        fieldJson.put("type", "field");
+        fieldJson.put("field", field);
+        return fieldJson;
+    }
+
+}

+ 73 - 24
test1/src/main/java/tmp.java

@@ -1,30 +1,79 @@
-import java.util.HashMap;
+import com.alibaba.fastjson.JSONArray;
+import com.alibaba.fastjson.JSONObject;
+import java.util.UUID;
 
 public class tmp {
-    private static String solution(String value) {
-        String[] arr = value.split(",");
-        //valueArray是题目中的整型数组
-        int[] valueArray = new int[arr.length];
-        for (int i = 0; i < arr.length; i++) {
-            valueArray[i] = Integer.parseInt(arr[i]);
-        }
-
-        //Coding here...
-        int minValue = Integer.MAX_VALUE;
-        int maxD = 0;
-        for (int i = 0; i < valueArray.length; i++) {
-            if (valueArray[i] < minValue) {
-                minValue = valueArray[i];
-            } else if (valueArray[i] - minValue > maxD) {
-                maxD = valueArray[i] - minValue;
-            }
-        }
-
-        //在处理完问题之后,请将你的结果转换为字符串,并return
-        return String.valueOf(maxD);
+
+    public static JSONObject convert(JSONObject inputJson) {
+        JSONObject outputJson = new JSONObject();
+        JSONObject conditions = new JSONObject();
+        JSONArray children = new JSONArray();
+        String conjunction = inputJson.getString("relationship");
+
+        inputJson.getJSONArray("list").forEach(item -> {
+            JSONObject child = new JSONObject();
+            String id = UUID.randomUUID().toString();
+
+            child.put("id", id);
+
+            JSONObject left = new JSONObject();
+            left.put("type", "field");
+            left.put("field", ((JSONObject) item).getString("key"));
+
+            String op = ((JSONObject) item).getString("op");
+
+            Object right = ((JSONObject) item).getString("value");
+
+            child.put("left", left);
+            child.put("op", op);
+            child.put("right", right);
+
+            children.add(child);
+        });
+
+        conditions.put("id", UUID.randomUUID().toString());
+        conditions.put("conjunction", conjunction);
+        conditions.put("children", children);
+
+        outputJson.put("conditions", conditions);
+        return outputJson;
     }
 
     public static void main(String[] args) {
-        System.out.println(solution("7,1,5,3,6,4"));
+        String inputStr = "{\n" +
+                "        \"relationship\":\"and\",\n" +
+                "        \"list\":[\n" +
+                "                {\n" +
+                "                    \n" +
+                "                     \"key\": \"tag1\",\n" +
+                "                     \"op\": \"greater\",\n" +
+                "                     \"value\": 0\n" +
+                "                },{\n" +
+                "                    \n" +
+                "                     \"key\": \"tag2\",\n" +
+                "                     \"op\": \"equals\",\n" +
+                "                     \"value\": \"value\"\n" +
+                "                },{\n" +
+                "        \"relationship\":\"and\",\n" +
+                "        \"list\":[\n" +
+                "                {\n" +
+                "                    \n" +
+                "                     \"key\": \"tag1\",\n" +
+                "                     \"op\": \"greater\",\n" +
+                "                     \"value\": 0\n" +
+                "                },{\n" +
+                "                    \n" +
+                "                     \"key\": \"tag2\",\n" +
+                "                     \"op\": \"equals\",\n" +
+                "                     \"value\": \"value\"\n" +
+                "                }\n" +
+                "        ]\n" +
+                "}\n" +
+                "        ]\n" +
+                "}";
+
+        JSONObject inputJson = JSONObject.parseObject(inputStr);
+        JSONObject outputJson = tmp.convert(inputJson);
+        System.out.println(outputJson.toJSONString());
     }
-}
+}