// +---------------------------------------------------------------------- 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); } }