<?php

namespace app\admin\controller\HonoraryAchievement;

use app\BaseController;
use app\common\model\HonoraryAchievement\JointAchievement as ModelJointAchievement;
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 JointAchievement extends BaseController
{
    /**
     * 获取联考成绩列表
     */
    public function getJointAchievementList(Request $request): array
    {
        $params = $request->param();
        $con = [];

        $con = Tool::getOptionalQuery(['joint_achievement_title', 'LIKE'],['joint_achievement_status', '='],);

        $query = ModelJointAchievement::where($con)
            ->field([
                'joint_achievement_id',
                'joint_achievement_guid',
                'joint_achievement_title',
                'joint_achievement_cover',
                'joint_achievement_sort',
                'joint_achievement_status',
                'joint_achievement_content'
            ])
            ->order('joint_achievement_sort', 'asc');

        return msg("获取联考成绩列表成功!", $query);
    }

    /**
     * 编辑联考成绩
     */
    public function editJointAchievement(Request $request): array
    {
        Db::startTrans();
        try {
            $params = $request->param();
            $this->validate($params, [
                'joint_achievement_title|标题' => 'require',
                'joint_achievement_cover|封面' => 'require',
                'joint_achievement_sort|排序' => 'require'
            ]);
            $model = ModelJointAchievement::where('joint_achievement_guid', $params['joint_achievement_guid'])->find();
            if (!$model) throwErrorMsg("该联考成绩不存在", 1);
            $model->allowField([
                'joint_achievement_update_user_guid',
                'joint_achievement_title',
                'joint_achievement_cover',
                'joint_achievement_sort',
                'joint_achievement_status',
                'joint_achievement_content'
            ])->save($params);
            Db::commit();
            return msg('编辑成功!');
        } catch (\Throwable $th) {
            Db::rollback();
            throw  $th;
        }
    }

    /**
     * 添加联考成绩
     */
    public function addJointAchievement(Request $request): array
    {
        Db::startTrans();
        try {
            $params = $request->param();
            $this->validate($params, [
                'joint_achievement_title|标题' => 'require',
                'joint_achievement_cover|封面' => 'require',
                'joint_achievement_sort|排序' => 'require'
            ]);
            $model = ModelJointAchievement::create($params, [
                'joint_achievement_guid',
                'joint_achievement_create_user_guid',
                'joint_achievement_update_user_guid',
                'joint_achievement_title',
                'joint_achievement_cover',
                'joint_achievement_sort',
                'joint_achievement_status',
                'joint_achievement_content'
            ]);
            Db::commit();
            return msg('添加成功!');
        } catch (\Throwable $th) {
            Db::rollback();
            throw  $th;
        }
    }

    /**
     * 删除联考成绩
     */
    public function deleteJointAchievement(Request $request): array
    {
        Db::startTrans();
        try {
            $params = $request->param();
            $this->validate($params, [
                'joint_achievement_guid' => 'require',
            ]);
            $joint_achievement = ModelJointAchievement::where([
                'joint_achievement_guid' => explode(',', $params['joint_achievement_guid'])
            ])->select();
            $joint_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;
        $select = self::getJointAchievementList($request)['data'];
        return ModelJointAchievement::exportExcel($select);
    }
}
