This commit is contained in:
hashbro
2026-09-17 00:25:39 +08:00
parent a49fb2415d
commit 2d18331feb
13 changed files with 475 additions and 91 deletions
+23 -4
View File
@@ -40,7 +40,7 @@ class AnalyticsReportService
return $this->present($from, $to, $agentUserId);
}
public function rebuild(?Carbon $from = null, ?Carbon $to = null, ?int $agentUserId = null): int
public function rebuild(?Carbon $from = null, ?Carbon $to = null, ?int $agentUserId = null, bool $force = false): int
{
[$from, $to] = $from !== null && $to !== null
? [$from->copy()->startOfDay(), $to->copy()->endOfDay()]
@@ -48,12 +48,15 @@ class AnalyticsReportService
$scope = DailyReportService::scopeKey($agentUserId);
$now = Carbon::now();
$refreshFrom = Carbon::now()->copy()->subDay()->startOfDay();
$days = 0;
$cursor = $from->copy()->startOfDay();
$end = $to->copy()->startOfDay();
while ($cursor->lte($end)) {
$this->rebuildDay($cursor, $scope, $agentUserId, $now);
$days++;
if ($force || $cursor->gte($refreshFrom) || ! $this->dayHasSurvival($scope, $cursor)) {
$this->rebuildDay($cursor, $scope, $agentUserId, $now);
$days++;
}
$cursor->addDay();
}
@@ -87,7 +90,7 @@ class AnalyticsReportService
}
}
private function rebuildDay(Carbon $day, string $scope, ?int $agentUserId, Carbon $now): void
public function rebuildDay(Carbon $day, string $scope, ?int $agentUserId, Carbon $now): void
{
$from = $day->copy()->startOfDay();
$to = $day->copy()->endOfDay();
@@ -386,6 +389,22 @@ class AnalyticsReportService
->count();
}
private function dayHasSurvival(string $scope, Carbon $day): bool
{
$date = $day->toDateString();
return AnalyticsDailyDim::query()
->where('scope_key', $scope)
->where('stat_date', $date)
->where('kind', AnalyticsDailyDim::KIND_SURVIVAL)
->exists()
&& AnalyticsDailyDim::query()
->where('scope_key', $scope)
->where('stat_date', $date)
->where('kind', AnalyticsDailyDim::KIND_SURVIVAL_WALLET)
->exists();
}
private function versionDimSql(string $column): string
{
return "CASE WHEN {$column} IS NULL OR TRIM({$column}) = '' THEN '未知' ELSE TRIM({$column}) END";