| 1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677 |
- <?php
- /**
- * 统计用户累计数据.
- */
- namespace App\Console\Commands\Statistics;
- use App\Jobs\Statistics\AgentAttrsJob;
- use Carbon\Carbon;
- use Illuminate\Console\Command;
- use App\Models\Mysql\ChannelModel;
- class StatisticsAgentAttrs extends Command
- {
- /**
- * The name and signature of the console command.
- *
- * @var string
- */
- protected $signature = 'statistics:users_attrs {--date=}';
- /**
- * The console command description.
- *
- * @var string
- */
- protected $description = '统计用户累计数据, 可选参数--date=2021-03-04.';
- /**
- * Create a new command instance.
- *
- * @return void
- */
- public function __construct()
- {
- parent::__construct();
- }
- /**
- * Execute the console command.
- *
- * @return mixed
- */
- public function handle()
- {
- $date = $this->getArgsDate();
- // 指派调度统计某1天的所有日活.
- dispatch(new AgentAttrsJob($date));
- $this->comment('['. Carbon::now()->toDateTimeString() .'][statistics:users_attrs]['. $date .']总代每日累计数据统计,丢入队列');
- // 分片处理.
- $chunkPer = 1000;
- // 只能由下而上计算
- ChannelModel::orderBy('id', 'DESC')->chunk($chunkPer, function ($res) use ($date) {
- foreach ($res as $item) {
- if (!empty($item->uid)) {
- dispatch(new AgentAttrsJob($date, $item->uid));
- }
- }
- });
- $this->comment('['. Carbon::now()->toDateTimeString() .'][statistics:users_attrs]['. $date .']单独渠道代理每日累计数据统计,全部丢入队列');
- }
- /**
- * 获取日期参数.
- * @return string
- */
- private function getArgsDate()
- {
- $date = $this->option('date');
- if (empty($date)) {
- // 没有传参数,则默认使用执行脚本服务器环境昨天日期为准.
- $date = Carbon::now()->subDays(1)->toDateString();
- }
- return $date;
- }
- }
|