<?php

namespace App\Modules\Book\Models;

use Illuminate\Database\Eloquent\Model;

class Chapter extends Model
{
    protected  $table = 'chapters';
    protected  $fillable = ['bid','name','sequence','size','is_vip','prev_cid','next_cid','recent_update_at','content','ly_chapter_id'];

    /**
     * 获取章节内容
     * @param $chapter_id
     * @return mixed
     */
    public static function getChapterById($chapter_id){
        return self::where('id',$chapter_id)->select('id','bid','name','sequence','size','is_vip','prev_cid','next_cid','recent_update_at','content','ly_chapter_id')->first();
    }

    /**
     * 获取章节列表
     * @param $bid
     * @return mixed
     */
    public static function getChapterLists($bid){
        return self::where('bid',$bid)->select('id','bid','name','sequence','size','is_vip','prev_cid','next_cid','recent_update_at','ly_chapter_id')->orderBy('sequence','asc')->get();
    }

    /**
     * 获取章节列表分页
     * @param $bid
     * @return mixed
     */
    public static function getChapterListsPage($bid,$page_size=15){
        return self::where('bid',$bid)->select('id','bid','name','sequence','size','is_vip','prev_cid','next_cid','recent_update_at','ly_chapter_id')->orderBy('sequence','asc')->paginate($page_size);
    }

    /**
     * 获取章节名称
     * @param $chapter_id
     * @return mixed
     */
    public static  function getChapterNameById($chapter_id){
        return self::where('id',$chapter_id)->select('id','bid','name','sequence','size','is_vip','prev_cid','next_cid','recent_update_at')->first();
    }


    /**
     * 获取前5章内容
     * @param $bid
     * @return mixed
     */
    public static function getTopFiveChapter($bid){
        $limit = 8;
        return self::where('bid',$bid)->select('id','bid','name','sequence','size','is_vip','prev_cid','next_cid','recent_update_at','content','ly_chapter_id')
            ->orderBy('sequence','asc')->limit($limit)->get();
    }

    /**
     * 修改vip章节
     * @param $bid
     * @return mixed
     */
    public static function editVip($bid,$seq){
        $res1 = self::where('bid',$bid)->where('sequence','>=',$seq)->update(['is_vip'=>1]);    
        $res2 = self::where('bid',$bid)->where('sequence','<',$seq)->update(['is_vip'=>0]);  
        return   $res1 && $res2;
    }

     /**
     * 获取章节分页 有章节内容,仅供后台使用后台
     * @param $bid
     * @return mixed
     */
    public static function getChapterPage($bid,$page_size=15){
        return self::where('bid',$bid)->select('id','bid','name','sequence','size','is_vip','prev_cid','next_cid','recent_update_at','ly_chapter_id','content')->orderBy('sequence','asc')->paginate($page_size);    
    }
}
