<?php

namespace App\Console\Commands\User;

use App\Modules\Book\Models\Book;
use App\Modules\Book\Models\BookCategory;
use App\Modules\Book\Models\NovelUserPorperty;
use App\Modules\OfficialAccount\Models\TempForceSubscribeUsers;
use App\Modules\SendOrder\Models\SendOrder;
use App\Modules\User\Models\User;
use Illuminate\Console\Command;

class SetUserNovelPorperty extends Command
{
    /**
     * The name and signature of the console command.
     *
     * @var string
     */
    protected $signature = 'set_user_novel_property {--is_init=}';

    /**
     * The console command description.
     *
     * @var string
     */
    protected $description = '设置小说用户属性';

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

    /**
     * 书籍分类
     */
    private $categorys;

    /**
     * Execute the console command.
     *
     * @return mixed
     */
    public function handle()
    {
        $this->categorys = BookCategory::all();
        $is_init = $this->option('is_init');
        if ($is_init) {
            $this->runAllUsers();
        } else {
            myLog('test')->info('set_user_novel_property begin: ' . now());
            $users = $this->findUsers();
            foreach ($users as $user) {
                $property =  $this->findUserProperty($user->uid);
                if ($property) {
                    $this->createNovelUserPorperty($user->uid, $property);
                }
            }
            myLog('test')->info('set_user_novel_property end: ' . now());
        }
    }

    private function findUsers()
    {
        $model = NovelUserPorperty::model(date('Ymd'));
        $max_id = $model->max('id');
        $user = $model->find($max_id);
        $max = TempForceSubscribeUsers::where('uid', $user->uid)->first();
        return TempForceSubscribeUsers::where('id', '>', $max->id)
            ->where('uid', '>', 0)
            ->where('last_interactive_time', '>=', date('Y-m-d H:i:s', strtotime('-1 hours -20 seconds')))
            ->select('uid')
            ->get();
    }

    private function runAllUsers()
    {
        $max_ids = TempForceSubscribeUsers::max('id');
        $i = 0;
        myLog('test')->info('set_user_novel_property begin: ' . now());
        while ($i <= $max_ids) {
            $users = TempForceSubscribeUsers::where('id', '<=', $i + 3000)
                ->where('id', '>', $i)
                ->where('uid', '>', 0)
                ->select('uid')
                ->get();
            foreach ($users as $user) {
                $property =  $this->findUserProperty($user->uid);
                if ($property) {
                    $this->createNovelUserPorperty($user->uid, $property);
                }
            }
            $i += 3000;
            myLog('test')->info('set_user_novel_property id: ' . $i);
        }
        myLog('test')->info('set_user_novel_property end: ' . now());
    }

    private function findUserProperty(int $uid)
    {
        $user = User::where('id', $uid)->select('send_order_id')->first();
        if ($user) {
            $send_order_id = $user->send_order_id;
            $send_order = SendOrder::where('id', $send_order_id)->select('book_id')->first();
            if ($send_order) {
                $book_id = $send_order->book_id;
                $book = Book::where('id', $book_id)->select('category_id')->first();
                if ($book) {
                    $category_id = $book->category_id;
                    $category = $this->categorys->where('id', $category_id)->first();
                    return $category;
                }
            }
        }
    }

    private function createNovelUserPorperty(int $uid, BookCategory $property)
    {
        for ($i = 0; $i < 3; $i++) {
            if ($i) {
                $date = date('Ymd', strtotime('+' . $i . ' days'));
            } else {
                $date = date('Ymd');
            }
            NovelUserPorperty::model($date)->updateOrCreate([
                'uid' => $uid
            ], [
                'category_id' => $property->id,
                'category' => $property->category_name,
                'sex' => $property->channel_name == '女频' ? 2 : 1,
            ]);
        }
    }
}
