Pārlūkot izejas kodu

增加分类目录

zhou-hao 5 gadi atpakaļ
vecāks
revīzija
4b7f37e3ad

+ 24 - 0
jetlinks-manager/device-manager/src/main/java/org/jetlinks/community/device/entity/DeviceCategory.java

@@ -0,0 +1,24 @@
+package org.jetlinks.community.device.entity;
+
+import lombok.Getter;
+import lombok.Setter;
+import org.hswebframework.web.api.crud.entity.GenericTreeSortSupportEntity;
+
+import java.util.List;
+
+@Getter
+@Setter
+public class DeviceCategory extends GenericTreeSortSupportEntity<String> {
+
+    private String id;
+
+    private String key;
+
+    private String name;
+
+    private String parentId;
+
+    private List<DeviceCategory> children;
+
+
+}

+ 73 - 0
jetlinks-manager/device-manager/src/main/java/org/jetlinks/community/device/web/DeviceCategoryController.java

@@ -0,0 +1,73 @@
+package org.jetlinks.community.device.web;
+
+import com.alibaba.fastjson.JSON;
+import lombok.extern.slf4j.Slf4j;
+import org.hswebframework.web.api.crud.entity.TreeSupportEntity;
+import org.jetlinks.community.device.entity.DeviceCategory;
+import org.springframework.core.io.ClassPathResource;
+import org.springframework.util.StreamUtils;
+import org.springframework.web.bind.annotation.GetMapping;
+import org.springframework.web.bind.annotation.RequestMapping;
+import org.springframework.web.bind.annotation.RestController;
+import reactor.core.publisher.Flux;
+
+import java.nio.charset.StandardCharsets;
+import java.util.ArrayList;
+import java.util.List;
+
+@RestController
+@RequestMapping("/device/category")
+@Slf4j
+public class DeviceCategoryController {
+
+
+    static List<DeviceCategory> statics;
+
+
+    static void rebuild(String parentId, List<DeviceCategory> children) {
+        if (children == null) {
+            return;
+        }
+        for (DeviceCategory child : children) {
+            String id = child.getId();
+            child.setId(parentId + "|" + id + "|");
+            child.setParentId(parentId + "|");
+            rebuild(parentId + "|" + id, child.getChildren());
+        }
+    }
+
+    static {
+        try {
+            ClassPathResource resource = new ClassPathResource("device-category.json");
+            String json = StreamUtils.copyToString(resource.getInputStream(), StandardCharsets.UTF_8);
+
+            List<DeviceCategory> all = JSON.parseArray(json, DeviceCategory.class);
+
+            List<DeviceCategory> root = TreeSupportEntity.list2tree(all, DeviceCategory::setChildren);
+
+            for (DeviceCategory category : root) {
+                String id = category.getId();
+
+                category.setId("|" + id + "|");
+                category.setParentId("|" + category.getParentId()+"|");
+                rebuild("|" + id, category.getChildren());
+            }
+
+            statics = all;
+
+        } catch (Exception e) {
+            statics = new ArrayList<>();
+            log.error(e.getMessage(), e);
+        }
+    }
+
+    @GetMapping
+    public Flux<DeviceCategory> getAllCategory() {
+        return Flux.fromIterable(statics);
+    }
+
+    @GetMapping("/_tree")
+    public Flux<DeviceCategory> getAllCategoryTree() {
+        return Flux.fromIterable(TreeSupportEntity.list2tree(statics, DeviceCategory::setChildren));
+    }
+}

Failā izmaiņas netiks attēlotas, jo tās ir par lielu
+ 2282 - 0
jetlinks-manager/device-manager/src/main/resources/device-category.json