School.php 7.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252
  1. <?php
  2. // +----------------------------------------------------------------------
  3. // | 商城系统 [ 致力于通过产品和服务,帮助商家高效化开拓市场 ]
  4. // +----------------------------------------------------------------------
  5. // | Copyright (c) 2017~2021 https://www.thinkphp.com All rights reserved.
  6. // +----------------------------------------------------------------------
  7. // | Licensed 这不是一个自由软件,不允许对程序代码以任何形式任何目的的再发行
  8. // +----------------------------------------------------------------------
  9. // | Author: thinkphp <admin@yiovo.com>
  10. // +----------------------------------------------------------------------
  11. declare (strict_types=1);
  12. namespace app\common\model;
  13. use cores\BaseModel;
  14. use think\facade\Cache;
  15. use think\model\Collection;
  16. use think\model\relation\HasMany;
  17. use think\model\relation\HasOne;
  18. use think\Paginator;
  19. /**
  20. * 学校模型类
  21. * Class School
  22. * @package app\common\model
  23. */
  24. class School extends BaseModel
  25. {
  26. protected $globalScope = [''];
  27. // 定义表名
  28. protected $name = 'schools';
  29. // 定义主键
  30. protected $pk = 'id';
  31. protected $levelTypes = [1=>'中职',2=>'高职'];
  32. /**
  33. * 获取列表
  34. * @param array $param 查询条件
  35. * @param int $listRows 分页数量
  36. * @return mixed
  37. * @throws \think\db\exception\DbException
  38. */
  39. public function getList(array $param = [], int $listRows = 15)
  40. {
  41. // 筛选条件
  42. $query = $this->getQueryFilter($param);
  43. // 排序条件
  44. $sort = $this->setQuerySort($param);
  45. var_dump($sort);
  46. // 范围查询
  47. $field = '*';
  48. $lng = isset($param['longitude'])? $param['longitude'] : 0;
  49. $lat = isset($param['latitude'])? $param['latitude'] : 0;
  50. if($lng && $lat){
  51. $field .= ",ROUND(
  52. 6378.138 * 2 * ASIN(
  53. SQRT(
  54. POW(
  55. SIN(
  56. (
  57. {$lat} * PI() / 180 - ".$this->name.".`latitude` * PI() / 180
  58. ) / 2
  59. ),
  60. 2
  61. ) + COS({$lat} * PI() / 180) * COS(".$this->name.".`latitude` * PI() / 180) * POW(
  62. SIN(
  63. (
  64. {$lng} * PI() / 180 - ".$this->name.".`longitude` * PI() / 180
  65. ) / 2
  66. ),
  67. 2
  68. )
  69. )
  70. ) * 1000
  71. ) AS distance";
  72. $sort = ' distance desc, hot_order desc';
  73. }
  74. // 执行查询
  75. $list = $query->alias($this->name)
  76. ->order($sort)
  77. ->field($field)
  78. ->paginate($listRows);
  79. // 整理列表数据并返回
  80. return $list;
  81. }
  82. /**
  83. * 设置商品展示的数据
  84. * @param Collection|Paginator $list 商品列表
  85. * @param callable|null $callback 回调函数
  86. * @return mixed
  87. */
  88. protected function setListData($list, callable $callback = null)
  89. {
  90. if ($list->isEmpty()) return $list;
  91. // 遍历商品列表整理数据
  92. foreach ($list as &$item) {
  93. $data = $this->setData($item, $callback);
  94. }
  95. return $list;
  96. }
  97. /**
  98. * 整理数据
  99. * @param Collection|static $info
  100. * @param callable|null $callback
  101. * @return mixed
  102. */
  103. protected function setData($info, callable $callback = null)
  104. {
  105. // 回调函数
  106. is_callable($callback) && call_user_func($callback, $info);
  107. return $info->hidden(array_merge($this->hidden, ['albums']));
  108. }
  109. /**
  110. * 检索查询条件
  111. * @param array $params
  112. * @return \think\db\BaseQuery
  113. */
  114. private function getQueryFilter(array $params)
  115. {
  116. $filter = [];
  117. // 实例化新查询对象
  118. $query = $this->getNewQuery();
  119. // 学校层次,类型
  120. !empty($params['education_levels']) && $filter[] = ['education_levels', '=', "{$params['education_levels']}"];
  121. // 实例化新查询对象
  122. return $query->where($filter)->where(function($query) use ($params){
  123. // 关键词
  124. if(!empty($params['keyword'])){
  125. $query->where('school_name','like', "%{$params['keyword']}%");
  126. // $query->where('school_name','like', "%{$params['keyword']}%")->whereOr('address','like',"%{$params['keyword']}%");
  127. }
  128. // 匹配同专业学校
  129. $specialityName = isset($params['speciality_name'])? $params['speciality_name'] : '';
  130. if($specialityName){
  131. $query->whereIn('id', SchoolSpeciality::getSchools($specialityName));
  132. }
  133. // 过滤学校
  134. $schoolId = isset($params['school_id'])? $params['school_id'] : 0;
  135. if($schoolId){
  136. $query->whereNotIn('id', [$schoolId]);
  137. }
  138. });
  139. }
  140. /**
  141. * 检索排序条件
  142. * @param array $param
  143. * @return array|string[]
  144. */
  145. private function setQuerySort(array $param = [])
  146. {
  147. $params = $this->setQueryDefaultValue($param, [
  148. 'sortType' => 'all', // 排序类型
  149. 'hot_order' => false, // 热门排序 (true高到低 false低到高)
  150. ]);
  151. // 排序规则
  152. $sort = [];
  153. if ($params['sortType'] === 'all') {
  154. $sort = ['hot_order' => 'desc'];
  155. } elseif ($params['sortType'] === 'view') {
  156. $sort = ['views' => 'desc'];
  157. } elseif ($params['sortType'] === 'time') {
  158. $sort = ['id' => 'desc'];
  159. }
  160. return array_merge($sort, [$this->getPk() => 'desc']);
  161. }
  162. /**
  163. * 关联图库
  164. * @return HasMany
  165. */
  166. public function albums(): HasMany
  167. {
  168. return $this->hasMany('SchoolAlbum','school_id','id');
  169. }
  170. /**
  171. * 关联地区
  172. * @return HasOne
  173. */
  174. public function city(): HasOne
  175. {
  176. return $this->HasOne('region','id','city_id')->bind(['name as city_name']);
  177. }
  178. /**
  179. * 关联地区
  180. * @return HasOne
  181. */
  182. public function region(): HasOne
  183. {
  184. return $this->HasOne('region','id','region_id')->bind(['name as region_name']);
  185. }
  186. /**
  187. * @param $userId
  188. * @return mixed
  189. */
  190. public function userSchool($userId, $field=''){
  191. $data = \think\facade\Cache::get("caches:user:schoolData:{$userId}");
  192. if($data){
  193. return $data;
  194. }
  195. $data = self::alias($this->name)
  196. ->leftJoin('user_info ui','ui.school_id='.$this->name.'.id')
  197. ->where(['ui.user_id'=> $userId])
  198. ->find();
  199. if($data){
  200. Cache::set("caches:user:schoolData:{$userId}", $data, rand(5, 10));
  201. }
  202. return $data;
  203. }
  204. /**
  205. * 访问量
  206. * @param $id
  207. */
  208. public static function setIncViews($id){
  209. (new static())->setInc(['id'=> $id], 'views', 1);
  210. }
  211. /**
  212. * 获取详情
  213. * @param int $id
  214. * @param array $with
  215. * @return static
  216. */
  217. public static function detail(int $id, array $with = [])
  218. {
  219. return static::get($id, $with);
  220. }
  221. }