| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236 |
- <?php
- // +----------------------------------------------------------------------
- // | 萤火商城系统 [ 致力于通过产品和服务,帮助商家高效化开拓市场 ]
- // +----------------------------------------------------------------------
- // | Copyright (c) 2017~2021 https://www.yiovo.com All rights reserved.
- // +----------------------------------------------------------------------
- // | Licensed 这不是一个自由软件,不允许对程序代码以任何形式任何目的的再发行
- // +----------------------------------------------------------------------
- // | Author: 萤火科技 <admin@yiovo.com>
- // +----------------------------------------------------------------------
- declare (strict_types=1);
- namespace app\api\model;
- use app\common\library\helper;
- use app\common\model\Region;
- use app\common\model\School as SchoolModel;
- use think\facade\Db;
- /**
- * 学校模型类
- * Class School
- * @package app\api\model
- */
- class School extends SchoolModel
- {
- protected $globalScope = [''];
- /**
- * 隐藏字段
- * @var array
- */
- protected $hidden = [
- 'update_time'
- ];
- /**
- * 获取列表
- * @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, ['audit_status' => 1]);
- // 获取商品列表
- $list = parent::getList($params, $listRows);
- if ($list->isEmpty()) {
- return $list;
- }
- // 隐藏冗余的字段
- $list->hidden(array_merge($this->hidden, ['albums']));
- // 整理列表数据并返回
- return $this->setListDataFromApi($list);
- }
- /**
- * 获取专业同类学校列表
- * @param array $param 查询条件
- * @param int $listRows 分页数量
- * @return mixed|\think\model\Collection|\think\Paginator
- * @throws \think\db\exception\DbException
- */
- public function getSpecialityList(int $specialityId, array $param = [], int $listRows = 15)
- {
- // 整理查询参数
- $params = array_merge($param, ['audit_status' => 1]);
- // 获取商品列表
- $list = parent::getList($params, $listRows);
- if ($list->isEmpty()) {
- return $list;
- }
- // 整理列表数据并返回
- return $this->setListDataFromApi($list);
- }
- /**
- * 设置展示的数据 api模块
- * @param $info
- * @return mixed
- */
- private function setListDataFromApi($info)
- {
- $specialityModel = new SchoolSpeciality;
- return $this->setListData($info, function ($data) use($specialityModel) {
- $data['speciality'] = $specialityModel->getHots($data['id']);
- // 整理数据 api模块
- $this->setDataFromApi($data);
- // 隐藏冗余的字段
- $this->hidden(array_merge($this->hidden, ['recruit_phone','post_code','index_url','albums','introduce','recruitment','characteristic','book_fields']));
- });
- }
- /**
- * 整理数据 api模块
- * @param $info
- * @return mixed
- */
- private function setDataFromApi($info)
- {
- return $this->setData($info, function ($data) {
- // logo封面
- $data['logo'] = $data['logo']? getPreview($data['logo']) : '';
- $data['labels'] = $data['labels']? explode(',', $data['labels']) : [];
- // 图片列表
- $data['albums'] = helper::getArrayColumn($data['albums'], 'album_url');
- // 地区
- $data['province_name'] = $data['province_id']? Region::getNameById($data['province_id']) : '';
- $data['city_name'] = $data['city_id']? Region::getNameById($data['city_id']) : '';
- $data['region_name'] = $data['region_id']? Region::getNameById($data['region_id']) : '';
- // 分类等级
- $data['education_levels_text'] = isset($this->levelTypes[$data['education_levels']])? $this->levelTypes[$data['education_levels']] : '无';
- // 距离
- if(!is_null($data['distance'])){
- $data['distance'] = $data['distance']? ($data['distance']<1000? "{$data['distance']}m" : ($data['distance']/1000).'km') :'';
- }
- // 浏览数
- if(!is_null($data['views'])){
- $data['views'] = $data['views']? ($data['views']<10000? "{$data['views']}" : round($data['views']/10000,1).'w') :'';
- }
- });
- }
- /**
- * 获取选项列表
- * @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(['audit_status'=> 1])
- ->where(function ($query) use ($param){
- $keyword = isset($param['keyword'])? trim($param['keyword']) : '';
- if($keyword){
- $query->where('school_name','like',"%{$keyword}%");
- }
- })
- ->field($field?:'id,school_name,type,level,hot_order')
- ->order('hot_order desc,views desc')
- ->select();
- }
- /**
- * 获取学校信息
- * @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['id'] = (int)$where;
- }
- $data = static::get($filter, $with);
- $data->hidden(['book_fields']);
- if($data['logo']){
- $data['logo'] = getPreview($data['logo']);
- }
- if(is_int($where)){
- self::setIncViews((int)$where);
- }
- return $data;
- }
- /**
- * 获取用户所属招生学校
- * @param $userId
- * @param string $field
- * @return mixed
- */
- public static function getUserSchool($userId, $field=''){
- $model = new SchoolModel;
- return $model->userSchool($userId, $field??'id, school_name');
- }
- /**
- * 获取对比学校数据
- * @param $ids
- * @param int $listRows
- * @return array
- * @throws \think\db\exception\DataNotFoundException
- * @throws \think\db\exception\DbException
- * @throws \think\db\exception\ModelNotFoundException
- */
- public function getComparisonList($ids, $listRows = 3)
- {
- $list = self::whereIn('id',$ids)
- ->where(['audit_status'=> 1])
- ->field('school_name,logo,stablish_time,property,education_levels,type,level,area,students,city_id,region_id,views,labels')
- ->limit($listRows)
- ->order(Db::raw("field(id,".implode(',', $ids).")"))
- ->select();
- $datas = [];
- $list = $list? $list->toArray() : [];
- if($list){
- foreach ($list as $v){
- $datas['school_name'][] = $v['school_name'];
- $datas['logo'][] = $v['logo']? getPreview($v['logo']) : '';
- $datas['stablish_time'][] = $v['stablish_time'];
- $datas['property'][] = $v['property'];
- $datas['type'][] = $v['type'];
- $datas['education_levels'][] = $v['education_levels']? ($v['education_levels']==1? '中职':'高职'):'';
- $datas['level'][] = $v['level'];
- $datas['area'][] = $v['area'];
- $datas['students'][] = $v['students'];
- $cityName = $v['city_id']? Region::getNameById($v['city_id']) : '';
- $regionName = $v['region_id']? Region::getNameById($v['region_id']) : '';
- $datas['address'][] = "{$cityName}{$regionName}";
- $datas['views'][] = $v['views']? ($v['views']<10000? "{$v['views']}" : round($v['views']/10000,1).'w') :'';
- $datas['labels'][] = $v['labels']? str_replace(',','、', $v['labels']) : '';
- }
- }
- return $datas;
- }
- }
|