<?php

namespace app\admin\controller\Enrol;

use app\BaseController;
use app\common\model\Enrol\EnrolIntro as ModelEnrolIntro;
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 EnrolIntro extends BaseController
{
    /**
     * 获取招生简介列表
     */
    public function getEnrolIntroList(Request $request): array
    {
        $params = $request->param();
        $con = [];

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

        $query = ModelEnrolIntro::where($con)
            ->field([
                'enrol_intro_id',
                'enrol_intro_guid',
                'enrol_intro_title',
                'enrol_intro_cover',
                'enrol_intro_content',
                'enrol_intro_sort',
            ])
            ->order('enrol_intro_sort', 'asc');

        return msg("获取招生简介列表成功!", $query);
    }

    /**
     * 编辑招生简介
     */
    public function editEnrolIntro(Request $request): array
    {
        $params = $request->param();
        $this->validate($params, [
            'enrol_intro_title|标题' => 'require',
            'enrol_intro_content|内容' => 'require'
        ]);
        $model = ModelEnrolIntro::where('enrol_intro_guid', $params['enrol_intro_guid'])->find();
        if (!$model) throwErrorMsg("该招生简介不存在", 1);
        $model->allowField([
            'enrol_intro_update_user_guid',
            'enrol_intro_title',
            'enrol_intro_cover',
            'enrol_intro_content',
            'enrol_intro_sort'
        ])->save($params);
        return msg('编辑成功!');
    }

    /**
     * 添加招生简介
     */
    public function addEnrolIntro(Request $request): array
    {
        $params = $request->param();
        $this->validate($params, [
            'enrol_intro_title|标题' => 'require',
            'enrol_intro_content|内容' => 'require'
        ]);
        $model = ModelEnrolIntro::create($params, [
            'enrol_intro_guid',
            'enrol_intro_create_user_guid',
            'enrol_intro_update_user_guid',
            'enrol_intro_title',
            'enrol_intro_cover',
            'enrol_intro_content',
            'enrol_intro_sort'
        ]);
        return msg('添加成功!');
    }

    /**
     * 删除招生简介
     */
    public function deleteEnrolIntro(Request $request): array
    {
        $params = $request->param();
        $this->validate($params, [
            'enrol_intro_guid' => 'require',
        ]);
        $enrol_intro = ModelEnrolIntro::where([
            'enrol_intro_guid' => explode(',', $params['enrol_intro_guid'])
        ])->select();
        $enrol_intro->delete();
        return msg('删除成功!');
    }
}
