School.php 6.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215
  1. <?php
  2. // +----------------------------------------------------------------------
  3. // | 萤火商城系统 [ 致力于通过产品和服务,帮助商家高效化开拓市场 ]
  4. // +----------------------------------------------------------------------
  5. // | Copyright (c) 2017~2021 https://www.yiovo.com All rights reserved.
  6. // +----------------------------------------------------------------------
  7. // | Licensed 这不是一个自由软件,不允许对程序代码以任何形式任何目的的再发行
  8. // +----------------------------------------------------------------------
  9. // | Author: 萤火科技 <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. }
  128. /**
  129. * 检索排序条件
  130. * @param array $param
  131. * @return array|string[]
  132. */
  133. private function setQuerySort(array $param = [])
  134. {
  135. $params = $this->setQueryDefaultValue($param, [
  136. 'sortType' => 'all', // 排序类型
  137. 'hot_order' => false, // 热门排序 (true高到低 false低到高)
  138. ]);
  139. // 排序规则
  140. $sort = [];
  141. if ($params['sortType'] === 'all') {
  142. $sort = ['hot_order' => 'desc'];
  143. } elseif ($params['sortType'] === 'view') {
  144. $sort = ['views' => 'desc'];
  145. }
  146. return array_merge($sort, [$this->getPk() => 'desc']);
  147. }
  148. /**
  149. * 关联图库
  150. * @return HasMany
  151. */
  152. public function albums(): HasMany
  153. {
  154. return $this->hasMany('SchoolAlbum','school_id','id');
  155. }
  156. /**
  157. * 关联地区
  158. * @return HasOne
  159. */
  160. public function city(): HasOne
  161. {
  162. return $this->HasOne('region','id','city_id')->bind(['name as city_name']);
  163. }
  164. /**
  165. * 关联地区
  166. * @return HasOne
  167. */
  168. public function region(): HasOne
  169. {
  170. return $this->HasOne('region','id','region_id')->bind(['name as region_name']);
  171. }
  172. /**
  173. * @param $userId
  174. * @return mixed
  175. */
  176. public function userSchool($userId, $field=''){
  177. $data = \think\facade\Cache::get("caches:user:schoolData:{$userId}");
  178. if($data){
  179. return $data;
  180. }
  181. $data = self::alias($this->name)
  182. ->leftJoin('user_info ui','ui.school_id='.$this->name.'.id')
  183. ->where(['ui.user_id'=> $userId])
  184. ->find();
  185. if($data){
  186. Cache::set("caches:user:schoolData:{$userId}", $data, rand(5, 10));
  187. }
  188. return $data;
  189. }
  190. }