StatisticsAgentAttrsToday.php 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111
  1. <?php
  2. /**
  3. * 统计用户今日数据.
  4. */
  5. namespace App\Console\Commands\Statistics;
  6. use App\Jobs\Statistics\AgentAttrsTodayJob;
  7. use App\Models\Mongo\PayLogsModel;
  8. use App\Models\Mongo\UsersModel;
  9. use App\Repositories\Statistics\StatisticsAgentAttrsRepository;
  10. use App\Repositories\Statistics\StatisticsCommonRepository;
  11. use Carbon\Carbon;
  12. use Illuminate\Console\Command;
  13. use App\Models\Mysql\ChannelModel;
  14. class StatisticsAgentAttrsToday extends Command
  15. {
  16. /**
  17. * The name and signature of the console command.
  18. *
  19. * @var string
  20. */
  21. protected $signature = 'statistics:users_attrs_today {--date=}';
  22. /**
  23. * The console command description.
  24. *
  25. * @var string
  26. */
  27. protected $description = '统计用户累计数据, 可选参数--date=2021-03-04.';
  28. /**
  29. * 统计通用仓库.
  30. * @var null|StatisticsCommonRepository
  31. */
  32. protected $commonRepo = null;
  33. /**
  34. * Create a new command instance.
  35. *
  36. * @return void
  37. */
  38. public function __construct()
  39. {
  40. parent::__construct();
  41. }
  42. /**
  43. * Execute the console command.
  44. *
  45. * @return mixed
  46. */
  47. public function handle()
  48. {
  49. $date = $this->getArgsDate();
  50. $nowDateObj = Carbon::now();
  51. if ($nowDateObj->hour == 0 && $nowDateObj->isToday()) {
  52. // 凌晨00点,就跑昨日的. 解决昨日23~24点数据没统计.
  53. $date = $nowDateObj->subDays(1)->toDateString();
  54. }
  55. $repo = new StatisticsAgentAttrsRepository();
  56. $repo->initTodayMongo($date); // 清空今天的日志.
  57. // 指派调度统计某1天的所有用户数据.
  58. dispatch(new AgentAttrsTodayJob($date));
  59. logger('StatisticsAgentAttrsToday', ['comment' => '['. Carbon::now()->toDateTimeString() .'][statistics:users_attrs_today]['. $date .']官方今日数据统计,丢入队列']);
  60. $this->comment('['. Carbon::now()->toDateTimeString() .'][statistics:users_attrs_today]['. $date .']官方今日数据统计,丢入队列');
  61. $channelUids = $this->getCommonRepo()->getValidChannelUidsByDate($date);
  62. if (!empty($channelUids)) {
  63. foreach ($channelUids as $channelUid) {
  64. // 调度异步任务.
  65. dispatch(new AgentAttrsTodayJob($date, $channelUid));
  66. }
  67. } else {
  68. logger('StatisticsAgentAttrsToday', ['msg' => 'channelUids empty', 'channelUids' => $channelUids]);
  69. }
  70. logger('StatisticsAgentAttrsToday', ['comment' => '['. Carbon::now()->toDateTimeString() .'][statistics:users_attrs_today]['. $date .']单独渠道代理今日数据统计,全部丢入队列']);
  71. $this->comment('['. Carbon::now()->toDateTimeString() .'][statistics:users_attrs_today]['. $date .']单独渠道代理今日数据统计,全部丢入队列');
  72. }
  73. /**
  74. * 获取日期参数.
  75. * @return string
  76. */
  77. private function getArgsDate()
  78. {
  79. $date = $this->option('date');
  80. if (empty($date)) {
  81. // 没有传参数, 拿今天的
  82. $date = Carbon::now()->toDateString();
  83. }
  84. return $date;
  85. }
  86. /**
  87. * @return StatisticsCommonRepository|\Illuminate\Contracts\Foundation\Application|mixed|null
  88. */
  89. private function getCommonRepo()
  90. {
  91. if (!$this->commonRepo instanceof StatisticsCommonRepository) {
  92. $this->commonRepo = app(StatisticsCommonRepository::class);
  93. }
  94. return $this->commonRepo;
  95. }
  96. }