<?php

namespace app\admin\model;

use support\Db;
use support\Model;

/**
 * 万悦书院模型
 * Class Users
 * @package app\admin\model
 */
class LifeBook extends Model
{
    const ROLE_STATUS = 1;

    const IS_DEL_YES = 1;
    const IS_DEL_NO  = 0;
    const BOOK_IS_SHOW = [
        self::IS_DEL_YES => '已删除',
        self::IS_DEL_NO  => '未删除'
    ];
    /**
     * The table associated with the model.
     *
     * @var string
     */
    protected $table = 'life_book';

    public $timestamps = false;

    /**
     * Notes:获取课程列表
     * @param string $keywords
     * @param int $page
     * @param int $limit
     * @return array
     * User: ZQ
     * Date: 2022/10/14
     */
    public static function getBookList(int $page, int $limit, string $keywords)
    {
        $list = static::select(['life_book.*','category.category_name','teacher.teacher_name'])
            ->where(['life_book_is_del'=>static::IS_DEL_NO])
            ->when($keywords != '', function ($query) use ($keywords){
                $query->where('life_book_name', 'like', '%' . $keywords . '%');
            })
            ->leftJoin('category','category_id','=','life_book.life_book_category_id')
            ->leftJoin('teacher','teacher_id','=','life_book.life_book_teacher_id')
            ->orderBy('life_book_create_time','DESC')
            ->forPage($page, $limit)
            ->get();
        $count = static::where(['life_book_is_del'=>static::IS_DEL_NO])
            ->when($keywords != '', function ($query) use ($keywords){
                $query->where('life_book_name', 'like', '%' . $keywords . '%');
            })
            ->count();
        return [$list, $count];
    }

    /**
     * Notes:获取所有课程
     * @return array
     * User: ZQ
     * Date: 2022/10/14
     */
    public static function getBookAll()
    {
        $list = static::select(['life_book_id','life_book_name'])
            ->where(['life_book_is_del'=>static::IS_DEL_NO])
            ->orderBy('life_book_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 bookMation($admin_cinema)
    {
        $where = [];
        $where['life_book_is_del'] = static::IS_DEL_NO;
        $mation = static::where($where)
            ->when($admin_cinema != '', function ($query) use ($admin_cinema){
                $query->whereIn('life_book_id', $admin_cinema);
            })
            ->get('life_book_name');
        return $mation;
    }

}
