<?php

namespace App\Modules\Book\Services;

use App\Modules\BaseService;
use App\Modules\Book\Models\Book;
use App\Modules\Book\Models\Chapter;
use Hashids;
use Redis;
use App\Modules\Statistic\Services\WapVisitStatService;
use DB;

/**
 * 
 * @method static int decodeBidStatic(string $bid) 解析bid
 */
class BookService
{
    use BaseService;

    public function decodeBid(string $bid)
    {
        return Hashids::decode($bid)[0];
    }

    /**
     * 获取book中存在的分类信息id
     * @return array
     */
    public static  function getCategoryId()
    {
        return Book::getCategoryId();
    }

    /**
     * 修改图书描述
     * @param $bid
     * @param $intro
     * @return mixed
     *
     */
    public  static function  updateIntro($bid, $intro)
    {
        return Book::where('id', $bid)->update(['intro' => $intro]);
    }

    /**
     * 设置章节价格
     * @param int $channel_id
     * @param int $price
     */
    public static function setChapterPrice(int $channel_id, float $price): void
    {
        try {
            Redis::hset('book_chapter_price', $channel_id, $price);
        } catch (\Exception $e) { }
    }

    /**
     * 获取渠道设置的价格
     * @param int $channel_id
     * @return int
     */
    public static function getChapterPrice(int $channel_id)
    {
        try {
            $fee =  Redis::hget('book_chapter_price', $channel_id);
            if ($fee)
                return (float) $fee;
            else
                return 0;
        } catch (\Exception $e) { }
        return 0;
    }

    public static function getBookStatistics($smart_push_books)
    {
        // 获取书籍统计数据
        if (!empty($smart_push_books)) {
            foreach ($smart_push_books as $key => $smart_push_book) {
                $book_statistics = WapVisitStatService::smartPushTestBookStats($smart_push_book->bid);
                $smart_push_books[$key]->uv = $book_statistics['uv'];
                $smart_push_books[$key]->pv = $book_statistics['pv'];
                $smart_push_books[$key]->charge_amount = $book_statistics['charge_amount'];
                $smart_push_books[$key]->charge_user_num = $book_statistics['charge_user_num'];
                $smart_push_books[$key]->book_amount = $book_statistics['book_amount'];
                $smart_push_books[$key]->book_user_num = $book_statistics['book_user_num'];
                $smart_push_books[$key]->real_push_user_num = $book_statistics['real_push_user_num'];
                $smart_push_books[$key]->second_chapter_uv = $book_statistics['second_chapter_uv'];
            }
        }
        return $smart_push_books;
    }

    public static function getBookById($bid)
    {
        return Book::find($bid);
    }

    public static function newYunQiBook($bid)
    {
        $old = DB::table('book_yunqi')->where('yq_bid', $bid)->where('type', 'NEW_YUNQI')->first();
        if ($old) {
            return -1;
        }
        $new_yunqi_book = DB::connection('new_yunqi')
            ->table('books')
            ->join('book_configs', 'books.id', '=', 'book_configs.bid')
            ->select(
                'books.id',
                'books.author',
                'books.author',
                'books.intro',
                'books.category_name',
                'books.keyword',
                'books.status',
                'books.chapter_count',
                'books.size',
                'books.last_chapter',
                'book_configs.book_name as name',
                'book_configs.cover',
                'book_configs.force_subscribe_chapter_seq',
                'book_configs.charge_type',
                'book_configs.roles'
            )
            ->where('books.id', $bid)
            ->first();
        if (!$new_yunqi_book) {
            return -2;
        }

        $book = Book::create(
            [
                'ly_bid' => 0, 'name' => $new_yunqi_book->name, 'author' => $new_yunqi_book->author, 'intro' => $new_yunqi_book->intro, 'cover' => $new_yunqi_book->cover,
                'category_name' => $new_yunqi_book->category_name, 'keyword' => $new_yunqi_book->keyword, 'category_id' => 0, 'status' => $new_yunqi_book->status,
                'chapter_count' => $new_yunqi_book->chapter_count, 'first_cid' => 0, 'last_cid' => 0, 'size' => $new_yunqi_book->size, 'last_chapter' => $new_yunqi_book->last_chapter,
                'sequence' => 0, 'yq_bid' => $bid
            ]
        );
        DB::table('book_yunqi')->insert([
            'bid' => $book->id,
            'yq_bid' => $bid,
            'type' => 'NEW_YUNQI',
            'created_at' => date('Y-m-d H:i:s'),
            'updated_at' => date('Y-m-d H:i:s')
        ]);
        for ($i = 1; $i <= $new_yunqi_book->chapter_count; $i++) {
            $temp = DB::connection('new_yunqi')->table('chapters')->where('bid', $bid)->where('sequence', $i)->select('name', 'content', 'is_vip', 'size')->first();
            if ($temp) {
                Chapter::create([
                    'bid' => $book->id,
                    'name' => $temp->name,
                    'sequence' => $i,
                    'is_vip' => $temp->is_vip,
                    'size' => $temp->size,
                    'prev_cid' => 0,
                    'next_cid' => 0,
                    'recent_update_at' => date('Y-m-d H:i:s'),
                    'content' => $temp->content,
                    'ly_chapter_id' => 0
                ]);
            }
        }
        \Artisan::call('book:afs', ['bid' => [$book->id]]);
        DB::table('book_configs')->where('bid', $book->id)->update([
            'force_subscribe_chapter_seq' => $new_yunqi_book->force_subscribe_chapter_seq,
            'roles' => $new_yunqi_book->roles,
            'charge_type' => $new_yunqi_book->charge_type,
            'cp_source' => 'new_yunqi'
        ]);
        return 0;
    }
}
