| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111 |
- <?php
- /**
- * 统计用户今日数据.
- */
- namespace App\Console\Commands\Statistics;
- use App\Jobs\Statistics\AgentAttrsTodayJob;
- use App\Models\Mongo\PayLogsModel;
- use App\Models\Mongo\UsersModel;
- use App\Repositories\Statistics\StatisticsAgentAttrsRepository;
- use App\Repositories\Statistics\StatisticsCommonRepository;
- use Carbon\Carbon;
- use Illuminate\Console\Command;
- use App\Models\Mysql\ChannelModel;
- class StatisticsAgentAttrsToday extends Command
- {
- /**
- * The name and signature of the console command.
- *
- * @var string
- */
- protected $signature = 'statistics:users_attrs_today {--date=}';
- /**
- * The console command description.
- *
- * @var string
- */
- protected $description = '统计用户累计数据, 可选参数--date=2021-03-04.';
- /**
- * 统计通用仓库.
- * @var null|StatisticsCommonRepository
- */
- protected $commonRepo = null;
- /**
- * Create a new command instance.
- *
- * @return void
- */
- public function __construct()
- {
- parent::__construct();
- }
- /**
- * Execute the console command.
- *
- * @return mixed
- */
- public function handle()
- {
- $date = $this->getArgsDate();
- $nowDateObj = Carbon::now();
- if ($nowDateObj->hour == 0 && $nowDateObj->isToday()) {
- // 凌晨00点,就跑昨日的. 解决昨日23~24点数据没统计.
- $date = $nowDateObj->subDays(1)->toDateString();
- }
- $repo = new StatisticsAgentAttrsRepository();
- $repo->initTodayMongo($date); // 清空今天的日志.
- // 指派调度统计某1天的所有用户数据.
- dispatch(new AgentAttrsTodayJob($date));
- logger('StatisticsAgentAttrsToday', ['comment' => '['. Carbon::now()->toDateTimeString() .'][statistics:users_attrs_today]['. $date .']官方今日数据统计,丢入队列']);
- $this->comment('['. Carbon::now()->toDateTimeString() .'][statistics:users_attrs_today]['. $date .']官方今日数据统计,丢入队列');
- $channelUids = $this->getCommonRepo()->getValidChannelUidsByDate($date);
- if (!empty($channelUids)) {
- foreach ($channelUids as $channelUid) {
- // 调度异步任务.
- dispatch(new AgentAttrsTodayJob($date, $channelUid));
- }
- } else {
- logger('StatisticsAgentAttrsToday', ['msg' => 'channelUids empty', 'channelUids' => $channelUids]);
- }
- logger('StatisticsAgentAttrsToday', ['comment' => '['. Carbon::now()->toDateTimeString() .'][statistics:users_attrs_today]['. $date .']单独渠道代理今日数据统计,全部丢入队列']);
- $this->comment('['. Carbon::now()->toDateTimeString() .'][statistics:users_attrs_today]['. $date .']单独渠道代理今日数据统计,全部丢入队列');
- }
- /**
- * 获取日期参数.
- * @return string
- */
- private function getArgsDate()
- {
- $date = $this->option('date');
- if (empty($date)) {
- // 没有传参数, 拿今天的
- $date = Carbon::now()->toDateString();
- }
- return $date;
- }
- /**
- * @return StatisticsCommonRepository|\Illuminate\Contracts\Foundation\Application|mixed|null
- */
- private function getCommonRepo()
- {
- if (!$this->commonRepo instanceof StatisticsCommonRepository) {
- $this->commonRepo = app(StatisticsCommonRepository::class);
- }
- return $this->commonRepo;
- }
- }
|