| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220 |
- <?php
- // +----------------------------------------------------------------------
- // | 商城系统 [ 致力于通过产品和服务,帮助商家高效化开拓市场 ]
- // +----------------------------------------------------------------------
- // | Copyright (c) 2017~2021 https://www.thinkphp.com All rights reserved.
- // +----------------------------------------------------------------------
- // | Licensed 这不是一个自由软件,不允许对程序代码以任何形式任何目的的再发行
- // +----------------------------------------------------------------------
- // | Author: thinkphp <admin@yiovo.com>
- // +----------------------------------------------------------------------
- declare (strict_types=1);
- namespace app\api\model;
- use app\common\exception\BaseException;
- use app\common\library\helper;
- use app\common\model\Region;
- use app\common\model\SchoolSpeciality as SchoolSpecialityModel;
- use think\facade\Cache;
- /**
- * 学校专业模型类
- * Class SchoolSpeciality
- * @package app\api\model
- */
- class SchoolSpeciality extends SchoolSpecialityModel
- {
- /**
- * 隐藏字段
- * @var array
- */
- protected $hidden = [
- 'update_time'
- ];
- /**
- * 获取列表
- * @param array $param 查询条件
- * @param int $listRows 分页数量
- * @return mixed|\think\model\Collection|\think\Paginator
- * @throws \think\db\exception\DbException
- */
- public function getList(array $param = [], int $listRows = 12)
- {
- // 整理查询参数
- $params = array_merge($param, ['status' => 1]);
- // 获取商品列表
- $list = parent::getList($params, $listRows);
- if ($list->isEmpty()) {
- return $list;
- }
- // 整理列表数据并返回
- return $this->setListDataFromApi($list);
- }
- /**
- * 设置展示的数据 api模块
- * @param $info
- * @return mixed
- */
- private function setListDataFromApi($info)
- {
- return $this->setListData($info, function ($data){
- // 整理数据 api模块
- $this->setDataFromApi($data);
- // 隐藏冗余的字段
- $this->hidden(array_merge($this->hidden, ['introduce','curriculum','job_direction','get_certificate','status']));
- });
- }
- /**
- * 整理数据 api模块
- * @param $info
- * @return mixed
- */
- private function setDataFromApi($info)
- {
- return $this->setData($info, function ($data) {
- // logo封面
- $data['speciality_logo'] = $data['speciality_logo']? getPreview($data['speciality_logo']) : '';
- // 已报名人数
- if(!is_null($data['recruit_num'])){
- $bookNum = SpecialityBook::getBooks($data['speciality_id']);
- $data['book_num'] = intval($bookNum);
- $data['remainder_num'] = max(0,$data['recruit_num'] - $bookNum);
- }
- // 浏览数
- if(!is_null($data['views'])){
- $data['views'] = $data['views']? ($data['views']<10000? "{$data['views']}" : round($data['views']/10000,1).'w') :'';
- }
- if(isset($data['school']['logo'])){
- $data['school']['logo'] = $data['school']['logo']? getPreview($data['school']['logo']) : '';
- }
- });
- }
- /**
- * 获取选项列表
- * @param array $param 查询条件
- * @param int $listRows 分页数量
- * @return mixed|\think\model\Collection|\think\Paginator
- * @throws \think\db\exception\DbException
- */
- public function getOptionList(array $param = [], string $field='')
- {
- return $this->where(['status'=> 1])
- ->where(function ($query) use ($param){
- $keyword = isset($param['keyword'])? trim($param['keyword']) : '';
- if($keyword){
- $query->where('speciality_name','like',"%{$keyword}%");
- }
- $schoolId = isset($param['school_id'])? intval($param['school_id']) : 0;
- if($schoolId>0){
- $query->where('school_id','=',$schoolId);
- }
- })
- ->field($field?:'speciality_id,speciality_name')
- ->order('views desc,speciality_id desc')
- ->select();
- }
- /**
- * 获取学校的专业
- * @param int $school_id 学校ID
- * @param string $field 返回字段
- * @param int $limit 返回数量
- * @return SchoolSpeciality[]|array|\think\Collection
- * @throws \think\db\exception\DataNotFoundException
- * @throws \think\db\exception\DbException
- * @throws \think\db\exception\ModelNotFoundException
- */
- public function getHots(int $school_id, $field='', $limit=3)
- {
- $cacheKey = "caches:speciality:hots:{$school_id}_{$limit}_".md5($school_id.$field);
- $datas = Cache::get($cacheKey);
- if($datas){
- return $datas;
- }
- $where = ['status'=>1];
- if($school_id){
- $where['school_id'] = $school_id;
- }
- $datas = $this->where($where)
- ->field($field?:'speciality_id,speciality_name,school_id,recruit_num')
- ->order('views desc,speciality_id desc')
- ->limit($limit?? 3)
- ->select()??[];
- if($datas){
- Cache::set($cacheKey, $datas, rand(5, 10));
- }
- return $datas;
- }
- /**
- * 获取学校的专业
- * @param string $name 专业名称
- * @param string $field 返回字段
- * @return SchoolSpeciality[]|array|\think\Collection
- * @throws \think\db\exception\DataNotFoundException
- * @throws \think\db\exception\DbException
- * @throws \think\db\exception\ModelNotFoundException
- */
- public function getDataByName(string $name, $field='')
- {
- $data = $this->where(['status'=>1])
- ->where('speciality_name','like', "%{$name}%")
- ->field($field?:'speciality_id,speciality_name,school_id,recruit_num')
- ->order('views desc,speciality_id desc')
- ->find();
- if($data['speciality_id']){
- $bookNum = SpecialityBook::getBooks($data['speciality_id']);
- $data['book_num'] = intval($bookNum);
- $data['remainder_num'] = max(0,$data['recruit_num'] - $bookNum);
- }
- return $data;
- }
- /**
- * 获取详情并累计访问次数
- * @param int $articleId 文章ID
- * @return static|null
- * @throws BaseException
- */
- public static function getDetail(int $id)
- {
- // 获取文章详情
- $detail = parent::detail($id, ['school']);
- if (empty($detail) || $detail['status'] != 1) {
- throwError('很抱歉,当前数据不存在');
- }
- $datas = $detail['speciality_logo']? [['image'=>getPreview($detail['speciality_logo'])]] : [];
- $albums = isset($detail['albums'])? $detail['albums'] : [];
- $detail['albums'] = array_merge($datas, $albums);
- // 累积阅读数
- static::setIncViews($id);
- return (new SchoolSpeciality)->setDataFromApi($detail);
- }
- /**
- * 访问量
- * @param $id
- */
- public static function setIncViews($id){
- (new SchoolSpecialityModel)->setInc(['speciality_id'=> $id], 'views', 1);
- }
- }
|