Selaa lähdekoodia

feat: 完成了动态sql

DingXiaoYu 2 vuotta sitten
vanhempi
commit
40a4d1789e

+ 7 - 0
src/main/java/com/seecoder/BlueWhale/controller/ProductController.java

@@ -1,5 +1,6 @@
 package com.seecoder.BlueWhale.controller;
 
+import com.seecoder.BlueWhale.enums.CategoryEnum;
 import com.seecoder.BlueWhale.service.ProductService;
 import com.seecoder.BlueWhale.vo.ProductVO;
 import com.seecoder.BlueWhale.vo.ResultVO;
@@ -34,4 +35,10 @@ public class ProductController {
     public ResultVO<ProductVO> getProduct(@PathVariable(value = "id") Integer id){
         return ResultVO.buildSuccess(productService.getProduct(id));
     }
+
+    @GetMapping("/condition")
+    public ResultVO<List<ProductVO>> getProductsWithCondition(@RequestParam("name")String name, @RequestParam("category")CategoryEnum category,
+                                                              @RequestParam("price")Double price){
+        return ResultVO.buildSuccess(productService.getProductsWithCondition(name,category,price));
+    }
 }

+ 6 - 0
src/main/java/com/seecoder/BlueWhale/repository/ProductRepository.java

@@ -1,12 +1,18 @@
 package com.seecoder.BlueWhale.repository;
 
+import com.seecoder.BlueWhale.enums.CategoryEnum;
 import com.seecoder.BlueWhale.po.Product;
 
 import java.util.List;
 import org.springframework.data.jpa.repository.JpaRepository;
+import org.springframework.data.jpa.repository.Query;
 
 public interface ProductRepository extends JpaRepository<Product, Integer> {
     List<Product> findAllByStoreId(Integer storeId);
 
     Product findByStoreIdAndName(Integer storeId,String name);
+
+    @Query("SELECT * FROM Product WHERE 1=1")
+    List<Product> findProductsWithDynamicConditions(String condition, String name,
+                                                    CategoryEnum category,Double price);
 }

+ 3 - 0
src/main/java/com/seecoder/BlueWhale/service/ProductService.java

@@ -2,6 +2,7 @@ package com.seecoder.BlueWhale.service;
 
 import java.util.List;
 
+import com.seecoder.BlueWhale.enums.CategoryEnum;
 import com.seecoder.BlueWhale.vo.ProductVO;
 
 public interface ProductService {
@@ -9,4 +10,6 @@ public interface ProductService {
     Boolean addStock(Integer id, Integer number);
     List<ProductVO> getAllProducts(Integer storeId);
     ProductVO getProduct(Integer id);
+
+    List<ProductVO> getProductsWithCondition(String name, CategoryEnum category, Double price);
 }

+ 19 - 0
src/main/java/com/seecoder/BlueWhale/serviceImpl/ProductServiceImpl.java

@@ -1,5 +1,6 @@
 package com.seecoder.BlueWhale.serviceImpl;
 
+import com.seecoder.BlueWhale.enums.CategoryEnum;
 import com.seecoder.BlueWhale.repository.ProductRepository;
 import com.seecoder.BlueWhale.repository.StoreRepository;
 import com.seecoder.BlueWhale.exception.BlueWhaleException;
@@ -8,6 +9,7 @@ import com.seecoder.BlueWhale.po.Store;
 import com.seecoder.BlueWhale.service.ProductService;
 import com.seecoder.BlueWhale.vo.ProductVO;
 
+import java.util.ArrayList;
 import java.util.List;
 import java.util.stream.Collectors;
 
@@ -65,4 +67,21 @@ public class ProductServiceImpl implements ProductService {
         }
         return product.toVO();
     }
+
+    @Override
+    public List<ProductVO> getProductsWithCondition(String name, CategoryEnum category, Double price) {
+        List<String> conditions=new ArrayList<>();
+        if (name!=null && name.length()>0){
+            conditions.add("name = :name");
+        }
+        if (category!=null){
+            conditions.add("category = :category");
+        }
+        if (price!=null && price>0){
+            conditions.add("price <= :price");
+        }
+        String condition=String.join(",",conditions);
+        List<Product> products=productRepository.findProductsWithDynamicConditions(condition,name,category,price);
+        return products.stream().map(Product::toVO).collect(Collectors.toList());
+    }
 }