<?php
/**
 * Created by PhpStorm.
 * User: z-yang
 * Date: 2019/1/11
 * Time: 9:50
 */

namespace App\Modules\YunQi\Services;

use App\Modules\OfficialAccount\Services\ForceSubscribeService;
use App\Modules\YunQi\Models\YqZsyTest;

class YqZsyTestService
{
    public static function getRandomBooks($uid,$num=1):array{
        $sex = ForceSubscribeService::getSimpleSexByUid($uid);
        if($sex == 1){
            $channel_name = '男频';
        }else{
            $channel_name = '女频';
        }
        $books = YqZsyTest::join('books','books.id','=','yq_zsy_test.bid')
            ->join('book_categories','book_categories.id','=','books.category_id')
            ->where('book_categories.channel_name',$channel_name)
            ->select('yq_zsy_test.bid')->where('type','yunqi')->get();
        if($books->count()<=0){
            return [];
        }
        $num = $books->count()>$num ?$num:$books->count();
        return $books->random($num)->pluck('bid')->all();
    }

    public static function getByBid($bid){
        return YqZsyTest::where('bid',$bid)->select('id','bid','type')->first();
    }
}