<?php
/**
 * Created by PhpStorm.
 * User: z-yang
 * Date: 2018/7/25
 * Time: 15:08
 */

namespace App\Modules\Book\Services;

use App\Modules\Book\Models\BookChannelScore;

class BookChannelScoreService
{
    public static function add(int $distribution_channel_id,string $score,int $bid){
        return BookChannelScore::create(compact('distribution_channel_id','bid','score'));
    }

    public static function get(int $distribution_channel_id,int $bid){
        return BookChannelScore::where('distribution_channel_id',$distribution_channel_id)
            ->where('bid',$bid)
            ->select('score','id')
            ->first();
    }

    public static function update(int $id,string $score){
        return BookChannelScore::where('id',$id)->update(compact('score'));
    }

    public static function score(int $distribution_channel_id,string $score,int $bid){
        $exist = self::get($distribution_channel_id,$bid);
        if($exist){
            return self::update($exist->id,$score);
        }

        return self::add($distribution_channel_id,$score,$bid);
    }
}