<?php
/**
 * Created by PhpStorm.
 * User: hp
 * Date: 2017/11/21
 * Time: 8:59
 */

namespace App\Modules\MediaPush\Models;

use Illuminate\Database\Eloquent\Model;
use App\Modules\MediaPush\Models\MediaPushRelationBooks;

class MediaPushBookConfigs extends Model
{
    protected $table = 'media_push_book_configs';
    protected $fillable = ['id', 'headline', 'cover','bid', 'created_at', 'updated_at'];

    static function getByBid($bid)
    {
        return self::where('bid',$bid)->first();
    }

    static function getBids()
    {
        return self::select('bid')->get();
    }
    
    static function getDefaultHotBook()
    {
    	return self::where('is_enabled',1)->orderBy('id','asc')->first();
    }
    
    /**
     * 获取推荐的书籍
     */
    static function getRecommendHotBooks($from_bids)
    {
    	$recommend_bids = [];
    	$default_book = self::getDefaultHotBook();
    	$default_bid = isset($default_book->bid)?$default_book->bid:0;
    	$default_relation = MediaPushRelationBooks::getByBid($default_bid);
    	foreach ($from_bids as $from_bid){
    		$ralation = MediaPushRelationBooks::getByBid($from_bid);
    		if(!empty($ralation)){
    			$recommend_bids[$from_bid] = explode(',',$ralation->pro_bids);
    		}else{
    			$recommend_bids[$from_bid] = explode(',',$default_relation->pro_bids);
    		}
    	}
    	
    	return $recommend_bids;
    }
    
    /**
     * 获取推荐的书籍的素材
     */
    static function getRecommendHotBookMasterials($bid)
    {
    	try{
    		$recommend_bid_materials = [];
    		$ralations = MediaPushRelationBooks::getByBid($bid);
    		$ralation_bids = explode(',',$ralations->pro_bids);
    		\Log::info('getRecommendHotBookMasterials:$bid:'.$bid.' $ralation_bids:'.json_encode($ralation_bids));
    		if(!empty($ralation_bids)){
    			foreach($ralation_bids as $ralation_bid){
    				\Log::info('$ralation_bid:'.$ralation_bid);
    				$material = self::getByBid($ralation_bid);
    				$recommend_bid_materials[] = $material;
    			}
    		}
    	}catch(\Exception $e){
    		
    	}
    	
    	return $recommend_bid_materials;
    }
    
    public static function getMediaPushBooks($num)
    {
    	return self::whereNotNull('cover')->where('is_enabled',1)->get()->random($num);

    }

    public static function getInfos($params = [], $isAll = false)
    {
        $search_object = self::leftjoin('book_configs', 'media_push_book_configs.bid', '=', 'book_configs.bid');
        $search_object->where('is_enabled', 1);
        $search_object->select('media_push_book_configs.id','media_push_book_configs.bid', 'book_configs.book_name', 'media_push_book_configs.headline', 'media_push_book_configs.cover', 'book_configs.charge_type');

        if (isset($params['headline']) && $params['headline']) {
            $search_object->where('media_push_book_configs.headline', 'like', '%' . $params['headline'] . '%');
        }

        if (isset($params['book_name']) && $params['book_name']) {
            $search_object->where('book_configs.book_name', 'like', '%' . $params['book_name'] . '%');
        }

        if (isset($params['charge_type']) && $params['charge_type']) {
            $search_object->where('book_configs.charge_type', $params['charge_type']);
        }

        \Log::info($search_object->toSql());
        if ($isAll) {
            return $search_object->get();
        } else {
            return $search_object->paginate();
        }
    }
}