School.php 7.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237
  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. // 范围查询
  46. $field = '*';
  47. $lng = isset($param['longitude'])? $param['longitude'] : 0;
  48. $lat = isset($param['latitude'])? $param['latitude'] : 0;
  49. if($lng && $lat){
  50. $field .= ",ROUND(
  51. 6378.138 * 2 * ASIN(
  52. SQRT(
  53. POW(
  54. SIN(
  55. (
  56. {$lat} * PI() / 180 - ".$this->name.".`latitude` * PI() / 180
  57. ) / 2
  58. ),
  59. 2
  60. ) + COS({$lat} * PI() / 180) * COS(".$this->name.".`latitude` * PI() / 180) * POW(
  61. SIN(
  62. (
  63. {$lng} * PI() / 180 - ".$this->name.".`longitude` * PI() / 180
  64. ) / 2
  65. ),
  66. 2
  67. )
  68. )
  69. ) * 1000
  70. ) AS distance";
  71. $sort = ' distance desc, hot_order desc';
  72. }
  73. // 执行查询
  74. $list = $query->alias($this->name)
  75. ->order($sort)
  76. ->field($field)
  77. ->paginate($listRows);
  78. // 整理列表数据并返回
  79. return $list;
  80. }
  81. /**
  82. * 设置商品展示的数据
  83. * @param Collection|Paginator $list 商品列表
  84. * @param callable|null $callback 回调函数
  85. * @return mixed
  86. */
  87. protected function setListData($list, callable $callback = null)
  88. {
  89. if ($list->isEmpty()) return $list;
  90. // 遍历商品列表整理数据
  91. foreach ($list as &$item) {
  92. $data = $this->setData($item, $callback);
  93. }
  94. return $list;
  95. }
  96. /**
  97. * 整理数据
  98. * @param Collection|static $info
  99. * @param callable|null $callback
  100. * @return mixed
  101. */
  102. protected function setData($info, callable $callback = null)
  103. {
  104. // 回调函数
  105. is_callable($callback) && call_user_func($callback, $info);
  106. return $info->hidden(array_merge($this->hidden, ['albums']));
  107. }
  108. /**
  109. * 检索查询条件
  110. * @param array $params
  111. * @return \think\db\BaseQuery
  112. */
  113. private function getQueryFilter(array $params)
  114. {
  115. $filter = [];
  116. // 实例化新查询对象
  117. $query = $this->getNewQuery();
  118. // 学校层次,类型
  119. !empty($params['education_levels']) && $filter[] = ['education_levels', '=', "{$params['education_levels']}"];
  120. // 实例化新查询对象
  121. return $query->where($filter)->where(function($query) use ($params){
  122. // 关键词
  123. if(!empty($params['keyword'])){
  124. $query->where('school_name','like', "%{$params['keyword']}%");
  125. // $query->where('school_name','like', "%{$params['keyword']}%")->whereOr('address','like',"%{$params['keyword']}%");
  126. }
  127. // 匹配同专业学校
  128. $specialityName = isset($params['speciality_name'])? $params['speciality_name'] : '';
  129. if($specialityName){
  130. $query->whereIn('id', SchoolSpeciality::getSchools($specialityName));
  131. }
  132. // 过滤学校
  133. $schoolId = isset($params['school_id'])? $params['school_id'] : 0;
  134. if($schoolId){
  135. $query->whereNotIn('id', [$schoolId]);
  136. }
  137. });
  138. }
  139. /**
  140. * 检索排序条件
  141. * @param array $param
  142. * @return array|string[]
  143. */
  144. private function setQuerySort(array $param = [])
  145. {
  146. $params = $this->setQueryDefaultValue($param, [
  147. 'sortType' => 'all', // 排序类型
  148. 'hot_order' => false, // 热门排序 (true高到低 false低到高)
  149. ]);
  150. // 排序规则
  151. $sort = [];
  152. if ($params['sortType'] === 'all') {
  153. $sort = ['hot_order' => 'desc'];
  154. } elseif ($params['sortType'] === 'view') {
  155. $sort = ['views' => 'desc'];
  156. }
  157. return array_merge($sort, [$this->getPk() => 'desc']);
  158. }
  159. /**
  160. * 关联图库
  161. * @return HasMany
  162. */
  163. public function albums(): HasMany
  164. {
  165. return $this->hasMany('SchoolAlbum','school_id','id');
  166. }
  167. /**
  168. * 关联地区
  169. * @return HasOne
  170. */
  171. public function city(): HasOne
  172. {
  173. return $this->HasOne('region','id','city_id')->bind(['name as city_name']);
  174. }
  175. /**
  176. * 关联地区
  177. * @return HasOne
  178. */
  179. public function region(): HasOne
  180. {
  181. return $this->HasOne('region','id','region_id')->bind(['name as region_name']);
  182. }
  183. /**
  184. * @param $userId
  185. * @return mixed
  186. */
  187. public function userSchool($userId, $field=''){
  188. $data = \think\facade\Cache::get("caches:user:schoolData:{$userId}");
  189. if($data){
  190. return $data;
  191. }
  192. $data = self::alias($this->name)
  193. ->leftJoin('user_info ui','ui.school_id='.$this->name.'.id')
  194. ->where(['ui.user_id'=> $userId])
  195. ->find();
  196. if($data){
  197. Cache::set("caches:user:schoolData:{$userId}", $data, rand(5, 10));
  198. }
  199. return $data;
  200. }
  201. /**
  202. * 访问量
  203. * @param $id
  204. */
  205. public static function setIncViews($id){
  206. (new static())->setInc(['id'=> $id], 'views', 1);
  207. }
  208. }