Преглед изворни кода

设备分类目录接口更新

lihao пре 4 година
родитељ
комит
cbc81904d9

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

@@ -1,26 +1,36 @@
 package org.jetlinks.community.device.entity;
 
 import io.swagger.v3.oas.annotations.media.Schema;
+import lombok.Data;
 import lombok.Getter;
 import lombok.Setter;
+import org.hswebframework.web.api.crud.entity.GenericEntity;
 import org.hswebframework.web.api.crud.entity.GenericTreeSortSupportEntity;
 
+import javax.persistence.Column;
+import javax.persistence.Table;
 import java.util.List;
 
 @Getter
 @Setter
+@Table(name = "dev_product_cate")
+@Data
 public class DeviceCategory extends GenericTreeSortSupportEntity<String> {
 
     @Schema(description = "ID")
+    @Column(name = "id")
     private String id;
 
     @Schema(description = "标识")
+    @Column(name = "key")
     private String key;
 
     @Schema(description = "名称")
+    @Column(name = "name")
     private String name;
 
     @Schema(description = "父节点标识")
+    @Column(name = "parent_id")
     private String parentId;
 
     @Schema(description = "子节点")

+ 15 - 0
jetlinks-manager/device-manager/src/main/java/org/jetlinks/community/device/service/DeviceCategoryService.java

@@ -0,0 +1,15 @@
+package org.jetlinks.community.device.service;
+
+import lombok.extern.slf4j.Slf4j;
+import org.hswebframework.web.crud.service.GenericReactiveCrudService;
+import org.jetlinks.community.device.entity.DeviceCategory;
+import org.jetlinks.community.device.entity.DeviceInstanceEntity;
+import org.springframework.stereotype.Service;
+import reactor.core.publisher.Mono;
+
+@Service
+@Slf4j
+public class DeviceCategoryService extends GenericReactiveCrudService<DeviceCategory, String> {
+
+
+}

+ 78 - 17
jetlinks-manager/device-manager/src/main/java/org/jetlinks/community/device/web/DeviceCategoryController.java

@@ -2,30 +2,102 @@ package org.jetlinks.community.device.web;
 
 import com.alibaba.fastjson.JSON;
 import io.swagger.v3.oas.annotations.Operation;
+import io.swagger.v3.oas.annotations.Parameter;
 import io.swagger.v3.oas.annotations.tags.Tag;
+import jdk.nashorn.internal.ir.Block;
+import lombok.AllArgsConstructor;
 import lombok.extern.slf4j.Slf4j;
+import org.hswebframework.ezorm.rdb.mapping.ReactiveRepository;
 import org.hswebframework.web.api.crud.entity.TreeSupportEntity;
+import org.hswebframework.web.authorization.Authentication;
+import org.hswebframework.web.crud.web.reactive.ReactiveServiceCrudController;
+import org.hswebframework.web.exception.BusinessException;
 import org.jetlinks.community.device.entity.DeviceCategory;
+import org.jetlinks.community.device.entity.DeviceInstanceEntity;
+import org.jetlinks.community.device.service.DeviceCategoryService;
+import org.springframework.beans.factory.annotation.Autowired;
 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 org.springframework.web.bind.annotation.*;
+import reactor.core.Disposable;
 import reactor.core.publisher.Flux;
+import reactor.core.publisher.Mono;
 
 import java.nio.charset.StandardCharsets;
 import java.util.ArrayList;
 import java.util.List;
+import java.util.function.Function;
+import java.util.stream.Collectors;
 
 @RestController
 @RequestMapping("/device/category")
 @Slf4j
+@AllArgsConstructor
 @Tag(name = "设备分类目录")
-public class DeviceCategoryController {
+public class DeviceCategoryController{
 
 
-    static List<DeviceCategory> statics;
+    private final DeviceCategoryService service;
+
+    @PostMapping("/add")
+    @Operation(summary = "添加目录")
+    public Mono<?>  add(@RequestBody Mono<DeviceCategory> payload){
+        return this.service.insert(payload)
+            .thenReturn(payload)
+            .onErrorMap(e->new BusinessException("服务器繁忙,请稍后重试",e));
+    }
+
+    @DeleteMapping("/{id}")
+    @Operation(summary = "删除目录")
+    public Mono<?> remove(@PathVariable @Parameter(description = "设备ID") String id){
+        return this.service.createDelete()
+            .where(DeviceCategory::getId,id)
+            .execute()
+            .then();
+    }
+
+    @PutMapping
+    @Operation(summary = "更新目录")
+    public Mono<?> updata(@RequestBody Mono<DeviceCategory> payload){
+        return payload.flatMap(data->
+            this.service.createUpdate()
+                .set(data)
+                .where(DeviceCategory::getId,data.getId())
+                .execute()
+                .then()
+        );
+    }
+
+    @GetMapping("/{id}/detail")
+    @Operation(summary = "目录详情")
+    public Mono<?> detail(@PathVariable @Parameter(description = "设备ID") String id){
+        return this.service.createQuery()
+            .where(DeviceCategory::getId,id)
+            .fetchOne();
+    }
+
+    @GetMapping
+    @Operation(summary = "获取全部分类目录")
+    public Flux<DeviceCategory> getAllCategory() {
+        return this.service.createQuery()
+            .fetch();
+    //        return Flux.fromIterable(statics);
+    }
+
+    @GetMapping("/_tree")
+    @Operation(summary = "获取全部分类目录(树结构)")
+    public Flux<DeviceCategory> getAllCategoryTree() {
+        return this.service
+            .createQuery()
+            .fetch()
+            .collectList()
+            .flatMapMany(data->{
+                return Flux.fromIterable(TreeSupportEntity.list2tree(data, DeviceCategory::setChildren));
+            });
+    }
 
+    /*
+    static List<DeviceCategory> statics;
 
     static void rebuild(String parentId, List<DeviceCategory> children) {
         if (children == null) {
@@ -63,16 +135,5 @@ public class DeviceCategoryController {
             log.error(e.getMessage(), e);
         }
     }
-
-    @GetMapping
-    @Operation(summary = "获取全部分类目录")
-    public Flux<DeviceCategory> getAllCategory() {
-        return Flux.fromIterable(statics);
-    }
-
-    @GetMapping("/_tree")
-    @Operation(summary = "获取全部分类目录(树结构)")
-    public Flux<DeviceCategory> getAllCategoryTree() {
-        return Flux.fromIterable(TreeSupportEntity.list2tree(statics, DeviceCategory::setChildren));
-    }
+     */
 }