@@ -139,4 +139,79 @@ impl Member {
139139
140140 Ok ( history)
141141 }
142+
143+ async fn present_count_by_date (
144+ & self ,
145+ ctx : & Context < ' _ > ,
146+ start_date : NaiveDate ,
147+ end_date : NaiveDate ,
148+ ) -> Result < i64 > {
149+ if end_date < start_date {
150+ return Err ( "end_date must be >= start_date" . into ( ) ) ;
151+ }
152+
153+ let pool = ctx. data :: < Arc < PgPool > > ( ) . expect ( "Pool must be in context." ) ;
154+
155+ let records: i64 = sqlx:: query_scalar (
156+ "
157+ SELECT COUNT(att.is_present)
158+ FROM attendance att
159+ INNER JOIN member m ON att.member_id = m.member_id
160+ WHERE att.member_id = $3
161+ AND att.is_present = true
162+ AND att.date BETWEEN $1 AND $2" ,
163+ )
164+ . bind ( start_date)
165+ . bind ( end_date)
166+ . bind ( self . member_id )
167+ . fetch_one ( pool. as_ref ( ) )
168+ . await ?;
169+
170+ Ok ( records)
171+ }
172+
173+ async fn absent_count_by_date (
174+ & self ,
175+ ctx : & Context < ' _ > ,
176+ start_date : NaiveDate ,
177+ end_date : NaiveDate ,
178+ ) -> Result < i64 > {
179+ if end_date < start_date {
180+ return Err ( "end_date must be >= start_date" . into ( ) ) ;
181+ }
182+
183+ let pool = ctx. data :: < Arc < PgPool > > ( ) . expect ( "Pool must be in context." ) ;
184+
185+ let working_days: i64 = sqlx:: query_scalar (
186+ "
187+ SELECT COUNT(*)
188+ FROM (
189+ SELECT date
190+ FROM attendance
191+ where date between $1 and $2 GROUP BY date
192+ HAVING BOOL_or(is_present = true)
193+ );
194+ " ,
195+ )
196+ . bind ( start_date)
197+ . bind ( end_date)
198+ . fetch_one ( pool. as_ref ( ) )
199+ . await ?;
200+
201+ let present: i64 = sqlx:: query_scalar (
202+ "
203+ SELECT COUNT(att.is_present)
204+ FROM attendance att
205+ WHERE att.member_id = $3
206+ AND att.is_present = true
207+ AND att.date BETWEEN $1 AND $2" ,
208+ )
209+ . bind ( start_date)
210+ . bind ( end_date)
211+ . bind ( self . member_id )
212+ . fetch_one ( pool. as_ref ( ) )
213+ . await ?;
214+
215+ Ok ( working_days - present)
216+ }
142217}
0 commit comments