onQueue('stat'); //$this->u = $u; //$this->d = $d; //$this->userId = $userId; $this->data =$data; $this->server = $server; $this->protocol = $protocol; $this->recordType = $recordType; } /** * Execute the job. * * @return void */ public function handle() { $recordAt = strtotime(date('Y-m-d')); if ($this->recordType === 'm') { // } try { DB::beginTransaction(); foreach(array_keys($this->data) as $userId){ $userdata = StatUser::where('record_at', $recordAt) ->where('server_rate', $this->server['rate']) ->where('user_id', $userId) ->lockForUpdate()->first(); if ($userdata) { $userdata->update([ 'u' => $userdata['u'] + ($this->data[$userId][0] * $this->server['rate']), 'd' => $userdata['d'] + ($this->data[$userId][1] * $this->server['rate']) ]); } else { $insertData[] = [ 'user_id' => $userId, 'server_rate' => $this->server['rate'], 'u' => $this->data[$userId][0], 'd' => $this->data[$userId][1], 'record_type' => $this->recordType, 'record_at' => $recordAt ]; } } if (!empty($insertData)) { StatUser::upsert($insertData, ['user_id', 'server_rate', 'record_at']); } DB::commit(); } catch (\Exception $e) { DB::rollback(); abort(500, '用户统计数据失败'. $e->getMessage()); } } }