School.php 7.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258
  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. //echo $query->getLastSql();
  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. $status = isset($params['audit_status'])? intval($params['audit_status']) : -1;
  123. if($status>=0){
  124. $filter[] = ['audit_status', '=', $status];
  125. }
  126. // 实例化新查询对象
  127. return $query->where($filter)->where(function($query) use ($params){
  128. // 关键词
  129. if(!empty($params['keyword'])){
  130. $query->where('school_name','like', "%{$params['keyword']}%");
  131. // $query->where('school_name','like', "%{$params['keyword']}%")->whereOr('address','like',"%{$params['keyword']}%");
  132. }
  133. // 匹配同专业学校
  134. $specialityName = isset($params['speciality_name'])? $params['speciality_name'] : '';
  135. if($specialityName){
  136. $query->whereIn('id', SchoolSpeciality::getSchools($specialityName));
  137. }
  138. // 过滤学校
  139. $schoolId = isset($params['school_id'])? $params['school_id'] : 0;
  140. if($schoolId){
  141. $query->whereNotIn('id', [$schoolId]);
  142. }
  143. });
  144. }
  145. /**
  146. * 检索排序条件
  147. * @param array $param
  148. * @return array|string[]
  149. */
  150. private function setQuerySort(array $param = [])
  151. {
  152. $params = $this->setQueryDefaultValue($param, [
  153. 'sortType' => 'hot', // 排序类型
  154. 'hot_order' => false, // 热门排序 (true高到低 false低到高)
  155. ]);
  156. // 排序规则
  157. $sort = [];
  158. if ($params['sortType'] === 'all') {
  159. $sort = ['id' => 'desc'];
  160. } if ($params['sortType'] === 'hot') {
  161. $sort = ['hot_order' => 'desc'];
  162. } elseif ($params['sortType'] === 'view') {
  163. $sort = ['views' => 'desc'];
  164. }
  165. return array_merge($sort, [$this->getPk() => 'desc']);
  166. }
  167. /**
  168. * 关联图库
  169. * @return HasMany
  170. */
  171. public function albums(): HasMany
  172. {
  173. return $this->hasMany('SchoolAlbum','school_id','id');
  174. }
  175. /**
  176. * 关联地区
  177. * @return HasOne
  178. */
  179. public function city(): HasOne
  180. {
  181. return $this->HasOne('region','id','city_id')->bind(['name as city_name']);
  182. }
  183. /**
  184. * 关联地区
  185. * @return HasOne
  186. */
  187. public function region(): HasOne
  188. {
  189. return $this->HasOne('region','id','region_id')->bind(['name as region_name']);
  190. }
  191. /**
  192. * @param $userId
  193. * @return mixed
  194. */
  195. public function userSchool($userId, $field=''){
  196. $data = \think\facade\Cache::get("caches:user:schoolData:{$userId}");
  197. if($data){
  198. return $data;
  199. }
  200. $data = self::alias($this->name)
  201. ->leftJoin('user_info ui','ui.school_id='.$this->name.'.id')
  202. ->where(['ui.user_id'=> $userId])
  203. ->find();
  204. if($data){
  205. Cache::set("caches:user:schoolData:{$userId}", $data, rand(5, 10));
  206. }
  207. return $data;
  208. }
  209. /**
  210. * 访问量
  211. * @param $id
  212. */
  213. public static function setIncViews($id){
  214. (new static())->setInc(['id'=> $id], 'views', 1);
  215. }
  216. /**
  217. * 获取详情
  218. * @param int $id
  219. * @param array $with
  220. * @return static
  221. */
  222. public static function detail(int $id, array $with = [])
  223. {
  224. return static::get($id, $with);
  225. }
  226. }