* +---------------------------------------------------------------------- */ /** * school * @author crud自动生成代码 * @date 2023/10/11 14:32:28 */ namespace app\services\crud; use app\services\BaseServices; use think\exception\ValidateException; use app\dao\crud\SchoolDao; use crmeb\services\FormBuilder; /** * Class CrudService * @date 2023/10/11 * @package app\services\crud */ class SchoolServices extends BaseServices { /** * SchoolServices constructor. * @param SchoolDao $dao */ public function __construct(SchoolDao $dao) { $this->dao = $dao; } /** * 主页数据接口 * @param array $where * @return array * @date 2023/10/11 */ public function getCrudListIndex(array $where = []) { [$page, $limit] = $this->getPageValue(); $model = $this->dao->searchCrudModel($where, 'school_name,school_u_phone,school_u_name,id,school_code', 'id desc', []); return ['count' => $model->count(), 'list' => $model->page($page ?: 1, $limit ?: 10)->select()->toArray()]; } /** * 编辑和获取表单 * @date 2023/10/11 * @param int $id * @return array */ public function getCrudForm(int $id = 0) { $url = '/crud/school'; $info = []; if ($id) { $info = $this->dao->get($id); if (!$info) { throw new ValidateException(100026); } $url .= '/' . $id; } $rule = []; $rule[] = FormBuilder::input("school_name", "学校名称", $info["school_name"] ?? ''); $rule[] = FormBuilder::input("school_u_phone", "联系人手机号", $info["school_u_phone"] ?? ''); $rule[] = FormBuilder::input("school_u_name", "联系人姓名", $info["school_u_name"] ?? ''); return create_form('新增/编辑学校', $rule, $url, $id ? 'PUT' : 'POST'); } /** * 新增 * @date 2023/10/11 * @param array $data * @return mixed */ public function crudSave(array $data) { $res = $this->dao->save($data); $res->save(['school_code' => creat_code('X') . $res->id]); return $res; } /** * 修改 * @date 2023/10/11 * @param int $id * @param array $data * @return \crmeb\basic\BaseModel */ public function crudUpdate(int $id, array $data) { return $this->dao->update($id, $data); } /** * 查询邀请码 */ public function selectSchoolCodeData(string $code) { return $this->dao->selectSchoolCodeData($code); } /** * 查询全部学校 */ public function selectSchoolData() { return $this->dao->selectSchoolDao(); } /** * 通过ID校验学校是否存在 */ public function selectSchoolById($id) { $info = $this->dao->get($id); if (!$info) { throw new ValidateException(100026); } return $info; } public function schoolStatisticData() { $model = $this->dao->getThisModel()->field('id')->where('is_del', 0); return $model->count(); } public function getUserSchoolData($schoolIds) { return $this->dao->getColumn([['id', 'IN', $schoolIds]], 'school_name', 'id'); } public function getNameToData($schoolName) { $model = $this->dao->getThisModel()->field('id')->where('school_name', $schoolName)->where('is_del', 0); return $model->select()->toArray(); } }