1
1
package Ness .Backend .domain .report ;
2
2
3
- import Ness .Backend .domain .chat .dto .request .PostFastApiUserChatDto ;
4
- import Ness .Backend .domain .chat .dto .response .PostFastApiAiChatDto ;
5
3
import Ness .Backend .domain .member .MemberRepository ;
6
4
import Ness .Backend .domain .member .entity .Member ;
5
+ import Ness .Backend .domain .report .dto .request .PostFastApiUserMemoryDto ;
7
6
import Ness .Backend .domain .report .dto .request .PostFastApiUserRecommendDto ;
7
+ import Ness .Backend .domain .report .dto .request .PostFastApiUserTagDto ;
8
8
import Ness .Backend .domain .report .dto .response .*;
9
9
import Ness .Backend .domain .report .entity .ReportMemory ;
10
10
import Ness .Backend .domain .report .entity .ReportRecommend ;
11
11
import Ness .Backend .domain .report .entity .ReportTag ;
12
+ import Ness .Backend .global .fastApi .FastApiMemoryApi ;
12
13
import Ness .Backend .global .fastApi .FastApiRecommendApi ;
14
+ import Ness .Backend .global .fastApi .FastApiTagApi ;
13
15
import lombok .RequiredArgsConstructor ;
14
16
import lombok .extern .slf4j .Slf4j ;
15
17
import org .springframework .stereotype .Service ;
16
18
17
19
import java .time .DayOfWeek ;
18
- import java .time .LocalTime ;
19
20
import java .time .ZoneId ;
20
21
import java .time .ZonedDateTime ;
21
22
import java .util .List ;
@@ -28,89 +29,148 @@ public class ReportService {
28
29
private final ReportTagRepository reportTagRepository ;
29
30
private final ReportRecommendRepository reportRecommendRepository ;
30
31
private final FastApiRecommendApi fastApiRecommendApi ;
32
+ private final FastApiTagApi fastApiTagApi ;
33
+ private final FastApiMemoryApi fastApiMemoryApi ;
31
34
private final MemberRepository memberRepository ;
32
35
33
36
public GetReportMemoryListDto getMemory (Long id ){
34
- // 2주치의 데이터 가져오기
35
- ZonedDateTime now = ZonedDateTime .now (ZoneId .of ("Asia/Seoul" ));
36
- ZonedDateTime startOfWeek = now .with (DayOfWeek .MONDAY ).withHour (0 ).withMinute (0 ).withSecond (0 ).withNano (0 );
37
- ZonedDateTime startOfLastWeek = startOfWeek .minusWeeks (1 );
37
+ // 오늘 날짜 가져오기
38
+ ZonedDateTime now = getToday ();
38
39
39
- List < ReportMemory > reportMemories = reportMemoryRepository .findReportMemoriesByMember_idAndCreatedDateBetweenOrderByCreatedDateAsc (id , startOfLastWeek , now );
40
+ ReportMemory reportMemory = reportMemoryRepository .findTodayReportMemoryByMember_Id (id );
40
41
41
- //ReportMemoryListResponseDto에 매핑
42
- List <GetReportMemoryDto > getReportMemoryDtos = reportMemories .stream ()
43
- .map (memory -> GetReportMemoryDto .builder ()
44
- .id (memory .getId ())
45
- .createdDate (memory .getCreatedDate ().toString ())
46
- .pictureUrl (memory .getPictureUrl ())
47
- .build ())
48
- .toList ();
42
+ if (reportMemory == null ){
43
+ // 오늘치가 없다면 새롭게 생성하기
44
+ String memory = postNewAiMemory (id , now );
49
45
50
- return new GetReportMemoryListDto (getReportMemoryDtos );
46
+ Member memberEntity = memberRepository .findMemberById (id );
47
+
48
+ ReportMemory newMemory = ReportMemory .builder ()
49
+ .createdDate (now )
50
+ .memory (memory )
51
+ .member (memberEntity )
52
+ .build ();
53
+
54
+ reportMemoryRepository .save (newMemory );
55
+ }
56
+
57
+ // 2주치의 데이터 가져오기
58
+ List <ReportMemory > reportMemories = reportMemoryRepository .findTwoWeekUserMemoryByMember_Id (id );
59
+ return createReportMemoryListDto (reportMemories );
51
60
}
52
61
53
62
public GetReportTagListDto getTag (Long id ){
54
- // 지난 달 10일~이번 달 9일 간의 데이터 가져오기
55
- ZonedDateTime now = ZonedDateTime .now (ZoneId .of ("Asia/Seoul" ));
56
- ZonedDateTime lastMonth10 = now .withMonth (now .getMonthValue () - 1 ).withDayOfMonth (10 ).withHour (0 ).withMinute (0 ).withSecond (0 ).withNano (0 );
57
- ZonedDateTime thisMonth9 = now .withMonth (now .getMonthValue ()).withDayOfMonth (9 ).withHour (0 ).withMinute (0 ).withSecond (0 ).withNano (0 );
58
- List <ReportTag > reportTags = reportTagRepository .findReportTagsByMember_idAndCreatedDateBetweenOrderByCreatedDateAsc (id , lastMonth10 , thisMonth9 );
63
+ // 오늘 날짜 가져오기
64
+ ZonedDateTime now = getToday ();
59
65
60
- //ReportTagListResponseDto에 매핑
61
- List <GetReportTagDto > getReportTagDtos = reportTags .stream ()
62
- .map (tag -> GetReportTagDto .builder ()
63
- .id (tag .getId ())
64
- .createdDate (tag .getCreatedDate ().toString ())
65
- .tagTitle (tag .getTagTitle ())
66
- .tagDesc (tag .getTagDesc ())
67
- .build ())
68
- .toList ();
66
+ List <ReportTag > reportTags = reportTagRepository .findLastMonthReportTagByMember_Id (id );
69
67
70
- return new GetReportTagListDto (getReportTagDtos );
68
+ if (reportTags == null ) {
69
+ PostFastApiAiTagDto aiDto = postNewAiTag (id , getToday ());
70
+
71
+ Member memberEntity = memberRepository .findMemberById (id );
72
+ /*
73
+ for (String tag : aiDto.getTags()) {
74
+ ReportTag reportTag = ReportTag.builder()
75
+ .tagTitle()
76
+ .tagDesc()
77
+ .createdDate(now.withDayOfMonth(1).withHour(0).withMinute(0).withSecond(0).withNano(0))
78
+ .member(memberEntity)
79
+ .build();
80
+
81
+ reportTagRepository.save(reportTag);
82
+ }
83
+ */
84
+ }
85
+
86
+ return createReportTagListDto (reportTags );
71
87
}
72
88
73
89
public GetReportRecommendDto getRecommend (Long id ){
74
- // 이번 달의 데이터 가져오기
75
- ZonedDateTime now = ZonedDateTime .now (ZoneId .of ("Asia/Seoul" ));
76
- ZonedDateTime today = now .toLocalDate ().atStartOfDay (now .getZone ());
90
+ // 오늘 날짜 가져오기
91
+ ZonedDateTime now = getToday ();
77
92
78
93
ReportRecommend reportRecommend = reportRecommendRepository .findTodayReportRecommendByMember_Id (id );
79
94
80
95
if (reportRecommend == null ){
81
96
//새로운 한 줄 추천 생성하기
82
- String answer = postNewAiRecommend (id , today );
97
+ String answer = postNewAiRecommend (id , now );
83
98
String parsedAnswer = parseAiRecommend (answer );
84
99
85
100
Member memberEntity = memberRepository .findMemberById (id );
86
101
87
102
ReportRecommend newRecommend = ReportRecommend .builder ()
88
- .createdDate (today )
89
- .recommendText (answer )
103
+ .createdDate (now )
104
+ .recommendText (parsedAnswer )
90
105
.member (memberEntity )
91
106
.build ();
92
107
93
108
//새롭게 생성된 한 줄 추천 저장하기
94
109
reportRecommendRepository .save (newRecommend );
95
110
96
- return GetReportRecommendDto .builder ()
97
- .id (newRecommend .getId ())
98
- .createdDate (newRecommend .getCreatedDate ().toString ())
99
- .recommendText (newRecommend .getRecommendText ())
100
- .build ();
111
+ return createReportRecommendDto (newRecommend .getId (), newRecommend .getCreatedDate ().toString (), newRecommend .getRecommendText ());
101
112
} else {
102
- return GetReportRecommendDto .builder ()
103
- .id (reportRecommend .getId ())
104
- .createdDate (reportRecommend .getCreatedDate ().toString ())
105
- .recommendText (reportRecommend .getRecommendText ())
106
- .build ();
113
+ return createReportRecommendDto (reportRecommend .getId (), reportRecommend .getCreatedDate ().toString (), reportRecommend .getRecommendText ());
107
114
}
108
115
}
109
116
117
+ public ZonedDateTime getToday (){
118
+ return ZonedDateTime .now (ZoneId .of ("Asia/Seoul" ));
119
+ }
120
+
121
+ public GetReportMemoryListDto createReportMemoryListDto (List <ReportMemory > reportMemories ) {
122
+ //ReportMemoryListResponseDto에 매핑
123
+ List <GetReportMemoryDto > getReportMemoryDtos = reportMemories .stream ()
124
+ .map (memory -> GetReportMemoryDto .builder ()
125
+ .id (memory .getId ())
126
+ .createdDate (memory .getCreatedDate ().toString ())
127
+ .memory (memory .getMemory ())
128
+ .build ())
129
+ .toList ();
130
+
131
+ return new GetReportMemoryListDto (getReportMemoryDtos );
132
+ }
133
+
134
+ public GetReportTagListDto createReportTagListDto (List <ReportTag > reportTags ) {
135
+
136
+ List <GetReportTagDto > getReportTagDtos = reportTags .stream ()
137
+ .map (tag -> GetReportTagDto .builder ()
138
+ .id (tag .getId ())
139
+ .createdDate (tag .getCreatedDate ().toString ())
140
+ .tagTitle (tag .getTagTitle ())
141
+ .tagDesc (tag .getTagDesc ())
142
+ .build ())
143
+ .toList ();
144
+
145
+ return new GetReportTagListDto (getReportTagDtos );
146
+ }
147
+
148
+ public GetReportRecommendDto createReportRecommendDto (Long id , String date , String text ){
149
+ return GetReportRecommendDto .builder ()
150
+ .id (id )
151
+ .createdDate (date )
152
+ .recommendText (text )
153
+ .build ();
154
+ }
155
+
110
156
public String parseAiRecommend (String text ){
111
157
return text .replace ("\" " , "" );
112
158
}
113
159
160
+ public String postNewAiMemory (Long id , ZonedDateTime today ){
161
+ PostFastApiUserMemoryDto userDto = PostFastApiUserMemoryDto .builder ()
162
+ .member_id (id .intValue ())
163
+ .user_persona ("" )
164
+ .schedule_datetime_start (today )
165
+ .schedule_datetime_end (today )
166
+ .build ();
167
+
168
+ //Fast API에 전송하기
169
+ PostFastApiAiMemoryDto aiDto = fastApiMemoryApi .creatFastApiMemory (userDto );
170
+
171
+ return aiDto .getMemory ();
172
+ }
173
+
114
174
public String postNewAiRecommend (Long id , ZonedDateTime today ){
115
175
PostFastApiUserRecommendDto userDto = PostFastApiUserRecommendDto .builder ()
116
176
.member_id (id .intValue ())
@@ -120,8 +180,20 @@ public String postNewAiRecommend(Long id, ZonedDateTime today){
120
180
.build ();
121
181
122
182
//Fast API에 전송하기
123
- PostFastApiAiRecommendDto AiDto = fastApiRecommendApi .creatFastApiRecommend (userDto );
183
+ PostFastApiAiRecommendDto aiDto = fastApiRecommendApi .creatFastApiRecommend (userDto );
184
+
185
+ return aiDto .getAnswer ();
186
+ }
187
+
188
+ public PostFastApiAiTagDto postNewAiTag (Long id , ZonedDateTime today ){
189
+ PostFastApiUserTagDto userDto = PostFastApiUserTagDto .builder ()
190
+ .member_id (id .intValue ())
191
+ .user_persona ("" )
192
+ .schedule_datetime_start (today )
193
+ .schedule_datetime_end (today )
194
+ .build ();
124
195
125
- return AiDto .getAnswer ();
196
+ //Fast API에 전송하고 값 받아오기
197
+ return fastApiTagApi .creatFastApiTag (userDto );
126
198
}
127
199
}
0 commit comments