<?php

namespace app\admin\controller\ContactUs;

use app\BaseController;
use app\common\model\ContactUs\Signup as ModelSignup;
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 Signup extends BaseController
{
    /**
     * 获取在线报名列表接口
     *
     * @param Request request
     * @return array
     * @date 2023-04-20
     * @author xjh
     * @since 1.0.0
     */
    public function getSignupList(): array
    {
        $con = Tool::getOptionalQuery(
            ['signup.signup_user_name', 'LIKE'],
            ['signup.signup_user_phone', 'LIKE'],
            ['signup.signup_user_parents_phone', 'LIKE'],
            ['signup.signup_user_home_address', 'LIKE'],
            ['signup.signup_user_original_school', 'LIKE'],
            ['signup.signup_user_arrival_time', 'LIKE'],
            ['signup.signup_user_grad_type', '='],
            ['signup.signup_status', '='],
            ['signup.classes_guid', '='],
            ['signup.signup_create_time', 'BETWEEN', 'data_time'],
        );
        $query = ModelSignup::where($con)
            ->field([
                'signup.signup_id',
                'signup.signup_guid',
                'signup.signup_user_name',
                'signup.signup_user_phone',
                'signup.signup_user_parents_phone',
                'signup.signup_user_home_address',
                'signup.signup_user_original_school',
                'signup.signup_user_arrival_time',
                'signup.classes_guid',
                'signup.signup_user_grad_type',
                'signup.signup_status',
                'classes.classes_name',
                'signup.signup_create_time',
            ])
            ->leftJoin('classes', 'classes.classes_guid = signup.classes_guid')
            ->order('signup_create_time', 'desc');

        return msg("获取在线报名列表成功!", $query);
    }

    /**
     * 在线报名受理接口
     *
     * @param Request request
     * @return array
     * @date 2023-04-20
     * @author xjh
     * @since 1.0.0
     */
    public function handleSignupStatus(Request $request): array
    {
        return msg(ModelSignup::handleStatus($request));
    }

    /**
     * 删除在线报名接口
     *
     * @param Request request
     * @return array
     * @date 2023-04-20
     * @author xjh
     * @since 1.0.0
     */
    public function deleteSignup(Request $request): array
    {
        Db::startTrans();
        try {
            $params = $request->param();
            $this->validate($params, ['signup_guid|在线报名guid' => 'require',]);
            $signup = ModelSignup::where(['signup_guid' => explode(',', $params['signup_guid'])])->select();
            $signup->delete();
            Db::commit();
            return msg('删除成功!');
        } catch (\Throwable $th) {
            Db::rollback();
            throw  $th;
        }
    }

    /**
     * 导出Excel接口
     *
     * @param Request request
     * @return array
     * @date 2023-04-20
     * @author xjh
     * @since 1.0.0
     */
    public function exportExcel(Request $request): void
    {
        $params = $request->param();
        $con = [
            ['classes.classes_delete_time', 'NULL', null],
        ];
        if (isset($params['signup_guids']) && $params['signup_guids']) {
            $con['signup.signup_guid'] = explode(',', $params['signup_guids']);
        }
        $select = ModelSignup::where($con)
            ->leftJoin('classes', 'classes.classes_guid = signup.classes_guid')
            ->order('signup_create_time', 'desc')
            ->select()
            ->toArray();

        ModelSignup::exportExcel($select);
    }
}
