| 1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465 |
- <?php
- namespace App\Module\Transaction\Services;
- use App\Module\Test\Models\Test as TestModel;
- use Illuminate\Support\Facades\DB;
- class TestStatisticsService implements TestStatisticsServiceInterface
- {
- /**
- * 获取总数统计
- *
- * @return array
- */
- public function getTotalStatistics(): array
- {
- return [
- 'total' => TestModel::count(),
- 'active' => TestModel::where('status', 1)->count(),
- 'inactive' => TestModel::where('status', 0)->count(),
- 'deleted' => TestModel::onlyTrashed()->count(),
- ];
- }
- /**
- * 获取状态分布统计
- *
- * @return array
- */
- public function getStatusDistribution(): array
- {
- return TestModel::select('status', DB::raw('count(*) as count'))
- ->groupBy('status')
- ->get()
- ->mapWithKeys(function ($item) {
- return [$item->status => $item->count];
- })
- ->toArray();
- }
- /**
- * 获取每日创建数量统计
- *
- * @param int $days
- * @return array
- */
- public function getDailyCreationStatistics(int $days = 7): array
- {
- $startDate = now()->subDays($days)->startOfDay();
- $endDate = now()->endOfDay();
- return TestModel::select(
- DB::raw('DATE(created_at) as date'),
- DB::raw('count(*) as count')
- )
- ->whereBetween('created_at', [$startDate, $endDate])
- ->groupBy('date')
- ->orderBy('date')
- ->get()
- ->mapWithKeys(function ($item) {
- return [$item->date => $item->count];
- })
- ->toArray();
- }
- }
|