// +---------------------------------------------------------------------- namespace App\Services\Api; use App\Models\MemberCollectModel; use App\Models\MemberModel; use App\Models\VideoCollectModel; use App\Models\VideoModel; use App\Services\BaseService; use App\Services\RedisService; use BN\Red; use Illuminate\Support\Facades\DB; /** * 短视频收藏点赞管理-服务类 * @author laravel开发员 * @since 2020/11/11 * @package App\Services\Api */ class VideoCollectService extends BaseService { // 静态对象 protected static $instance = null; /** * 构造函数 * @author laravel开发员 * @since 2020/11/11 * MemberCollectService constructor. */ public function __construct() { $this->model = new VideoCollectModel(); } /** * 静态入口 * @return static|null */ public static function make() { if (!self::$instance) { self::$instance = (new static()); } return self::$instance; } /** * 列表数据 * @param $params * @param int $pageSize * @return array */ public function getDataList($userId, $params, $pageSize = 15, $field='') { $where = ['a.mark' => 1,'a.status'=>1,'b.status'=>1,'b.mark'=>1]; $field = $field? $field : 'lev_a.*,lev_b.id as cid,lev_b.user_id as collect_user_id,lev_a.type as collect_type'; $sortType = isset($params['sort_type']) ? $params['sort_type'] : 1; $order = 'id desc'; if($sortType == 1){ $order = 'lev_b.create_time desc, lev_b.id desc'; } $list = $this->model->with(['member']) ->from('video_collect as b') ->leftJoin('video as a', 'a.id', '=', 'b.collect_id') ->where($where) ->where(function ($query) use ($params) { $userId = isset($params['user_id']) ? $params['user_id'] : 0; if ($userId > 0) { $query->where('a.user_id', $userId); } $collectUserId = isset($params['collect_uid']) ? $params['collect_uid'] : 0; if ($collectUserId > 0) { $query->where('a.collect_uid', $collectUserId); } $isRead = isset($params['is_read'])? $params['is_read'] : 0; $isRead = $isRead>0? $isRead : 0; if ($isRead > 0) { $query->where('a.is_read', $isRead); } $type = isset($params['type'])? $params['type'] : 0; $type = $type>0? $type : 1; if ($type > 0) { $query->where('a.type', $type); } }) ->where(function ($query) use ($params) { $keyword = isset($params['kw']) ? $params['kw'] : ''; if ($keyword) { $query->where('a.title', 'like', "%{$keyword}%") ->orWhere('a.tags','like',"%{$keyword}%") ->orWhere('a.description','like',"%{$keyword}%"); } }) ->selectRaw($field) ->orderByRaw($order) ->paginate($pageSize > 0 ? $pageSize : 9999999); $list = $list ? $list->toArray() : []; if ($list) { foreach ($list['data'] as &$item) { $item['time_text'] = isset($item['create_time']) && $item['create_time']? dateFormat($item['create_time']) : ''; $item['thumb'] = isset($item['thumb']) && $item['thumb'] ? get_image_url($item['thumb']) : ''; $item['file_url'] = isset($item['file_url']) && $item['file_url'] ? get_image_url($item['file_url']) : ''; if(isset($item['albums'])){ $albums = $item['albums']? json_decode($item['albums'], true):[]; $item['albums'] = $albums? get_images_preview($albums) : []; } if(isset($item['music_url'])){ $item['music_url'] = $item['music_url']? get_image_url($item['music_url']) : ''; } $member = isset($item['member'])? $item['member'] : []; if($member){ $member['avatar'] = isset($member['avatar'])? get_image_url($member['avatar']) : ''; } $item['tags'] = isset($item['tags']) && $item['tags']? explode(',', $item['tags']) : []; $item['like_num'] = isset($item['like_num']) && $item['like_num']? format_num($item['like_num']) : 0; $item['collect_num'] = isset($item['collect_num']) && $item['collect_num']? format_num($item['collect_num']) : 0; $item['views'] = isset($item['views']) && $item['views']? format_num($item['views']) : 0; $item['member'] = $member; } } return [ 'pageSize' => $pageSize, 'total' => isset($list['total']) ? $list['total'] : 0, 'list' => isset($list['data']) ? $list['data'] : [] ]; } /** * 是否已经收藏或点赞过 * @param $userId * @param $collectUid * @param int $type 类型: * @return array|mixed */ public function checkCollect($userId, $collectId, $type=1) { $cacheKey = "caches:videos:collect:u{$userId}_c{$collectId}_{$type}"; $data = RedisService::get($cacheKey); if($data){ return $data? 1: 2; } $data = $this->model->where(['user_id'=> $userId,'collect_id'=> $collectId,'type'=> $type,'status'=>1,'mark'=>1])->value('id'); if($data){ RedisService::set($cacheKey, $data, rand(5, 10)); } return $data? 1 : 2; } /** * 缓存信息 * @param $userId * @param $collectId * @param $type * @return array|mixed */ public function getCollectCacheInfo($userId, $collectId, $type, $sourceType=1) { $cacheKey = "caches:videos:collect:temp_{$userId}_{$collectId}_{$type}_{$sourceType}"; $info = RedisService::get($cacheKey); if($info || RedisService::exists($cacheKey)){ return $info; } $info = $this->model->where(['user_id'=> $userId,'collect_id'=> $collectId,'source_type'=>$sourceType,'type'=> $type])->select(['id','user_id','status'])->first(); $info = $info? $info->toArray() : []; if($info){ RedisService::set($cacheKey, $info, rand(10,30)); } return $info; } /** * 推荐数据 * @param $userId 用户 * @return array|mixed */ public function getRecommendData($userId, $type=1) { $cacheKey = "caches:videos:recommend:{$userId}_{$type}"; $data = RedisService::get($cacheKey); if($data || RedisService::exists($cacheKey)){ return $data? $data : []; } $arr = ['uids'=>[$userId],'tags'=>[],'category'=>[]]; $datas = $this->model->where(['user_id'=> $userId,'source_type'=> $type,'status'=>1,'mark'=>1]) ->where('collect_uid','>', 0) ->select(['collect_uid','tags','category_id']) ->orderByRaw('rand()') ->limit(rand(3,5)) ->get(); $datas = $datas? $datas->toArray() : []; if($datas){ foreach ($datas as $item) { $uid = isset($item['collect_uid'])? $item['collect_uid'] : 0; $categoryId = isset($item['category_id'])? $item['category_id'] : 0; $tags = isset($item['tags'])? explode(',', $item['tags']) : []; if($uid){ $arr['uids'][] = $uid; } if($categoryId){ $arr['category'][] = $categoryId; } if($tags){ $tags = array_filter($tags); $arr['tags'] = array_merge($arr['tags'], $tags); $arr['tags'] = array_unique($arr['tags']); } } RedisService::set($cacheKey, $arr, rand(3600, 7200)); } return $arr; } /** * 获取直播间当前用户的打赏金额和打赏排名 * @param $userId 用户ID * @param $liveId 直播ID */ public function getRankData($userId, $liveId) { $datas = MemberModel::from('member as a') ->leftJoin('video_collect as b',function($join) use($liveId){ $join->on('a.id','=','b.user_id') ->where(['b.type'=>1,'b.collect_id'=> $liveId,'b.source_type'=>2,'b.status'=>1,'b.mark'=>1]); }) ->where(['a.status'=>1,'a.mark'=>1]) ->where('b.id','>', 0) ->select(['a.id','b.create_time','b.reward_total','b.collect_id as live_id']) ->groupBy('id') ->orderBy('b.reward_total','desc') ->orderBy('b.create_time','desc') ->limit(210) ->get(); $datas = $datas? $datas->toArray() : []; $rankData = ['rank'=>0,'reward_total'=>0]; if($datas){ foreach($datas as $k => $item) { if($item['id'] == $userId){ $rankData['rank'] = ($k+1) >200? 0 : ($k+1); $rankData['reward_total'] = $item['reward_total']; } } } // 用户排名在200名外的数据 if($rankData['rank'] <= 0 && $rankData['reward_total']<=0 && count($datas) > 200){ $rewardTotal = $this->model->where(['user_id'=> $userId,'type'=>1,'collect_id'=> $liveId,'source_type'=>2,'status'=>1,'mark'=>1]) ->value('reward_total'); $rankData['reward_total'] = $rewardTotal? floatval($rewardTotal) : 0; } return $rankData; } /** * 收藏点赞 * @param $userId * @param $dynamicId * @param $type * @param int $status * @return mixed */ public function collect($userId, $params) { try { $collectId = isset($params['id']) ? intval($params['id']) : 0; $type = isset($params['type']) ? intval($params['type']) : 2; $status = isset($params['status']) ? intval($params['status']) : 1; if ($collectId <= 0 || !in_array($type, [2, 3]) || !in_array($status, [1, 2])) { $this->error = 2014; return false; } $collectInfo = $this->getCollectCacheInfo($userId, $collectId, $type); $id = isset($collectInfo['id']) ? $collectInfo['id'] : 0; // 信息 $info = VideoModel::where(['id' => $collectId, 'mark' => 1])->select(['id', 'user_id', 'tags'])->first(); $collectUid = isset($info['user_id']) ? $info['user_id'] : 0; if (empty($info)) { $this->error = 1039; return false; } $data = [ 'user_id' => $userId, 'type' => $type, 'source_type' => 1, 'collect_id' => $collectId, 'collect_uid' => $collectUid, 'tags' => isset($info['tags']) ? $info['tags'] : '', 'update_time' => time(), 'status' => $status, 'mark' => 1, ]; DB::beginTransaction(); if (!$id) { $data['create_time'] = time(); if (!$this->model->insertGetId($data)) { DB::rollBack(); return false; } } else { if (!$this->model->where('id', $id)->update($data)) { DB::rollBack(); return false; } } $updateData = ['update_time' => time()]; if ($type == 2) { $updateData['collect_num'] = DB::raw('collect_num ' . ($status == 1 ? '+ 1' : '-1')); } else if ($type == 3) { $updateData['like_num'] = DB::raw('like_num ' . ($status == 1 ? '+ 1' : '-1')); } if (!VideoModel::where(['id' => $collectId, 'mark' => 1])->update($updateData)) { DB::rollBack(); return false; } $this->error = 1002; DB::commit(); // 消息 if($status == 1){ if($type == 2){ MessageService::make()->pushMessage($collectUid, '有人收藏了你的视频','有人收藏了你的视频', 5, $userId); }else if($type == 3){ MessageService::make()->pushMessage($collectUid, '有人点赞了你的视频','有人点赞了你的视频', 5, $userId); } // 收藏/点赞视频任务处理 TaskService::make()->updateTask($userId,$type==2?11:6, $collectId); } RedisService::clear("caches:videos:collect:u{$userId}_c{$collectId}_{$type}"); RedisService::clear("caches:videos:collect:temp_{$userId}_{$collectId}_{$type}_1"); RedisService::clear("caches:videos:recommend:{$userId}_1"); return true; }catch (\Exception $exception){ $this->error = $exception-> getMessage(); return false; } } /** * 获取(被)收藏/关注/点赞数量 * @param $userId 用户ID * @param int $type 类型:1-关注,2-收藏,3-点赞喜欢 * @param int $ctype 查询类型:1-被关注/收藏/点赞,2-关注/收藏/点赞 * @return array|mixed */ public function getCount($userId, $type=1, $ctype=1) { $cacheKey = "caches:videos:collect:{$userId}_{$type}_{$ctype}"; $data = RedisService::get($cacheKey); if($data){ return $data; } $field = 'collect_uid'; if($ctype == 2){ $field = 'user_id'; } $data = $this->model->where([$field=> $userId,'type'=>$type,'status'=>1,'mark'=>1])->count('id'); if($data){ RedisService::set($cacheKey, $data, 3600); } return $data; } /** * 取消 * @param $userId * @return bool */ public function cancel($userId) { // 参数 $ids = request()->post('ids',''); $ids = $ids? explode(',', $ids) : []; $type = request()->post('type',0); if (empty($ids) && $type == 0) { $this->error = 1033; return false; } if($type){ $this->model->where(['user_id'=> $userId,'status'=>1,'mark'=>1])->update(['status'=>2,'update_time'=>time()]); }else { $this->model->where(['user_id'=> $userId,'status'=>1,'mark'=>1])->whereIn('id', $ids)->update(['status'=>2,'update_time'=>time()]); } $this->error = 1002; return true; } }