<?php

namespace app\admin\controller\AboutUs\TeachingEnvir;

use app\BaseController;
use app\common\model\AboutUs\TeachingEnvir\TeachingEnvir as ModelTeachingEnvir;
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 app\common\exception\Tool;
use think\facade\Db;
use think\facade\Env;


class TeachingEnvir extends BaseController
{
    /**
     * 获取教学环境列表接口
     *
     * @param Request request
     * @return array
     * @date 2023-04-22
     * @author xjh
     * @since 1.0.0
     */
    public function getTeachingEnvirList(Request $request): array
    {
        $con = Tool::getOptionalQuery(
            ['teaching_envir_title', 'LIKE'],
            ['teaching_envir_intro', 'LIKE'],
            ['teaching_envir_details', 'LIKE'],
            ['teaching_envir.teaching_envir_type_guid', '='],
        );

        $query = ModelTeachingEnvir::where($con)
            ->field([
                'teaching_envir_id',
                'teaching_envir_guid',
                'teaching_envir_title',
                'teaching_envir_img',
                'teaching_envir_intro',
                'teaching_envir_details',
                'teaching_envir.teaching_envir_type_guid',
                'teaching_envir_type_name',
            ])
            ->leftJoin('teaching_envir_type', 'teaching_envir_type.teaching_envir_type_guid = teaching_envir.teaching_envir_type_guid')
            ->order('teaching_envir_create_time', 'desc');

        return msg("获取教学环境列表成功!", $query);
    }

    /**
     * 编辑教学环境接口
     *
     * @param Request request
     * @return array
     * @date 2023-04-22
     * @author xjh
     * @since 1.0.0
     */
    public function editTeachingEnvir(Request $request): array
    {
        $params = $request->param();
        $this->validate($params, [
            'teaching_envir_title|教学环境标题' => 'require',
            'teaching_envir_img|教学环境图片' => 'require',
            'teaching_envir_intro|教学环境简介' => 'require',
            'teaching_envir_details|教学环境详情' => 'require',
            'teaching_envir_type_guid|教学环境类型guid' => 'require',
        ]);
        $model = ModelTeachingEnvir::where('teaching_envir_guid', $params['teaching_envir_guid'])->find();
        if (!$model) throwErrorMsg("该教学环境不存在", 1);
        $model->allowField([
            'teaching_envir_update_user_guid',
            'teaching_envir_title',
            'teaching_envir_img',
            'teaching_envir_intro',
            'teaching_envir_type_guid',
            'teaching_envir_details'
        ])->save($params);
        return msg('编辑成功!');
    }

    /**
     * 添加教学环境接口
     *
     * @param Request request
     * @return array
     * @date 2023-04-22
     * @author xjh
     * @since 1.0.0
     */
    public function addTeachingEnvir(Request $request): array
    {
        $params = $request->param();
        $this->validate($params, [
            'teaching_envir_title|教学环境标题' => 'require',
            'teaching_envir_img|教学环境图片' => 'require',
            'teaching_envir_intro|教学环境简介' => 'require',
            'teaching_envir_details|教学环境详情' => 'require',
            'teaching_envir_type_guid|教学环境类型guid' => 'require',
        ]);
        ModelTeachingEnvir::create($params, [
            'teaching_envir_guid',
            'teaching_envir_create_user_guid',
            'teaching_envir_update_user_guid',
            'teaching_envir_title',
            'teaching_envir_img',
            'teaching_envir_type_guid',
            'teaching_envir_intro',
            'teaching_envir_details'
        ]);
        return msg('添加成功!');
    }

    /**
     * 删除教学环境接口
     *
     * @param Request request
     * @return array
     * @date 2023-04-22
     * @author xjh
     * @since 1.0.0
     */
    public function deleteTeachingEnvir(Request $request): array
    {
        $params = $request->param();
        $this->validate($params, [
            'teaching_envir_guid' => 'require',
        ]);
        $teaching_envir = ModelTeachingEnvir::where([
            'teaching_envir_guid' => explode(',', $params['teaching_envir_guid'])
        ])->select();
        $teaching_envir->delete();
        return msg('删除成功!');
    }
}
