|
|
@@ -13,7 +13,10 @@ declare (strict_types=1);
|
|
|
namespace app\common\model;
|
|
|
|
|
|
use cores\BaseModel;
|
|
|
+use think\model\Collection;
|
|
|
+use think\model\relation\HasMany;
|
|
|
use think\model\relation\HasOne;
|
|
|
+use think\Paginator;
|
|
|
|
|
|
/**
|
|
|
* 学校模型类
|
|
|
@@ -28,5 +31,163 @@ class School extends BaseModel
|
|
|
// 定义主键
|
|
|
protected $pk = 'id';
|
|
|
|
|
|
+ protected $levelTypes = [1=>'中职',2=>'高职'];
|
|
|
|
|
|
+ /**
|
|
|
+ * 获取列表
|
|
|
+ * @param array $param 查询条件
|
|
|
+ * @param int $listRows 分页数量
|
|
|
+ * @return mixed
|
|
|
+ * @throws \think\db\exception\DbException
|
|
|
+ */
|
|
|
+ public function getList(array $param = [], int $listRows = 15)
|
|
|
+ {
|
|
|
+ // 筛选条件
|
|
|
+ $query = $this->getQueryFilter($param);
|
|
|
+ // 排序条件
|
|
|
+ $sort = $this->setQuerySort($param);
|
|
|
+
|
|
|
+ // 范围查询
|
|
|
+ $field = '*';
|
|
|
+ $lng = isset($param['longitude'])? $param['longitude'] : 0;
|
|
|
+ $lat = isset($param['latitude'])? $param['latitude'] : 0;
|
|
|
+ if($lng && $lat){
|
|
|
+ $field .= ",ROUND(
|
|
|
+ 6378.138 * 2 * ASIN(
|
|
|
+ SQRT(
|
|
|
+ POW(
|
|
|
+ SIN(
|
|
|
+ (
|
|
|
+ {$lat} * PI() / 180 - ".$this->name.".`latitude` * PI() / 180
|
|
|
+ ) / 2
|
|
|
+ ),
|
|
|
+ 2
|
|
|
+ ) + COS({$lat} * PI() / 180) * COS(".$this->name.".`latitude` * PI() / 180) * POW(
|
|
|
+ SIN(
|
|
|
+ (
|
|
|
+ {$lng} * PI() / 180 - ".$this->name.".`longitude` * PI() / 180
|
|
|
+ ) / 2
|
|
|
+ ),
|
|
|
+ 2
|
|
|
+ )
|
|
|
+ )
|
|
|
+ ) * 1000
|
|
|
+ ) AS distance";
|
|
|
+ $sort = ' distance desc, hot_order desc';
|
|
|
+ }
|
|
|
+
|
|
|
+ // 执行查询
|
|
|
+ $list = $query->alias($this->name)
|
|
|
+ ->order($sort)
|
|
|
+ ->field($field)
|
|
|
+ ->paginate($listRows);
|
|
|
+
|
|
|
+ // 整理列表数据并返回
|
|
|
+ return $list;
|
|
|
+ }
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 设置商品展示的数据
|
|
|
+ * @param Collection|Paginator $list 商品列表
|
|
|
+ * @param callable|null $callback 回调函数
|
|
|
+ * @return mixed
|
|
|
+ */
|
|
|
+ protected function setListData($list, callable $callback = null)
|
|
|
+ {
|
|
|
+ if ($list->isEmpty()) return $list;
|
|
|
+ // 遍历商品列表整理数据
|
|
|
+ foreach ($list as &$item) {
|
|
|
+ $data = $this->setData($item, $callback);
|
|
|
+ }
|
|
|
+ return $list;
|
|
|
+ }
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 整理数据
|
|
|
+ * @param Collection|static $info
|
|
|
+ * @param callable|null $callback
|
|
|
+ * @return mixed
|
|
|
+ */
|
|
|
+ protected function setData($info, callable $callback = null)
|
|
|
+ {
|
|
|
+ // 回调函数
|
|
|
+ is_callable($callback) && call_user_func($callback, $info);
|
|
|
+ return $info->hidden(array_merge($this->hidden, ['albums']));
|
|
|
+ }
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 检索查询条件
|
|
|
+ * @param array $params
|
|
|
+ * @return \think\db\BaseQuery
|
|
|
+ */
|
|
|
+ private function getQueryFilter(array $params)
|
|
|
+ {
|
|
|
+ $filter = [];
|
|
|
+
|
|
|
+ // 实例化新查询对象
|
|
|
+ $query = $this->getNewQuery();
|
|
|
+
|
|
|
+ // 学校层次,类型
|
|
|
+ !empty($params['education_levels']) && $filter[] = ['education_levels', '=', "{$params['education_levels']}"];
|
|
|
+
|
|
|
+ // 实例化新查询对象
|
|
|
+ return $query->where($filter)->where(function($query) use ($params){
|
|
|
+ // 关键词
|
|
|
+ if(!empty($params['keyword'])){
|
|
|
+ $query->where('school_name','like', "%{$params['keyword']}%");
|
|
|
+// $query->where('school_name','like', "%{$params['keyword']}%")->whereOr('address','like',"%{$params['keyword']}%");
|
|
|
+ }
|
|
|
+ });
|
|
|
+ }
|
|
|
+
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 检索排序条件
|
|
|
+ * @param array $param
|
|
|
+ * @return array|string[]
|
|
|
+ */
|
|
|
+ private function setQuerySort(array $param = [])
|
|
|
+ {
|
|
|
+ $params = $this->setQueryDefaultValue($param, [
|
|
|
+ 'sortType' => 'all', // 排序类型
|
|
|
+ 'hot_order' => false, // 热门排序 (true高到低 false低到高)
|
|
|
+ ]);
|
|
|
+ // 排序规则
|
|
|
+ $sort = [];
|
|
|
+ if ($params['sortType'] === 'all') {
|
|
|
+ $sort = ['hot_order' => 'desc'];
|
|
|
+ } elseif ($params['sortType'] === 'view') {
|
|
|
+ $sort = ['views' => 'desc'];
|
|
|
+ }
|
|
|
+
|
|
|
+ return array_merge($sort, [$this->getPk() => 'desc']);
|
|
|
+ }
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 关联图库
|
|
|
+ * @return HasMany
|
|
|
+ */
|
|
|
+ public function albums(): HasMany
|
|
|
+ {
|
|
|
+ return $this->hasMany('SchoolAlbum','school_id','id');
|
|
|
+ }
|
|
|
+
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 关联地区
|
|
|
+ * @return HasOne
|
|
|
+ */
|
|
|
+ public function city(): HasOne
|
|
|
+ {
|
|
|
+ return $this->HasOne('region','id','city_id')->bind(['name as city_name']);
|
|
|
+ }
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 关联地区
|
|
|
+ * @return HasOne
|
|
|
+ */
|
|
|
+ public function region(): HasOne
|
|
|
+ {
|
|
|
+ return $this->HasOne('region','id','region_id')->bind(['name as region_name']);
|
|
|
+ }
|
|
|
}
|