|
12 | 12 | import com.querydsl.core.types.dsl.DateTimePath; |
13 | 13 | import com.querydsl.jpa.impl.JPAQueryFactory; |
14 | 14 | import jakarta.persistence.EntityManager; |
15 | | -import jakarta.persistence.TypedQuery; |
| 15 | +import com.querydsl.core.types.dsl.CaseBuilder; |
16 | 16 | import lombok.RequiredArgsConstructor; |
17 | 17 | import org.springframework.data.domain.Page; |
18 | 18 | import org.springframework.data.domain.PageImpl; |
@@ -58,27 +58,45 @@ public Page<TaskEntity> findTasksAssignedByManager(Long processorId, Pageable pa |
58 | 58 |
|
59 | 59 | @Override |
60 | 60 | public List<TeamMemberTaskResponse> findTeamStatus(Long memberId, FilterTeamStatusRequest filter) { |
61 | | - // 1. 담당자 목록을 가져옴 (페이징 제거) |
62 | | - List<Long> processorIds = queryFactory |
63 | | - .select(taskEntity.processor.memberId) |
64 | | - .from(taskEntity) |
65 | | - .groupBy(taskEntity.processor.memberId) |
66 | | - .orderBy("기여도순".equals(filter.sortBy()) ? |
67 | | - taskEntity.taskId.count().desc() : |
68 | | - taskEntity.processor.nickname.asc()) |
69 | | - .fetch(); |
| 61 | + BooleanBuilder builder = new BooleanBuilder(); |
| 62 | + |
| 63 | + // 담당자 ID 필터링 |
| 64 | + if (memberId != null) { |
| 65 | + builder.and(taskEntity.processor.memberId.eq(memberId)); |
| 66 | + } |
| 67 | + |
| 68 | + // 작업 타이틀 필터링 |
| 69 | + if (filter.taskTitle() != null && !filter.taskTitle().isEmpty()) { |
| 70 | + builder.and(taskEntity.title.containsIgnoreCase(filter.taskTitle())); |
| 71 | + } |
| 72 | + |
| 73 | + // 1차 카테고리 필터링 |
| 74 | + if (!filter.mainCategoryIds().isEmpty()) { |
| 75 | + builder.and(taskEntity.category.mainCategory.categoryId.in(filter.mainCategoryIds())); |
| 76 | + } |
70 | 77 |
|
71 | | - if (processorIds.isEmpty()) { |
72 | | - return List.of(); // 결과가 없으면 빈 리스트 반환 |
| 78 | + // 2차 카테고리 필터링 |
| 79 | + if (!filter.categoryIds().isEmpty()) { |
| 80 | + builder.and(taskEntity.category.categoryId.in(filter.categoryIds())); |
73 | 81 | } |
74 | 82 |
|
75 | | - // 2. 담당자별 작업 조회 (페이징 제거) |
| 83 | + // 정렬 조건 적용 |
| 84 | + OrderSpecifier<?> orderBy = "기여도순".equals(filter.sortBy()) |
| 85 | + ? new CaseBuilder() |
| 86 | + .when(taskEntity.taskStatus.eq(TaskStatus.IN_PROGRESS) |
| 87 | + .or(taskEntity.taskStatus.eq(TaskStatus.PENDING_COMPLETED))) |
| 88 | + .then(1) |
| 89 | + .otherwise(0) |
| 90 | + .desc() |
| 91 | + : taskEntity.processor.nickname.asc(); |
| 92 | + |
| 93 | + // 쿼리 실행 |
76 | 94 | List<TaskEntity> taskEntities = queryFactory |
77 | 95 | .selectFrom(taskEntity) |
78 | | - .where(taskEntity.processor.memberId.in(processorIds)) |
| 96 | + .where(builder) |
| 97 | + .orderBy(orderBy) |
79 | 98 | .fetch(); |
80 | 99 |
|
81 | | - // 3. 담당자별 그룹핑 |
82 | 100 | return taskEntities.stream() |
83 | 101 | .collect(Collectors.groupingBy(t -> t.getProcessor().getMemberId())) |
84 | 102 | .entrySet().stream() |
@@ -111,48 +129,10 @@ public List<TeamMemberTaskResponse> findTeamStatus(Long memberId, FilterTeamStat |
111 | 129 | }).collect(Collectors.toList()); |
112 | 130 | } |
113 | 131 |
|
114 | | - |
115 | | - |
116 | | - private String buildQueryString(FilterTeamStatusRequest filter) { |
117 | | - StringBuilder queryStr = new StringBuilder("SELECT t FROM TaskEntity t " + |
118 | | - "JOIN FETCH t.processor p " + |
119 | | - "WHERE (:memberId IS NULL OR p.memberId = :memberId) "); |
120 | | - |
121 | | - if (!filter.taskTitle().isEmpty()) { |
122 | | - queryStr.append("AND t.title LIKE :title "); |
123 | | - } |
124 | | - if (!filter.mainCategoryIds().isEmpty()) { |
125 | | - queryStr.append("AND t.category.mainCategory.id IN :mainCategories "); |
126 | | - } |
127 | | - if (!filter.categoryIds().isEmpty()) { |
128 | | - queryStr.append("AND t.category.id IN :categories "); |
129 | | - } |
130 | | - |
131 | | - if ("기여도순".equals(filter.sortBy())) { |
132 | | - queryStr.append("ORDER BY (SELECT COUNT(te) FROM TaskEntity te WHERE te.processor = p AND te.taskStatus IN ('IN_PROGRESS', 'PENDING_COMPLETED')) DESC"); |
133 | | - } else { |
134 | | - queryStr.append("ORDER BY p.nickname ASC"); |
135 | | - } |
136 | | - |
137 | | - return queryStr.toString(); |
138 | | - } |
139 | | - |
140 | 132 | private boolean isValidTitle(FilterTeamStatusRequest filter) { |
141 | 133 | return filter.taskTitle() != null && !filter.taskTitle().isEmpty(); |
142 | 134 | } |
143 | 135 |
|
144 | | - private void setQueryParameters(TypedQuery<TaskEntity> query, FilterTeamStatusRequest filter) { |
145 | | - if (isValidTitle(filter)) { |
146 | | - query.setParameter("title", "%" + filter.taskTitle() + "%"); |
147 | | - } |
148 | | - if (!filter.mainCategoryIds().isEmpty()) { |
149 | | - query.setParameter("mainCategories", filter.mainCategoryIds()); |
150 | | - } |
151 | | - if (!filter.categoryIds().isEmpty()) { |
152 | | - query.setParameter("categories", filter.categoryIds()); |
153 | | - } |
154 | | - } |
155 | | - |
156 | 136 | @Override |
157 | 137 | public Page<TaskEntity> findPendingApprovalTasks(Pageable pageable, FilterTaskListRequest filterTaskListRequest) { |
158 | 138 | BooleanBuilder builder = createFilter(filterTaskListRequest); |
|
0 commit comments