first(); Log::info("查询任务开始"); // 获取账号列表 $res = Rocketgo::account_list(); $usernames = array_column($res, 'username'); // 批量查询用户信息 $phones = UserPhone::whereIn('phone', $usernames)->get()->keyBy('phone'); $currentTimestamp = time(); $updateData = []; $updateData2 = []; // 批量处理用户信息 foreach ([$res, Rocketgo::account_storehouse_list()] as $index => $accountList) { $phoneMap = ($index === 0) ? $phones : UserPhone::whereIn('phone', array_column($accountList, 'username'))->get()->keyBy('phone'); $allUsernames = array_column($accountList, 'username'); foreach ($accountList as $v) { $phone = $phoneMap[$v['username']] ?? null; if (!$phone) { continue; } // 处理在线和离线状态 if ($v['logged'] == 1) { self::processOnlineStatus($phone, $v, $currentTimestamp, $updateData); } else { self::processOfflineStatus($phone, $currentTimestamp, $updateData); } // 更新状态 if ($index === 0) { $updateData[] = $phone; } else { $updateData2[] = $phone; } } // 更新离线用户状态 UserPhone::whereNotIn('phone', $allUsernames) ->where('status', '!=', 2) ->update(['status' => 2, 'last_time' => $currentTimestamp]); UserPhone::where('status', 2)->update(['last_time' => $currentTimestamp, 'time' => 0]); } // 批量更新用户状态 self::batchUpdatePhones($updateData); self::batchUpdatePhones($updateData2); $end_time = time(); Log::info("Task2: Processed " . count($usernames) . " phones in " . ($end_time - $start_time) . " seconds"); } /** * 处理用户在线状态 */ private static function processOnlineStatus($phone, $v, $currentTimestamp, &$updateData) { if ($phone->time > 3600) { // 计算超过 1 小时的积分 $hours = intdiv($phone->time, 3600); $score = $hours * 20; $remainingTime = $phone->time % 3600; // 更新用户积分和时长 $phone->score += $score; $phone->day_score += $score; $phone->time = $remainingTime; // 保存用户收益到 UserReward 表 UserRewardDao::Onhookincome($phone->user_id, $score, $phone->phone); } // 保存在线时长记录 UserPhoneLogDao::setOnlineTimeByPhone($phone->phone, 1, $currentTimestamp); $phone->status = 1; $phone->time += $currentTimestamp - $phone->last_time; $phone->last_time = $currentTimestamp; $updateData[] = $phone; } /** * 处理用户离线状态 */ private static function processOfflineStatus($phone, $currentTimestamp, &$updateData) { $phone->status = 0; $phone->last_time = $currentTimestamp; // 保存在线时长记录 UserPhoneLogDao::setOnlineTimeByPhone($phone->phone, 0, $currentTimestamp); $updateData[] = $phone; } /** * 批量更新用户状态 */ private static function batchUpdatePhones($phones) { // 如果用户列表不为空,批量更新 if (!empty($phones)) { UserPhone::whereIn('id', array_column($phones, 'id'))->update([ 'status' => 1, 'time' => $phones[0]->time, // 更新时间等信息 'last_time' => $phones[0]->last_time, ]); } } }