<?php

namespace app\admin\controller\Enrol;

use app\BaseController;
use app\common\model\Enrol\EnterSchoolProcess as ModelEnterSchoolProcess;
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 EnterSchoolProcess extends BaseController
{
    /**
     * 获取入学流程列表
     */
    public function getEnterSchoolProcessList(Request $request): array
    {
        $params = $request->param();
        $con = [];

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

        $query = ModelEnterSchoolProcess::where($con)
            ->field([
                'enter_school_process_id',
                'enter_school_process_guid',
                'enter_school_process_title',
                'enter_school_process_intro',
                'enter_school_process_sort'
            ])
            ->order('enter_school_process_sort', 'asc');

        return msg("获取入学流程列表成功!", $query);
    }

    /**
     * 编辑入学流程
     */
    public function editEnterSchoolProcess(Request $request): array
    {
        $params = $request->param();
        $this->validate($params, [
            'enter_school_process_title|标题' => 'require',
            'enter_school_process_intro|介绍' => 'require',
            'enter_school_process_sort|排序' => 'require'
        ]);
        $model = ModelEnterSchoolProcess::where('enter_school_process_guid', $params['enter_school_process_guid'])->find();
        if (!$model) throwErrorMsg("该入学流程不存在", 1);
        $model->allowField([
            'enter_school_process_update_user_guid',
            'enter_school_process_title',
            'enter_school_process_intro',
            'enter_school_process_sort'
        ])->save($params);
        return msg('编辑成功!');
    }

    /**
     * 添加入学流程
     */
    public function addEnterSchoolProcess(Request $request): array
    {
        $params = $request->param();
        $this->validate($params, [
            'enter_school_process_title|标题' => 'require',
            'enter_school_process_intro|介绍' => 'require',
            'enter_school_process_sort|排序' => 'require'
        ]);
        $model = ModelEnterSchoolProcess::create($params, [
            'enter_school_process_guid',
            'enter_school_process_create_user_guid',
            'enter_school_process_update_user_guid',
            'enter_school_process_title',
            'enter_school_process_intro',
            'enter_school_process_sort'
        ]);
        return msg('添加成功!');
    }

    /**
     * 删除入学流程
     */
    public function deleteEnterSchoolProcess(Request $request): array
    {
        $params = $request->param();
        $this->validate($params, [
            'enter_school_process_guid' => 'require',
        ]);
        $enter_school_process = ModelEnterSchoolProcess::where([
            'enter_school_process_guid' => explode(',', $params['enter_school_process_guid'])
        ])->select();
        $enter_school_process->delete();
        return msg('删除成功!');
    }

    /**
     * 导出Excel
     */
    public function exportExcel(Request $request)
    {
        $params = $request->param();
        $select = ModelEnterSchoolProcess::field([
            'enter_school_process_title',
            'enter_school_process_intro',
            'enter_school_process_sort'
        ])
            ->order('enter_school_process_sort', 'asc')
            ->select();
        return ModelEnterSchoolProcess::exportExcel($select);
    }
}
