<?php

namespace App\Models\Book;

use DB;
use Hashids;
use Illuminate\Database\Eloquent\Model;
use Log;
use Redis;

class BookConfig extends Model
{
    protected $table = 'book_configs';
    protected $fillable = ['bid', 'force_subscribe_chapter_seq', 'price', 'cover', 'book_name',
        'copyright', 'charge_type', 'hot', 'is_on_shelf', 'source_domain', 'recommend_index', 'roles', 'test_status', 'plan_push_user_num', 'test_update_time',
        'is_show_index_content', 'click_count', 'promotion_domain', 'copyright_limit_data', 'recommend_cid', 'is_high_quality', 'vip_seq','editor_recommend','is_current_week_promotion','unit_price'];
  

    /**
     * 根据id数组获取图书信息
     * @param array $bid_arr
     * @param array $order
     * @return mixed
     */
    public static function getBooksByIds(array $bid_arr, array $order = [],  $is_on_shelf = '')
    {
        if(empty($is_on_shelf)) $is_on_shelf = 2;
        
        $res = self::join('books', 'book_configs.bid', '=', 'books.id')
            ->leftjoin('book_categories', 'books.category_id', 'book_categories.id')
            ->select('book_configs.bid', 'book_configs.force_subscribe_chapter_seq', 'book_configs.vip_seq', 'book_configs.price',
                'book_configs.cover', 'book_configs.book_name', 'book_configs.copyright', 'book_configs.charge_type', 'book_configs.is_on_shelf',
                'books.author', 'books.intro', 'book_categories.category_name', 'category_id', 'status', 'chapter_count', 'book_configs.click_count',
                'first_cid', 'last_cid', 'size', 'last_chapter', 'books.keyword', 'book_configs.recommend_index', 'book_configs.is_show_index_content',
                'book_configs.product_id', 'book_categories.channel_name', 'books.last_cid', 'books.last_chapter', 'book_configs.product_id', 'books.updated_at as last_update_time',
                'book_configs.copyright_limit_data', 'book_configs.promotion_domain', 'books.name as old_name', 'book_configs.recommend_cid'
            )
            ->whereIn('book_configs.bid', $bid_arr);
        
         if($is_on_shelf !='all' && !empty($is_on_shelf)) {
             $res->where('is_on_shelf',$is_on_shelf);
         }

        if ($order) {
            $res->orderBy($order[0], $order[1]);
        } else {
            $str = implode(',', $bid_arr);
            $field = 'bid,' . $str;
            $res->orderBy(DB::raw('field(' . $field . ')'));
        }

        return $res->limit(50)->get();
    }


    /**
     * 根据bid获取图书信息
     * @param $bid
     * @return mixed
     */
    public static function getBookById($bid)
    {
        if (empty($bid)) return null;
        return self::join('books', 'book_configs.bid', '=', 'books.id')
            ->leftjoin('book_categories', 'books.category_id', 'book_categories.id')
            ->select('book_configs.bid', 'book_configs.is_on_shelf', 'book_configs.force_subscribe_chapter_seq', 'book_configs.vip_seq', 'book_configs.cp_source', 'book_configs.price', 'book_configs.cover', 'book_configs.book_name',
                'book_configs.copyright', 'book_configs.created_at', 'book_configs.charge_type', 'book_configs.is_on_shelf', 'books.author', 'books.intro', 'book_categories.category_name',
                'category_id', 'status', 'chapter_count', 'first_cid', 'last_cid', 'size', 'last_chapter', 'books.keyword', 'book_configs.recommend_index','book_configs.test_status','book_configs.unit_price','book_configs.calculate_price_type',
                'book_configs.is_show_index_content', 'book_configs.click_count', 'book_configs.product_id', 'book_categories.channel_name', 'books.last_cid', 'books.updated_at as last_update_time',
                'books.last_chapter', 'book_configs.product_id', 'book_configs.copyright_limit_data', 'book_configs.promotion_domain', 'books.name as old_name', 'book_configs.recommend_cid', 'book_configs.is_high_quality'
                , 'book_configs.copyright_from'
            )->where('book_configs.bid', $bid)->first();

    }

    /**
     * 通过书名模糊搜索bid
     * @param $book_name
     * @return mixed
     */
    static function getIdByName($book_name)
    {
        return self::select('bid')->where('book_name', 'like', '%' . $book_name . '%')->get();
    }
}
