<?php

namespace app\api\controller\Home;

use app\BaseController;
use app\common\model\Home\HomeEnv as ModelHomeEnv;
use app\common\model\Home\HomeWorks as ModelHomeWorks;
use app\common\model\HonoraryAchievement\JointAchievement as ModelJointAchievement;
use app\common\model\AboutUs\TeachingEnvir\TeachingEnvirType as ModelTeachingEnvirType;
use app\common\model\ExaminationInformation\InfoArticle as ModelInfoArticle;
use app\common\model\HonoraryAchievement\SchoolCategory as ModelSchoolCategory;
use app\common\model\TeachersStrength\Teacher as ModelTeachers;
use app\common\model\Enrol\Classes as ModelClasses;
use app\Request;
use think\Validate;
use think\exception\ValidateException;
use think\facade\Filesystem;
use app\common\arw\adjfut\src\Excel;
use app\common\arw\adjfut\src\UploadFile;
use think\facade\Db;
use app\common\exception\Tool;
use think\facade\Env;


class Home extends BaseController
{

    /**
     * 获取首页接口
     */
    public function getHome(Request $request)
    {
        $params = $request->param();
        $con = [];

        // 资讯文章列表
        $info_article_list = self::getHomeInfoArticleList();

        // 首页环境列表
        $home_env_img_list = self::getHomeEnvList();

        // 录取院校列表
        $school_category_list = self::getHomeSchoolCategoryList();

        // 联考成绩列表
        $joint_achievement = self::getJointAchievementList();

        // 作品
        $works_list = self::getHomeWorksList();

        // 名师
        $teacher_list = self::getHomeTeachersList();

        // 班型
        $classes_list = self::getHomeClassesList();


        $query = [
            'info_article_list' => $info_article_list,
            'home_env_img_list' => $home_env_img_list,
            'school_category_list' => $school_category_list,
            'joint_achievement' => $joint_achievement,
            'works_list' => $works_list,
            'teacher_list' => $teacher_list,
            'classes_list' => $classes_list,
        ];

        return msg(0, "获取首页接口成功!", [
            'data' => $query,
        ]);
    }



    /**
     * 获取资讯文章列表
     */
    public function getHomeInfoArticleList()
    {
        $info_article_data = ModelinfoArticle::where('info_article_status', 2)
            ->field([
                'a.info_article_id',
                'a.info_article_type_guid',
                'b.info_article_type_id',
                'b.info_article_type_name',
                'a.info_article_title',
                'a.info_article_create_time',
                'a.info_article_cover',
                'a.info_article_order',
                'b.info_article_type_sort',
            ])
            ->alias('a')
            ->leftjoin('info_article_type b', 'a.info_article_type_guid = b.info_article_type_guid')
            ->hidden(['info_article_type_guid'])
            ->select()->toArray();

        return ModelinfoArticle::buildApiHomeInfoArticleList($info_article_data);
    }



    /**
     * 获取首页环境列表
     */
    public function getHomeEnvList()
    {
        $query = ModelHomeEnv::field([
            'home_env_img',
            'home_env_sort'
        ])
            ->order('home_env_sort', 'asc')
            ->select();

        return $query;
    }

    /**
     * 获取录取院校列表
     */
    public function getHomeSchoolCategoryList()
    {
        $query = ModelSchoolCategory::field([
            'school_category_name',
            'school_category_icon',
            'school_category_total',
        ])
            ->where('school_category_status', 2)
            ->order('school_category_sort', 'asc')
            ->select();

        return $query;
    }


    /**
     * 获取联考成绩列表
     */
    public function getJointAchievementList()
    {
        $query = ModelJointAchievement::field([
                'joint_achievement_id',
                'joint_achievement_title',
                'joint_achievement_cover',
                'joint_achievement_sort',
            ])
            ->where('joint_achievement_status', 2)
            ->order('joint_achievement_sort', 'asc')
            ->select();

        return $query;
    }


    /**
     * 获取首页作品列表
     */
    public function getHomeWorksList()
    {
        $query = ModelHomeWorks::field([
            'home_works_author_type',
            'home_works_img',
            'home_works_sort',
            'home_works_intro'
        ])
            ->append(['home_works_author_type_name'])
            ->hidden(['home_works_author_type'])
            ->order('home_works_sort', 'asc')
            ->select();

        return $query;
    }

    /**
     * 获取名师列表
     */
    public function getHomeTeachersList()
    {
        $query = ModelTeachers::field([
            'a.teacher_id',
            'a.teacher_name',
            'a.teacher_position',
            'a.teacher_img',
            'a.teacher_intro',
            'a.subject_guid',
            'b.subject_name',
            'a.teacher_order'
        ])
            ->where('teacher_status', 2)
            ->alias('a')
            ->leftjoin('subject b', 'a.subject_guid = b.subject_guid')
            ->append(['teacher_eng_name'])
            ->hidden(['subject_guid', 'teacher_order'])
            ->order('teacher_order', 'asc')
            ->select();

        return $query;
    }


    /**
     * 获取班型列表
     */
    public function getHomeClassesList()
    {
        $query = ModelClasses::where('classes_status', 2)
            ->field([
                'classes_id',
                'classes_name',
                'classes_cover',
                'classes_desc',
            ])
            ->order('classes_sort', 'asc')
            ->select();

        return $query;
    }
}
