NoticeService.php 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148
  1. <?php
  2. // +----------------------------------------------------------------------
  3. // | LARAVEL8.0 框架 [ LARAVEL ][ RXThinkCMF ]
  4. // +----------------------------------------------------------------------
  5. // | 版权所有 2017~2021 LARAVEL研发中心
  6. // +----------------------------------------------------------------------
  7. // | 官方网站: http://www.laravel.cn
  8. // +----------------------------------------------------------------------
  9. // | Author: laravel开发员 <laravel.qq.com>
  10. // +----------------------------------------------------------------------
  11. namespace App\Services\Common;
  12. use App\Models\NoticeModel;
  13. use App\Services\BaseService;
  14. use App\Services\RedisService;
  15. /**
  16. * 通知公告-服务类
  17. * @author laravel开发员
  18. * @since 2020/11/11
  19. * Class NoticeService
  20. * @package App\Services\Common
  21. */
  22. class NoticeService extends BaseService
  23. {
  24. protected static $instance = null;
  25. /**
  26. * 构造函数
  27. * @author laravel开发员
  28. * @since 2020/11/11
  29. * NoticeService constructor.
  30. */
  31. public function __construct()
  32. {
  33. $this->model = new NoticeModel();
  34. }
  35. /**
  36. * 静态入口
  37. * @return static|null
  38. */
  39. public static function make()
  40. {
  41. if (!self::$instance) {
  42. self::$instance = (new static());
  43. }
  44. return self::$instance;
  45. }
  46. /**
  47. * 列表数据
  48. * @param $params
  49. * @param int $pageSize
  50. * @return array
  51. */
  52. public function getDataList($params, $pageSize = 15)
  53. {
  54. $where = ['a.mark' => 1,'status'=>1];
  55. $status = isset($params['status'])? $params['status'] : 0;
  56. if($status>0){
  57. $where['a.status'] = $status;
  58. }
  59. $list = $this->model->from('notice as a')
  60. ->where($where)
  61. ->where(function ($query) use($params){
  62. $keyword = isset($params['keyword'])? $params['keyword'] : '';
  63. if($keyword){
  64. $query->where('a.title','like',"%{$keyword}%");
  65. }
  66. })
  67. ->select(['a.*'])
  68. ->orderBy('a.id','desc')
  69. ->paginate($pageSize > 0 ? $pageSize : 9999999);
  70. $list = $list? $list->toArray() :[];
  71. if($list){
  72. foreach($list['data'] as &$item){
  73. $item['create_time'] = $item['create_time']? datetime($item['create_time'],'Y-m-d H.i.s') : '';
  74. }
  75. }
  76. return [
  77. 'pageSize'=> $pageSize,
  78. 'total'=>isset($list['total'])? $list['total'] : 0,
  79. 'list'=> isset($list['data'])? $list['data'] : []
  80. ];
  81. }
  82. /**
  83. * 设置置顶
  84. * @return array
  85. * @since 2020/11/21
  86. * @author laravel开发员
  87. */
  88. public function setIsTop()
  89. {
  90. $data = request()->all();
  91. if (!$data['id']) {
  92. return message('记录ID不能为空', false);
  93. }
  94. if (!$data['is_top']) {
  95. return message('设置置顶不能为空', false);
  96. }
  97. $error = '';
  98. $item = [
  99. 'id' => $data['id'],
  100. 'is_top' => $data['is_top']
  101. ];
  102. $rowId = $this->model->edit($item, $error);
  103. if (!$rowId) {
  104. return message($error, false);
  105. }
  106. return message();
  107. }
  108. /**
  109. * 首页推荐公告
  110. * @param $num
  111. * @return array|mixed
  112. */
  113. public function getRecommandList($num)
  114. {
  115. $cacheKey = "caches:notices:{$num}";
  116. $datas = RedisService::get($cacheKey);
  117. if($datas){
  118. return $datas;
  119. }
  120. $datas = $this->model->where(['status'=>1,'mark'=>1])->orderBy('create_time','desc')->limit($num)->get();
  121. $datas = $datas? $datas->toArray() : [];
  122. if($datas){
  123. RedisService::set($cacheKey, $datas, rand(10,20));
  124. }
  125. return $datas;
  126. }
  127. /**
  128. * @param $id
  129. * @return array|string
  130. */
  131. public function getInfo($id)
  132. {
  133. return $this->model->getInfo($id);
  134. }
  135. }