<?php

namespace App\Modules\Book\Models;

use Illuminate\Database\Eloquent\Model;

class SuperiorHistoryBooks extends Model
{
    protected $table = 'superior_history_book';
    protected $fillable = ['id','bid', 'pid', 'category_id','status','created_at', 'updated_at',
        'recommend_index','uv','register_uv_rate','force_sub_rate','pay_force_sub_rate','pay_uv_rate','charge_uv_rate','order_index','level'];

    public static function getSuperiorList($pid,$page_size=15,$paginate=true,$search=[]){
        $filter = [
            ['superior_history_book.status','=',1],
            ['book_configs.is_on_shelf','>=',1],
            ['book_configs.is_on_shelf','<=',2]
        ];
        if($pid){
            $filter[] = ['superior_history_book.pid','=',$pid];
        }
        if($search){
            $filter = array_merge($filter,$search);
        }
        $res = self::where($filter)
            ->select(['superior_history_book.id','superior_history_book.bid','superior_history_book.register_uv_rate','superior_history_book.pay_uv_rate','superior_history_book.charge_uv_rate','book_configs.book_name','superior_history_book.pid','superior_history_book.created_at','superior_history_book.order_index'])
            ->leftjoin('book_configs','book_configs.bid','=','superior_history_book.bid')
            ->orderBy('superior_history_book.order_index','asc')
            ->orderBy('superior_history_book.recommend_index','desc')
            ->orderBy('superior_history_book.id','asc');
        if($paginate){
            $res = $res->paginate($page_size);
        }else{
            $res = $res->get();
        }
        return $res;
    }
}
