<?php

namespace App\Modules\Book\Models;

use Illuminate\Database\Eloquent\Model;

class BookSpecialChannel extends Model
{
    protected $table = 'channel_special_books';
    protected $fillable = ['id', 'bid', 'channel_user_id', 'created_at', 'updated_at'];

    /**
     * 设置书本的渠道标识
     * @param $bid  书本id
     * @param $channels 渠道列表
     * @return mixed
     */
    public static function addSpecialChannle($bid, $channels)
    {
        $data = self::where('bid', $bid)->delete();
        foreach ($channels as $channel) {
            self::create(['bid' => $bid, 'channel_user_id' => $channel]);
        }
        return true;
    }

    /**
     * 获取书本设置的渠道标识
     * @param $bid
     * @return mixed
     */
    public static function getChannelsByBid($bid)
    {
        return self::select('channel_user_id')->where('bid', $bid)->get();
    }

    /**
     * 清除书本设置的渠道标识
     * @param $bid
     * @return mixed
     */
    public static function clearSpecialChannle($bid)
    {
        return self::where('bid', $bid)->delete();
    }

    /**
     * 获取渠道下配置的所有的书本id列表
     * @param $channel_user_id
     * @return array
     */
    public static function getBids($channel_user_id)
    {
        $data= self::where('channel_user_id', $channel_user_id)->select('bid')->get();
        $bids=[];
        foreach ($data as $item){
            $bids[]=$item->bid;
        }
        return $bids;
    }
}
