<?php

namespace app\model\api;

use think\facade\Db;
use think\Model;


class CourseModel extends Model
{

    protected $table = 'table_education_course';

    /**
     * 添加课程
     * @param $data
     * @return mixed
     */
    public function addCourse($data)
    {
        $result = Db::table($this->table)->insert($data);
        return $result;
    }





    /**
     * 获取课程列表
     *
     * @param string $course_topic
     * @param int $page
     * @param int $pagesize
     * @return mixed
     */
    public function getCourse($course_topic = '', $page = 1, $pagesize = 10)
    {
        $query = $this->order('course_id ASC');


        if (!empty($course_topic)) {
            $query->whereLike('course_topic', '%' . $course_topic . '%');
        }

        $list = $query->paginate([
            'page' => $page,
            'list_rows' => $pagesize,
        ]);

        return $list;
    }


    /**
     * 删除课程
     * @param $course_id
     * @return mixed
     */
    public function deleteCourse($course_id)
    {
        $result = Db::table($this->table)->where('course_id', $course_id)->delete();
        return $result;
    }

    /**
     * 查找课程
     * @param $keyword
     * @return mixed
     */
    public function searchCourse($keyword)
    {
        $result = Db::table($this->table)->where('title', 'like', '%' . $keyword . '%')->select();
        return $result;
    }

    public function getCourseDetail($courseId)
    {
        $courseDetail = Db::name('education_course')
            ->alias('c')
            ->join('education_gr g', 'c.gr_id=g.gr_id')
            ->where('c.course_id', $courseId)
            ->find();
        return $courseDetail;
    }




    /**
     * 修改课程状态
     * @param $course_id
     * @param $course_status
     * @return mixed
     */
    public function updateCourseStatus($course_id, $course_status)
    {
        $result = Db::table($this->table)->where('course_id', $course_id)->update(['status' => $course_status]);
        return $result;
    }

    /**
     * 修改课程信息
     * @param $course_id
     * @param $data
     * @return mixed
     */
    public function updateCourseInfo($course_id, $data)
    {
        $result = Db::table($this->table)->where('course_id', $course_id)->update($data);
        return $result;
    }



}