<?php

namespace app\model\api;

use think\model;

class Education extends model
{
    protected $table = 'education_gr';

    // 获取教育组列表
    public function getEducationGrList($where = [], $limit = 20, $order = 'od asc,recommend desc,gr_id desc')
    {
        $query = $this->where($where)->limit($limit)->order($order);
        $grList = $query->select();
        foreach ($grList as &$gr) {
            $count = $this->getCourseCount($gr['gr_id']);
            $gr['count'] = $count['count'];
            $gr['alll'] = $count['alll'] + $gr['times'];
        }
        return $grList;
    }


    // 获取课程总数和播放总数
    public function getCourseCount($grId)
    {
        $count = $this->table('education_course')->where(['gr_id' => $grId, 'course_status' => 1, 'course_audit' => 1])->field('count(*) as count, sum(course_play_count) as alll')->find();
        return $count ?: ['count' => 0, 'alll' => 0];
    }


    // 获取推荐教育组列表
    public function getRecommendEducationGrList($limit = 9, $order = 'od asc,recommend desc,gr_id desc')
    {
        $recommendList = $this->where(['recommend' => 1])->limit($limit)->order($order)->select();
        $recommendListResult = [];
        foreach ($recommendList as $recommend) {
            $recommendListResult[] = [
                'img' => $recommend['indeximg'],
                'href' => url('kcz', ['gr_id' => $recommend['gr_id']]),
            ];
        }
        return $recommendListResult;
    }

}