|
|
@@ -1,21 +1,21 @@
|
|
|
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;
|
|
|
import com.seecoder.BlueWhale.po.Product;
|
|
|
import com.seecoder.BlueWhale.po.Store;
|
|
|
+import com.seecoder.BlueWhale.repository.ProductRepository;
|
|
|
+import com.seecoder.BlueWhale.repository.StoreRepository;
|
|
|
import com.seecoder.BlueWhale.service.ProductService;
|
|
|
import com.seecoder.BlueWhale.vo.ProductVO;
|
|
|
+import org.springframework.beans.factory.annotation.Autowired;
|
|
|
+import org.springframework.stereotype.Service;
|
|
|
|
|
|
-import java.util.ArrayList;
|
|
|
+import javax.persistence.EntityManager;
|
|
|
+import javax.persistence.Query;
|
|
|
import java.util.List;
|
|
|
import java.util.stream.Collectors;
|
|
|
|
|
|
-import org.springframework.beans.factory.annotation.Autowired;
|
|
|
-import org.springframework.stereotype.Service;
|
|
|
-
|
|
|
@Service
|
|
|
public class ProductServiceImpl implements ProductService {
|
|
|
|
|
|
@@ -25,6 +25,9 @@ public class ProductServiceImpl implements ProductService {
|
|
|
@Autowired
|
|
|
StoreRepository storeRepository;
|
|
|
|
|
|
+ @Autowired
|
|
|
+ EntityManager entityManager;
|
|
|
+
|
|
|
@Override
|
|
|
public Boolean create(ProductVO productVO) {
|
|
|
if (productRepository.findByStoreIdAndName(productVO.getStoreId(),productVO.getName())!=null){
|
|
|
@@ -70,18 +73,21 @@ public class ProductServiceImpl implements ProductService {
|
|
|
|
|
|
@Override
|
|
|
public List<ProductVO> getProductsWithCondition(String name, CategoryEnum category, Double price) {
|
|
|
- List<String> conditions=new ArrayList<>();
|
|
|
+ String condition="SELECT * FROM Product WHERE 1=1";
|
|
|
if (name!=null && name.length()>0){
|
|
|
- conditions.add("name = :name");
|
|
|
+ condition=condition.concat("AND name = :name");
|
|
|
}
|
|
|
if (category!=null){
|
|
|
- conditions.add("category = :category");
|
|
|
+ condition=condition.concat("AND category = :category");
|
|
|
}
|
|
|
if (price!=null && price>0){
|
|
|
- conditions.add("price <= :price");
|
|
|
+ condition=condition.concat("AND price <= :price");
|
|
|
}
|
|
|
- String condition=String.join(",",conditions);
|
|
|
- List<Product> products=productRepository.findProductsWithDynamicConditions(condition,name,category,price);
|
|
|
+ Query query=entityManager.createQuery(condition);
|
|
|
+ query.setParameter("name",name);
|
|
|
+ query.setParameter("category",category);
|
|
|
+ query.setParameter("price",price);
|
|
|
+ List<Product> products=query.getResultList();
|
|
|
return products.stream().map(Product::toVO).collect(Collectors.toList());
|
|
|
}
|
|
|
}
|