<?php

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

namespace App\Jobs;

use App\Modules\Subscribe\Services\OrderService;
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 AsyncOrderStats implements ShouldQueue
{
    use Dispatchable, InteractsWithQueue, Queueable, SerializesModels;

    private $orderId;
    private $order_info = null;

    private $uid = 0;

    public function __construct(int $order_id)
    {
        $this->orderId = $order_id;
    }

    /**
     * Execute the job.
     *
     * @return void
     */
    public function handle()
    {
        $this->init();
        //qkm推送统计
        //$this->qkmPushOrderStats();
        //activity 活动统计
        $this->activityStats();
        //$this->saveActivityOrder();
        //图书充值统计
        $this->bookChargeStats();
    }

    private function init()
    {
        $this->order_info = OrderService::getById($this->orderId);
        $this->uid = $this->order_info->uid;
    }

    private function qkmPushOrderStats()
    {
        $push_id = Redis::hget('book_read:' . $this->uid, 'qkm_push');
        if (!$push_id) return;
        $push_time = Redis::hget('qkm_push:time', $push_id);
        if (empty($push_time) || !is_numeric($push_time)) return;
        $time = ceil((time() - $push_time) / 86400);
        if ($time > 0 && $time <= 31) {
            $price = $this->order_info->price * 100;
            Redis::hincrby('qkm_push:amount', $push_id, $price);
        }
    }

    private function activityStats()
    {
        $item = $this->order_info;
        if ($item->status != 'PAID') return;
        if (!$item->activity_id) return;
        $day = date('Y-m-d', strtotime($item->created_at));
        $exist = DB::table('activity_statistic_all')
            ->where('activity_id', $item->activity_id)
            ->where('day', $day)
            ->select('id', 'amount')
            ->where('distribution_channel_id', $item->distribution_channel_id)->first();
        if ($exist) {
            DB::table('activity_statistic_all')->where('id', $exist->id)->update(
                [
                    'amount' => $exist->amount + $item->price * 100,
                    'updated_at' => date('Y-m-d H:i:s')
                ]
            );
            return;
        }
        $pv = 0;
        $uv = 0;
        DB::table('activity_statistic_all')->insert([
            'activity_id' => $item->activity_id, 'day' => $day, 'distribution_channel_id' => $item->distribution_channel_id,
            'amount' => $item->price * 100, 'pv' => $pv, 'uv' => $uv, 'created_at' => date('Y-m-d H:i:s'), 'updated_at' => date('Y-m-d H:i:s')
        ]);
    }

    private function saveActivityOrder()
    {
        if ($this->order_info->status != 'PAID') return;
        if (!$this->order_info->activity_id) return;
        DB::table('activity_order')->insert([
            'order_id' => $this->orderId,
            'activity_id' => $this->order_info->activity_id,
            'distribution_channel_id' => $this->order_info->distribution_channel_id,
            'created_at' => $this->order_info->created_at,
            'updated_at' => date('Y-m-d H:i:s')
        ]);
    }

    private function bookChargeStats(){
        $bid = $this->order_info->from_bid;
        if(!$bid) return ;
        $day = date('Y-m-d', strtotime($this->order_info->created_at));
        $price = $this->order_info->price * 100;
        Redis::hincrby('book:charge:stats:'.$day, $bid, $price);
    }
}
