School.php 7.0 KB

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