From 1ebe2726a5f8fe55f01481b37d1ac1f75a088520 Mon Sep 17 00:00:00 2001
From: Crunchy <3114200645@qq.com>
Date: 星期四, 25 四月 2024 16:34:23 +0800
Subject: [PATCH] 2024-4-25 资源要求-设备管理后端调整

---
 cnas-server/src/main/java/com/yuanchu/mom/controller/DeviceController.java |   72 +++++++++++++++++++++--------------
 1 files changed, 43 insertions(+), 29 deletions(-)

diff --git a/cnas-server/src/main/java/com/yuanchu/mom/controller/DeviceController.java b/cnas-server/src/main/java/com/yuanchu/mom/controller/DeviceController.java
index c5f3e08..6715974 100644
--- a/cnas-server/src/main/java/com/yuanchu/mom/controller/DeviceController.java
+++ b/cnas-server/src/main/java/com/yuanchu/mom/controller/DeviceController.java
@@ -4,19 +4,21 @@
 import cn.hutool.core.lang.UUID;
 import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
 import com.yuanchu.mom.annotation.ValueAuth;
+import com.yuanchu.mom.dto.DeviceDto;
 import com.yuanchu.mom.pojo.Device;
 import com.yuanchu.mom.service.DeviceService;
 import com.yuanchu.mom.utils.JackSonUtil;
 import com.yuanchu.mom.vo.Result;
 import io.swagger.annotations.Api;
+import io.swagger.annotations.ApiImplicitParam;
+import io.swagger.annotations.ApiImplicitParams;
 import io.swagger.annotations.ApiOperation;
-import lombok.AllArgsConstructor;
+import org.springframework.beans.factory.annotation.Value;
 import org.springframework.web.bind.annotation.*;
 import org.springframework.web.multipart.MultipartFile;
 
-import javax.servlet.http.HttpServletRequest;
+import javax.annotation.Resource;
 import java.io.File;
-import java.io.IOException;
 import java.util.HashMap;
 import java.util.Map;
 
@@ -24,18 +26,22 @@
  * 璁惧(DeviceController)琛ㄦ帶鍒跺眰
  */
 @Api(tags = "璁惧")
-@AllArgsConstructor
 @RestController
 @RequestMapping("/deviceScope")
 public class DeviceController {
 
+
+    @Resource
     private DeviceService deviceService;
+
+    @Value("${file.path}")
+    private String filePath;
 
     @ApiOperation(value = "鏌ヨ璁惧璇︽儏鍒楄〃")
     @PostMapping("/selectDeviceParameter")
     public Result selectDeviceParameter(@RequestBody Map<String, Object> data) throws Exception {
         Page page = JackSonUtil.unmarshal(JackSonUtil.marshal(data.get("page")), Page.class);
-        Device itemParameter = JackSonUtil.unmarshal(JackSonUtil.marshal(data.get("entity")), Device.class);
+        DeviceDto itemParameter = JackSonUtil.unmarshal(JackSonUtil.marshal(data.get("entity")), DeviceDto.class);
         return Result.success(deviceService.selectDeviceParameter(page, itemParameter));
     }
 
@@ -79,32 +85,30 @@
     }
 
     //鍥剧墖涓婁紶
-    @ApiOperation(value = "鍥剧墖涓婁紶")
-    @RequestMapping(value = "/uploadFile", method = RequestMethod.POST)
-    public Result uploadFile(MultipartFile file, HttpServletRequest request) throws IOException {
-        //鑾峰彇鏂囦欢鍚嶇О
-        HashMap<String, String> list = new HashMap<>();
+    @ApiOperation(value = "璁惧鍥剧墖涓婁紶")
+    @PostMapping("/uploadFile")
+    public Result uploadFile(MultipartFile file) {
+        String urlString;
+        String pathName;
         String filename = file.getOriginalFilename();
-        String s = filename.substring(filename.lastIndexOf("."), filename.length());
-
-        System.out.println(filename);
-        //鑾峰彇鐪熷疄璺緞
-        String realPath = request.getServletContext().getRealPath("/Users/gaoaoy/webapp/images");
-        //鍒涘缓鏂囦欢
-        File file1 = new File(realPath);
-        if (!file1.exists()) {
-            file1.mkdirs();
+        try {
+            String path = filePath;
+            File realpath = new File(path);
+            if (!realpath.exists()) {
+                realpath.mkdirs();
+            }
+            pathName = UUID.randomUUID() + "_" + file.getOriginalFilename();
+            urlString = realpath + "/" + pathName;
+            file.transferTo(new File(urlString));
+            HashMap<String, String> map = new HashMap<>();
+            map.put("name", filename);
+            map.put("url", pathName);
+            return Result.success(map);
+        } catch (Exception e) {
+            e.printStackTrace();
+            System.err.println("鍥剧墖涓婁紶閿欒");
+            return null;
         }
-        file.transferTo(new File(file1 + "/" + filename));
-        String ss= filename;
-        UUID uuid = UUID.randomUUID();
-        ss = uuid.toString() + s;
-        String ful = "/Users/gaoaoy/webapp/images" + ss;
-        System.out.println(file1);
-
-        list.put("url", ful);
-        list.put("name", filename);
-        return Result.success(list);
     }
 
     @ApiOperation(value = "鑾峰彇璁惧璐熻矗浜�")
@@ -113,5 +117,15 @@
     public Result selectDevicePrincipal() {
         return Result.success(deviceService.selectDevicePrincipal());
     }
+
+    @ApiOperation(value = "閫氳繃璁惧鍒嗙被鑾峰彇璁惧鍒楄〃")
+    @PostMapping("/selectDeviceByCategory")
+    @ValueAuth
+    @ApiImplicitParams({
+            @ApiImplicitParam(name = "category", value = "璁惧鍒嗙被", dataTypeClass = String.class),
+    })
+    public Result selectDeviceByCategory(String category) {
+        return Result.success(deviceService.selectDeviceByCategory(category));
+    }
 }
 

--
Gitblit v1.9.3