<?php


namespace App\Console\Commands\Push;


use App\Cache\Push\PushCache;
use App\Modules\Push\Models\QappPushTask;
use Illuminate\Console\Command;

class PushStats extends Command
{
    /**
     * The name and signature of the console command.
     *
     * @var string
     */
    protected $signature = 'push:stats';

    /**
     * The console command description.
     *
     * @var string
     */
    protected $description = '推送PVUV统计';

    public function handle()
    {
        $date = date('Ymd', strtotime('-1 day'));

        // 获取所有有效的推送任务
        $tasks = QappPushTask::getAllValidTasks();
        if (empty($tasks)) {
            return false;
        }

        foreach ($tasks as $task) {
            $pushId = getProp($task, 'id');
            $pv     = getProp($task, 'pv');
            $uv     = getProp($task, 'uv');

            // 获取缓存数据
            $cachePv = PushCache::getPushyPv($pushId, $date);
            $cacheUv = PushCache::getPushUv($pushId, $date);

            if(!empty($cachePv) || !empty($cacheUv)){
                myLog('pushStats')->info('', compact('date', 'pushId', 'pv', 'uv', 'cachePv', 'cacheUv'));

                // 更新pv uv
                QappPushTask::updatePushTask($pushId, [
                    'pv' => $pv + $cachePv,
                    'uv' => $uv + $cacheUv,
                ]);
            }
        }

        return true;
    }
}