| | |
| | | import com.example.firstapp.database.entity.Msg |
| | | import com.example.firstapp.model.CourierStat |
| | | import com.example.firstapp.model.DailyStat |
| | | import com.example.firstapp.model.HeatmapStat |
| | | import io.reactivex.Completable |
| | | import kotlinx.coroutines.flow.Flow |
| | | |
| | |
| | | fun getCourierStatsByWeek(date: Long): Flow<List<CourierStat>> |
| | | |
| | | @Query(""" |
| | | WITH RECURSIVE weeks(week_start, week_end) AS ( |
| | | -- 从选定的日期开始计算周 |
| | | SELECT |
| | | date(datetime(:date/1000, 'unixepoch', 'localtime'), 'weekday 1', '-28 days') as week_start, |
| | | date(datetime(:date/1000, 'unixepoch', 'localtime'), 'weekday 0', '-28 days') as week_end |
| | | UNION ALL |
| | | SELECT |
| | | date(week_start, '+7 days'), |
| | | date(week_end, '+7 days') |
| | | FROM weeks |
| | | WHERE date(week_start) <= date(datetime(:date/1000, 'unixepoch', 'localtime')) |
| | | LIMIT 5 |
| | | ) |
| | | SELECT |
| | | CAST(strftime('%W', week_start) AS INTEGER) as date, |
| | | COUNT(code.id) as count, |
| | | week_start as week_start |
| | | FROM weeks |
| | | LEFT JOIN code ON datetime(code.createtime) BETWEEN datetime(weeks.week_start) |
| | | AND datetime(weeks.week_end, '+23 hours', '+59 minutes', '+59 seconds') |
| | | GROUP BY weeks.week_start |
| | | ORDER BY weeks.week_start ASC |
| | | """) |
| | | fun getDailyStatsByWeek(date: Long): Flow<List<DailyStat>> |
| | | WITH RECURSIVE dates(date_value) AS ( |
| | | -- 从当前日期往前3周的周一开始 |
| | | SELECT date(datetime(:date/1000, 'unixepoch', 'localtime'), 'weekday 0', '-21 days') as date_value |
| | | UNION ALL |
| | | -- 每次加7天,直到后2周 |
| | | SELECT date(date_value, '+7 days') |
| | | FROM dates |
| | | WHERE date_value < date(datetime(:date/1000, 'unixepoch', 'localtime'), 'weekday 0', '+14 days') |
| | | ) |
| | | SELECT |
| | | strftime('%Y-%m-%d', date_value) as date, |
| | | COUNT(c.id) as count, |
| | | date_value as weekStart |
| | | FROM dates d |
| | | LEFT JOIN code c ON strftime('%Y-%m-%d', c.createtime) BETWEEN |
| | | strftime('%Y-%m-%d', d.date_value) |
| | | AND strftime('%Y-%m-%d', date(d.date_value, '+6 days')) |
| | | GROUP BY d.date_value |
| | | ORDER BY d.date_value ASC |
| | | LIMIT :weekCount |
| | | """) |
| | | fun getWeeklyStats(date: Long, weekCount: Int): Flow<List<DailyStat>> |
| | | |
| | | @Query(""" |
| | | SELECT * FROM code |
| | |
| | | ORDER BY createtime DESC |
| | | """) |
| | | fun getPackagesByWeek(date: Long): Flow<List<Code>> |
| | | |
| | | @Query(""" |
| | | SELECT type as courierName, COUNT(*) as count |
| | | FROM code |
| | | WHERE strftime('%Y-%m', substr(createtime, 1, 10)) = |
| | | strftime('%Y-%m', datetime(:date/1000, 'unixepoch', 'localtime')) |
| | | GROUP BY type |
| | | ORDER BY count DESC |
| | | """) |
| | | fun getCourierStatsByMonth(date: Long): Flow<List<CourierStat>> |
| | | |
| | | @Query(""" |
| | | SELECT type as courierName, COUNT(*) as count |
| | | FROM code |
| | | WHERE strftime('%Y', substr(createtime, 1, 10)) = |
| | | strftime('%Y', datetime(:date/1000, 'unixepoch', 'localtime')) |
| | | GROUP BY type |
| | | ORDER BY count DESC |
| | | """) |
| | | fun getCourierStatsByYear(date: Long): Flow<List<CourierStat>> |
| | | |
| | | // @Query(""" |
| | | // WITH RECURSIVE months(month_start) AS ( |
| | | // SELECT date(datetime(:date/1000, 'unixepoch', 'localtime'), 'start of month', '-11 months') |
| | | // UNION ALL |
| | | // SELECT date(month_start, '+1 month') |
| | | // FROM months |
| | | // WHERE date(month_start) < date(datetime(:date/1000, 'unixepoch', 'localtime'), 'start of month') |
| | | // ) |
| | | // SELECT |
| | | // strftime('%m', month_start) as date, |
| | | // COUNT(code.id) as count, |
| | | // month_start as week_start |
| | | // FROM months |
| | | // LEFT JOIN code ON strftime('%Y-%m', code.createtime) = strftime('%Y-%m', months.month_start) |
| | | // GROUP BY months.month_start |
| | | // ORDER BY months.month_start ASC |
| | | // """) |
| | | // fun getMonthlyStats(date: Long): Flow<List<DailyStat>> |
| | | |
| | | @Query(""" |
| | | WITH RECURSIVE years(year_start) AS ( |
| | | SELECT date(datetime(:date/1000, 'unixepoch', 'localtime'), 'start of year', '-4 years') |
| | | UNION ALL |
| | | SELECT date(year_start, '+1 year') |
| | | FROM years |
| | | WHERE date(year_start) < date(datetime(:date/1000, 'unixepoch', 'localtime'), 'start of year') |
| | | ) |
| | | SELECT |
| | | strftime('%Y', year_start) as date, |
| | | COUNT(code.id) as count, |
| | | year_start as week_start |
| | | FROM years |
| | | LEFT JOIN code ON strftime('%Y', code.createtime) = strftime('%Y', years.year_start) |
| | | GROUP BY years.year_start |
| | | ORDER BY years.year_start ASC |
| | | """) |
| | | fun getYearlyStats(date: Long): Flow<List<DailyStat>> |
| | | |
| | | @Query(""" |
| | | WITH RECURSIVE dates(date) AS ( |
| | | SELECT date(datetime(:date/1000, 'unixepoch', 'localtime'), 'start of year') |
| | | UNION ALL |
| | | SELECT date(date, '+1 day') |
| | | FROM dates |
| | | WHERE date < date(datetime(:date/1000, 'unixepoch', 'localtime'), 'start of year', '+1 year', '-1 day') |
| | | ) |
| | | SELECT |
| | | CAST(strftime('%w', dates.date) AS INTEGER) as dayOfWeek, |
| | | CAST(strftime('%W', dates.date) AS INTEGER) as weekOfYear, |
| | | COUNT(code.id) as count |
| | | FROM dates |
| | | LEFT JOIN code ON date(code.createtime) = dates.date |
| | | GROUP BY dates.date |
| | | ORDER BY dates.date |
| | | """) |
| | | fun getYearlyHeatmap(date: Long): Flow<List<HeatmapStat>> |
| | | |
| | | @Query(""" |
| | | WITH RECURSIVE days(date) AS ( |
| | | SELECT date(datetime(:date/1000, 'unixepoch', 'localtime'), 'start of month') |
| | | UNION ALL |
| | | SELECT date(date, '+1 day') |
| | | FROM days |
| | | WHERE date < date(datetime(:date/1000, 'unixepoch', 'localtime'), 'start of month', '+1 month', '-1 day') |
| | | ) |
| | | SELECT |
| | | strftime('%d', days.date) || '' as date, -- 确保 date 不为空 |
| | | COUNT(code.id) as count, |
| | | days.date as week_start |
| | | FROM days |
| | | LEFT JOIN code ON date(code.createtime) = days.date |
| | | GROUP BY days.date |
| | | ORDER BY days.date ASC |
| | | """) |
| | | fun getMonthlyStats(date: Long): Flow<List<DailyStat>> |
| | | |
| | | @Query(""" |
| | | WITH RECURSIVE months(month_start) AS ( |
| | | SELECT date(datetime(:date/1000, 'unixepoch', 'localtime'), 'start of year') as month_start |
| | | UNION ALL |
| | | SELECT date(month_start, '+1 month') |
| | | FROM months |
| | | WHERE strftime('%m', month_start) < '12' |
| | | ) |
| | | SELECT |
| | | strftime('%m', month_start) as date, |
| | | COUNT(code.id) as count, |
| | | month_start as weekStart |
| | | FROM months |
| | | LEFT JOIN code ON strftime('%Y-%m', code.createtime) = strftime('%Y-%m', months.month_start) |
| | | GROUP BY months.month_start |
| | | ORDER BY months.month_start ASC |
| | | """) |
| | | fun getYearMonthlyStats(date: Long): Flow<List<DailyStat>> |
| | | } |