StatisticsNewUsers.php 2.3 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980
  1. <?php
  2. /**
  3. * 统计新增用户相关.
  4. */
  5. namespace App\Console\Commands\Statistics;
  6. use App\Jobs\Statistics\NewUsersChannelJob;
  7. use Carbon\Carbon;
  8. use Illuminate\Console\Command;
  9. use App\Models\Mysql\ChannelModel;
  10. class StatisticsNewUsers extends Command
  11. {
  12. /**
  13. * The name and signature of the console command.
  14. *
  15. * @var string
  16. */
  17. protected $signature = 'statistics:new_users {--date=}';
  18. /**
  19. * The console command description.
  20. *
  21. * @var string
  22. */
  23. protected $description = '统计新增用户相关, 可选参数--date=2021-03-04.';
  24. /**
  25. * Create a new command instance.
  26. *
  27. * @return void
  28. */
  29. public function __construct()
  30. {
  31. parent::__construct();
  32. }
  33. /**
  34. * Execute the console command.
  35. *
  36. * @return mixed
  37. */
  38. public function handle()
  39. {
  40. $date = $this->getArgsDate();
  41. // 指派调度统计某1天的所有新增用户.
  42. dispatch(new NewUsersChannelJob($date));
  43. $this->comment('['. Carbon::now()->toDateTimeString() .'][statistics:new_users]['. $date .']所有每日新增用户统计,丢入队列');
  44. // 指派调度统计某1天的官方新增用户.
  45. dispatch(new NewUsersChannelJob($date, ''));
  46. $this->comment('['. Carbon::now()->toDateTimeString() .'][statistics:new_users]['. $date .']官方每日新增用户统计,丢入队列');
  47. // 分片处理.
  48. $chunkPer = 1000;
  49. ChannelModel::orderBy('id', 'ASC')->chunk($chunkPer, function ($res) use ($date) {
  50. foreach ($res as $item) {
  51. if (!empty($item->uid)) {
  52. dispatch(new NewUsersChannelJob($date, $item->uid));
  53. }
  54. }
  55. });
  56. $this->comment('['. Carbon::now()->toDateTimeString() .'][statistics:new_users]['. $date .']单独渠道代理每日新增用户统计,全部丢入队列');
  57. }
  58. /**
  59. * 获取日期参数.
  60. * @return string
  61. */
  62. private function getArgsDate()
  63. {
  64. $date = $this->option('date');
  65. if (empty($date)) {
  66. // 没有传参数,则默认使用执行脚本服务器环境昨天日期为准.
  67. $date = Carbon::now()->subDays(1)->toDateString();
  68. }
  69. return $date;
  70. }
  71. }