setName('UserDdFlushRecharge') ->addOption('db_index', 'd', Option::VALUE_REQUIRED, 'user库索引 0-511', null) ->setDescription('刷新用户充值记录剩余永久书币'); } public function execute(Input $input, Output $output) { Request::instance()->module('admin'); $dbIndex = $input->getOption('db_index'); $num = Config::get('db.user_num'); $redis = Redis::instance(); try { if ($dbIndex == null) { //没传即全部 $end = $num - 1; $dbIndex = "0,{$end}"; } $dbIndexs = explode(',', $dbIndex); if (count($dbIndexs) !=2) { //只传开始值 $begin = $end = $dbIndexs[0]; } else { list($begin, $end) = $dbIndexs; } $begin += $num; $end += $num; while ($begin <= $end) { $total = model("User")->setConnect($begin)->where('flush_state', 'eq', 0)->count(); $gone = 0; model("User")->setConnect($begin)->where('flush_state', 'eq', 0)->chunk(100, function ($rows) use ($redis, $begin, $total, &$gone) { if (count($rows) > 0) { $gone += count($rows); foreach ($rows as $row) { //需要刷充值记录表剩余多少永久书币 if ($row['kandian'] > 0) { UserDdFlushService::instance()->flushUserRechargeRemainKandian($row['id'], $row['kandian']); Log::info("刷新永久书币 用户{$row['id']} 刷新脚本状态成功"); } $row->save(['flush_state' => 1]); //清理缓存 $redis->del("UN:".$row['id']); } $perent = round($gone / $total, 4) * 100; $index = $begin - 512; echo "刷新永久书币 用户库ID:{$index}, 运行了{$perent}%, " . time() . PHP_EOL; unset($rows); } else { return false; } }, 'id', 'asc'); $begin++; } } catch (Exception $e) { Log::info("刷新永久书币 异常:". $e->getMessage()); $output->write("刷新永久书币 异常:". $e->getMessage()); } $output->write("刷新永久书币 结束"); } }