Skip to content

Commit f1333a4

Browse files
authored
Merge pull request #1 from TeamDoubleO/KW-637/refactor/log-service-refactoring
[KW-637] Kw 637/refactor/log service refactoring
2 parents 5691b62 + 28cff56 commit f1333a4

5 files changed

Lines changed: 64 additions & 40 deletions

File tree

src/main/java/com/doubleo/logservice/domain/log/consumer/AreaEnterLogConsumer.java

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22

33
import com.doubleo.logservice.domain.log.domain.EnterLog;
44
import com.doubleo.logservice.domain.log.repository.EnterLogRepository;
5+
import com.doubleo.logservice.global.enums.VisitCategory;
56
import java.util.List;
67
import java.util.Map;
78
import javax.annotation.PostConstruct;
@@ -58,7 +59,8 @@ public void consumeMessages() {
5859
Long.parseLong((String) data.get("areaId")),
5960
Long.parseLong((String) data.get("memberId")),
6061
(String) data.get("memberName"),
61-
Long.parseLong((String) data.get("passId")));
62+
Long.parseLong((String) data.get("passId")),
63+
VisitCategory.valueOf((String) data.get("visitCategory")));
6264

6365
enterLogRepository.save(enterLog);
6466
redisTemplate.opsForStream().acknowledge(GROUP, msg);

src/main/java/com/doubleo/logservice/domain/log/domain/EnterLog.java

Lines changed: 20 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
package com.doubleo.logservice.domain.log.domain;
22

33
import com.doubleo.logservice.domain.common.model.BaseEntity;
4+
import com.doubleo.logservice.global.enums.VisitCategory;
45
import jakarta.persistence.*;
56
import lombok.AccessLevel;
67
import lombok.Builder;
@@ -30,23 +31,40 @@ public class EnterLog extends BaseEntity {
3031
@Column(name = "pass_id", nullable = false)
3132
private Long passId;
3233

34+
@Column(name = "visit_category")
35+
@Enumerated(EnumType.STRING)
36+
private VisitCategory visitCategory;
37+
3338
@Builder(access = AccessLevel.PRIVATE)
34-
private EnterLog(String tenantId, Long areaId, Long memberId, String memberName, Long passId) {
39+
private EnterLog(
40+
String tenantId,
41+
Long areaId,
42+
Long memberId,
43+
String memberName,
44+
Long passId,
45+
VisitCategory visitCategory) {
3546
this.tenantId = tenantId;
3647
this.areaId = areaId;
3748
this.memberId = memberId;
3849
this.memberName = memberName;
3950
this.passId = passId;
51+
this.visitCategory = visitCategory;
4052
}
4153

4254
public static EnterLog createEnterLog(
43-
String tenantId, Long areaId, Long memberId, String memberName, Long passId) {
55+
String tenantId,
56+
Long areaId,
57+
Long memberId,
58+
String memberName,
59+
Long passId,
60+
VisitCategory visitCategory) {
4461
return EnterLog.builder()
4562
.tenantId(tenantId)
4663
.areaId(areaId)
4764
.memberId(memberId)
4865
.memberName(memberName)
4966
.passId(passId)
67+
.visitCategory(visitCategory)
5068
.build();
5169
}
5270
}

src/main/java/com/doubleo/logservice/domain/log/repository/BuildingEnterLogRepository.java

Lines changed: 36 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -14,27 +14,31 @@
1414
public interface BuildingEnterLogRepository extends JpaRepository<BuildingEnterLog, Long> {
1515
@Query(
1616
"""
17-
SELECT COUNT(*) FROM BuildingEnterLog l
18-
WHERE l.direction = 'IN'
19-
AND l.createdDt >= :start AND l.createdDt < :end
20-
""")
21-
int countInLogsAtHour(@Param("start") LocalDateTime start, @Param("end") LocalDateTime end);
17+
SELECT COUNT(l) FROM BuildingEnterLog l
18+
WHERE l.direction = 'IN'
19+
AND l.createdDt >= :start
20+
AND l.createdDt < :end
21+
AND l.tenantId = :tenantId
22+
""")
23+
int countInLogsAtHour(
24+
@Param("start") LocalDateTime start,
25+
@Param("end") LocalDateTime end,
26+
@Param("tenantId") String tenantId);
2227

2328
Page<BuildingEnterLog> findAllByTenantId(String tenantId, Pageable pageable);
2429

2530
@Query(
2631
"""
27-
SELECT new com.doubleo.logservice.domain.stats.dto.request.UpdateDailyEntryStatsRequest(
28-
b.buildingId, b.memberName, p.visitCategory, COUNT(b)
29-
)
30-
FROM BuildingEnterLog b
31-
JOIN IssuedLog p ON b.passId = p.passId
32-
WHERE b.direction = 'IN'
33-
AND b.createdDt >= :start
34-
AND b.createdDt < :end
35-
AND b.tenantId = :tenantId
36-
GROUP BY b.buildingId, b.memberName, p.visitCategory
37-
""")
32+
SELECT new com.doubleo.logservice.domain.stats.dto.request.UpdateDailyEntryStatsRequest(
33+
b.buildingId, b.memberName, b.visitCategory, COUNT(b)
34+
)
35+
FROM BuildingEnterLog b
36+
WHERE b.direction = 'IN'
37+
AND b.createdDt >= :start
38+
AND b.createdDt < :end
39+
AND b.tenantId = :tenantId
40+
GROUP BY b.buildingId, b.memberName, b.visitCategory
41+
""")
3842
List<UpdateDailyEntryStatsRequest> countDailyGrouped(
3943
@Param("start") LocalDateTime start,
4044
@Param("end") LocalDateTime end,
@@ -56,15 +60,14 @@ Long countEnteredBetween(
5660

5761
@Query(
5862
"""
59-
SELECT COUNT(b)
60-
FROM BuildingEnterLog b
61-
JOIN IssuedLog p ON b.passId = p.passId
62-
WHERE b.direction = 'IN'
63-
AND b.createdDt >= :start
64-
AND b.createdDt < :end
65-
AND b.tenantId = :tenantId
66-
AND p.visitCategory = :visitCategory
67-
""")
63+
SELECT COUNT(b)
64+
FROM BuildingEnterLog b
65+
WHERE b.direction = 'IN'
66+
AND b.createdDt >= :start
67+
AND b.createdDt < :end
68+
AND b.tenantId = :tenantId
69+
AND b.visitCategory = :visitCategory
70+
""")
6871
int countEnteredByCategory(
6972
@Param("start") LocalDateTime start,
7073
@Param("end") LocalDateTime end,
@@ -73,15 +76,14 @@ int countEnteredByCategory(
7376

7477
@Query(
7578
"""
76-
SELECT COUNT(b)
77-
FROM BuildingEnterLog b
78-
JOIN IssuedLog p ON b.passId = p.passId
79-
WHERE b.direction = 'OUT'
80-
AND b.createdDt >= :start
81-
AND b.createdDt < :end
82-
AND b.tenantId = :tenantId
83-
AND p.visitCategory = :visitCategory
84-
""")
79+
SELECT COUNT(b)
80+
FROM BuildingEnterLog b
81+
WHERE b.direction = 'OUT'
82+
AND b.createdDt >= :start
83+
AND b.createdDt < :end
84+
AND b.tenantId = :tenantId
85+
AND b.visitCategory = :visitCategory
86+
""")
8587
int countExitedByCategory(
8688
@Param("start") LocalDateTime start,
8789
@Param("end") LocalDateTime end,

src/main/java/com/doubleo/logservice/domain/stats/repository/IssuedLogQueryRepository.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -32,8 +32,8 @@ SELECT COUNT(il)
3232
WHERE ila.tenantId = :tenantId
3333
AND ila.areaCode = :areaCode
3434
AND il.visitCategory IN :categories
35-
AND il.startAt <= :endOfDay
36-
AND il.expiredAt >= :startOfDay
35+
AND il.createdDt <= :endOfDay
36+
AND il.createdDt >= :startOfDay
3737
""";
3838

3939
TypedQuery<Long> query = em.createQuery(jpql, Long.class);

src/main/java/com/doubleo/logservice/domain/stats/service/StatsServiceImpl.java

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -61,7 +61,9 @@ public List<HourlyEntryResponse> getHourlyEntryList() {
6161
if (cachedCount != null) {
6262
total = Integer.parseInt(cachedCount);
6363
} else {
64-
int count = buildingEnterLogRepository.countInLogsAtHour(hour, hour.plusHours(1));
64+
int count =
65+
buildingEnterLogRepository.countInLogsAtHour(
66+
hour, hour.plusHours(1), tenantId);
6567
redisTemplate
6668
.opsForValue()
6769
.set(redisKey, String.valueOf(count), Duration.ofSeconds(10));

0 commit comments

Comments
 (0)