<?php

namespace app\admin\controller\HonoraryAchievement;

use app\BaseController;
use app\common\model\HonoraryAchievement\SchoolCategory as ModelSchoolCategory;
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 SchoolCategory extends BaseController
{
    /**
     * 获取录取院校分类列表
     */
    public function getSchoolCategoryList(Request $request, $isExport = false): array
    {
        $params = $request->param();
        $con = [];

        $con = Tool::getOptionalQuery(['school_category_name', 'LIKE'], ['school_category_status', "="],);

        $query = ModelSchoolCategory::where($con)
            ->field([
                'school_category_id',
                'school_category_guid',
                'school_category_title',
                'school_category_total',
                'school_category_name',
                'school_category_img',
                'school_category_icon',
                'school_category_status',
                'school_category_sort'
            ])
            ->order('school_category_sort', 'asc');

        return $isExport ? $query->select()->toArray() : msg("获取录取院校分类列表成功!", $query);
    }

    /**
     * 编辑录取院校分类
     */
    public function editSchoolCategory(Request $request): array
    {
        $params = $request->param();
        $this->validate($params, [
            'school_category_title|标题' => 'require',
            'school_category_name|名称' => 'require',
            'school_category_img|图片' => 'require',
            'school_category_sort|排序' => 'require'
        ]);
        $model = ModelSchoolCategory::where('school_category_guid', $params['school_category_guid'])->find();
        if (!$model) throwErrorMsg("该录取院校分类不存在", 1);
        $model->allowField([
            'school_category_update_user_guid',
            'school_category_title',
            'school_category_name',
            'school_category_total',
            'school_category_img',
            'school_category_icon',
            'school_category_status',
            'school_category_sort'
        ])->save($params);
        return msg('编辑成功!');
    }

    /**
     * 添加录取院校分类
     */
    public function addSchoolCategory(Request $request): array
    {
        $params = $request->param();
        $this->validate($params, [
            'school_category_title|标题' => 'require',
            'school_category_name|名称' => 'require',
            'school_category_img|图片' => 'require',
            'school_category_sort|排序' => 'require'
        ]);
        $model = ModelSchoolCategory::create($params, [
            'school_category_guid',
            'school_category_create_user_guid',
            'school_category_update_user_guid',
            'school_category_title',
            'school_category_icon',
            'school_category_total',
            'school_category_name',
            'school_category_img',
            'school_category_status',
            'school_category_sort'
        ]);
        return msg('添加成功!');
    }

    /**
     * 删除录取院校分类
     */
    public function deleteSchoolCategory(Request $request): array
    {
        $params = $request->param();
        $this->validate($params, [
            'school_category_guid' => 'require',
        ]);
        $school_category = ModelSchoolCategory::where([
            'school_category_guid' => explode(',', $params['school_category_guid'])
        ])->select();
        $school_category->delete();
        return msg('删除成功!');
    }

    /**
     * 导出Excel
     */
    public function exportExcel(Request $request)
    {
        ModelSchoolCategory::exportExcel(self::getSchoolCategoryList($request, true));
    }
}
