StatisticsAgentAttrs.php 2.0 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677
  1. <?php
  2. /**
  3. * 统计用户累计数据.
  4. */
  5. namespace App\Console\Commands\Statistics;
  6. use App\Jobs\Statistics\AgentAttrsJob;
  7. use Carbon\Carbon;
  8. use Illuminate\Console\Command;
  9. use App\Models\Mysql\ChannelModel;
  10. class StatisticsAgentAttrs extends Command
  11. {
  12. /**
  13. * The name and signature of the console command.
  14. *
  15. * @var string
  16. */
  17. protected $signature = 'statistics:users_attrs {--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 AgentAttrsJob($date));
  43. $this->comment('['. Carbon::now()->toDateTimeString() .'][statistics:users_attrs]['. $date .']总代每日累计数据统计,丢入队列');
  44. // 分片处理.
  45. $chunkPer = 1000;
  46. // 只能由下而上计算
  47. ChannelModel::orderBy('id', 'DESC')->chunk($chunkPer, function ($res) use ($date) {
  48. foreach ($res as $item) {
  49. if (!empty($item->uid)) {
  50. dispatch(new AgentAttrsJob($date, $item->uid));
  51. }
  52. }
  53. });
  54. $this->comment('['. Carbon::now()->toDateTimeString() .'][statistics:users_attrs]['. $date .']单独渠道代理每日累计数据统计,全部丢入队列');
  55. }
  56. /**
  57. * 获取日期参数.
  58. * @return string
  59. */
  60. private function getArgsDate()
  61. {
  62. $date = $this->option('date');
  63. if (empty($date)) {
  64. // 没有传参数,则默认使用执行脚本服务器环境昨天日期为准.
  65. $date = Carbon::now()->subDays(1)->toDateString();
  66. }
  67. return $date;
  68. }
  69. }