statUser(); $this->statServer(); $this->stat(); $this->info('耗时' . (microtime(true) - $startAt)); } private function statServer() { $createdAt = time(); $recordAt = strtotime('-1 day', strtotime(date('Y-m-d'))); $statService = new StatisticalService(); $statService->setStartAt($recordAt); $stats = $statService->getStatUser(); DB::beginTransaction(); foreach ($stats as $stat) { if (!StatServer::insert([ 'server_id' => $stat['server_id'], 'server_type' => $stat['server_type'], 'u' => $stat['u'], 'd' => $stat['d'], 'created_at' => $createdAt, 'updated_at' => $createdAt, 'record_type' => 'd', 'record_at' => $recordAt ])) { DB::rollback(); throw new \Exception('stat server fail'); } } DB::commit(); $statService->clearStatServer(); } private function statUser() { $createdAt = time(); $recordAt = strtotime('-1 day', strtotime(date('Y-m-d'))); $statService = new StatisticalService(); $statService->setStartAt($recordAt); $stats = $statService->getStatUser(); DB::beginTransaction(); foreach ($stats as $stat) { if (!StatUser::insert([ 'user_id' => $stat['user_id'], 'u' => $stat['u'], 'd' => $stat['d'], 'server_rate' => $stat['server_rate'], 'created_at' => $createdAt, 'updated_at' => $createdAt, 'record_type' => 'd', 'record_at' => $recordAt ])) { DB::rollback(); throw new \Exception('stat user fail'); } } DB::commit(); $statService->clearStatUser(); } private function stat() { $startAt = strtotime('-1 day', strtotime(date('Y-m-d'))); $statisticalService = new StatisticalService(); $statisticalService->setRecordAt($startAt); $data = $statisticalService->generateStatData(); $statistic = Stat::where('record_at', $startAt) ->where('record_type', 'd') ->first(); if ($statistic) { $statistic->update($data); return; } Stat::create($data); } }