<?php

namespace app\admin\controller\HonoraryAchievement;

use app\BaseController;
use app\common\model\HonoraryAchievement\Achievement as ModelAchievement;
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 Achievement extends BaseController
{
    /**
     * 获取录取院校成绩列表
     */
    public function getAchievementList(Request $request): array
    {
        $params = $request->param();
        $con = [];

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

        $query = ModelAchievement::where($con)
            ->field([
                'a.achievement_id',
                'a.achievement_guid',
                'a.school_category_guid',
                'b.school_category_name',
                'a.achievement_title',
                'a.achievement_content',
                'a.achievement_sub_content',
                'a.achievement_sort'
            ])
            ->alias('a')
            ->leftjoin('school_category b', 'a.school_category_guid = b.school_category_guid')
            ->order('achievement_sort', 'asc');

        return msg("获取录取院校成绩列表成功!", $query);
    }

    /**
     * 编辑录取院校成绩
     */
    public function editAchievement(Request $request): array
    {
        Db::startTrans();
        try {
            $params = $request->param();
            $this->validate($params, [
                'school_category_guid|录取院校' => 'require',
                'achievement_title|标题' => 'require',
                'achievement_content|内容' => 'require',
                'achievement_sub_content|副内容' => 'require',
                'achievement_sort|排序' => 'require'
            ]);
            $model = ModelAchievement::where('achievement_guid', $params['achievement_guid'])->find();
            if (!$model) throwErrorMsg("该录取院校成绩不存在", 1);
            $model->allowField([
                'achievement_update_user_guid',
                'school_category_guid',
                'achievement_title',
                'achievement_content',
                'achievement_sub_content',
                'achievement_sort'
            ])->save($params);
            Db::commit();
            return msg('编辑成功!');
        } catch (\Throwable $th) {
            Db::rollback();
            throw  $th;
        }
    }

    /**
     * 添加录取院校成绩
     */
    public function addAchievement(Request $request): array
    {
        Db::startTrans();
        try {
            $params = $request->param();
            $this->validate($params, [
                'school_category_guid|录取院校' => 'require',
                'achievement_title|标题' => 'require',
                'achievement_content|内容' => 'require',
                'achievement_sub_content|副内容' => 'require',
                'achievement_sort|排序' => 'require'
            ]);
            $model = ModelAchievement::create($params, [
                'achievement_guid',
                'achievement_create_user_guid',
                'achievement_update_user_guid',
                'school_category_guid',
                'achievement_title',
                'achievement_content',
                'achievement_sub_content',
                'achievement_sort'
            ]);
            Db::commit();
            return msg('添加成功!');
        } catch (\Throwable $th) {
            Db::rollback();
            throw  $th;
        }
    }

    /**
     * 删除录取院校成绩
     */
    public function deleteAchievement(Request $request): array
    {
        Db::startTrans();
        try {
            $params = $request->param();
            $this->validate($params, [
                'achievement_guid' => 'require',
            ]);
            $achievement = ModelAchievement::where([
                'achievement_guid' => explode(',', $params['achievement_guid'])
            ])->select();
            $achievement->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::getAchievementList($request)['data'];
        return ModelAchievement::exportExcel($select);
    }

    /**
     * 下载导入模板
     */
    public function downloadTemplate(Request $request)
    {
        $params = $request->param();
        $data = [
            array_values(ModelAchievement::EXCELFIELD),
            ['清华大学美术学院', '2016年', '刘畅、潘欣玥', '录取清华大学', "1"]
        ];
        $excel = (new Excel())->exporTsheet($data);
        $excel->save('录取院校成绩导入模板.xlsx');
    }

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

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