Skip to content

Commit

Permalink
fix : #1 issue fix
Browse files Browse the repository at this point in the history
  • Loading branch information
krkarma777 committed Mar 1, 2024
1 parent 8de7455 commit 466b36e
Show file tree
Hide file tree
Showing 4 changed files with 70 additions and 29 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -13,33 +13,20 @@ public class ProductForSalesVolumeSortDTO {
private String productName;
private Double price;
private Integer stock;
private String username; // 판매자 이름
private List<String> imageUrls;
private Long totalQuantity;
private User user;

public ProductForSalesVolumeSortDTO(Long productID, String productName, Double price, Integer stock, String username, List<String> imageUrls, Long totalQuantity, User user) {
public ProductForSalesVolumeSortDTO(Long productID, String productName, Double price, Integer stock, List<String> imageUrls, Long totalQuantity, User user) {
this.productID = productID;
this.productName = productName;
this.price = price;
this.stock = stock;
this.username = username;
this.imageUrls = imageUrls;
this.totalQuantity = totalQuantity;
this.user = user;
}

public ProductForSalesVolumeSortDTO(Long productID, String productName, Double price, Integer stock, String username, Long totalQuantity, User user) {
this.productID = productID;
this.productName = productName;
this.price = price;
this.stock = stock;
this.username = username;
this.totalQuantity = totalQuantity;
this.user = user;
}



public ProductForSalesVolumeSortDTO() {
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -48,10 +48,10 @@ public interface ProductRepository extends JpaRepository<Product, Long> {
List<Product> findAllProducts();

@Query(value = "SELECT new com.bulkpurchase.domain.dto.product.ProductForSalesVolumeSortDTO" +
"(p.productID, p.productName, p.price, p.stock, p.user.username,SUM(od.quantity) , p.user)" +
"(p.productID, p.productName, p.price, p.stock, p.user.username, p.imageUrls,SUM(od.quantity) , p.user)" +
"FROM OrderDetail od JOIN od.product p " +
"WHERE p.productName LIKE %:productName% " +
"GROUP BY p.productID, p.productName, p.price, p.stock, p.user.username,od.order, p.user " +
"GROUP BY p.productID, p.productName, p.price, p.stock, p.user.username, p.imageUrls,od.order, p.user " +
"ORDER BY SUM(od.quantity) DESC",
countQuery = "SELECT COUNT(DISTINCT p.productID) " +
"FROM OrderDetail od JOIN od.product p " +
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,16 +2,20 @@

import com.bulkpurchase.domain.dto.product.ProductForCouponDTO;
import com.bulkpurchase.domain.dto.product.ProductForSalesVolumeSortDTO;
import com.bulkpurchase.domain.entity.product.Product;
import com.bulkpurchase.domain.entity.user.User;
import com.bulkpurchase.domain.enums.ProductStatus;
import com.bulkpurchase.domain.repository.product.ProductRepository;
import jakarta.persistence.EntityManager;
import jakarta.persistence.Query;
import lombok.RequiredArgsConstructor;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageImpl;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Pageable;
import org.springframework.stereotype.Service;
import com.bulkpurchase.domain.entity.product.Product;
import com.bulkpurchase.domain.repository.product.ProductRepository;

import java.util.ArrayList;
import java.util.List;
import java.util.Optional;
import java.util.stream.Collectors;
Expand All @@ -21,6 +25,7 @@
public class ProductService {

private final ProductRepository productRepository;
private final EntityManager entityManager;

public List<Product> findPopularProductsByCategory(Long categoryID) {
return productRepository.findPopularProductsByCategory(categoryID);
Expand Down Expand Up @@ -82,7 +87,56 @@ public Page<Product> findPageByProductNameContaining(Pageable pageable, String p
return productRepository.findByProductNameContaining(pageable, productName);
}

public Page<ProductForSalesVolumeSortDTO> findProductsBySearchTermAndSortBySalesVolume(String productName, Pageable pageable) {
return productRepository.findByProductNameContainingAndOrderBySalesVolume(productName, pageable);
public Page<ProductForSalesVolumeSortDTO> findProductsBySalesVolume(String productName, Pageable pageable) {
String sql = "SELECT p.PRODUCTID as productID, p.PRODUCT_NAME as productName, p.PRICE as price, p.STOCK as stock, u.USERNAME as username, SUM(od.quantity) as totalQuantity " +
"FROM order_details od " +
"JOIN products p ON od.PRODUCTID = p.PRODUCTID " +
"JOIN users u ON p.USERID = u.USERID " +
"WHERE p.product_name LIKE :productName " +
"GROUP BY p.PRODUCTID, u.USERNAME, p.STOCK, p.PRICE, p.PRODUCT_NAME " +
"ORDER BY totalQuantity DESC";

Query query = entityManager.createNativeQuery(sql);
query.setParameter("productName", "%" + productName + "%");

// 페이징 처리
int totalRows = query.getResultList().size(); // 전체 결과 수
query.setFirstResult((int) pageable.getOffset()); // 페이징 시작점
query.setMaxResults(pageable.getPageSize()); // 페이지 크기

List<Object[]> results = query.getResultList();
List<ProductForSalesVolumeSortDTO> products = new ArrayList<>();
for (Object[] result : results) {
User user = new User();
user.setUsername((String) result[4]);

products.add(new ProductForSalesVolumeSortDTO(
((Number) result[0]).longValue(), // productID
(String) result[1], // productName
((Number) result[2]).doubleValue(), // price
((Number) result[3]).intValue(), // stock
null, // imageUrls 추후 추가
((Number) result[5]).longValue(), // totalQuantity
user // User=
));
}

return new PageImpl<>(products, pageable, totalRows);
}

public List<String> findImageUrlsByProductId(Long productID) {
String sql = "SELECT IMAGE_URL FROM KRKARMA777.PRODUCT_IMAGE_URLS WHERE PRODUCTID = :productID";
Query query = entityManager.createNativeQuery(sql);
query.setParameter("productID", productID);
return query.getResultList();
}

public List<ProductForSalesVolumeSortDTO> completeProductDTOs(List<ProductForSalesVolumeSortDTO> products) {
for (ProductForSalesVolumeSortDTO product : products) {
List<String> imageUrls = findImageUrlsByProductId(product.getProductID());
product.setImageUrls(imageUrls);
}
return products;
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,8 @@
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestParam;

import java.util.List;

@Controller
@RequiredArgsConstructor
public class ProductSearchController {
Expand All @@ -36,20 +38,17 @@ public String productSearchView(@RequestParam(value = "q", required = false) Str
if (sortDir == null) {
sortDir = "desc";
}
if (productName == null) {
return "redirect:/";
}


if (sortField == null) {
sortField = "productID";
}

if (productName == null) {
return "redirect:/";
}
if (sortField.equals("salesVolume")) {
Page<ProductForSalesVolumeSortDTO> productPage = productService.findProductsBySearchTermAndSortBySalesVolume(productName, PageRequest.of(page - 1, size));
Page<ProductForSalesVolumeSortDTO> initialProductsPage = productService.findProductsBySalesVolume(productName, PageRequest.of(page - 1, size));
List<ProductForSalesVolumeSortDTO> productPage = productService.completeProductDTOs(initialProductsPage.getContent());
model.addAttribute("productPage", productPage);
model.addAttribute("totalPages", productPage.getTotalPages());
System.out.println("productPage = " + productPage);
model.addAttribute("totalPages", initialProductsPage.getTotalPages());
} else {
Sort sort = Sort.by(Sort.Direction.fromString(sortDir), sortField);
Pageable pageable = PageRequest.of(page-1, size, sort);
Expand All @@ -64,4 +63,5 @@ public String productSearchView(@RequestParam(value = "q", required = false) Str
model.addAttribute("sortDir", sortDir);
return "product/productSearchView";
}

}

0 comments on commit 466b36e

Please sign in to comment.