mirror of
				https://github.com/v2board/v2board.git
				synced 2025-11-01 01:41:47 +08:00 
			
		
		
		
	update: statistics service
This commit is contained in:
		| @@ -1,23 +1,108 @@ | ||||
| <?php | ||||
| namespace App\Services; | ||||
|  | ||||
| use App\Models\CommissionLog; | ||||
| use App\Models\Order; | ||||
| use App\Models\Stat; | ||||
| use App\Models\StatServer; | ||||
| use App\Models\User; | ||||
| use Illuminate\Support\Facades\Cache; | ||||
| use Illuminate\Support\Facades\DB; | ||||
|  | ||||
| class StatisticalService { | ||||
|     protected $userStats; | ||||
|     protected $recordAt; | ||||
|     protected $startAt; | ||||
|     protected $endAt; | ||||
|     protected $serverStats; | ||||
|  | ||||
|     public function __construct($recordAt = NULL) | ||||
|     public function __construct() | ||||
|     { | ||||
|         ini_set('memory_limit', -1); | ||||
|         $this->recordAt = $recordAt ?? strtotime(date('Y-m-d')); | ||||
|         $this->userStats = Cache::get("stat_user_{$this->recordAt}"); | ||||
|     } | ||||
|  | ||||
|     public function setStartAt($timestamp) { | ||||
|         $this->startAt = $timestamp; | ||||
|     } | ||||
|  | ||||
|     public function setEndAt($timestamp) { | ||||
|         $this->endAt = $timestamp; | ||||
|     } | ||||
|  | ||||
|     public function setServerStats() { | ||||
|         $this->serverStats = Cache::get("stat_server_{$this->startAt}"); | ||||
|         $this->serverStats = json_decode($this->serverStats, true) ?? []; | ||||
|         if (!is_array($this->serverStats)) { | ||||
|             $this->serverStats = []; | ||||
|         } | ||||
|     } | ||||
|  | ||||
|     public function setUserStats() { | ||||
|         $this->userStats = Cache::get("stat_user_{$this->startAt}"); | ||||
|         $this->userStats = json_decode($this->userStats, true) ?? []; | ||||
|         if (!is_array($this->userStats)) { | ||||
|             $this->userStats = []; | ||||
|         } | ||||
|     } | ||||
|  | ||||
|     public function generateStatData(): array | ||||
|     { | ||||
|         $startAt = $this->startAt; | ||||
|         $endAt = $this->endAt; | ||||
|         if (!$startAt || !$endAt) { | ||||
|             $startAt = strtotime(date('Y-m-d')); | ||||
|             $endAt = strtotime('+1 day', $startAt); | ||||
|         } | ||||
|         $data = [ | ||||
|             'record_type' => 'd', | ||||
|             'paid_rate' => 0, | ||||
|             'record_at' => $startAt | ||||
|         ]; | ||||
|         $data['order_count'] = Order::where('created_at', '>=', $startAt) | ||||
|             ->where('created_at', '<', $endAt) | ||||
|             ->count(); | ||||
|         $data['order_total'] = Order::where('created_at', '>=', $startAt) | ||||
|             ->where('created_at', '<', $endAt) | ||||
|             ->sum('total_amount'); | ||||
|         $data['paid_count'] = Order::where('paid_at', '>=', $startAt) | ||||
|             ->where('paid_at', '<', $endAt) | ||||
|             ->whereNotIn('status', [0, 2]) | ||||
|             ->count(); | ||||
|         $data['paid_total'] = Order::where('paid_at', '>=', $startAt) | ||||
|             ->where('paid_at', '<', $endAt) | ||||
|             ->whereNotIn('status', [0, 2]) | ||||
|             ->sum('total_amount'); | ||||
|         $commissionLogBuilder = CommissionLog::where('created_at', '>=', $startAt) | ||||
|             ->where('created_at', '<', $endAt); | ||||
|         $data['commission_count'] = $commissionLogBuilder->count(); | ||||
|         $data['commission_amount'] = $commissionLogBuilder->sum('get_amount'); | ||||
|         $data['register_count'] = User::where('created_at', '>=', $startAt) | ||||
|             ->where('created_at', '<', $endAt) | ||||
|             ->count(); | ||||
|         $data['invite_count'] = User::where('created_at', '>=', $startAt) | ||||
|             ->where('created_at', '<', $endAt) | ||||
|             ->whereNotNull('invite_user_id') | ||||
|             ->count(); | ||||
|         $data['transfer_used_total'] = StatServer::where('created_at', '>=', $startAt) | ||||
|                 ->where('created_at', '<', $endAt) | ||||
|                 ->select(DB::raw('SUM(u) + SUM(d) as total')) | ||||
|                 ->value('total') ?? 0; | ||||
|         return $data; | ||||
|     } | ||||
|  | ||||
|     public function statServer($serverId, $serverType, $data) | ||||
|     { | ||||
|         foreach (array_keys($data) as $key) { | ||||
|             $this->serverStats[$serverType] = $this->serverStats[$serverType] ?? []; | ||||
|             if (isset($this->serverStats[$serverType][$serverId])) { | ||||
|                 $this->serverStats[$serverType][$serverId][0] += $data[$key][0]; | ||||
|                 $this->serverStats[$serverType][$serverId][1] += $data[$key][1]; | ||||
|             } else { | ||||
|                 $this->serverStats[$serverType][$serverId] = $data[$key]; | ||||
|             } | ||||
|         } | ||||
|         Cache::set("stat_server_{$this->startAt}", json_encode($this->serverStats)); | ||||
|     } | ||||
|  | ||||
|     public function statUser($rate, $data) | ||||
|     { | ||||
|         foreach (array_keys($data) as $key) { | ||||
| @@ -29,9 +114,24 @@ class StatisticalService { | ||||
|                 $this->userStats[$rate][$key] = $data[$key]; | ||||
|             } | ||||
|         } | ||||
|         Cache::set("stat_user_{$this->recordAt}", json_encode($this->userStats)); | ||||
|         Cache::set("stat_user_{$this->startAt}", json_encode($this->userStats)); | ||||
|     } | ||||
|  | ||||
|     public function getStatUserByUserID($userId): array | ||||
|     { | ||||
|         $stats = []; | ||||
|         foreach (array_keys($this->userStats) as $rate) { | ||||
|             if (!isset($this->userStats[$rate][$userId])) continue; | ||||
|             $stats[] = [ | ||||
|                 'record_at' => $this->startAt, | ||||
|                 'server_rate' => $rate, | ||||
|                 'u' => $this->userStats[$rate][$userId][0], | ||||
|                 'd' => $this->userStats[$rate][$userId][1], | ||||
|                 'user_id' => $userId | ||||
|             ]; | ||||
|         } | ||||
|         return $stats; | ||||
|     } | ||||
|  | ||||
|     public function getStatUser() | ||||
|     { | ||||
| @@ -51,8 +151,32 @@ class StatisticalService { | ||||
|         return $stats; | ||||
|     } | ||||
|  | ||||
|  | ||||
|     public function getStatServer() | ||||
|     { | ||||
|         $stats = []; | ||||
|         foreach ($this->serverStats as $serverType => $v) { | ||||
|             foreach (array_keys($v) as $serverId) { | ||||
|                 if (isset($v[$serverId])) { | ||||
|                     $stats[] = [ | ||||
|                         'server_id' => $serverId, | ||||
|                         'server_type' => $serverType, | ||||
|                         'u' => $v[$serverId][0], | ||||
|                         'd' => $v[$serverId][1], | ||||
|                     ]; | ||||
|                 } | ||||
|             } | ||||
|         } | ||||
|         return $stats; | ||||
|     } | ||||
|  | ||||
|     public function clearStatUser() | ||||
|     { | ||||
|         Cache::forget("stat_user_{$this->recordAt}"); | ||||
|         Cache::forget("stat_user_{$this->startAt}"); | ||||
|     } | ||||
|  | ||||
|     public function clearStatServer() | ||||
|     { | ||||
|         Cache::forget("stat_server_{$this->startAt}"); | ||||
|     } | ||||
| } | ||||
|   | ||||
		Reference in New Issue
	
	Block a user