School.php 6.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188
  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\api\model;
  13. use app\common\library\helper;
  14. use app\common\model\Region;
  15. use app\common\model\School as SchoolModel;
  16. /**
  17. * 学校模型类
  18. * Class School
  19. * @package app\api\model
  20. */
  21. class School extends SchoolModel
  22. {
  23. protected $globalScope = [''];
  24. /**
  25. * 隐藏字段
  26. * @var array
  27. */
  28. protected $hidden = [
  29. 'update_time'
  30. ];
  31. /**
  32. * 获取列表
  33. * @param array $param 查询条件
  34. * @param int $listRows 分页数量
  35. * @return mixed|\think\model\Collection|\think\Paginator
  36. * @throws \think\db\exception\DbException
  37. */
  38. public function getList(array $param = [], int $listRows = 15)
  39. {
  40. // 整理查询参数
  41. $params = array_merge($param, ['audit_status' => 1]);
  42. // 获取商品列表
  43. $list = parent::getList($params, $listRows);
  44. if ($list->isEmpty()) {
  45. return $list;
  46. }
  47. // 隐藏冗余的字段
  48. $list->hidden(array_merge($this->hidden, ['albums']));
  49. // 整理列表数据并返回
  50. return $this->setListDataFromApi($list);
  51. }
  52. /**
  53. * 获取专业同类学校列表
  54. * @param array $param 查询条件
  55. * @param int $listRows 分页数量
  56. * @return mixed|\think\model\Collection|\think\Paginator
  57. * @throws \think\db\exception\DbException
  58. */
  59. public function getSpecialityList(int $specialityId, array $param = [], int $listRows = 15)
  60. {
  61. // 整理查询参数
  62. $params = array_merge($param, ['audit_status' => 1]);
  63. // 获取商品列表
  64. $list = parent::getList($params, $listRows);
  65. if ($list->isEmpty()) {
  66. return $list;
  67. }
  68. // 隐藏冗余的字段
  69. $list->hidden(array_merge($this->hidden, ['albums']));
  70. // 整理列表数据并返回
  71. return $this->setListDataFromApi($list);
  72. }
  73. /**
  74. * 设置展示的数据 api模块
  75. * @param $info
  76. * @return mixed
  77. */
  78. private function setListDataFromApi($info)
  79. {
  80. $specialityModel = new SchoolSpeciality;
  81. return $this->setListData($info, function ($data) use($specialityModel) {
  82. $data['speciality'] = $specialityModel->getHots($data['id']);
  83. // 整理数据 api模块
  84. $this->setDataFromApi($data);
  85. });
  86. }
  87. /**
  88. * 整理数据 api模块
  89. * @param $info
  90. * @return mixed
  91. */
  92. private function setDataFromApi($info)
  93. {
  94. return $this->setData($info, function ($data) {
  95. // logo封面
  96. $data['logo'] = $data['logo']? getPreview($data['logo']) : '';
  97. $data['labels'] = $data['labels']? explode(',', $data['labels']) : [];
  98. // 图片列表
  99. $data['albums'] = helper::getArrayColumn($data['albums'], 'album_url');
  100. // 地区
  101. $data['province_name'] = $data['province_id']? Region::getNameById($data['province_id']) : '';
  102. $data['city_name'] = $data['city_id']? Region::getNameById($data['city_id']) : '';
  103. $data['region_name'] = $data['region_id']? Region::getNameById($data['region_id']) : '';
  104. // 分类等级
  105. $data['education_levels_text'] = isset($this->levelTypes[$data['education_levels']])? $this->levelTypes[$data['education_levels']] : '无';
  106. // 距离
  107. if(!is_null($data['distance'])){
  108. $data['distance'] = $data['distance']? ($data['distance']<1000? "{$data['distance']}m" : ($data['distance']/1000).'km') :'';
  109. }
  110. // 浏览数
  111. if(!is_null($data['views'])){
  112. $data['views'] = $data['views']? ($data['views']<10000? "{$data['views']}" : round($data['views']/10000,1).'w') :'';
  113. }
  114. });
  115. }
  116. /**
  117. * 获取选项列表
  118. * @param array $param 查询条件
  119. * @param int $listRows 分页数量
  120. * @return mixed|\think\model\Collection|\think\Paginator
  121. * @throws \think\db\exception\DbException
  122. */
  123. public function getOptionList(array $param = [], string $field='')
  124. {
  125. return $this->where(['audit_status'=> 1])
  126. ->where(function ($query) use ($param){
  127. $keyword = isset($param['keyword'])? trim($param['keyword']) : '';
  128. if($keyword){
  129. $query->where('school_name','like',"%{$keyword}%");
  130. }
  131. })
  132. ->field($field?:'id,school_name,type,level,hot_order')
  133. ->order('hot_order desc,views desc')
  134. ->select();
  135. }
  136. /**
  137. * 获取学校信息
  138. * @param $where
  139. * @param array $with
  140. * @return static|array|false|null
  141. */
  142. public static function detail($where, array $with = [])
  143. {
  144. $filter = [];
  145. if (is_array($where)) {
  146. $filter = array_merge($filter, $where);
  147. } else {
  148. $filter['id'] = (int)$where;
  149. }
  150. $data = static::get($filter, $with);
  151. $data->hidden(['book_fields']);
  152. if($data['logo']){
  153. $data['logo'] = getPreview($data['logo']);
  154. }
  155. return $data;
  156. }
  157. /**
  158. * 获取用户所属招生学校
  159. * @param $userId
  160. * @param string $field
  161. * @return mixed
  162. */
  163. public static function getUserSchool($userId, $field=''){
  164. $model = new SchoolModel;
  165. return $model->userSchool($userId, $field??'id, school_name');
  166. }
  167. }