<?php
/**
 * Created by PhpStorm.
 * User: tandunzhao
 * Date: 2017/11/17
 * Time: 下午4:44
 */

namespace App\Modules\Book\Models;


use Illuminate\Database\Eloquent\Model;


class UserShelfBooks extends Model
{
    protected $tables = 'user_shelf_books';

    protected $fillable = ['uid', 'distribution_channel_id', 'bid'];

    /**
     * 根据uid获取书架
     */
    static function getUserShelfBooksListByUid($uid,$page_size=15,$is_all=false)
    {

        //$shelfBooks = self::where('uid', isset($uid) ? $uid : '');
        $shelfBooks = self::where('uid', $uid)
            ->join('book_configs','user_shelf_books.bid','=','book_configs.bid')
            ->join('books','user_shelf_books.bid','=','books.id')
            ->select('book_configs.cover','book_configs.book_name','user_shelf_books.bid',
                'user_shelf_books.distribution_channel_id','user_shelf_books.bid','user_shelf_books.id',
                'user_shelf_books.uid','user_shelf_books.updated_at','books.first_cid'
            )->orderBy('user_shelf_books.id','desc');

        if($is_all)
        {
            return $shelfBooks->get();
        }else{
            return $shelfBooks->paginate($page_size);
        }
    
    }

    /**
     * 根据uid,bid获取书架
     * 用于判断用户是添加本书
     * @param $uid
     * @param $bid
     * @return mixed
     */
    static function getUserShelfBooksListByUidAndBid($uid,$bid){
        return self::where('uid',$uid)->where('bid',$bid)->select('id','uid','distribution_channel_id','bid')->first();
    }

    static function getShelfById($id){
        return self::where('user_shelf_books.id', $id)->join('book_configs','user_shelf_books.bid','=','book_configs.bid')
            ->select('book_configs.cover','book_configs.book_name','user_shelf_books.bid',
                'user_shelf_books.distribution_channel_id','user_shelf_books.bid','user_shelf_books.id','user_shelf_books.uid','user_shelf_books.updated_at')->first();
    }
    
}