12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091 |
- <?php
- /**
- * Created by: PhpStorm
- * User: lytian
- * Date: 2019/9/24
- * Time: 11:00
- */
- namespace app\admin\command;
- use app\main\service\UserDdFlushService;
- use think\Config;
- use think\console\Command;
- use think\console\Input;
- use think\console\input\Option;
- use think\console\Output;
- use think\Exception;
- use think\Log;
- use think\Request;
- /**
- * 刷新用户的消费记录 刷扣量的历史数据
- * Class UserDdFlushConsume
- * @package app\admin\command
- */
- class UserDdFlushConsume extends Command
- {
- public function configure()
- {
- $this->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("刷用户消费记录 结束");
- }
- }
|