<?php

namespace App\Console\Commands;

use Illuminate\Console\Command;
use Redis;
use DB;
use Hashids;

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

    /**
     * The console command description.
     *
     * @var string
     */
    protected $description = 'latest three days customer msg';

    /**
     * Create a new command instance.
     *
     * @return void
     */
    public function __construct()
    {
        parent::__construct();
    }

    /**
     * Execute the console command.
     *
     * @return mixed
     */
    public function handle()
    {
        $this->start();
    }

    private function start()
    {
        Redis::del('latestcustomerinfo');
        $sites = redisEnv('CUSTOM_CHAPTER_ORDER_SITES', '');
        if (!$sites) return;
        $sites = explode(',', $sites);
        $result = \App\Modules\OfficialAccount\Models\CustomSendMsgs::where('send_time', '>=', date('Y-m-d H:i:s', time() - 15 * 60 - 3 * 86400))
            ->where('send_time', '<=', date('Y-m-d H:i:s'))
            ->whereIn('distribution_channel_id', $sites)
            ->select('id', 'redirect_url', 'send_time')
            ->get();
        if ($result->isEmpty()) return;
        foreach ($result as $item) {
            if (!$item->redirect_url) continue;
            $url_info = parse_url($item->redirect_url);
            if (isset($url_info['path']) && isset($url_info['query'])) {
                if (strpos('/reader', $url_info['path']) === false &&
                    strpos('/yun', $url_info['path']) === false &&
                    strpos('/detail', $url_info['path']) === false &&
                    strpos('/custom_msgs_page', $url_info['path']) === false
                ) {
                    continue;
                }
                $bid = null;
                if (strpos('/reader', $url_info['path']) !== false) {
                    parse_str($url_info['query'], $param);
                    if (!isset($param['bid'])) {
                        continue;
                    }
                    if ($param['bid'] == 'undefined') continue;
                    $bid_info = Hashids::decode($param['bid']);
                    $bid = isset($bid_info[0]) ? $bid_info[0] : 0;
                }

                if (strpos('/custom_msgs_page', $url_info['path']) !== false) {
                    $temp = urldecode($url_info['query']);
                    $temp_info = parse_url($temp);
                    parse_str($temp_info['query'], $param);
                    if (!isset($param['bid'])) {
                        continue;
                    }
                    if ($param['bid'] == 'undefined') continue;
                    $bid_info = Hashids::decode($param['bid']);
                    $bid = isset($bid_info[0]) ? $bid_info[0] : 0;
                }

                /*if (strpos('/detail',$url_info['path']) !== false) {
                    parse_str($url_info['query'],$param);
                    if (!isset($param['id'])) {
                        continue;
                    }
                    if($param['id'] == 'undefined') continue;
                    $bid_info = \Hashids::decode($param['id']);
                    $bid = isset($bid_info[0])?$bid_info[0]:0;
                }

                if (strpos($url_info['path'],'/yun/') !== false) {
                    $send_order_id = str_replace('/yun/','',$url_info['path']);
                    $send_order_info = \App\Modules\SendOrder\Services\SendOrderService::getById($send_order_id);
                    if($send_order_info){
                        $bid = $send_order_info->book_id;
                    }
                }*/
                if (!$bid) continue;

                Redis::hset('latestcustomerinfo', $item->id, json_encode([
                    'bid' => $bid,
                    'send_time' => $item->send_time
                ]));
            }

        }
    }
}
