School.php 8.3 KB

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