@@ -15,16 +15,42 @@ def __init__(self, session: AsyncSession = Depends(get_db_session)):
1515 self .session = session
1616
1717 async def get_lotto_draws (
18- self , cursor : Optional [int ] = None , limit : int = 10
18+ self ,
19+ user_id : Optional [str ] = None ,
20+ cursor : Optional [int ] = None ,
21+ limit : int = 10 ,
1922 ) -> Tuple [List [LottoDraws ], Optional [int ]]:
20- query = select (LottoDraws ).order_by (desc (LottoDraws .round ))
23+ if user_id :
24+ query = (
25+ select (
26+ LottoDraws ,
27+ (LottoRecommendations .id .is_not (None )).label ("has_recommendation" ),
28+ )
29+ .outerjoin (
30+ LottoRecommendations ,
31+ (LottoDraws .round == LottoRecommendations .round )
32+ & (LottoRecommendations .user_id == user_id ),
33+ )
34+ .order_by (desc (LottoDraws .round ))
35+ )
36+ else :
37+ query = select (LottoDraws ).order_by (desc (LottoDraws .round ))
38+
2139 if cursor :
2240 query = query .where (LottoDraws .round < cursor )
2341
2442 query = query .limit (limit )
2543
2644 result = await self .session .execute (query )
27- draws = result .scalars ().all ()
45+
46+ if user_id :
47+ rows = result .fetchall ()
48+ draws = []
49+ for draw , has_recommendation in rows :
50+ draw .has_recommendation = bool (has_recommendation )
51+ draws .append (draw )
52+ else :
53+ draws = result .scalars ().all ()
2854
2955 next_cursor = draws [- 1 ].round if len (draws ) == limit else None
3056 return draws , next_cursor
@@ -57,6 +83,66 @@ async def get_latest_round(self) -> Optional[int]:
5783 latest = result .scalar_one_or_none ()
5884 return latest
5985
86+ async def create_lotto_draw (self , lotto_data : dict ) -> LottoDraws :
87+ """로또 추첨 데이터를 생성합니다."""
88+ lotto_draw = LottoDraws (** lotto_data )
89+ self .session .add (lotto_draw )
90+ await self .session .flush ()
91+ await self .session .refresh (lotto_draw )
92+ return lotto_draw
93+
94+ async def get_lotto_draw_by_round (self , round : int ) -> Optional [LottoDraws ]:
95+ """특정 회차의 로또 추첨 데이터를 조회합니다."""
96+ query = select (LottoDraws ).where (LottoDraws .round == round )
97+ result = await self .session .execute (query )
98+ return result .scalar_one_or_none ()
99+
100+ async def update_lotto_statistics (self , lotto_data : dict ) -> None :
101+ """로또 통계 데이터를 업데이트합니다."""
102+ # 메인 번호들 (1-6번)과 보너스 번호
103+ main_numbers = [
104+ lotto_data ["num1" ],
105+ lotto_data ["num2" ],
106+ lotto_data ["num3" ],
107+ lotto_data ["num4" ],
108+ lotto_data ["num5" ],
109+ lotto_data ["num6" ],
110+ ]
111+ bonus_number = lotto_data ["bonus_num" ]
112+
113+ # 당첨된 번호들만 수집 (중복 제거)
114+ winning_numbers = set (main_numbers + [bonus_number ])
115+
116+ # 당첨된 번호들의 통계만 조회
117+ stats_query = select (LottoStatistics ).where (
118+ LottoStatistics .num .in_ (winning_numbers )
119+ )
120+ result = await self .session .execute (stats_query )
121+ existing_stats = {stat .num : stat for stat in result .scalars ().all ()}
122+
123+ # 각 당첨 번호에 대해 통계 업데이트
124+ for num in winning_numbers :
125+ if num in existing_stats :
126+ stat = existing_stats [num ]
127+ else :
128+ # 새로운 통계 생성
129+ stat = LottoStatistics (
130+ num = num , main_count = 0 , bonus_count = 0 , total_count = 0
131+ )
132+ self .session .add (stat )
133+
134+ # 카운트 업데이트
135+ if num in main_numbers :
136+ stat .main_count += 1
137+ stat .total_count += 1
138+ elif num == bonus_number :
139+ stat .bonus_count += 1
140+ stat .total_count += 1
141+
142+ # 마지막 출현 정보 업데이트
143+ stat .last_round = lotto_data ["round" ]
144+ stat .last_date = lotto_data ["draw_date" ]
145+
60146 async def create_lotto_recommendation (
61147 self , user_id : str , round : int , content : dict
62148 ) -> LottoRecommendations :
0 commit comments