Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. Weโ€™ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[๐Ÿš€feat][โš ๏ธ!HOTFIX] 11์ฐจ ๋ฐฐํฌ #110

Merged
merged 8 commits into from
Jun 7, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
8 commits
Select commit Hold shift + click to select a range
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
27 changes: 24 additions & 3 deletions src/main/java/Ness/Backend/domain/main/MainService.java
Original file line number Diff line number Diff line change
Expand Up @@ -11,22 +11,43 @@
import org.springframework.transaction.annotation.Transactional;

import java.util.List;
import java.util.concurrent.CompletableFuture;
import java.util.concurrent.ExecutionException;

@Service
@RequiredArgsConstructor
@Transactional
public class MainService {
private final ReportService reportService;
private final TodoService todoService;

public GetMainDto getMain(Long memberId){
List<GetScheduleDto> scheduleDtos = todoService.getTodo(memberId);
PostFastApiAiRecommendActivityDto activityDtos = reportService.getRecommendActivity(memberId);
try {
CompletableFuture<List<GetScheduleDto>> scheduleFuture = CompletableFuture.supplyAsync(() -> todoService.getTodo(memberId));
CompletableFuture<PostFastApiAiRecommendActivityDto> activityFuture = CompletableFuture.supplyAsync(() -> reportService.getRecommendActivity(memberId));
CompletableFuture<Void> combinedFuture = CompletableFuture.allOf(scheduleFuture, activityFuture);

// ๋‘ ์ž‘์—…์ด ๋ชจ๋‘ ์™„๋ฃŒ๋  ๋•Œ๊นŒ์ง€ ๊ธฐ๋‹ค๋ฆฐ๋‹ค.(๊ฐ๊ฐ ์ง„ํ–‰๋˜๋Š” ๊ฒƒ์€ ๋งž์Œ, ๊ฐ€์žฅ ๋Šฆ์€ ์ž‘์—… ์™„๋ฃŒ์— ๋งž์ถค)
combinedFuture.join();
List<GetScheduleDto> scheduleDtos = scheduleFuture.get();
PostFastApiAiRecommendActivityDto activityDtos = activityFuture.get();
return toEntity(scheduleDtos, activityDtos);

}
// ๋น„๋™๊ธฐ ์—๋Ÿฌ ๋ฐœ์ƒ์‹œ ๋™๊ธฐ ์ฒ˜๋ฆฌ๋ฅผ ํ•˜๋”๋ผ๋„ ๋ฉ”์ธ API ์ œ๊ณต๋˜๋„๋ก ๋กœ์ง ์„ค์ •
catch (Exception exception){
List<GetScheduleDto> scheduleDtos = todoService.getTodo(memberId);
PostFastApiAiRecommendActivityDto activityDtos = reportService.getRecommendActivity(memberId);
return toEntity(scheduleDtos, activityDtos);
}
}

//Entity -> DTO
public GetMainDto toEntity(List<GetScheduleDto> scheduleDtos, PostFastApiAiRecommendActivityDto activityDtos){
return GetMainDto.builder()
.recommendText(activityDtos.getAnswer())
.activityList(activityDtos.getActivityList())
.scheduleList(scheduleDtos)
.build();
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -12,19 +12,26 @@ public interface ReportMemoryRepository extends JpaRepository<ReportMemory, Long
// ํŠน์ • ๋งด๋ฒ„์˜ ์˜ค๋Š˜ ํ•˜๋ฃจ ์ƒ์„ฑ๋œ ๋ฐ์ดํ„ฐ๋งŒ ๋ฐ˜ํ™˜
@Query( value = "SELECT * FROM report_memory " +
"WHERE member_id = :memberId " +
"AND DATE(CONVERT_TZ(created_date, '+00:00', '+09:00')) = CURDATE() " +
"AND DATE(CONVERT_TZ(created_date, '+00:00', '+09:00')) = DATE(CONVERT_TZ(NOW(), '+00:00', '+09:00')) " +
"ORDER BY created_date ASC",
nativeQuery = true)
ReportMemory findTodayReportMemoryByMember_Id(
List<ReportMemory> findTodayReportMemoryByMember_Id(
@Param("memberId") Long memberId);

@Query( value = "SELECT * FROM report_memory " +

// ํŠน์ • ๋งด๋ฒ„์˜ 2์ฃผ์น˜ ์ƒ์„ฑ๋œ ๋ฐ์ดํ„ฐ ๋ฐ˜ํ™˜(๋‹จ, ํ•˜๋ฃจ์— ๋งŒ๋“œ์‹œ ํ•œ๊ฐœ์”ฉ ๋ฐ˜ํ™˜, 2์ฃผ์น˜๋ฉด ์ตœ๋Œ€ 14๊ฐœ)
@Query(value = "SELECT * FROM ( " +
"SELECT report_memory.*, ROW_NUMBER() OVER (PARTITION BY DATE(CONVERT_TZ(created_date, '+00:00', '+09:00')) " +
"ORDER BY created_date DESC) as row_num " +
"FROM report_memory " +
"WHERE member_id = :memberId " +
"AND created_date >= DATE_SUB(NOW(), INTERVAL 2 WEEK) " +
"ORDER BY created_date ASC",
"AND created_date >= CONVERT_TZ(DATE_SUB(NOW(), INTERVAL 2 WEEK), '+00:00', '+09:00') " +
") AS subquery " +
"WHERE row_num = 1 " +
"ORDER BY created_date ASC " +
"LIMIT 14",
nativeQuery = true)
List<ReportMemory> findTwoWeekUserMemoryByMember_Id(
@Param("memberId") Long memberId);

List<ReportMemory> findReportMemoriesByMember_idAndCreatedDateBetweenOrderByCreatedDateAsc(Long id, ZonedDateTime startOfWeek, ZonedDateTime now);
}
74 changes: 51 additions & 23 deletions src/main/java/Ness/Backend/domain/report/ReportService.java
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,9 @@

import Ness.Backend.domain.member.MemberRepository;
import Ness.Backend.domain.member.entity.Member;
import Ness.Backend.domain.profile.ProfileRepository;
import Ness.Backend.domain.profile.entity.PersonaType;
import Ness.Backend.domain.profile.entity.Profile;
import Ness.Backend.domain.report.dto.request.PostFastApiUserMemoryDto;
import Ness.Backend.domain.report.dto.request.PostFastApiUserRecommendActivityDto;
import Ness.Backend.domain.report.dto.request.PostFastApiUserTagDto;
Expand Down Expand Up @@ -37,19 +40,22 @@ public class ReportService {
private final FastApiTagApi fastApiTagApi;
private final FastApiMemoryApi fastApiMemoryApi;
private final MemberRepository memberRepository;
private final ProfileRepository profileRepository;

/* ๋ฉ”๋ชจ๋ฆฌ ๊ฐ€์ ธ์˜ค๋Š” ๋กœ์ง */
public GetReportMemoryListDto getMemory(Long id){
public GetReportMemoryListDto getMemory(Long memberId){
log.info("getMemory called by member: " + memberId);

// ์˜ค๋Š˜ ๋‚ ์งœ ๊ฐ€์ ธ์˜ค๊ธฐ
ZonedDateTime now = getToday();

ReportMemory reportMemory = reportMemoryRepository.findTodayReportMemoryByMember_Id(id);
List<ReportMemory> reportMemory = reportMemoryRepository.findTodayReportMemoryByMember_Id(memberId);

if (reportMemory == null){
if (reportMemory.isEmpty()){
// ์˜ค๋Š˜์น˜๊ฐ€ ์—†๋‹ค๋ฉด ์ƒˆ๋กญ๊ฒŒ ์ƒ์„ฑํ•˜๊ธฐ
String memory = postNewAiMemory(id, now);
String memory = postNewAiMemory(memberId, now);

Member memberEntity = memberRepository.findMemberById(id);
Member memberEntity = memberRepository.findMemberById(memberId);

ReportMemory newMemory = ReportMemory.builder()
.createdDate(now)
Expand All @@ -61,7 +67,7 @@ public GetReportMemoryListDto getMemory(Long id){
}

// 2์ฃผ์น˜์˜ ๋ฐ์ดํ„ฐ ๊ฐ€์ ธ์˜ค๊ธฐ
List<ReportMemory> reportMemories = reportMemoryRepository.findTwoWeekUserMemoryByMember_Id(id);
List<ReportMemory> reportMemories = reportMemoryRepository.findTwoWeekUserMemoryByMember_Id(memberId);
return createReportMemoryListDto(reportMemories);
}

Expand All @@ -78,10 +84,11 @@ public GetReportMemoryListDto createReportMemoryListDto(List<ReportMemory> repor
return new GetReportMemoryListDto(getReportMemoryDtos);
}

public String postNewAiMemory(Long id, ZonedDateTime today){
public String postNewAiMemory(Long memberId, ZonedDateTime today){
String persona = getPersona(memberId);
PostFastApiUserMemoryDto userDto = PostFastApiUserMemoryDto.builder()
.member_id(id.intValue())
.user_persona("")
.member_id(memberId.intValue())
.user_persona(persona)
.schedule_datetime_start(today)
.schedule_datetime_end(today)
.build();
Expand All @@ -93,16 +100,18 @@ public String postNewAiMemory(Long id, ZonedDateTime today){
}

/* ํ…Œ๊ทธ ๊ฐ€์ ธ์˜ค๋Š” ๋กœ์ง */
public GetReportTagListDto getTag(Long id){
public GetReportTagListDto getTag(Long memberId){
log.info("getTag called by member: " + memberId);

// ์˜ค๋Š˜ ๋‚ ์งœ ๊ฐ€์ ธ์˜ค๊ธฐ
ZonedDateTime now = getToday();

List<ReportTag> reportTags = reportTagRepository.findLastMonthReportTagByMember_Id(id);
List<ReportTag> reportTags = reportTagRepository.findLastMonthReportTagByMember_Id(memberId);

if (reportTags == null || reportTags.isEmpty()) {
PostFastApiAiTagListDto aiDto = postNewAiTag(id, getToday());
PostFastApiAiTagListDto aiDto = postNewAiTag(memberId, getToday());

Member memberEntity = memberRepository.findMemberById(id);
Member memberEntity = memberRepository.findMemberById(memberId);

for (PostFastApiAiTagDto tag : aiDto.getTags()) {
ReportTag reportTag = ReportTag.builder()
Expand All @@ -113,7 +122,7 @@ public GetReportTagListDto getTag(Long id){
.build();
reportTagRepository.save(reportTag);
}
return createReportTagListDto(reportTagRepository.findLastMonthReportTagByMember_Id(id));
return createReportTagListDto(reportTagRepository.findLastMonthReportTagByMember_Id(memberId));
} else {
return createReportTagListDto(reportTags);
}
Expand All @@ -132,14 +141,15 @@ public GetReportTagListDto createReportTagListDto(List<ReportTag> reportTags){
return new GetReportTagListDto(getReportTagDtos);
}

public PostFastApiAiTagListDto getAiTag(Long id){
return postNewAiTag(id, getToday());
public PostFastApiAiTagListDto getAiTag(Long memberId){
return postNewAiTag(memberId, getToday());
}

public PostFastApiAiTagListDto postNewAiTag(Long id, ZonedDateTime today){
public PostFastApiAiTagListDto postNewAiTag(Long memberId, ZonedDateTime today){
String persona = getPersona(memberId);
PostFastApiUserTagDto userDto = PostFastApiUserTagDto.builder()
.member_id(id.intValue())
.user_persona("")
.member_id(memberId.intValue())
.user_persona(persona)
.schedule_datetime_start(today)
.schedule_datetime_end(today)
.build();
Expand All @@ -150,11 +160,13 @@ public PostFastApiAiTagListDto postNewAiTag(Long id, ZonedDateTime today){

/* ํ•œ ์ค„ ์ถ”์ฒœ ๋ฐ ์—‘ํ‹ฐ๋น„ํ‹ฐ ๊ฐ€์ ธ์˜ค๋Š” ๋กœ์ง */
public PostFastApiAiRecommendActivityDto getRecommendActivity(Long memberId){
log.info("getRecommendActivity called by member: " + memberId);

// ์˜ค๋Š˜ ๋‚ ์งœ ๊ฐ€์ ธ์˜ค๊ธฐ
ZonedDateTime now = getToday();
List<ReportRecommend> reportRecommends = reportRecommendRepository.findTodayReportRecommendByMember_Id(memberId);

if(reportRecommends.isEmpty()){
if(reportRecommends == null || reportRecommends.isEmpty()){
//์ƒˆ๋กœ์šด ํ•œ ์ค„ ์ถ”์ฒœ ๋ฐ ์—‘ํ‹ฐ๋น„ํ‹ฐ ์ƒ์„ฑํ•˜๊ธฐ
PostFastApiAiRecommendActivityDto aiDto = postNewAiRecommend(memberId, now);
aiDto.setAnswer(parseAiRecommend(aiDto.getAnswer()));
Expand Down Expand Up @@ -203,10 +215,11 @@ public PostFastApiAiRecommendActivityDto getRecommendActivity(Long memberId){
}
}

public PostFastApiAiRecommendActivityDto postNewAiRecommend(Long id, ZonedDateTime today){
public PostFastApiAiRecommendActivityDto postNewAiRecommend(Long memberId, ZonedDateTime today){
String persona = getPersona(memberId);
PostFastApiUserRecommendActivityDto userDto = PostFastApiUserRecommendActivityDto.builder()
.member_id(id.intValue())
.user_persona("") // ๋นˆ ๋ฌธ์ž์—ด์€ default
.member_id(memberId.intValue())
.user_persona(persona)
.schedule_datetime_start(today)
.schedule_datetime_end(today)
.build();
Expand All @@ -223,4 +236,19 @@ public ZonedDateTime getToday(){
return ZonedDateTime.now(ZoneId.of("Asia/Seoul"));
}

public String getPersona(Long memberId){
Profile profileEntity = profileRepository.findProfileByMember_Id(memberId);
PersonaType personaType = profileEntity.getPersonaType();

String persona = "default";
if (personaType == PersonaType.HARDNESS){
persona = "hard";
}
if (personaType == PersonaType.CALMNESS){
persona = "calm";
}

return persona;
}

}
2 changes: 2 additions & 0 deletions src/main/java/Ness/Backend/domain/todo/TodoService.java
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,8 @@ public class TodoService {

/* ์ผ์ • ๊ด€๋ จ ํ•œ ์ค„ ์ถ”์ฒœ ๊ฐ€์ ธ์˜ค๋Š” ๋กœ์ง */
public List<GetScheduleDto> getTodo(Long memberId){
log.info("getTodo called by member: " + memberId);

Profile userProfile = profileRepository.findProfileByMember_Id(memberId);
// ์˜ค๋Š˜ ๋‚ ์งœ ๊ฐ€์ ธ์˜ค๊ธฐ
ZonedDateTime now = time.getToday();
Expand Down
Loading