* +---------------------------------------------------------------------- */ /** * 年级管理 * @author crud自动生成代码 * @date 2023/10/13 10:40:08 */ namespace app\services\crud; use app\services\BaseServices; use think\exception\ValidateException; use app\dao\crud\SchoolGradeDao; use crmeb\services\FormBuilder; use app\services\crud\SchoolServices; /** * Class CrudService * @date 2023/10/13 * @package app\services\crud */ class SchoolGradeServices extends BaseServices { /** * SchoolGradeServices constructor. * @param SchoolGradeDao $dao */ public function __construct(SchoolGradeDao $dao) { $this->dao = $dao; } /** * 主页数据接口 * @param array $where * @return array * @date 2023/10/13 */ public function getCrudListIndex(array $where = []) { [$page, $limit] = $this->getPageValue(); $model = $this->dao->searchCrudModel($where, '*', 'id desc', ['schoolIdHasOne']); $model->where('is_del', 0); return ['count' => $model->count(), 'list' => $model->page($page ?: 1, $limit ?: 10)->select()->toArray()]; } /** * 编辑和获取表单 * @date 2023/10/13 * @param int $id * @return array */ public function getCrudForm(int $id = 0) { $url = '/crud/school_grade'; $info = []; if ($id) { $info = $this->dao->get($id); if (!$info) { throw new ValidateException(100026); } $url .= '/' . $id; } $rule = []; $rule[] = FormBuilder::input("grade_name", "年级名称", $info["grade_name"] ?? ''); $SchoolServices = app()->make(SchoolServices::class); $formSchoolName = FormBuilder::select("school_id", "学校名称", $info["school_id"] ?? '')->setOptions($SchoolServices->selectSchoolData())->filterable(1); if ($id) { $formSchoolName->disabled(true); } $rule[] = $formSchoolName; return create_form('年级管理', $rule, $url, $id ? 'PUT' : 'POST'); } /** * 新增 * @date 2023/10/13 * @param array $data * @return mixed */ public function crudSave(array $data) { $SchoolServices = app()->make(SchoolServices::class); $schoolData = $SchoolServices->selectSchoolById($data['school_id']); $data['school_name'] = $schoolData['school_name']; return $this->dao->save($data); } /** * 修改 * @date 2023/10/13 * @param int $id * @param array $data * @return \crmeb\basic\BaseModel */ public function crudUpdate(int $id, array $data) { $SchoolServices = app()->make(SchoolServices::class); $schoolData = $SchoolServices->selectSchoolById($data['school_id']); $data['school_name'] = $schoolData['school_name']; return $this->dao->update($id, $data); } /** * 查询全部年级 */ public function selectGradeData() { return $this->dao->selectGradeDao(); } /** * 通过ID校验年级是否存在 */ public function selectGradeById($id) { $info = $this->dao->get($id); if (!$info) { throw new ValidateException(100026); } return $info; } /** * 某一个学校下的全部年级 */ public function selectSchoolGradeData($schoolId) { return $this->dao->selectSchoolGradeData($schoolId); } /** * 通过学校ID校验班级是否可删除 */ public function selectBySchoolId($schoolId) { $res = $this->dao->getOne([ 'school_id' => $schoolId, 'is_del' => 0 ]); return is_null($res) ? false : true; } }