增加分类目录

This commit is contained in:
zhou-hao 2020-06-28 15:58:28 +08:00
parent 656ff23231
commit 4b7f37e3ad
3 changed files with 2379 additions and 0 deletions

View File

@ -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;
}

View File

@ -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));
}
}

File diff suppressed because it is too large Load Diff