<?php

namespace App\Modules\Book\Models;

use Illuminate\Database\Eloquent\Model;

class SuperiorNewBooks extends Model
{
    protected $table = 'superior_new_book';
    protected $fillable = ['id','bid', 'pid', 'category_id','status','created_at','updated_at','recommend_index','order_index'];

    public static function getSuperiorTotal($pid) {
        return self::where([['status','=',1],['pid','=',$pid]])->count('id');
    }

    public static function getLastSuperiorItem($pid) {
        return self::where([['status','=',1],['pid','=',$pid]])
            ->orderBy('created_at','asc')
            ->first();
    }
    public static function getSuperiorList($pid,$serach=[]){
        $filter = [
            ['superior_new_book.status','=',1],
            ['book_configs.is_on_shelf','>=',1],
            ['book_configs.is_on_shelf','<=',2]
        ];
        if($pid){
            $filter[] = ['superior_new_book.pid','=',$pid];
        }
        if($serach){
            $filter = array_merge($filter,$serach);
        }
        return self::where($filter)
            ->select(['superior_new_book.id','superior_new_book.bid','superior_new_book.recommend_index','superior_new_book.pid','superior_new_book.order_index','book_configs.book_name'])
            ->join('book_configs','book_configs.bid','=','superior_new_book.bid')
            //->join('books','books.id','=','superior_new_book.bid')
            ->orderBy('superior_new_book.order_index','desc')
            ->orderBy('superior_new_book.recommend_index','desc')
            ->get();
    }
}
