<?php

namespace App\Modules\Book\Models;

use Illuminate\Database\Eloquent\Model;

class BookSubscribleChapter extends Model
{
    protected $table = 'book_subscribe_chapter';
    protected $fillable = ['book_id', 'subscribe_chapter_id', 'channel_id'];

    /**
     * 设置强关章节序号
     */
    public static function setSubcribleChapter($data)
    {
        $old = self::where('book_id', $data['book_id'])->where('channel_id', $data['channel_id'])->first();
        if ($old) {
            $old->subscribe_chapter_id = $data['subscribe_chapter_id'];
            $old->save();
            return $old;
        } else {
            return self::create($data);
        }
    }

    /**
     * 查询渠道设置的书籍的强关章节序号
     * $book_id:书本id
     * $channelId:渠道id
     */
    public static function getSubcribleChapter($book_id, $channelId)
    {
        return self::select('subscribe_chapter_id')->where('book_id', $book_id)->where('channel_id', $channelId)->first();
    }
}