|
|
@@ -169,15 +169,15 @@ class VideoCollectService extends BaseService
|
|
|
* @param $type
|
|
|
* @return array|mixed
|
|
|
*/
|
|
|
- public function getCollectCacheInfo($userId, $collectId, $type)
|
|
|
+ public function getCollectCacheInfo($userId, $collectId, $type, $sourceType=1)
|
|
|
{
|
|
|
- $cacheKey = "caches:videos:collect:temp_{$userId}_{$collectId}_{$type}";
|
|
|
+ $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,'type'=> $type])->select(['id','user_id','status'])->first();
|
|
|
+ $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));
|
|
|
@@ -186,18 +186,23 @@ class VideoCollectService extends BaseService
|
|
|
return $info;
|
|
|
}
|
|
|
|
|
|
- public function getRecommendData($userId)
|
|
|
+ /**
|
|
|
+ * 推荐数据
|
|
|
+ * @param $userId 用户
|
|
|
+ * @return array|mixed
|
|
|
+ */
|
|
|
+ public function getRecommendData($userId, $type=1)
|
|
|
{
|
|
|
- $cacheKey = "caches:videos:recommend:{$userId}";
|
|
|
+ $cacheKey = "caches:videos:recommend:{$userId}_{$type}";
|
|
|
$data = RedisService::get($cacheKey);
|
|
|
if($data || RedisService::exists($cacheKey)){
|
|
|
return $data? $data : [];
|
|
|
}
|
|
|
|
|
|
- $arr = ['uids'=>[$userId],'tags'=>[]];
|
|
|
- $datas = $this->model->where(['user_id'=> $userId,'status'=>1,'mark'=>1])
|
|
|
+ $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'])
|
|
|
+ ->select(['collect_uid','tags','category_id'])
|
|
|
->orderRaw('rand()')
|
|
|
->limit(rand(3,5))
|
|
|
->get();
|
|
|
@@ -206,11 +211,16 @@ class VideoCollectService extends BaseService
|
|
|
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);
|
|
|
@@ -234,67 +244,73 @@ class VideoCollectService extends BaseService
|
|
|
*/
|
|
|
public function collect($userId, $params)
|
|
|
{
|
|
|
- $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;
|
|
|
- }
|
|
|
+ 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;
|
|
|
+ $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;
|
|
|
- }
|
|
|
+ // 信息
|
|
|
+ $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,
|
|
|
- 'collect_id'=> $collectId,
|
|
|
- 'collect_uid'=> $collectUid,
|
|
|
- 'tags'=> isset($info['tags'])? $info['tags'] : '',
|
|
|
- 'update_time'=> time(),
|
|
|
- 'status'=> $status,
|
|
|
- 'mark'=> 1,
|
|
|
- ];
|
|
|
+ $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;
|
|
|
+ }
|
|
|
+ }
|
|
|
|
|
|
- DB::beginTransaction();
|
|
|
- if(!$id){
|
|
|
- $data['create_time'] = time();
|
|
|
- if(!$this->model->insertGetId($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'));
|
|
|
}
|
|
|
- }else{
|
|
|
- if(!$this->model->where('id', $id)->update($data)){
|
|
|
+ if (!VideoModel::where(['id' => $collectId, 'mark' => 1])->update($updateData)) {
|
|
|
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();
|
|
|
+ $this->error = 1002;
|
|
|
+ DB::commit();
|
|
|
+ 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;
|
|
|
}
|
|
|
-
|
|
|
- $this->error = 1002;
|
|
|
- DB::commit();
|
|
|
- RedisService::clear("caches:videos:collect:u{$userId}_c{$collectId}_{$type}");
|
|
|
- RedisService::clear("caches:videos:collect:temp_{$userId}_{$collectId}_{$type}");
|
|
|
- RedisService::clear("caches:videos:recommend:{$userId}");
|
|
|
- return true;
|
|
|
}
|
|
|
|
|
|
/**
|