| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185 |
- <?php
- // +----------------------------------------------------------------------
- // | LARAVEL8.0 框架 [ LARAVEL ][ RXThinkCMF ]
- // +----------------------------------------------------------------------
- // | 版权所有 2017~2021 LARAVEL研发中心
- // +----------------------------------------------------------------------
- // | 官方网站: http://www.laravel.cn
- // +----------------------------------------------------------------------
- // | Author: laravel开发员 <laravel.qq.com>
- // +----------------------------------------------------------------------
- namespace App\Services\Common;
- use App\Models\VideoDatasModel;
- use App\Services\BaseService;
- use App\Services\RedisService;
- /**
- * 表单数据管理-服务类
- * @author laravel开发员
- * @since 2020/11/11
- * @package App\Services\Common
- */
- class VideosDatasService extends BaseService
- {
- /**
- * 构造函数
- * @author laravel开发员
- * @since 2020/11/11
- * AdService constructor.
- */
- public function __construct()
- {
- $this->model = new VideoDatasModel();
- }
- /**
- * 列表
- * @param $params
- * @param int $pageSize
- * @return array
- */
- public function getDataList($params, $pageSize = 15)
- {
- $query = $this->getQuery($params);
- $list = $query->with(['category'])->select(['a.*'])
- ->orderBy('a.create_time','desc')
- ->orderBy('a.id','desc')
- ->paginate($pageSize > 0 ? $pageSize : 9999999);
- $list = $list? $list->toArray() :[];
- if($list){
- foreach($list['data'] as &$item){
- $item['create_time'] = $item['create_time']? datetime($item['create_time'],'Y-m-d H.i.s') : '';
- }
- }
- return [
- 'pageSize'=> $pageSize,
- 'total'=>isset($list['total'])? $list['total'] : 0,
- 'list'=> isset($list['data'])? $list['data'] : []
- ];
- }
- /**
- * 查询
- * @param $params
- * @return \Illuminate\Database\Eloquent\Builder
- */
- public function getQuery($params)
- {
- $where = ['a.mark' => 1];
- $status = isset($params['status'])? $params['status'] : 1;
- if($status>0){
- $where['a.status'] = $status;
- }else{
- unset($where['a.status']);
- }
- $model = $this->model->from('videos_datas as a')
- ->where($where)
- ->where(function ($query) use($params){
- $keyword = isset($params['keyword'])? $params['keyword'] : '';
- if($keyword){
- $query->where(function ($query) use ($keyword){
- $query->where('a.real_name','like',"%{$keyword}%")->orWhere('a.phone','like',"%{$keyword}%");
- });
- }
- $examType = isset($params['exam_type'])? $params['exam_type'] : 0;
- if($examType>0){
- $query->where('a.exam_type', $examType);
- }
- $categoryId = isset($params['category_id'])? $params['category_id'] : 0;
- if($categoryId>0){
- $query->where('a.category_id', $categoryId);
- }
- $subjectType = isset($params['subject_type'])? $params['subject_type'] : 0;
- if($subjectType>0){
- $query->where('a.subject_type', $subjectType);
- }
- // 日期
- $date = isset($params['date']) ? $params['date'] : [];
- $start = isset($date[0])? $date[0] : '';
- $end = isset($date[1])? $date[1] : '';
- $end = $start>=$end? '' : $end;
- if ($start) {
- $query->where('a.create_time','>=', strtotime($start));
- }
- if($end){
- $query->where('a.create_time','<=', strtotime($end));
- }
- });
- return $model;
- }
- /**
- * 添加或编辑
- * @return array
- * @since 2020/11/11
- * @author laravel开发员
- */
- public function edit()
- {
- $data = request()->all();
- return parent::edit($data);
- }
- /**
- * 删除七天之前标记软删除的数据
- */
- public function delete()
- {
- $this->model->where('mark', 0)->where('update_time', '<=', time() - 7 * 86400)->delete();
- return parent::delete();
- }
- /**
- * 表单数据提交
- * @param $userId
- * @param $params
- */
- public function formSubmit($userId, $params)
- {
- $type = isset($params['type'])? $params['type'] : 1;
- $cacheKey = "caches:videos:form_{$userId}:{$type}";
- if(RedisService::get($cacheKey.'_lock')){
- $this->error = '请不要频繁提交';
- return false;
- }
- $check = $this->model->where(['user_id'=>$userId,'type'=>$type,'mark'=>1])
- ->where('create_time','>=',time() - 300)
- ->value('id');
- if($check){
- $this->error = '您近期已经提交过,请5分钟后再试~';
- return false;
- }
- RedisService::set($cacheKey.'_lock', $params, rand(20,30));
- $data = [
- 'user_id'=> $userId,
- 'real_name'=> isset($params['real_name'])? trim($params['real_name']) : '',
- 'phone'=> isset($params['phone'])? trim($params['phone']) : '',
- 'type'=> isset($params['type'])? intval($params['type']) : 0,
- 'exam_type'=> isset($params['exam_type'])? intval($params['exam_type']) : 1,
- 'category_id'=> isset($params['category_id'])? intval($params['category_id']) : 0,
- 'subject_type'=> isset($params['subject_type'])? intval($params['subject_type']) : 0,
- 'create_time'=> time(),
- ];
- if(!$this->model->insertGetId($data)){
- RedisService::clear($cacheKey.'_lock');
- $this->error = '数据提交失败,请返回重试~';
- return false;
- }
- RedisService::clear($cacheKey.'_lock');
- $this->error = '数据提交成功,请耐心等候客服联系您~';
- return true;
- }
- }
|