<?php

namespace app\admin\controller\HonoraryAchievement;

use app\BaseController;
use app\common\model\HonoraryAchievement\SchoolLeaders as ModelSchoolLeaders;
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 SchoolLeaders extends BaseController
{
    /**
     * 获取录取学院领军者列表
     */
    public function getSchoolLeadersList(Request $request): array
    {
        $params = $request->param();
        $con = [];

        $con = Tool::getOptionalQuery(['a.school_category_guid', '='], ['school_leaders_name', 'LIKE'],['school_leaders_school', 'LIKE'],);

        $query = ModelSchoolLeaders::where($con)
            ->field([
                'a.school_leaders_id',
                'a.school_leaders_guid',
                'a.school_category_guid',
                'b.school_category_name',
                'a.school_leaders_name',
                'a.school_leaders_school',
                'a.school_leaders_img',
                'a.school_leaders_sort'
            ])
            ->alias('a')
            ->leftjoin('school_category b', 'a.school_category_guid = b.school_category_guid')
            ->order('school_leaders_sort', 'asc');

        return msg("获取录取学院领军者列表成功!", $query);
    }

    /**
     * 编辑录取学院领军者
     */
    public function editSchoolLeaders(Request $request): array
    {
        Db::startTrans();
        try {
            $params = $request->param();
            $this->validate($params, [
                'school_category_guid|录取院校' => 'require',
                'school_leaders_name|名称' => 'require',
                'school_leaders_school|所属院校' => 'require',
                'school_leaders_img|图片' => 'require'
            ]);
            $model = ModelSchoolLeaders::where('school_leaders_guid', $params['school_leaders_guid'])->find();
            if (!$model) throwErrorMsg("该录取学院领军者不存在", 1);
            $model->allowField([
                'school_leaders_update_user_guid',
                'school_category_guid',
                'school_leaders_name',
                'school_leaders_school',
                'school_leaders_img',
                'school_leaders_sort'
            ])->save($params);
            Db::commit();
            return msg('编辑成功!');
        } catch (\Throwable $th) {
            Db::rollback();
            throw  $th;
        }
    }

    /**
     * 添加录取学院领军者
     */
    public function addSchoolLeaders(Request $request): array
    {
        Db::startTrans();
        try {
            $params = $request->param();
            $this->validate($params, [
                'school_category_guid|录取院校' => 'require',
                'school_leaders_name|名称' => 'require',
                'school_leaders_school|所属院校' => 'require',
                'school_leaders_img|图片' => 'require'
            ]);
            $model = ModelSchoolLeaders::create($params, [
                'school_leaders_guid',
                'school_leaders_create_user_guid',
                'school_leaders_update_user_guid',
                'school_category_guid',
                'school_leaders_name',
                'school_leaders_school',
                'school_leaders_img',
                'school_leaders_sort'
            ]);
            Db::commit();
            return msg('添加成功!');
        } catch (\Throwable $th) {
            Db::rollback();
            throw  $th;
        }
    }

    /**
     * 删除录取学院领军者
     */
    public function deleteSchoolLeaders(Request $request): array
    {
        Db::startTrans();
        try {
            $params = $request->param();
            $this->validate($params, [
                'school_leaders_guid' => 'require',
            ]);
            $school_leaders = ModelSchoolLeaders::where([
                'school_leaders_guid' => explode(',', $params['school_leaders_guid'])
            ])->select();
            $school_leaders->delete();
            Db::commit();
            return msg('删除成功!');
        } catch (\Throwable $th) {
            Db::rollback();
            throw  $th;
        }
    }

    /**
     * 导出Excel
     */
    public function exportExcel(Request $request)
    {
        $params = $request->param();
        $params['limit'] = 10000;
        // if (isset($params['signup_guids']) && $params['signup_guids']) {
        //     $con['signup.signup_guid'] = explode(',', $params['signup_guids']);
        // }
        $select = self::getSchoolLeadersList($request)['data'];
        return ModelSchoolLeaders::exportExcel($select);
    }

    /**
     * 下载导入模板
     */
    public function downloadTemplate(Request $request)
    {
        $params = $request->param();
        $data = [
            array_values(ModelSchoolLeaders::EXCELFIELD),
            ['清华大学美术学院', '糖金金', '广州第一大学', "1"]
        ];
        $excel = (new Excel())->exporTsheet($data);
        $excel->save('录取学院领军者导入模板.xlsx');
    }

    /**
     * 导入excel
     */
    public function importExcel(Request $request)
    {
        $file = new UploadFile('uploads', 'fileExt:xlsx');
        $file->putFile('school_leaders');

        $msg = ModelSchoolLeaders::importExcel($file);
        return [
            'code' => 0,
            'msg' => $msg
        ];
    }
}
