wesmiler 2 vuotta sitten
vanhempi
commit
de786d388b
2 muutettua tiedostoa jossa 31 lisäystä ja 12 poistoa
  1. 30 11
      app/Services/Api/VideoCollectService.php
  2. 1 1
      app/Services/Api/VideoService.php

+ 30 - 11
app/Services/Api/VideoCollectService.php

@@ -17,6 +17,7 @@ use App\Models\VideoModel;
 use App\Services\BaseService;
 use App\Services\BaseService;
 use App\Services\RedisService;
 use App\Services\RedisService;
 use BN\Red;
 use BN\Red;
+use Illuminate\Support\Facades\DB;
 
 
 /**
 /**
  * 短视频收藏点赞管理-服务类
  * 短视频收藏点赞管理-服务类
@@ -236,7 +237,7 @@ class VideoCollectService extends BaseService
         $collectId = isset($params['id'])? intval($params['id']) : 0;
         $collectId = isset($params['id'])? intval($params['id']) : 0;
         $type = isset($params['type'])? intval($params['type']) : 2;
         $type = isset($params['type'])? intval($params['type']) : 2;
         $status = isset($params['status'])? intval($params['status']) : 1;
         $status = isset($params['status'])? intval($params['status']) : 1;
-        if($collectId<=0 || !in_array($type, [1,2,3]) || !in_array($status, [1,2,3])){
+        if($collectId<=0 || !in_array($type, [2,3]) || !in_array($status, [2,3])){
             $this->error = 2014;
             $this->error = 2014;
             return false;
             return false;
         }
         }
@@ -267,20 +268,38 @@ class VideoCollectService extends BaseService
             'status'=> $status,
             'status'=> $status,
             'mark'=> 1,
             'mark'=> 1,
         ];
         ];
+
+        DB::beginTransaction();
         if(!$id){
         if(!$id){
             $data['create_time'] = time();
             $data['create_time'] = time();
-            $this->error = 1002;
-            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 $this->model->insertGetId($data);
+            if(!$this->model->insertGetId($data)){
+                DB::rollBack();
+                return false;
+            }
         }else{
         }else{
-            $this->error = 1002;
-            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 $this->model->where('id', $id)->update($data);
+            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();
+        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;
     }
     }
 
 
     /**
     /**

+ 1 - 1
app/Services/Api/VideoService.php

@@ -350,7 +350,7 @@ class VideoService extends BaseService
         // 浏览量
         // 浏览量
         $this->updateView($userId, $id);
         $this->updateView($userId, $id);
         $this->error = 1010;
         $this->error = 1010;
-        return ;
+        return true;
     }
     }
 
 
     /**
     /**