<?php

namespace app\admin\model;

use support\Db;
use support\Model;

/**
 * 万悦书院目录模型
 * Class Users
 * @package app\admin\model
 */
class LifeBookCourse extends Model
{
    const IS_DEL_YES = 1;
    const IS_DEL_NO  = 0;
    const COURSE_IS_SHOW = [
        self::IS_DEL_YES => '已删除',
        self::IS_DEL_NO  => '未删除'
    ];
    /**
     * The table associated with the model.
     *
     * @var string
     */
    protected $table = 'life_book_course';

    public $timestamps = false;

    /**
     * Notes:获取课程目录列表
     * @param string $keywords
     * @param int $page
     * @param int $limit
     * @return array
     * User: ZQ
     * Date: 2022/10/14
     */
    public static function getBookCourseList(int $page, int $limit, string $keywords)
    {
        $list = static::select(['life_book_course.*','life_book.life_book_name'])
            ->where(['course_is_del'=>static::IS_DEL_NO])
            ->when($keywords != '', function ($query) use ($keywords){
                $query->where('course_name', 'like', '%' . $keywords . '%');
            })
            ->leftJoin('life_book','life_book_id','=','life_book_course.course_book_id')
            ->orderBy('course_create_time','DESC')
            ->forPage($page, $limit)
            ->get();
        $count = static::where(['course_is_del'=>static::IS_DEL_NO])
            ->when($keywords != '', function ($query) use ($keywords){
                $query->where('course_name', 'like', '%' . $keywords . '%');
            })
            ->count();
        return [$list, $count];
    }

    /**
     * Notes:获取所有课程
     * @return array
     * User: ZQ
     * Date: 2022/10/14
     */
    public static function getBookAll()
    {
        $list = static::select(['course_id','course_name'])
            ->where(['course_is_del'=>static::IS_DEL_NO])
            ->orderBy('course_create_time','DESC')
            ->get();
        return $list;
    }

    /**
     * Notes:获取课程目录名称
     * @param string $account
     * @return \Illuminate\Database\Eloquent\Builder|\Illuminate\Database\Eloquent\Model|object|null
     * User: ZQ
     * Date: 2022/10/14
     */
    public static function courseMation($admin_cinema)
    {
        $where = [];
        $where['course_is_del'] = static::IS_DEL_NO;
        $mation = static::where($where)
            ->when($admin_cinema != '', function ($query) use ($admin_cinema){
                $query->whereIn('course_id', $admin_cinema);
            })
            ->get('course_name');
        return $mation;
    }

}
