<?php

/**
 * Created by PhpStorm.
 * User: z-yang
 * Date: 2020/5/4
 * Time: 16:11
 */

namespace App\Jobs;

use GuzzleHttp\Client;
use Illuminate\Bus\Queueable;
use Illuminate\Queue\SerializesModels;
use Illuminate\Queue\InteractsWithQueue;
use Illuminate\Contracts\Queue\ShouldQueue;
use Illuminate\Foundation\Bus\Dispatchable;
use Redis;
use DB;

class AsyncStats implements ShouldQueue
{
    use Dispatchable, InteractsWithQueue, Queueable, SerializesModels;

    private $param = [];
    private $uid;

    private $distribution_channel_id;

    private $task_name;

    public function __construct($param,  $uid, $distribution_channel_id, $task_name)
    {
        $this->param = $param;
        $this->uid = $uid;
        $this->distribution_channel_id = $distribution_channel_id;
        $this->task_name = $task_name;
    }

    /**
     * Execute the job.
     *
     * @return void
     */
    public function handle()
    {
        $this->qkmPush();
    }

    private function qkmPush()
    {
        if (!isset($this->param['qkm_push']) || empty($this->param['qkm_push'])) {
            return;
        }
        if (!isset($this->param['qkm_push_time']) || empty($this->param['qkm_push_time'])) {
            return;
        }
        if (!$this->uid) return;
        $push_id = $this->param['qkm_push'];
        //保存时间
        $send_time = $this->param['qkm_push_time'];

        $time = ceil((time() - $this->param['qkm_push_time']) / 86400);
        if ($time < 0 || $time >= 31) {
            return;
        }
        if ($time <= 30 && $time > 0) {
            Redis::hset('qkm_push:time', $push_id, $send_time);
            Redis::sadd('qkm_push:ids', $push_id);

            Redis::sadd('qkm_push:uv:' . $push_id, $this->uid);
            Redis::hset('book_read:' . $this->uid, 'qkm_push', $this->param['qkm_push']);
        }
    }
}
