SchoolSpeciality.php 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122
  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\SchoolSpeciality as SchoolSpecialityModel;
  16. /**
  17. * 学校专业模型类
  18. * Class SchoolSpeciality
  19. * @package app\api\model
  20. */
  21. class SchoolSpeciality extends SchoolSpecialityModel
  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 = 12)
  39. {
  40. // 整理查询参数
  41. $params = array_merge($param, ['status' => 1]);
  42. // 获取商品列表
  43. $list = parent::getList($params, $listRows);
  44. if ($list->isEmpty()) {
  45. return $list;
  46. }
  47. // 整理列表数据并返回
  48. return $this->setListDataFromApi($list);
  49. }
  50. /**
  51. * 设置展示的数据 api模块
  52. * @param $info
  53. * @return mixed
  54. */
  55. private function setListDataFromApi($info)
  56. {
  57. return $this->setListData($info, function ($data){
  58. // 整理数据 api模块
  59. $this->setDataFromApi($data);
  60. // 隐藏冗余的字段
  61. $this->hidden(array_merge($this->hidden, ['introduce','curriculum','job_direction','get_certificate','status']));
  62. });
  63. }
  64. /**
  65. * 整理数据 api模块
  66. * @param $info
  67. * @return mixed
  68. */
  69. private function setDataFromApi($info)
  70. {
  71. return $this->setData($info, function ($data) {
  72. // logo封面
  73. $data['speciality_logo'] = $data['speciality_logo']? getPreview($data['speciality_logo']) : '';
  74. // 已报名人数
  75. if(!is_null($data['recruit_num'])){
  76. $bookNum = SpecialityBook::getBooks($data['speciality_id']);
  77. $data['book_num'] = intval($bookNum);
  78. $data['remainder_num'] = max(0,$data['recruit_num'] - $bookNum);
  79. }
  80. // 浏览数
  81. if(!is_null($data['views'])){
  82. $data['views'] = $data['views']? ($data['views']<10000? "{$data['views']}" : round($data['views']/10000,1).'w') :'';
  83. }
  84. });
  85. }
  86. /**
  87. * 获取学校的专业
  88. * @param int $school_id 学校ID
  89. * @param string $field 返回字段
  90. * @param int $limit 返回数量
  91. * @return SchoolSpeciality[]|array|\think\Collection
  92. * @throws \think\db\exception\DataNotFoundException
  93. * @throws \think\db\exception\DbException
  94. * @throws \think\db\exception\ModelNotFoundException
  95. */
  96. public function getHots(int $school_id, $field='', $limit=3)
  97. {
  98. return $this->where(['school_id'=>$school_id,'status'=>1])
  99. ->field($field?:'speciality_id,speciality_name,school_id,recruit_num')
  100. ->order('views desc,speciality_id desc')
  101. ->limit($limit?? 3)
  102. ->select()??[];
  103. }
  104. }