input('sort','desc'); $paths = Asset::filter($request->all()) ->where('company_id',Auth::user()->company_id) ->allowed()->pluck('path')->toArray(); foreach ($paths as $path){ $parentAssets = explode(',', substr($path, 1, -1)); $fullAsset= array_merge($parentAssets,$fullAsset); } $resultAssets = Asset::whereIn('id', $fullAsset)->orderBy('created_at',$sort)->get()->each(function ($asset) { $asset->requirement_total = $asset->total_requirements_count; $asset->plan_total = $asset->total_plans_count; }); $resultAssetsTree=make_tree($resultAssets->toArray()); return $this->success([ 'data' => $resultAssetsTree ]); } //公共Asset查询 public function publicSearch(Request $request) { $fullAsset=[]; $paths = Asset::filter($request->all()) ->where('company_id',Auth::user()->company_id) ->allowed()->pluck('path')->toArray(); foreach ($paths as $path){ $parentAssets = explode(',', substr($path, 1, -1)); $fullAsset= array_merge($parentAssets,$fullAsset); } $resultAssets = Asset::whereIn('id', $fullAsset)->orderByDesc('created_at')->get(); $resultAssetsTree=make_tree($resultAssets->toArray()); return $this->success([ 'data' => $resultAssetsTree ]); } /** * Store a newly created resource in storage. */ public function store(CreateOrUpdateRequest $request) { $asset = Asset::create([ ...$request->all(), 'company_id' => Auth::user()->company_id, 'whitelist' => $request->whitelist ? sprintf(",%s,", implode(',', $request->whitelist)) : null, 'description' => $request->description? (new \App\Services\File\ImageUrlService)->interceptImageUrl($request->description) : null, 'created_by' => Auth::id(), ]); $parentAsset = $request->parent_id > 0 ? Asset::query()->findOrFail($request->parent_id) : null; $asset->path = $parentAsset ? $parentAsset?->path . $asset->id . "," : sprintf(",%s,", $asset->id); $asset->save(); ActionRepository::createByAsset($asset,ObjectAction::CREATED); return $this->created(); } /** * Display the specified resource. */ public function show(string $id) { $asset = Asset::allowed()->findOrFail($id); return new AssetResource($asset); } /** * Update the specified resource in storage. */ public function update(CreateOrUpdateRequest $request, string $id,UserService $service) { $asset = Asset::allowed()->findOrFail($id); $parentAsset = $request->parent_id > 0 ? Asset::query()->findOrFail($request->parent_id) : null; $isUpdateParent = $parentAsset?->id != $asset->parent_id; $path = $isUpdateParent ? $parentAsset->path . $asset->id . "," : $asset->path; $formData = [ ...$request->all(), 'whitelist' => $request->whitelist ? sprintf(",%s,", implode(',', $request->whitelist)) : null, 'description' => $request->description? (new \App\Services\File\ImageUrlService)->interceptImageUrl($request->description) : null, 'path' => $path ]; if ($isUpdateParent) { $children = Asset::query() ->where("id", "!=", $asset->id) ->where("path", "like", "%," . $asset->id . ",%") ->get(); foreach ($children as $child) { $child->fill([ 'path' => str_replace($asset->path, $path, $child->path), ]); $child->save(); } } $asset->fill($formData); $changes = ModelChangeDetector::detector(ActionObjectType::ASSET, $asset); $asset->save(); ActionRepository::createByAsset( $asset, ObjectAction::EDITED,objectChanges: $changes ); return $this->noContent(); } /** * Remove the specified resource from storage. */ public function destroy(string $id) { $asset = Asset::allowed()->findOrFail($id); if(!empty($asset->children()->first())){ throw new \Exception("Please remove the sub-level assets."); } $asset->delete(); ActionRepository::createByAsset($asset,ObjectAction::DELETED); return $this->noContent(); } public function report(string $id){ $asset = Asset::query()->allowed()->with(['requirements','plans','projects','children'])->findOrFail($id); //$asset = Asset::find($id); $assets=Asset::query()->allowed()->where("path","like","%,".$id.",%")->pluck("id"); $asset->setAttribute('child_id',$assets); return new AssetReportResource($asset); } public function tree() { $attachmentArray = []; $assets = Asset::select(['id', 'name', 'parent_id'])->withCount('library')->allowed()->get() ->each(function ($assets) use (&$attachmentArray) { $attachmentArray[]=[ 'parent_id'=>$assets->id, 'type'=>'attachment', 'name'=>'attachment', 'uuId'=>'attachment_'.$assets->id, 'id'=>'asset_id_'.$assets->id, ]; $assets->type='asset'; $assets->uniId=$assets->type.'_'.$assets->id; }); return $this->success([ 'data' => make_tree(array_merge($assets->toArray(),$attachmentArray)) ]); } public function dynamic(Request $request, string $id) { $asset = Asset::allowed($id)->findOrFail($id); return $this->success([ 'data' => ActionRepository::AssetDynamic($asset, $request->all()) ]); } }