SchoolSpeciality.php 6.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195
  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\api\model;
  13. use app\common\exception\BaseException;
  14. use app\common\library\helper;
  15. use app\common\model\Region;
  16. use app\common\model\SchoolSpeciality as SchoolSpecialityModel;
  17. use think\facade\Cache;
  18. /**
  19. * 学校专业模型类
  20. * Class SchoolSpeciality
  21. * @package app\api\model
  22. */
  23. class SchoolSpeciality extends SchoolSpecialityModel
  24. {
  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 = 12)
  40. {
  41. // 整理查询参数
  42. $params = array_merge($param, ['status' => 1]);
  43. // 获取商品列表
  44. $list = parent::getList($params, $listRows);
  45. if ($list->isEmpty()) {
  46. return $list;
  47. }
  48. // 整理列表数据并返回
  49. return $this->setListDataFromApi($list);
  50. }
  51. /**
  52. * 设置展示的数据 api模块
  53. * @param $info
  54. * @return mixed
  55. */
  56. private function setListDataFromApi($info)
  57. {
  58. return $this->setListData($info, function ($data){
  59. // 整理数据 api模块
  60. $this->setDataFromApi($data);
  61. // 隐藏冗余的字段
  62. $this->hidden(array_merge($this->hidden, ['introduce','curriculum','job_direction','get_certificate','status']));
  63. });
  64. }
  65. /**
  66. * 整理数据 api模块
  67. * @param $info
  68. * @return mixed
  69. */
  70. private function setDataFromApi($info)
  71. {
  72. return $this->setData($info, function ($data) {
  73. // logo封面
  74. $data['speciality_logo'] = $data['speciality_logo']? getPreview($data['speciality_logo']) : '';
  75. // 已报名人数
  76. if(!is_null($data['recruit_num'])){
  77. $bookNum = SpecialityBook::getBooks($data['speciality_id']);
  78. $data['book_num'] = intval($bookNum);
  79. $data['remainder_num'] = max(0,$data['recruit_num'] - $bookNum);
  80. }
  81. // 浏览数
  82. if(!is_null($data['views'])){
  83. $data['views'] = $data['views']? ($data['views']<10000? "{$data['views']}" : round($data['views']/10000,1).'w') :'';
  84. }
  85. if(isset($data['school']['logo'])){
  86. $data['school']['logo'] = $data['school']['logo']? getPreview($data['school']['logo']) : '';
  87. }
  88. });
  89. }
  90. /**
  91. * 获取学校的专业
  92. * @param int $school_id 学校ID
  93. * @param string $field 返回字段
  94. * @param int $limit 返回数量
  95. * @return SchoolSpeciality[]|array|\think\Collection
  96. * @throws \think\db\exception\DataNotFoundException
  97. * @throws \think\db\exception\DbException
  98. * @throws \think\db\exception\ModelNotFoundException
  99. */
  100. public function getHots(int $school_id, $field='', $limit=3)
  101. {
  102. $cacheKey = "caches:speciality:hots:{$school_id}_{$limit}_".md5($school_id.$field);
  103. $datas = Cache::get($cacheKey);
  104. if($datas){
  105. return $datas;
  106. }
  107. $where = ['status'=>1];
  108. if($school_id){
  109. $where['school_id'] = $school_id;
  110. }
  111. $datas = $this->where($where)
  112. ->field($field?:'speciality_id,speciality_name,school_id,recruit_num')
  113. ->order('views desc,speciality_id desc')
  114. ->limit($limit?? 3)
  115. ->select()??[];
  116. if($datas){
  117. Cache::set($cacheKey, $datas, rand(5, 10));
  118. }
  119. return $datas;
  120. }
  121. /**
  122. * 获取学校的专业
  123. * @param string $name 专业名称
  124. * @param string $field 返回字段
  125. * @return SchoolSpeciality[]|array|\think\Collection
  126. * @throws \think\db\exception\DataNotFoundException
  127. * @throws \think\db\exception\DbException
  128. * @throws \think\db\exception\ModelNotFoundException
  129. */
  130. public function getDataByName(string $name, $field='')
  131. {
  132. $data = $this->where(['status'=>1])
  133. ->where('speciality_name','like', "%{$name}%")
  134. ->field($field?:'speciality_id,speciality_name,school_id,recruit_num')
  135. ->order('views desc,speciality_id desc')
  136. ->find();
  137. if($data['speciality_id']){
  138. $bookNum = SpecialityBook::getBooks($data['speciality_id']);
  139. $data['book_num'] = intval($bookNum);
  140. $data['remainder_num'] = max(0,$data['recruit_num'] - $bookNum);
  141. }
  142. return $data;
  143. }
  144. /**
  145. * 获取详情并累计访问次数
  146. * @param int $articleId 文章ID
  147. * @return static|null
  148. * @throws BaseException
  149. */
  150. public static function getDetail(int $id)
  151. {
  152. // 获取文章详情
  153. $detail = parent::detail($id, ['school']);
  154. if (empty($detail) || $detail['status'] != 1) {
  155. throwError('很抱歉,当前数据不存在');
  156. }
  157. $datas = $detail['speciality_logo']? [['image'=>getPreview($detail['speciality_logo'])]] : [];
  158. $albums = isset($detail['albums'])? $detail['albums'] : [];
  159. $detail['albums'] = array_merge($datas, $albums);
  160. // 累积阅读数
  161. static::setIncViews($id);
  162. return (new SchoolSpeciality)->setDataFromApi($detail);
  163. }
  164. /**
  165. * 访问量
  166. * @param $id
  167. */
  168. public static function setIncViews($id){
  169. (new SchoolSpecialityModel)->setInc(['speciality_id'=> $id], 'views', 1);
  170. }
  171. }