get('view')) { case 'anthology': $table = ArticleCollection::where('collect_id',$request->get('id')); break; case 'article': $table = ArticleCollection::where('article_id',$request->get('id')); break; } $result = $table->select(['id','collect_id','article_id','level','title','children','deleted_at']) ->orderBy('id')->get(); return $this->ok(["rows"=>ArticleMapResource::collection($result),"count"=>count($result)]); } /** * Store a newly created resource in storage. * * @param \Illuminate\Http\Request $request * @return \Illuminate\Http\Response */ public function store(Request $request) { // $validated = $request->validate([ 'anthology_id' => 'required', 'operation' => 'required' ]); switch ($validated['operation']) { case 'add': # 添加多个文章到文集 $count=0; foreach ($request->get('article_id') as $key => $article) { # code... if(!ArticleCollection::where('article_id',$article) ->where('collect_id',$request->get('anthology_id')) ->exists()) { $new = new ArticleCollection; $new->id = app('snowflake')->id(); $new->article_id = $article; $new->collect_id = $request->get('anthology_id'); $new->title = Article::find($article)->title; $new->level = 1; $new->save(); $count++; } } return $this->ok($count); break; default: return $this->error('unknown operation'); break; } } /** * Display the specified resource. * * @param \App\Models\ArticleCollection $articleCollection * @return \Illuminate\Http\Response */ public function show(ArticleCollection $articleCollection) { // } /** * Update the specified resource in storage. * * @param \Illuminate\Http\Request $request * @param string $id * @return \Illuminate\Http\Response */ public function update(Request $request, string $id) { // $validated = $request->validate([ 'operation' => 'required' ]); switch ($validated['operation']) { case 'anthology': $delete = ArticleCollection::where('collect_id',$id)->delete(); $count=0; foreach ($request->get('data') as $key => $row) { # code... $new = new ArticleCollection; $new->id = app('snowflake')->id(); $new->article_id = $row["article_id"]; $new->collect_id = $id; $new->title = $row["title"]; $new->level = $row["level"]; $new->children = $row["children"]; $new->save(); $count++; } ArticleMapController::updateCollection($id); return $this->ok($count); break; } } /** * Remove the specified resource from storage. * * @param \App\Models\ArticleCollection $articleCollection * @return \Illuminate\Http\Response */ public function destroy(ArticleCollection $articleCollection) { // } public static function deleteArticle(string $articleId){ //查找有这个文章的文集 $collections = ArticleCollection::where('article_id',$articleId) ->select('collect_id') ->groupBy('collect_id') ->get(); //设置为删除 ArticleCollection::where('article_id',$articleId) ->update(['deleted_at'=>now()]); //查找没有下级文章的文集 $updateCollections = ArticleCollection::where('article_id',$articleId) ->where('children',0) ->select('collect_id') ->groupBy('collect_id') ->get(); //真的删除没有下级文章的文集中的文章 $count = ArticleCollection::where('article_id',$articleId) ->where('children',0) ->delete(); //更新改动的文集 foreach ($updateCollections as $collection) { # code... ArticleMapController::updateCollection($collection->collect_id); } return [count($collections),$count]; } public static function deleteCollection(string $collectionId){ $count = ArticleCollection::where('collect_id',$collectionId) ->delete(); return $count; } /** * 用表中的数据生成json,更新collection 表中的字段 */ public static function updateCollection(string $collectionId){ $result = ArticleCollection::where('collect_id',$collectionId) ->select(['article_id','level','title']) ->orderBy('id')->get(); Collection::where('uid',$collectionId) ->update(['article_list'=>json_encode($result,JSON_UNESCAPED_UNICODE)]); return count($result); } }