From 04ad2e514cdd2e5ba5128ea2d763cd67d687324c Mon Sep 17 00:00:00 2001 From: cloudroam <cloudroam> Date: 星期四, 06 三月 2025 11:40:02 +0800 Subject: [PATCH] fix: 4 --- app/src/main/java/com/example/firstapp/database/dao/CodeDao.kt | 165 +++++++++++++++++++++++++++++++++++++++++++++++++----- 1 files changed, 148 insertions(+), 17 deletions(-) diff --git a/app/src/main/java/com/example/firstapp/database/dao/CodeDao.kt b/app/src/main/java/com/example/firstapp/database/dao/CodeDao.kt index 343140a..39d54e2 100644 --- a/app/src/main/java/com/example/firstapp/database/dao/CodeDao.kt +++ b/app/src/main/java/com/example/firstapp/database/dao/CodeDao.kt @@ -9,6 +9,7 @@ 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 @@ -73,29 +74,159 @@ fun getPackagesByDay(date: Long): Flow<List<Code>> @Query(""" - SELECT * FROM code - WHERE strftime('%Y-%W', substr(createtime, 1, 10)) = - strftime('%Y-%W', datetime(:date/1000, 'unixepoch', 'localtime')) - ORDER BY createtime DESC - """) - fun getPackagesByWeek(date: Long): Flow<List<Code>> - - @Query(""" - SELECT category as courierName, COUNT(*) as count + SELECT type as courierName, COUNT(*) as count FROM code WHERE strftime('%Y-%W', substr(createtime, 1, 10)) = strftime('%Y-%W', datetime(:date/1000, 'unixepoch', 'localtime')) - GROUP BY category + GROUP BY type + ORDER BY count DESC """) fun getCourierStatsByWeek(date: Long): Flow<List<CourierStat>> @Query(""" - SELECT substr(createtime, 1, 10) as date, - COUNT(*) as count - FROM code - WHERE strftime('%Y-%W', substr(createtime, 1, 10)) = - strftime('%Y-%W', datetime(:date/1000, 'unixepoch', 'localtime')) - GROUP BY substr(createtime, 1, 10) + 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 + WHERE substr(createtime, 1, 10) = + date(:date/1000, 'unixepoch', 'localtime') + ORDER BY createtime DESC """) - fun getDailyStatsByWeek(date: Long): Flow<List<DailyStat>> + 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>> } -- Gitblit v1.9.3