Explorar o código

批量替换内容话题

wzq %!s(int64=5) %!d(string=hai) anos
pai
achega
7c85951b1d

+ 1 - 0
app/Http/Controllers/ConfigController.php

@@ -44,6 +44,7 @@ class ConfigController extends Controller
                 'add_data' => '增加数据',
                 'delete' => '删除内容',
                 'update_topic' => '编辑内容话题',
+                'change_topic' => '替换内容话题',
                 'suggest' => '推荐',
                 'cancel_suggest' => '取消推荐',
             ],

+ 44 - 0
app/Http/Controllers/Post/PostController.php

@@ -13,6 +13,7 @@ use App\Transformers\Post\CommentTransformer;
 use App\Transformers\Post\DetailTransformer;
 use App\Transformers\Post\LogTransformer;
 use App\Transformers\Post\PostStoreTransformer;
+use App\Transformers\Post\PostTopicTransformer;
 use App\Transformers\Post\PostTransformer;
 use App\Transformers\Post\StoreDetailTransformer;
 use Carbon\Carbon;
@@ -432,4 +433,47 @@ class PostController extends Controller
         $post = $this->postRepository->getStoreDetail($request->all());
         return $this->response->item($post, new StoreDetailTransformer());
     }
+
+    /**
+     * 内容列表(批量替换话题)
+     */
+    public function topicList(Request $request)
+    {
+        $productList = $this->postRepository->topicList($request->all());
+        $fractal = new Manager();
+        $resource = new Collection($productList, new PostTopicTransformer());
+        $resource->setPaginator(new IlluminatePaginatorAdapter($productList));
+        $data = $fractal->createData($resource)->toArray();
+        $data['extra'] = [
+            'filters' => [
+                'topic_id'
+            ],
+            'columns' => [
+                'id',
+                'created_at',
+                'uid',
+                'topic',
+                'img',
+                'content',
+            ]
+        ];
+        return $data;
+    }
+
+    /**
+     * 批量替换话题
+     */
+    public function TopicUpdate(Request $request)
+    {
+        $validator = Validator::make($request->all(), [
+            'topic_id' => 'required|integer',
+            'to_topic_id' => 'required|integer',
+            'ids' => 'required|array',
+            'ids.*' => 'required|integer',
+        ]);
+        if ($validator->fails()) {
+            return $this->response->error($validator->errors()->first(), 500);
+        }
+        return  $this->postRepository->TopicUpdate($request->all());
+    }
 }

+ 78 - 1
app/Repositories/Post/PostRepository.php

@@ -1164,7 +1164,7 @@ class PostRepository
         $token = JWTAuth::decode(JWTAuth::getToken());
         if ($token['type'] != 1) {
             return Response::create([
-                'message' => '只有运营才能删除内容',
+                'message' => '只有运营才能编辑内容话题',
                 'status_code' => 500
             ]);
         }
@@ -1396,4 +1396,81 @@ class PostRepository
     {
         return $this->postStore->find($request['id']);
     }
+
+    /**
+     * 内容列表(批量替换话题)
+     */
+    public function topicList($request)
+    {
+        $perPage = isset($request['per_page']) ? $request['per_page'] : 20;
+
+        return $this->post
+            ->where(function ($query) use ($request) {
+                if (isset($request['topic_id']) && $request['topic_id']) {
+                    $query->whereRaw('FIND_IN_SET(' . $request['topic_id'] . ',topic_ids)');
+                }else{
+                    $query->where('id', 0);
+                }
+            })
+            ->orderBy('id', 'desc')
+            ->paginate($perPage);
+    }
+
+    /**
+     * 批量替换话题
+     */
+    public function TopicUpdate($request)
+    {
+        $token = JWTAuth::decode(JWTAuth::getToken());
+        if ($token['type'] != 1) {
+            return Response::create([
+                'message' => '只有运营才能替换内容话题',
+                'status_code' => 500
+            ]);
+        }
+        $uid = $token['user']->id;
+        $username = $token['user']->username;
+        $topicId = $request['topic_id'];
+        $toTopicId = $request['to_topic_id'];
+
+
+        $this->post
+            ->whereIn('id',$request['ids'])
+            ->whereRaw('FIND_IN_SET(' . $topicId . ',topic_ids)')
+            ->chunk(100, function ($posts) use ($uid, $username, $topicId, $toTopicId) {
+                foreach($posts as $post){
+                    DB::beginTransaction();
+                    try {
+                        $topicIds = explode(',', $post->topic_ids);
+                        if(in_array($toTopicId, $topicIds)) continue;
+                        foreach($topicIds as &$val){
+                            if($val == $topicId) $val = $toTopicId;
+                        }
+                        $post->topic_ids = implode(',', $topicIds);
+                        $post->save();
+                        $this->postLog->create([
+                            'uid' => $uid,
+                            'operator_type' => 'admin',
+                            'post_id' => $post->id,
+                            'username' => $username,
+                            'log_type' => 'change_topic',
+                            'content' => json_encode(['change_topic' => "{$post->id}:{$topicId} => {$toTopicId}"]),
+                        ]);
+
+
+                        Log::debug("post_id".$post->id);
+
+                        DB::commit();
+                        Redis::HSET('post_info_' . $post->id, 'topic_ids', $post->topic_ids);
+                        Redis::zincrby('topic.just_use_count', 1, $toTopicId);
+
+                    } catch (QueryException $exception) {
+                        DB::rollBack();
+                        Log::debug('替换内容话题失败:'  . $exception->getMessage());
+                    }
+                }
+
+                return Response::create();
+            });
+    }
 }

+ 1 - 0
app/Transformers/Post/LogTransformer.php

@@ -22,6 +22,7 @@ class LogTransformer extends TransformerAbstract
             'add_share_count' => '增加分享数:',
             'delete' => '删除内容:',
             'update_topic' => '编辑内容话题:',
+            'change_topic' => '替换内容话题:',
             'suggest' => '推荐内容:',
             'cancel_suggest' => '取消推荐内容:',
         ];

+ 26 - 0
app/Transformers/Post/PostTopicTransformer.php

@@ -0,0 +1,26 @@
+<?php
+
+namespace App\Transformers\Post;
+
+use App\Models\Post;
+use App\Traits\PostTrait;
+use Illuminate\Support\Carbon;
+use League\Fractal\TransformerAbstract;
+
+class PostTopicTransformer extends TransformerAbstract
+{
+    use PostTrait;
+    public function transform(Post $post)
+    {
+        return [
+            'id' => $post['id'],
+            'created_at' => Carbon::parse($post['created_at'])->toDateTimeString(),
+            'uid' => $post['uid'],
+            'username' => $post['username'],
+            'avatar' => $post['avatar'],
+            'topic' => $this->getTopic($post['topic_ids'], 1),
+            'img' => $post['img'],
+            'content' => subtext(strip_tags($post['content']), 20),
+        ];
+    }
+}

+ 4 - 0
routes/api.php

@@ -45,6 +45,10 @@ $api->version('v1', [
             $api->post('post', 'PostController@create');
             //编辑发布内容话题
             $api->put('post/topic', 'PostController@updateTopic');
+            //内容列表(批量替换话题)
+            $api->get('post/topic/list', 'PostController@TopicList');
+            //批量替换话题
+            $api->put('post/topic/update', 'PostController@TopicUpdate');
             //内容列表
             $api->get('post', 'PostController@index');
             //内容详情