| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133 |
- <?php
- // +----------------------------------------------------------------------
- // | 商城系统 [ 致力于通过产品和服务,帮助商家高效化开拓市场 ]
- // +----------------------------------------------------------------------
- // | Copyright (c) 2017~2021 https://www.thinkphp.com All rights reserved.
- // +----------------------------------------------------------------------
- // | Licensed 这不是一个自由软件,不允许对程序代码以任何形式任何目的的再发行
- // +----------------------------------------------------------------------
- // | Author: thinkphp <admin@yiovo.com>
- // +----------------------------------------------------------------------
- declare (strict_types=1);
- namespace app\common\model;
- use cores\BaseModel;
- use think\model\relation\HasOne;
- /**
- * 学校图库模型类
- * Class SchoolNew
- * @package app\common\model
- */
- class SchoolNew extends BaseModel
- {
- protected $globalScope = [''];
- // 定义表名
- protected $name = 'school_news';
- // 定义主键
- protected $pk = 'news_id';
- /**
- * 关联学校
- * @return HasOne
- */
- public function school(): HasOne
- {
- return $this->HasOne('School','id','school_id')->field('id,school_name,logo')->bind(['school_name']);
- }
- /**
- * 获取学校新闻资讯列表
- * @param $schoolId
- * @param int $limitRow
- * @return array|mixed
- * @throws \think\db\exception\DataNotFoundException
- * @throws \think\db\exception\DbException
- * @throws \think\db\exception\ModelNotFoundException
- */
- public static function getShowList($schoolId, $type = 1, $limitRow=0){
- $cacheKey = "caches:schools:news:{$schoolId}_{$type}_{$limitRow}";
- if($list = \think\facade\Cache::get($cacheKey)){
- return $list;
- }
- $query = self::where(['school_id'=>$schoolId, 'type'=> $type, 'status'=>1]);
- if($limitRow){
- $query->limit($limitRow);
- }
- $list = $query->order('addtime desc,news_id desc')->select();
- $list->hidden(['content','update_time','status']);
- $list = $list? $list->toArray() :[];
- if($list){
- foreach ($list as &$item){
- $item['logo_preview'] = $item['logo']? getPreview($item['logo']) : '';
- $item['publish_time'] = $item['addtime']? date('Y/m/d', strtotime($item['addtime'])) : '';
- }
- \think\facade\Cache::set($cacheKey, $list, rand(10, 30));
- }
- return $list;
- }
- public function getList(array $params, $limitRow= 15){
- $cacheKey = "caches:schools:newsList:{$limitRow}_".md5(json_encode($params));
- if($list = \think\facade\Cache::get($cacheKey)){
- return $list;
- }
- $where = ['status'=> 1];
- $schoolId =
- $query = self::where(['status'=>1])->where(function($query) use($params){
- $schoolId = isset($params['school_id'])? $params['school_id'] : 0;
- if($schoolId){
- $query->where('school_id','=', $schoolId);
- }
- $type = isset($params['type'])? $params['type'] : 0;
- if($type){
- $query->where('type','=', $type);
- }
- $keyword = isset($params['keyword'])? $params['keyword'] : '';
- if($keyword){
- $query->where('title','like', "%{$keyword}%");
- }
- });
- if($limitRow){
- $query->limit($limitRow);
- }
- $list = $query->order('addtime desc,news_id desc')->paginate($limitRow);
- $list->hidden(['content','update_time','status']);
- if($list){
- foreach ($list as &$item){
- $item['logo_preview'] = $item['logo']? getPreview($item['logo']) : '';
- $item['publish_time'] = $item['addtime']? date('Y/m/d', strtotime($item['addtime'])) : '';
- }
- \think\facade\Cache::set($cacheKey, $list, rand(10, 30));
- }
- return $list;
- }
- /**
- * 详情
- * @param int $id
- * @param array $with
- * @return array|null|static
- */
- public static function detail(int $id, array $with = [])
- {
- return self::get($id, $with);
- }
- }
|