Files
cunkebao_v3/Server/application/command/SwitchFriendsCommand.php

84 lines
2.5 KiB
PHP
Raw Normal View History

<?php
namespace app\command;
use app\job\WorkbenchAutoLikeJob;
use think\console\Command;
use think\console\Input;
use think\console\input\Option;
use think\console\Output;
use think\facade\Cache;
use think\facade\Log;
use think\Queue;
use app\api\controller\AutomaticAssign;
class SwitchFriendsCommand extends Command
{
// 队列名称
protected $queueName = 'switch_friends';
protected function configure()
{
$this->setName('switch:friends')
->setDescription('切换好友命令');
}
protected function execute(Input $input, Output $output)
{
$cacheKey = 'allotWechatFriend';
$now = time();
$maxRetry = 5;
$retry = 0;
$switchedIds = [];
do {
$friends = Cache::get($cacheKey, []);
$original = $friends;
$toSwitch = [];
foreach ($friends as $friend) {
2025-07-08 09:59:21 +08:00
if (isset($friend['time']) && $friend['time'] < $now) {
$toSwitch[] = $friend;
}
}
if (empty($toSwitch)) {
$output->writeln('没有需要切换的好友');
return;
}
$automaticAssign = new AutomaticAssign();
foreach ($toSwitch as $friend) {
2025-07-08 10:32:37 +08:00
$friendId = !empty($friend['friendId']) ? $friend['friendId'] : $friend['id'];
$automaticAssign->allotWechatFriend([
2025-07-08 10:32:37 +08:00
'wechatFriendId' => $friendId,
'toAccountId' => $friend['accountId'],
], true);
2025-07-08 10:32:37 +08:00
$output->writeln('切换好友:' . $friendId . ' 到账号:' . $friend['accountId']);
$switchedIds[] = $friendId;
}
// 过滤掉已切换的,保留未切换和新进来的
$newFriends = Cache::get($cacheKey, []);
$updated = [];
foreach ($newFriends as $friend) {
2025-07-08 10:32:37 +08:00
$friendId = !empty($friend['friendId']) ? $friend['friendId'] : $friend['id'];
if (!in_array($friendId, $switchedIds)) {
$updated[] = $friend;
}
}
// 按time升序排序
usort($updated, function($a, $b) {
return ($a['time'] ?? 0) <=> ($b['time'] ?? 0);
});
$success = Cache::set($cacheKey, $updated);
$retry++;
} while (!$success && $retry < $maxRetry);
$output->writeln('切换完成,缓存已更新并排序');
}
}