Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -20,15 +20,16 @@ public interface BomRepository extends JpaRepository<Bom, Long> {
JOIN p.partGroup g
JOIN g.category c
WHERE (
COALESCE(:keyword, '') = ''
COALESCE(:keyword, '') = ''
OR p.name ILIKE CONCAT('%', :keyword, '%')
OR p.code ILIKE CONCAT('%', :keyword, '%')
OR b.bomCode ILIKE CONCAT('%', :keyword, '%')
)
AND (:categoryId IS NULL OR c.id = :categoryId)
AND (:groupId IS NULL OR g.id = :groupId)
AND (:status IS NULL OR b.status = :status)
AND (:complexity IS NULL OR b.complexity = :complexity)
ORDER BY b.createdAt DESC
ORDER BY b.bomCode ASC
""")
Page<Bom> findByFilters(
@Param("keyword") String keyword,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -149,7 +149,8 @@ public BomResponseDTO createBom(BomRequestDTO requestDTO) {
@Transactional(readOnly = true)
public PageResponseDTO<BomResponseDTO> getBoms(int page, int size) {

Pageable pageable = PageRequest.of(page, size, Sort.by(Sort.Direction.DESC, "createdAt"));
// BOM 코드 기준 오름차순 정렬
Pageable pageable = PageRequest.of(page, size, Sort.by("bomCode").ascending());
Page<Bom> bomPage = bomRepository.findAll(pageable);

return PageResponseDTO.<BomResponseDTO>builder()
Expand Down Expand Up @@ -318,7 +319,8 @@ public PageResponseDTO<BomResponseDTO> searchBoms(
int page,
int size
) {
Pageable pageable = PageRequest.of(page, size, Sort.by(Sort.Direction.DESC, "createdAt"));
// BOM 코드 기준 오름차순 정렬
Pageable pageable = PageRequest.of(page, size, Sort.by("bomCode").ascending());
Page<Bom> bomPage = bomRepository.findByFilters(
keyword,
categoryId,
Expand Down
33 changes: 23 additions & 10 deletions src/main/java/com/sampoom/backend/api/item/service/ItemService.java
Original file line number Diff line number Diff line change
Expand Up @@ -76,7 +76,7 @@ private PageResponseDTO<ItemResponseDTO> convertMaterials(PageResponseDTO<Materi
.build();
}

// ALL 타입 검색
// ALL 타입 검색 (효율성 개선)
private PageResponseDTO<ItemResponseDTO> searchAllItems(
String keyword,
Long partCategoryId,
Expand All @@ -85,17 +85,20 @@ private PageResponseDTO<ItemResponseDTO> searchAllItems(
int page,
int size
) {
// 전체 데이터를 한 번에 가져와서 병합 후 페이지네이션
// 각각 적절한 크기로 데이터 가져오기 (페이지 크기의 2배씩)
int fetchSize = size * 2;

// 부품 데이터를 모두 가져오기 (부품 관련 필터만 적용)
PageResponseDTO<PartListResponseDTO> allParts = partService.searchParts(keyword, partCategoryId, partGroupId, 0, Integer.MAX_VALUE);
List<ItemResponseDTO> partItems = allParts.getContent().stream()
// 부품 데이터 가져오기
PageResponseDTO<PartListResponseDTO> partResponse = partService.searchParts(
keyword, partCategoryId, partGroupId, 0, fetchSize);
List<ItemResponseDTO> partItems = partResponse.getContent().stream()
.map(ItemResponseDTO::ofPart)
.toList();

// 자재 데이터를 모두 가져오기 (자재 관련 필터만 적용)
PageResponseDTO<MaterialResponseDTO> allMaterials = materialService.searchMaterials(keyword, materialCategoryId, 0, Integer.MAX_VALUE);
List<ItemResponseDTO> materialItems = allMaterials.getContent().stream()
// 자재 데이터 가져오기
PageResponseDTO<MaterialResponseDTO> materialResponse = materialService.searchMaterials(
keyword, materialCategoryId, 0, fetchSize);
List<ItemResponseDTO> materialItems = materialResponse.getContent().stream()
.map(ItemResponseDTO::ofMaterial)
.toList();

Expand All @@ -104,8 +107,18 @@ private PageResponseDTO<ItemResponseDTO> searchAllItems(
allItems.addAll(partItems);
allItems.addAll(materialItems);

// 전체 개수
long totalElements = allItems.size();
// 코드순으로 정렬 (이미 각각은 정렬되어 있지만 병합 후 다시 정렬 필요)
allItems.sort((item1, item2) -> {
String code1 = item1.getCode();
String code2 = item2.getCode();
if (code1 == null && code2 == null) return 0;
if (code1 == null) return 1;
if (code2 == null) return -1;
return code1.compareTo(code2);
});

// 전체 개수 계산 (실제 DB에서 가져온 총 개수)
long totalElements = partResponse.getTotalElements() + materialResponse.getTotalElements();
int totalPages = (int) Math.ceil((double) totalElements / size);

// 현재 페이지에 해당하는 데이터 추출
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
import lombok.RequiredArgsConstructor;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Sort;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

Expand Down Expand Up @@ -250,15 +251,17 @@ public PageResponseDTO<MaterialResponseDTO> searchMaterials(
int page,
int size
) {
PageRequest pageable = PageRequest.of(page, size);
// 자재 코드순 정렬 추가
PageRequest pageable = PageRequest.of(page, size, Sort.by("materialCode").ascending());

Page<Material> materials = materialRepository.findAll((root, query, cb) -> {
List<Predicate> predicates = new ArrayList<>();

// keyword
// keyword (대소문자 구분 없음)
if (keyword != null && !keyword.isBlank()) {
Predicate nameLike = cb.like(root.get("name"), "%" + keyword + "%");
Predicate codeLike = cb.like(root.get("materialCode"), "%" + keyword + "%");
String lowerKeyword = "%" + keyword.toLowerCase() + "%";
Predicate nameLike = cb.like(cb.lower(root.get("name")), lowerKeyword);
Predicate codeLike = cb.like(cb.lower(root.get("materialCode")), lowerKeyword);
predicates.add(cb.or(nameLike, codeLike));
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@
import org.springframework.dao.DataIntegrityViolationException;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Sort;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

Expand Down Expand Up @@ -308,18 +309,20 @@ public PageResponseDTO<PartListResponseDTO> searchParts(
int page,
int size
) {
PageRequest pageable = PageRequest.of(page, size);
// 코드순 정렬 추가
PageRequest pageable = PageRequest.of(page, size, Sort.by("code").ascending());

Page<Part> parts = partRepository.findAll((root, query, cb) -> {
List<Predicate> predicates = new ArrayList<>();

// 상태 필터
predicates.add(cb.equal(root.get("status"), PartStatus.ACTIVE));

// keyword 검색
// keyword 검색 (대소문자 구분 없음)
if (keyword != null && !keyword.isBlank()) {
Predicate nameLike = cb.like(root.get("name"), "%" + keyword + "%");
Predicate codeLike = cb.like(root.get("code"), "%" + keyword + "%");
String lowerKeyword = "%" + keyword.toLowerCase() + "%";
Predicate nameLike = cb.like(cb.lower(root.get("name")), lowerKeyword);
Predicate codeLike = cb.like(cb.lower(root.get("code")), lowerKeyword);
predicates.add(cb.or(nameLike, codeLike));
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -102,7 +102,8 @@ public PageResponseDto<ProcessResponseDTO> search(String q, ProcessStatus status
int safePage = Math.max(page, 0);
int safeSize = Math.min(Math.max(size, 1), 100);

Pageable pageable = PageRequest.of(safePage, safeSize, Sort.by(Sort.Direction.DESC, "id"));
// 공정 코드 기준 오름차순 정렬
Pageable pageable = PageRequest.of(safePage, safeSize, Sort.by("code").ascending());

String keyword = (q == null || q.isBlank()) ? null : q.trim();
Page<Process> result = processRepository.search(keyword, status, categoryId, groupId, pageable);
Expand Down