<?php

namespace app\admin\controller\School;

use app\BaseController;
use app\common\model\School\Classes as ModelClasses;
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 think\facade\Db;
use think\facade\Env;


class Classes extends BaseController
{
    /**
     * 获取班级列表
     */
    public function getClassesList(Request $request): array
    {
        $params = $request->param();
        $con = [];

        if (isset($params['classes_name']) && $params['classes_name']) {
            $con[] = ['classes_name', 'LIKE', '%' . $params['classes_name'] . '%'];
        };

        $query = ModelClasses::where($con);
        $select = self::pageWrapper($query)
            ->field([
                'classes_id',
                'classes_guid',
                'classes_name'
            ])
            ->order('classes_update_time', 'desc')
            ->select();

        $count = $query->count();

        return [
            'code' => 0,
            'data' => $select,
            'count' => $count,
            'msg' => 'ok'
        ];
    }

    /**
     * 编辑班级
     */
    public function editClasses(Request $request): array
    {
        $params = $request->param();
        $this->validate($params, [
            'classes_name|班级名称' => 'require'
        ]);
        $model = ModelClasses::where('classes_guid', $params['classes_guid'])->find();
        if (!$model) throwErrorMsg("该班级不存在", 1);
        $model->allowField([
            'classes_update_user_guid',
            'classes_name'
        ])->save($params);
        return [
            'code' => 0,
            'msg' => '编辑成功'
        ];
    }

    /**
     * 添加班级
     */
    public function addClasses(Request $request): array
    {
        $params = $request->param();
        $this->validate($params, [
            'classes_name|班级名称' => 'require'
        ]);
        $model = ModelClasses::create($params, [
            'classes_guid',
            'classes_create_user_guid',
            'classes_update_user_guid',
            'classes_name'
        ]);
        return [
            'code' => 0,
            'msg' => '添加成功'
        ];
    }

    /**
     * 删除班级
     */
    public function deleteClasses(Request $request): array
    {
        $params = $request->param();
        $this->validate($params, [
            'classes_guid' => 'require',
        ]);
        $classes = ModelClasses::where([
            'classes_guid' => explode(',', $params['classes_guid'])
        ])->select();
        $classes->delete();
        return [
            'code' => 0,
            'msg' => "删除成功"
        ];
    }
}
