// +---------------------------------------------------------------------- declare (strict_types=1); namespace app\api\model; use app\api\service\User as UserService; use app\common\model\SourceShool as SourceShoolModel; /** * 生源学校模型类 * Class SourceShool * @package app\api\model */ class SourceShool extends SourceShoolModel { protected $globalScope = ['']; /** * 隐藏字段 * @var array */ protected $hidden = [ ]; /** * 获取列表 * @param array $param 查询条件 * @param int $listRows 分页数量 * @return mixed|\think\model\Collection|\think\Paginator * @throws \think\db\exception\DbException */ public function getList(array $param = [], int $listRows = 15) { // 整理查询参数 $params = array_merge($param, []); // 获取商品列表 $list = parent::getList($params, $listRows); if ($list->isEmpty()) { return $list; } // 隐藏冗余的字段 $list->hidden(array_merge($this->hidden, ['province_id'])); // 整理列表数据并返回 return $this->setListDataFromApi($list); } /** * 设置展示的数据 api模块 * @param $info * @return mixed */ private function setListDataFromApi($info) { $userInfo = UserService::getCurrentLoginUser(true); return $this->setListData($info, function ($data) use ($userInfo) { // 整理数据 api模块 $this->setDataFromApi($data, $userInfo); }); } /** * 整理数据 api模块 * @param $info * @return mixed */ private function setDataFromApi($info, User $userInfo) { return $this->setData($info, function ($data) use ($userInfo) { // 解锁统计 $lockedNum = rand(0, $data['students_num']); $data['locked_num'] = $lockedNum; $data['unlock_num'] = max(0, $data['students_num'] - $lockedNum); // 区域 $data['city_name'] = Region::getNameById($data['city_id']); $data['region_name'] = Region::getNameById($data['region_id']); $data->hidden(array_merge($this->hidden,['province_id'])); }); } /** * 获取选项列表 * @param array $param 查询条件 * @param int $listRows 分页数量 * @return mixed|\think\model\Collection|\think\Paginator * @throws \think\db\exception\DbException */ public function getOptionList(array $param = [], string $field='') { return $this->where(function ($query) use ($param){ $keyword = isset($param['keyword'])? trim($param['keyword']) : ''; if($keyword){ $query->where(function($query) use($keyword){ $query->where('source_shools_name','like',"%{$keyword}%") ->whereOr('address','like',"%{$keyword}%"); }); } $userId = isset($param['user_id'])? intval($param['user_id']) : 0; if($userId>0){ $query->whereNotIn('source_shools_id', SourceShoolApply::getSchoolsByUser($userId)); } }) ->field($field?:'source_shools_id as id,source_shools_name as school_name,students_num') ->order('students_num desc,id desc') ->select() ->each(function($item, $k) use ($param){ $userId = isset($param['user_id'])? intval($param['user_id']) : 0; $item['is_apply'] = $userId? SourceShoolApply::checkApplyByUser($userId, $item['id']) : 0; }); } /** * 获取学校信息 * @param $where * @param array $with * @return static|array|false|null */ public static function detail($where, array $with = []) { $filter = []; if (is_array($where)) { $filter = array_merge($filter, $where); } else { $filter['user_id'] = (int)$where; } return static::get($filter, $with); } /** * 获取学校字段信息 * @param $schoolId * @param string $field * @return mixed */ public static function getSchoolField($schoolId, $field='source_shools_name') { return static::where(['source_shools_id'=>$schoolId])->value($field); } }