SchoolNew.php 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133
  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\common\model;
  13. use cores\BaseModel;
  14. use think\model\relation\HasOne;
  15. /**
  16. * 学校图库模型类
  17. * Class SchoolNew
  18. * @package app\common\model
  19. */
  20. class SchoolNew extends BaseModel
  21. {
  22. protected $globalScope = [''];
  23. // 定义表名
  24. protected $name = 'school_news';
  25. // 定义主键
  26. protected $pk = 'news_id';
  27. /**
  28. * 关联学校
  29. * @return HasOne
  30. */
  31. public function school(): HasOne
  32. {
  33. return $this->HasOne('School','id','school_id')->field('id,school_name,logo')->bind(['school_name']);
  34. }
  35. /**
  36. * 获取学校新闻资讯列表
  37. * @param $schoolId
  38. * @param int $limitRow
  39. * @return array|mixed
  40. * @throws \think\db\exception\DataNotFoundException
  41. * @throws \think\db\exception\DbException
  42. * @throws \think\db\exception\ModelNotFoundException
  43. */
  44. public static function getShowList($schoolId, $type = 1, $limitRow=0){
  45. $cacheKey = "caches:schools:news:{$schoolId}_{$type}_{$limitRow}";
  46. if($list = \think\facade\Cache::get($cacheKey)){
  47. return $list;
  48. }
  49. $query = self::where(['school_id'=>$schoolId, 'type'=> $type, 'status'=>1]);
  50. if($limitRow){
  51. $query->limit($limitRow);
  52. }
  53. $list = $query->order('addtime desc,news_id desc')->select();
  54. $list->hidden(['content','update_time','status']);
  55. $list = $list? $list->toArray() :[];
  56. if($list){
  57. foreach ($list as &$item){
  58. $item['logo_preview'] = $item['logo']? getPreview($item['logo']) : '';
  59. $item['publish_time'] = $item['addtime']? date('Y/m/d', strtotime($item['addtime'])) : '';
  60. }
  61. \think\facade\Cache::set($cacheKey, $list, rand(10, 30));
  62. }
  63. return $list;
  64. }
  65. public function getList(array $params, $limitRow= 15){
  66. $cacheKey = "caches:schools:newsList:{$limitRow}_".md5(json_encode($params));
  67. if($list = \think\facade\Cache::get($cacheKey)){
  68. return $list;
  69. }
  70. $where = ['status'=> 1];
  71. $schoolId =
  72. $query = self::where(['status'=>1])->where(function($query) use($params){
  73. $schoolId = isset($params['school_id'])? $params['school_id'] : 0;
  74. if($schoolId){
  75. $query->where('school_id','=', $schoolId);
  76. }
  77. $type = isset($params['type'])? $params['type'] : 0;
  78. if($type){
  79. $query->where('type','=', $type);
  80. }
  81. $keyword = isset($params['keyword'])? $params['keyword'] : '';
  82. if($keyword){
  83. $query->where('title','like', "%{$keyword}%");
  84. }
  85. });
  86. if($limitRow){
  87. $query->limit($limitRow);
  88. }
  89. $list = $query->order('addtime desc,news_id desc')->paginate($limitRow);
  90. $list->hidden(['content','update_time','status']);
  91. if($list){
  92. foreach ($list as &$item){
  93. $item['logo_preview'] = $item['logo']? getPreview($item['logo']) : '';
  94. $item['publish_time'] = $item['addtime']? date('Y/m/d', strtotime($item['addtime'])) : '';
  95. }
  96. \think\facade\Cache::set($cacheKey, $list, rand(10, 30));
  97. }
  98. return $list;
  99. }
  100. /**
  101. * 详情
  102. * @param int $id
  103. * @param array $with
  104. * @return array|null|static
  105. */
  106. public static function detail(int $id, array $with = [])
  107. {
  108. return self::get($id, $with);
  109. }
  110. }