UserDdFlushConsume.php 2.8 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091
  1. <?php
  2. /**
  3. * Created by: PhpStorm
  4. * User: lytian
  5. * Date: 2019/9/24
  6. * Time: 11:00
  7. */
  8. namespace app\admin\command;
  9. use app\main\service\UserDdFlushService;
  10. use think\Config;
  11. use think\console\Command;
  12. use think\console\Input;
  13. use think\console\input\Option;
  14. use think\console\Output;
  15. use think\Exception;
  16. use think\Log;
  17. use think\Request;
  18. /**
  19. * 刷新用户的消费记录 刷扣量的历史数据
  20. * Class UserDdFlushConsume
  21. * @package app\admin\command
  22. */
  23. class UserDdFlushConsume extends Command
  24. {
  25. public function configure()
  26. {
  27. $this->setName('UserDdFlushConsume')
  28. ->addOption('db_index', 'd', Option::VALUE_REQUIRED, 'user库索引 0-511', null)
  29. ->setDescription('刷新用户扣量消费记录');
  30. }
  31. public function execute(Input $input, Output $output)
  32. {
  33. Request::instance()->module('admin');
  34. //仅处理有扣量订单用户
  35. try {
  36. $num = Config::get('db.user_num');
  37. $dbIndex = $input->getOption('db_index');
  38. if ($dbIndex == null) {
  39. //没传即全部
  40. $end = $num - 1;
  41. $dbIndex = "0,{$end}";
  42. }
  43. $dbIndexs = explode(',', $dbIndex);
  44. if (count($dbIndexs) !=2) {
  45. //只传开始值
  46. $begin = $end = $dbIndexs[0];
  47. } else {
  48. list($begin, $end) = $dbIndexs;
  49. }
  50. $begin += $num;
  51. $end += $num;
  52. while ($begin <= $end) {
  53. $total = model("Recharge")->setConnect($begin)->field("user_id")->where('dd', 'eq', 1)->group('user_id')->count();
  54. $gone = 0;
  55. model("Recharge")->setConnect($begin)->field("user_id")->where('dd', 'eq', 1)->group('user_id')->chunk(100, function ($rows) use ($begin, $total, $gone) {
  56. if (count($rows) > 0) {
  57. $gone += count($rows);
  58. foreach ($rows as $row) {
  59. UserDdFlushService::instance()->flushUserConsume($row['user_id']);
  60. Log::info("刷用户消费记录 用户:{$row['user_id']} 刷新完成");
  61. }
  62. $perent = round($gone / $total, 4) * 100;
  63. $index = $begin - 512;
  64. echo "刷用户消费记录 用户库ID:{$index}, 运行了{$perent}%, " . time() . PHP_EOL;
  65. } else {
  66. return false;
  67. }
  68. }, 'user_id', 'asc');
  69. $begin++;
  70. }
  71. } catch (Exception $e) {
  72. Log::info("刷用户消费记录 异常:". $e->getMessage());
  73. $output->write("刷用户消费记录 异常:". $e->getMessage());
  74. }
  75. $output->write("刷用户消费记录 结束");
  76. }
  77. }