VideosDatasService.php 6.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185
  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\VideoDatasModel;
  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 VideosDatasService extends BaseService
  22. {
  23. /**
  24. * 构造函数
  25. * @author laravel开发员
  26. * @since 2020/11/11
  27. * AdService constructor.
  28. */
  29. public function __construct()
  30. {
  31. $this->model = new VideoDatasModel();
  32. }
  33. /**
  34. * 列表
  35. * @param $params
  36. * @param int $pageSize
  37. * @return array
  38. */
  39. public function getDataList($params, $pageSize = 15)
  40. {
  41. $query = $this->getQuery($params);
  42. $list = $query->with(['category'])->select(['a.*'])
  43. ->orderBy('a.create_time','desc')
  44. ->orderBy('a.id','desc')
  45. ->paginate($pageSize > 0 ? $pageSize : 9999999);
  46. $list = $list? $list->toArray() :[];
  47. if($list){
  48. foreach($list['data'] as &$item){
  49. $item['create_time'] = $item['create_time']? datetime($item['create_time'],'Y-m-d H.i.s') : '';
  50. }
  51. }
  52. return [
  53. 'pageSize'=> $pageSize,
  54. 'total'=>isset($list['total'])? $list['total'] : 0,
  55. 'list'=> isset($list['data'])? $list['data'] : []
  56. ];
  57. }
  58. /**
  59. * 查询
  60. * @param $params
  61. * @return \Illuminate\Database\Eloquent\Builder
  62. */
  63. public function getQuery($params)
  64. {
  65. $where = ['a.mark' => 1];
  66. $status = isset($params['status'])? $params['status'] : 1;
  67. if($status>0){
  68. $where['a.status'] = $status;
  69. }else{
  70. unset($where['a.status']);
  71. }
  72. $model = $this->model->from('videos_datas as a')
  73. ->where($where)
  74. ->where(function ($query) use($params){
  75. $keyword = isset($params['keyword'])? $params['keyword'] : '';
  76. if($keyword){
  77. $query->where(function ($query) use ($keyword){
  78. $query->where('a.real_name','like',"%{$keyword}%")->orWhere('a.phone','like',"%{$keyword}%");
  79. });
  80. }
  81. $examType = isset($params['exam_type'])? $params['exam_type'] : 0;
  82. if($examType>0){
  83. $query->where('a.exam_type', $examType);
  84. }
  85. $categoryId = isset($params['category_id'])? $params['category_id'] : 0;
  86. if($categoryId>0){
  87. $query->where('a.category_id', $categoryId);
  88. }
  89. $subjectType = isset($params['subject_type'])? $params['subject_type'] : 0;
  90. if($subjectType>0){
  91. $query->where('a.subject_type', $subjectType);
  92. }
  93. // 日期
  94. $date = isset($params['date']) ? $params['date'] : [];
  95. $start = isset($date[0])? $date[0] : '';
  96. $end = isset($date[1])? $date[1] : '';
  97. $end = $start>=$end? '' : $end;
  98. if ($start) {
  99. $query->where('a.create_time','>=', strtotime($start));
  100. }
  101. if($end){
  102. $query->where('a.create_time','<=', strtotime($end));
  103. }
  104. });
  105. return $model;
  106. }
  107. /**
  108. * 添加或编辑
  109. * @return array
  110. * @since 2020/11/11
  111. * @author laravel开发员
  112. */
  113. public function edit()
  114. {
  115. $data = request()->all();
  116. return parent::edit($data);
  117. }
  118. /**
  119. * 删除七天之前标记软删除的数据
  120. */
  121. public function delete()
  122. {
  123. $this->model->where('mark', 0)->where('update_time', '<=', time() - 7 * 86400)->delete();
  124. return parent::delete();
  125. }
  126. /**
  127. * 表单数据提交
  128. * @param $userId
  129. * @param $params
  130. */
  131. public function formSubmit($userId, $params)
  132. {
  133. $type = isset($params['type'])? $params['type'] : 1;
  134. $cacheKey = "caches:videos:form_{$userId}:{$type}";
  135. if(RedisService::get($cacheKey.'_lock')){
  136. $this->error = '请不要频繁提交';
  137. return false;
  138. }
  139. $check = $this->model->where(['user_id'=>$userId,'type'=>$type,'mark'=>1])
  140. ->where('create_time','>=',time() - 300)
  141. ->value('id');
  142. if($check){
  143. $this->error = '您近期已经提交过,请5分钟后再试~';
  144. return false;
  145. }
  146. RedisService::set($cacheKey.'_lock', $params, rand(20,30));
  147. $data = [
  148. 'user_id'=> $userId,
  149. 'real_name'=> isset($params['real_name'])? trim($params['real_name']) : '',
  150. 'phone'=> isset($params['phone'])? trim($params['phone']) : '',
  151. 'type'=> isset($params['type'])? intval($params['type']) : 0,
  152. 'exam_type'=> isset($params['exam_type'])? intval($params['exam_type']) : 1,
  153. 'category_id'=> isset($params['category_id'])? intval($params['category_id']) : 0,
  154. 'subject_type'=> isset($params['subject_type'])? intval($params['subject_type']) : 0,
  155. 'create_time'=> time(),
  156. ];
  157. if(!$this->model->insertGetId($data)){
  158. RedisService::clear($cacheKey.'_lock');
  159. $this->error = '数据提交失败,请返回重试~';
  160. return false;
  161. }
  162. RedisService::clear($cacheKey.'_lock');
  163. $this->error = '数据提交成功,请耐心等候客服联系您~';
  164. return true;
  165. }
  166. }