VideosReadsService.php 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152
  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\VideoReadsModel;
  13. use App\Services\BaseService;
  14. use App\Services\RedisService;
  15. /**
  16. * 政策阅读视频管理-服务类
  17. * @author laravel开发员
  18. * @since 2020/11/11
  19. * @package App\Services\Common
  20. */
  21. class VideosReadsService extends BaseService
  22. {
  23. public static $instance = null;
  24. /**
  25. * 构造函数
  26. * @author laravel开发员
  27. * @since 2020/11/11
  28. * AdService constructor.
  29. */
  30. public function __construct()
  31. {
  32. $this->model = new VideoReadsModel();
  33. }
  34. /**
  35. * 静态入口
  36. * @return static|null
  37. */
  38. public static function make()
  39. {
  40. if (!self::$instance) {
  41. self::$instance = (new static());
  42. }
  43. return self::$instance;
  44. }
  45. /**
  46. * 列表
  47. * @param $params
  48. * @param int $pageSize
  49. * @return array
  50. */
  51. public function getDataList($params, $pageSize = 15)
  52. {
  53. $query = $this->getQuery($params);
  54. $list = $query->select(['a.*'])
  55. ->orderBy('a.create_time','desc')
  56. ->orderBy('a.id','desc')
  57. ->paginate($pageSize > 0 ? $pageSize : 9999999);
  58. $list = $list? $list->toArray() :[];
  59. if($list){
  60. foreach($list['data'] as &$item){
  61. $item['create_time'] = $item['create_time']? datetime($item['create_time'],'Y-m-d H.i.s') : '';
  62. }
  63. }
  64. return [
  65. 'pageSize'=> $pageSize,
  66. 'total'=>isset($list['total'])? $list['total'] : 0,
  67. 'list'=> isset($list['data'])? $list['data'] : []
  68. ];
  69. }
  70. /**
  71. * 查询
  72. * @param $params
  73. * @return \Illuminate\Database\Eloquent\Builder
  74. */
  75. public function getQuery($params)
  76. {
  77. $where = ['a.mark' => 1];
  78. $status = isset($params['status'])? $params['status'] : 1;
  79. if($status>0){
  80. $where['a.status'] = $status;
  81. }else{
  82. unset($where['a.status']);
  83. }
  84. $model = $this->model->from('videos_reads as a')
  85. ->where($where)
  86. ->where(function ($query) use($params){
  87. $keyword = isset($params['keyword'])? $params['keyword'] : '';
  88. if($keyword){
  89. $query->where(function ($query) use ($keyword){
  90. $query->where('a.name','like',"%{$keyword}%");
  91. });
  92. }
  93. });
  94. return $model;
  95. }
  96. /**
  97. * 添加或编辑
  98. * @return array
  99. * @since 2020/11/11
  100. * @author laravel开发员
  101. */
  102. public function edit()
  103. {
  104. $data = request()->all();
  105. return parent::edit($data);
  106. }
  107. /**
  108. * 删除七天之前标记软删除的数据
  109. */
  110. public function delete()
  111. {
  112. $this->model->where('mark', 0)->where('update_time', '<=', time() - 7 * 86400)->delete();
  113. return parent::delete();
  114. }
  115. /**
  116. * 首页列表
  117. * @return array|mixed
  118. */
  119. public function getIndexList()
  120. {
  121. $cacheKey = "caches:videos:readList";
  122. $datas = RedisService::get($cacheKey);
  123. if($datas){
  124. return $datas;
  125. }
  126. $datas = $this->model->where(['status'=>1,'mark'=>1])
  127. ->select(['name','poster','type','file_url','sort'])
  128. ->orderBy('sort','desc')
  129. ->orderBy('id','desc')
  130. ->get();
  131. $datas = $datas? $datas->toArray() : [];
  132. if($datas){
  133. $datas = array_chunk($datas, 2);
  134. RedisService::set($cacheKey, $datas, rand(300, 600));
  135. }
  136. return $datas;
  137. }
  138. }