SchoolSpeciality.php 7.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220
  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 array $param 查询条件
  93. * @param int $listRows 分页数量
  94. * @return mixed|\think\model\Collection|\think\Paginator
  95. * @throws \think\db\exception\DbException
  96. */
  97. public function getOptionList(array $param = [], string $field='')
  98. {
  99. return $this->where(['status'=> 1])
  100. ->where(function ($query) use ($param){
  101. $keyword = isset($param['keyword'])? trim($param['keyword']) : '';
  102. if($keyword){
  103. $query->where('speciality_name','like',"%{$keyword}%");
  104. }
  105. $schoolId = isset($param['school_id'])? intval($param['school_id']) : 0;
  106. if($schoolId>0){
  107. $query->where('school_id','=',$schoolId);
  108. }
  109. })
  110. ->field($field?:'speciality_id,speciality_name')
  111. ->order('views desc,speciality_id desc')
  112. ->select();
  113. }
  114. /**
  115. * 获取学校的专业
  116. * @param int $school_id 学校ID
  117. * @param string $field 返回字段
  118. * @param int $limit 返回数量
  119. * @return SchoolSpeciality[]|array|\think\Collection
  120. * @throws \think\db\exception\DataNotFoundException
  121. * @throws \think\db\exception\DbException
  122. * @throws \think\db\exception\ModelNotFoundException
  123. */
  124. public function getHots(int $school_id, $field='', $limit=3)
  125. {
  126. $cacheKey = "caches:speciality:hots:{$school_id}_{$limit}_".md5($school_id.$field);
  127. $datas = Cache::get($cacheKey);
  128. if($datas){
  129. return $datas;
  130. }
  131. $where = ['status'=>1];
  132. if($school_id){
  133. $where['school_id'] = $school_id;
  134. }
  135. $datas = $this->where($where)
  136. ->field($field?:'speciality_id,speciality_name,school_id,recruit_num')
  137. ->order('views desc,speciality_id desc')
  138. ->limit($limit?? 3)
  139. ->select()??[];
  140. if($datas){
  141. Cache::set($cacheKey, $datas, rand(5, 10));
  142. }
  143. return $datas;
  144. }
  145. /**
  146. * 获取学校的专业
  147. * @param string $name 专业名称
  148. * @param string $field 返回字段
  149. * @return SchoolSpeciality[]|array|\think\Collection
  150. * @throws \think\db\exception\DataNotFoundException
  151. * @throws \think\db\exception\DbException
  152. * @throws \think\db\exception\ModelNotFoundException
  153. */
  154. public function getDataByName(string $name, $field='')
  155. {
  156. $data = $this->where(['status'=>1])
  157. ->where('speciality_name','like', "%{$name}%")
  158. ->field($field?:'speciality_id,speciality_name,school_id,recruit_num')
  159. ->order('views desc,speciality_id desc')
  160. ->find();
  161. if($data['speciality_id']){
  162. $bookNum = SpecialityBook::getBooks($data['speciality_id']);
  163. $data['book_num'] = intval($bookNum);
  164. $data['remainder_num'] = max(0,$data['recruit_num'] - $bookNum);
  165. }
  166. return $data;
  167. }
  168. /**
  169. * 获取详情并累计访问次数
  170. * @param int $articleId 文章ID
  171. * @return static|null
  172. * @throws BaseException
  173. */
  174. public static function getDetail(int $id)
  175. {
  176. // 获取文章详情
  177. $detail = parent::detail($id, ['school']);
  178. if (empty($detail) || $detail['status'] != 1) {
  179. throwError('很抱歉,当前数据不存在');
  180. }
  181. $datas = $detail['speciality_logo']? [['image'=>getPreview($detail['speciality_logo'])]] : [];
  182. $albums = isset($detail['albums'])? $detail['albums'] : [];
  183. $detail['albums'] = array_merge($datas, $albums);
  184. // 累积阅读数
  185. static::setIncViews($id);
  186. return (new SchoolSpeciality)->setDataFromApi($detail);
  187. }
  188. /**
  189. * 访问量
  190. * @param $id
  191. */
  192. public static function setIncViews($id){
  193. (new SchoolSpecialityModel)->setInc(['speciality_id'=> $id], 'views', 1);
  194. }
  195. }