Parcourir la source

20230719(3)-完成资源controller(未测试)

iaun il y a 3 ans
Parent
commit
76840c7a8c

+ 114 - 8
src/main/java/cn/iaun/it/controller/ItResourceController.java

@@ -1,9 +1,8 @@
 package cn.iaun.it.controller;
 package cn.iaun.it.controller;
 
 
-import cn.iaun.it.model.ItResourceAttribute;
-import cn.iaun.it.model.ItResourceGroup;
-import cn.iaun.it.model.ItResourceType;
+import cn.iaun.it.model.*;
 import cn.iaun.it.service.ItResourceService;
 import cn.iaun.it.service.ItResourceService;
+import cn.iaun.it.util.MyPage;
 import cn.iaun.it.util.Result;
 import cn.iaun.it.util.Result;
 import cn.iaun.it.util.ResultCode;
 import cn.iaun.it.util.ResultCode;
 import com.baomidou.mybatisplus.core.metadata.IPage;
 import com.baomidou.mybatisplus.core.metadata.IPage;
@@ -22,7 +21,7 @@ public class ItResourceController {
         this.itResourceService = itResourceService;
         this.itResourceService = itResourceService;
     }
     }
 
 
-    @GetMapping(value = "/type/get")
+    @PostMapping(value = "/type/get")
     public Result<List<ItResourceType>> getItResourceTypes() {
     public Result<List<ItResourceType>> getItResourceTypes() {
         return new Result<>(ResultCode.SUCCESS, itResourceService.getResourceType());
         return new Result<>(ResultCode.SUCCESS, itResourceService.getResourceType());
     }
     }
@@ -73,7 +72,7 @@ public class ItResourceController {
         return new Result<>(result == 1 ? ResultCode.SUCCESS : ResultCode.FAILED, null);
         return new Result<>(result == 1 ? ResultCode.SUCCESS : ResultCode.FAILED, null);
     }
     }
 
 
-    @GetMapping(value = "/group/get")
+    @PostMapping(value = "/group/get")
     public Result<List<ItResourceGroup>> getItResourceGroups() {
     public Result<List<ItResourceGroup>> getItResourceGroups() {
         return new Result<>(ResultCode.SUCCESS, itResourceService.getResourceGroup());
         return new Result<>(ResultCode.SUCCESS, itResourceService.getResourceGroup());
     }
     }
@@ -99,20 +98,96 @@ public class ItResourceController {
     }
     }
 
 
     @PostMapping(value = "/attribute/get")
     @PostMapping(value = "/attribute/get")
-    public Result<IPage<ItResourceAttribute>> getItResourceAttribute(@Validated @RequestBody ItResourceAttributePageData page) {
+    public Result<MyPage<ItResourceAttribute>> getItResourceAttribute(@Validated @RequestBody ItResourceAttributeReqData page) {
         if (page.pageNo == null) {
         if (page.pageNo == null) {
             page.pageNo = 1;
             page.pageNo = 1;
         }
         }
         if (page.pageSize == null) {
         if (page.pageSize == null) {
             page.pageSize = 20;
             page.pageSize = 20;
         }
         }
+        IPage<ItResourceAttribute> pageData = itResourceService.getResourceAttribute(page.pageNo, page.pageSize, page.groupId);
+        MyPage<ItResourceAttribute> myPage = new MyPage<>(pageData);
+        return new Result<>(ResultCode.SUCCESS, myPage);
+    }
+
+    @PostMapping("/attribute/add")
+    public Result<?> addItResourceAttribute(@Validated @RequestBody ItResourceAttribute itResourceAttribute) {
+        int result = itResourceService.addResourceAttribute(itResourceAttribute);
+        return new Result<>(result == 1 ? ResultCode.SUCCESS : ResultCode.FAILED, null);
+    }
+
+    @PostMapping(value = "/attribute/del/{i}")
+    public Result<?> delItResourceAttribute(@PathVariable String i) {
+        long id = Long.parseLong(i);
+        int result = itResourceService.delResourceAttribute(id);
+        return new Result<>(result == 1 ? ResultCode.SUCCESS : ResultCode.FAILED, null);
+    }
+
+    @PostMapping("/attribute/set")
+    public Result<?> setItResourceAttribute(@Validated @RequestBody ItResourceAttribute itResourceAttribute) {
+        int result = itResourceService.setResourceAttribute(itResourceAttribute);
+        return new Result<>(result == 1 ? ResultCode.SUCCESS : ResultCode.FAILED, null);
+    }
+
+    @PostMapping("/get")
+    public Result<MyPage<ItResource>> getItResource(@Validated @RequestBody ItResourceReqData page) {
+        if (page.pageNo == null) {
+            page.pageNo = 1;
+        }
+        if (page.pageSize == null) {
+            page.pageSize = 20;
+        }
+        IPage<ItResource> iPage = itResourceService.getResource(page.getPageNo(), page.getPageSize(), page.getResourceTypeId());
+        MyPage<ItResource> myPage = new MyPage<>(iPage);
+        return new Result<>(ResultCode.SUCCESS, myPage);
+    }
+
+    @PostMapping("/add")
+    public Result<?> addItResource(@Validated @RequestBody ItResource itResource) {
+        int result = itResourceService.addResource(itResource);
+        return new Result<>(result == 1 ? ResultCode.SUCCESS : ResultCode.FAILED, null);
+    }
+
+    @PostMapping("/del/{i}")
+    public Result<?> delItResource(@PathVariable String i) {
+        long id = Long.parseLong(i);
+        int result = itResourceService.delResource(id);
+        return new Result<>(result == 1 ? ResultCode.SUCCESS : ResultCode.FAILED, null);
+    }
+
+    @PostMapping("/set")
+    public Result<?> setItResource(@Validated @RequestBody ItResource itResource) {
+        int result = itResourceService.setResource(itResource);
+        return new Result<>(result == 1 ? ResultCode.SUCCESS : ResultCode.FAILED, null);
+    }
+
+    @PostMapping("/value/get/{i}")
+    public Result<List<ItResourceValue>> getItResourceValue(@PathVariable String i) {
+        long id = Long.parseLong(i);
+        return new Result<>(ResultCode.SUCCESS, itResourceService.getResourceValues(id));
+    }
 
 
-        return new Result<>(ResultCode.SUCCESS,  itResourceService.getResourceAttribute(page.pageNo, page.pageSize, page.groupId));
+    @PostMapping("/value/add")
+    public Result<?> addItResourceValue(@RequestBody List<ItResourceValue> itResourceValues) {
+        return new Result<>(ResultCode.SUCCESS, itResourceService.addResourceValues(itResourceValues));
+    }
+
+    @PostMapping("/value/del/{i}")
+    public Result<?> delItResourceValue(@PathVariable String i) {
+        long id = Long.parseLong(i);
+        int result = itResourceService.delResourceValueByResourceId(id);
+        return new Result<>(result == 1 ? ResultCode.SUCCESS : ResultCode.FAILED, null);
+    }
+
+    @PostMapping("/value/set")
+    public Result<?> setItResourceValues(@RequestBody List<ItResourceValue> itResourceValues) {
+        int result = itResourceService.setResourceValueById(itResourceValues);
+        return new Result<>(result == 1 ? ResultCode.SUCCESS : ResultCode.FAILED, null);
     }
     }
 
 
 }
 }
 
 
-class ItResourceAttributePageData {
+class ItResourceAttributeReqData {
     Integer pageNo;
     Integer pageNo;
     Integer pageSize;
     Integer pageSize;
     @NotNull
     @NotNull
@@ -141,4 +216,35 @@ class ItResourceAttributePageData {
     public void setGroupId(Long groupId) {
     public void setGroupId(Long groupId) {
         this.groupId = groupId;
         this.groupId = groupId;
     }
     }
+}
+
+class ItResourceReqData {
+    Integer pageNo;
+    Integer pageSize;
+    @NotNull
+    Long resourceTypeId;
+
+    public Integer getPageNo() {
+        return pageNo;
+    }
+
+    public void setPageNo(Integer pageNo) {
+        this.pageNo = pageNo;
+    }
+
+    public Integer getPageSize() {
+        return pageSize;
+    }
+
+    public void setPageSize(Integer pageSize) {
+        this.pageSize = pageSize;
+    }
+
+    public Long getResourceTypeId() {
+        return resourceTypeId;
+    }
+
+    public void setResourceTypeId(Long resourceTypeId) {
+        this.resourceTypeId = resourceTypeId;
+    }
 }
 }

+ 4 - 3
src/main/java/cn/iaun/it/interceptor/PermissionInterceptor.java

@@ -1,15 +1,16 @@
 package cn.iaun.it.interceptor;
 package cn.iaun.it.interceptor;
 
 
-import cn.iaun.it.util.RedisUtil;
 import cn.iaun.it.util.ResultCode;
 import cn.iaun.it.util.ResultCode;
 import cn.iaun.it.util.WebException;
 import cn.iaun.it.util.WebException;
-import org.springframework.beans.factory.annotation.Autowired;
 import org.springframework.web.method.HandlerMethod;
 import org.springframework.web.method.HandlerMethod;
 import org.springframework.web.servlet.HandlerInterceptor;
 import org.springframework.web.servlet.HandlerInterceptor;
 
 
 import javax.servlet.http.HttpServletRequest;
 import javax.servlet.http.HttpServletRequest;
 import javax.servlet.http.HttpServletResponse;
 import javax.servlet.http.HttpServletResponse;
-import java.util.*;
+import java.util.ArrayList;
+import java.util.HashMap;
+import java.util.List;
+import java.util.Map;
 
 
 public class PermissionInterceptor implements HandlerInterceptor {
 public class PermissionInterceptor implements HandlerInterceptor {
     private Map<String, Long> getSys() {
     private Map<String, Long> getSys() {

+ 3 - 0
src/main/java/cn/iaun/it/model/ItResource.java

@@ -3,9 +3,12 @@ package cn.iaun.it.model;
 import com.baomidou.mybatisplus.annotation.IdType;
 import com.baomidou.mybatisplus.annotation.IdType;
 import com.baomidou.mybatisplus.annotation.TableId;
 import com.baomidou.mybatisplus.annotation.TableId;
 
 
+import javax.validation.constraints.NotBlank;
+
 public class ItResource {
 public class ItResource {
     @TableId(type = IdType.AUTO)
     @TableId(type = IdType.AUTO)
     private Long resourceId;
     private Long resourceId;
+    @NotBlank
     private String resourceName;
     private String resourceName;
     private Long typeId;
     private Long typeId;
     private Long groupId;
     private Long groupId;

+ 1 - 1
src/main/java/cn/iaun/it/service/ItResourceService.java

@@ -49,5 +49,5 @@ public interface ItResourceService {
 
 
     int delResourceValueByResourceId(Long resourceId);
     int delResourceValueByResourceId(Long resourceId);
 
 
-    int setResourceValueById(ItResourceValue itResourceValue);
+    int setResourceValueById(List<ItResourceValue> itResourceValue);
 }
 }

+ 5 - 2
src/main/java/cn/iaun/it/service/impl/ItResourceServiceImpl.java

@@ -133,7 +133,10 @@ public class ItResourceServiceImpl implements ItResourceService {
     }
     }
 
 
     @Override
     @Override
-    public int setResourceValueById(ItResourceValue itResourceValue) {
-        return itResourceValueMapper.updateResourceValueById(itResourceValue);
+    public int setResourceValueById(List<ItResourceValue> itResourceValues) {
+        for (ItResourceValue itResourceValue : itResourceValues) {
+            itResourceValueMapper.updateResourceValueById(itResourceValue);
+        }
+        return 1;
     }
     }
 }
 }

+ 45 - 11
src/main/java/cn/iaun/it/util/MyPage.java

@@ -1,32 +1,66 @@
 package cn.iaun.it.util;
 package cn.iaun.it.util;
 
 
 
 
+import com.baomidou.mybatisplus.core.metadata.IPage;
+
+import java.util.List;
+
 public class MyPage<T> {
 public class MyPage<T> {
-    Integer pageNo;
-    Integer pageSize;
-    T data;
+    long pageNo;
+    long pageSize;
+    long totalSize;
+    long totalPage;
+    List<T> rows;
+
+    public MyPage() {
+
+    }
+
+    public MyPage(IPage<T> iPage) {
+        this.pageNo = iPage.getCurrent();
+        this.pageSize = iPage.getSize();
+        this.totalPage = iPage.getPages();
+        this.totalSize = iPage.getTotal();
+        this.rows = iPage.getRecords();
+    }
 
 
-    public Integer getPageNo() {
+    public long getPageNo() {
         return pageNo;
         return pageNo;
     }
     }
 
 
-    public void setPageNo(Integer pageNo) {
+    public void setPageNo(long pageNo) {
         this.pageNo = pageNo;
         this.pageNo = pageNo;
     }
     }
 
 
-    public Integer getPageSize() {
+    public long getPageSize() {
         return pageSize;
         return pageSize;
     }
     }
 
 
-    public void setPageSize(Integer pageSize) {
+    public void setPageSize(long pageSize) {
         this.pageSize = pageSize;
         this.pageSize = pageSize;
     }
     }
 
 
-    public T getData() {
-        return data;
+    public long getTotalSize() {
+        return totalSize;
+    }
+
+    public void setTotalSize(long totalSize) {
+        this.totalSize = totalSize;
+    }
+
+    public long getTotalPage() {
+        return totalPage;
+    }
+
+    public void setTotalPage(long totalPage) {
+        this.totalPage = totalPage;
+    }
+
+    public List<T> getRows() {
+        return rows;
     }
     }
 
 
-    public void setData(T data) {
-        this.data = data;
+    public void setRows(List<T> rows) {
+        this.rows = rows;
     }
     }
 }
 }

+ 4 - 4
src/main/java/cn/iaun/it/util/ResultCode.java

@@ -1,10 +1,10 @@
 package cn.iaun.it.util;
 package cn.iaun.it.util;
 
 
 public enum ResultCode {
 public enum ResultCode {
-    SUCCESS(1, "成功"),
-    FAILED(-1, "失败"),
-    FAILED_PARAM_ERROR(-1001, "参数错误!"),
-    FAILED_PARAM_INVALID(-1002, "参数无效!"), // 如ID不存在等
+    SUCCESS(1, "success"),
+    FAILED(0, "failed"),
+    FAILED_PARAM_ERROR(1001, "参数错误!"),
+    FAILED_PARAM_INVALID(1002, "参数无效!"), // 如ID不存在等
 
 
     /* 用户模块 1001XXX */
     /* 用户模块 1001XXX */
     USER_WRONG_USER_OR_PASSWORD(1001001, "用户名或密码错误"),
     USER_WRONG_USER_OR_PASSWORD(1001001, "用户名或密码错误"),

+ 0 - 9
src/test/java/cn/iaun/it/ResourceServiceTest.java

@@ -14,15 +14,6 @@ public class ResourceServiceTest {
     @Autowired
     @Autowired
     private ItResourceService itResourceService;
     private ItResourceService itResourceService;
 
 
-    @Test
-    public void setResourceValueByIdTest() {
-        ItResourceValue itResourceValue = new ItResourceValue();
-        itResourceValue.setResourceId(4L);
-        itResourceValue.setAttributeId(13L);
-        itResourceValue.setAttributeValue("HUAWEI");
-        itResourceService.setResourceValueById(itResourceValue);
-    }
-
     @Test
     @Test
     public void addResourceValuesTest() {
     public void addResourceValuesTest() {
         ItResourceValue itResourceValue = new ItResourceValue();
         ItResourceValue itResourceValue = new ItResourceValue();