setName('UserDdFlushConsume') ->addOption('db_index', 'd', Option::VALUE_REQUIRED, 'user库索引 0-511', null) ->setDescription('刷新用户扣量消费记录'); } public function execute(Input $input, Output $output) { Request::instance()->module('admin'); //仅处理有扣量订单用户 try { $num = Config::get('db.user_num'); $dbIndex = $input->getOption('db_index'); 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("Recharge")->setConnect($begin)->field("user_id")->where('dd', 'eq', 1)->group('user_id')->count(); $gone = 0; model("Recharge")->setConnect($begin)->field("user_id")->where('dd', 'eq', 1)->group('user_id')->chunk(100, function ($rows) use ($begin, $total, $gone) { if (count($rows) > 0) { $gone += count($rows); foreach ($rows as $row) { UserDdFlushService::instance()->flushUserConsume($row['user_id']); Log::info("刷用户消费记录 用户:{$row['user_id']} 刷新完成"); } $perent = round($gone / $total, 4) * 100; $index = $begin - 512; echo "刷用户消费记录 用户库ID:{$index}, 运行了{$perent}%, " . time() . PHP_EOL; } else { return false; } }, 'user_id', 'asc'); $begin++; } } catch (Exception $e) { Log::info("刷用户消费记录 异常:". $e->getMessage()); $output->write("刷用户消费记录 异常:". $e->getMessage()); } $output->write("刷用户消费记录 结束"); } }