AssetController.php 6.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204
  1. <?php
  2. namespace App\Http\Controllers\API;
  3. use App\Http\Controllers\Controller;
  4. use App\Http\Requests\API\Asset\CreateOrUpdateRequest;
  5. use App\Http\Resources\API\AssetReportResource;
  6. use App\Http\Resources\API\AssetResource;
  7. use App\Models\Asset;
  8. use App\Models\Enums\ActionObjectType;
  9. use App\Models\Enums\ObjectAction;
  10. use App\Models\File;
  11. use App\Models\User;
  12. use App\Repositories\ActionRepository;
  13. use App\Services\File\ImageUrlService;
  14. use App\Services\History\ModelChangeDetector;
  15. use App\Services\User\UserService;
  16. use Illuminate\Http\Request;
  17. use Illuminate\Support\Facades\Auth;
  18. class AssetController extends Controller
  19. {
  20. /**
  21. * Display a listing of the resource.
  22. */
  23. public function index(Request $request)
  24. {
  25. $fullAsset=[];
  26. $sort=$request->input('sort','desc');
  27. $paths = Asset::filter($request->all())
  28. ->where('company_id',Auth::user()->company_id)
  29. ->allowed()->pluck('path')->toArray();
  30. foreach ($paths as $path){
  31. $parentAssets = explode(',', substr($path, 1, -1));
  32. $fullAsset= array_merge($parentAssets,$fullAsset);
  33. }
  34. $resultAssets = Asset::whereIn('id', $fullAsset)->orderBy('created_at',$sort)->get()->each(function ($asset) {
  35. $asset->requirement_total = $asset->total_requirements_count;
  36. $asset->plan_total = $asset->total_plans_count;
  37. });
  38. $resultAssetsTree=make_tree($resultAssets->toArray());
  39. return $this->success([
  40. 'data' => $resultAssetsTree
  41. ]);
  42. }
  43. //公共Asset查询
  44. public function publicSearch(Request $request)
  45. {
  46. $fullAsset=[];
  47. $paths = Asset::filter($request->all())
  48. ->where('company_id',Auth::user()->company_id)
  49. ->allowed()->pluck('path')->toArray();
  50. foreach ($paths as $path){
  51. $parentAssets = explode(',', substr($path, 1, -1));
  52. $fullAsset= array_merge($parentAssets,$fullAsset);
  53. }
  54. $resultAssets = Asset::whereIn('id', $fullAsset)->orderByDesc('created_at')->get();
  55. $resultAssetsTree=make_tree($resultAssets->toArray());
  56. return $this->success([
  57. 'data' => $resultAssetsTree
  58. ]);
  59. }
  60. /**
  61. * Store a newly created resource in storage.
  62. */
  63. public function store(CreateOrUpdateRequest $request)
  64. {
  65. $asset = Asset::create([
  66. ...$request->all(),
  67. 'company_id' => Auth::user()->company_id,
  68. 'whitelist' => $request->whitelist ? sprintf(",%s,", implode(',', $request->whitelist)) : null,
  69. 'description' => $request->description? (new \App\Services\File\ImageUrlService)->interceptImageUrl($request->description) : null,
  70. 'created_by' => Auth::id(),
  71. ]);
  72. $parentAsset = $request->parent_id > 0
  73. ? Asset::query()->findOrFail($request->parent_id)
  74. : null;
  75. $asset->path = $parentAsset ? $parentAsset?->path . $asset->id . "," : sprintf(",%s,", $asset->id);
  76. $asset->save();
  77. ActionRepository::createByAsset($asset,ObjectAction::CREATED);
  78. return $this->created();
  79. }
  80. /**
  81. * Display the specified resource.
  82. */
  83. public function show(string $id)
  84. {
  85. $asset = Asset::allowed()->findOrFail($id);
  86. return new AssetResource($asset);
  87. }
  88. /**
  89. * Update the specified resource in storage.
  90. */
  91. public function update(CreateOrUpdateRequest $request, string $id,UserService $service)
  92. {
  93. $asset = Asset::allowed()->findOrFail($id);
  94. $parentAsset = $request->parent_id > 0
  95. ? Asset::query()->findOrFail($request->parent_id)
  96. : null;
  97. $isUpdateParent = $parentAsset?->id != $asset->parent_id;
  98. $path = $isUpdateParent ? $parentAsset->path . $asset->id . "," : $asset->path;
  99. $formData = [
  100. ...$request->all(),
  101. 'whitelist' => $request->whitelist ? sprintf(",%s,", implode(',', $request->whitelist)) : null,
  102. 'description' => $request->description? (new \App\Services\File\ImageUrlService)->interceptImageUrl($request->description) : null,
  103. 'path' => $path
  104. ];
  105. if ($isUpdateParent) {
  106. $children = Asset::query()
  107. ->where("id", "!=", $asset->id)
  108. ->where("path", "like", "%," . $asset->id . ",%")
  109. ->get();
  110. foreach ($children as $child) {
  111. $child->fill([
  112. 'path' => str_replace($asset->path, $path, $child->path),
  113. ]);
  114. $child->save();
  115. }
  116. }
  117. $asset->fill($formData);
  118. $changes = ModelChangeDetector::detector(ActionObjectType::ASSET, $asset);
  119. $asset->save();
  120. ActionRepository::createByAsset(
  121. $asset, ObjectAction::EDITED,objectChanges: $changes
  122. );
  123. return $this->noContent();
  124. }
  125. /**
  126. * Remove the specified resource from storage.
  127. */
  128. public function destroy(string $id)
  129. {
  130. $asset = Asset::allowed()->findOrFail($id);
  131. if(!empty($asset->children()->first())){
  132. throw new \Exception("Please remove the sub-level assets.");
  133. }
  134. $asset->delete();
  135. ActionRepository::createByAsset($asset,ObjectAction::DELETED);
  136. return $this->noContent();
  137. }
  138. public function report(string $id){
  139. $asset = Asset::query()->allowed()->with(['requirements','plans','projects','children'])->findOrFail($id);
  140. //$asset = Asset::find($id);
  141. $assets=Asset::query()->allowed()->where("path","like","%,".$id.",%")->pluck("id");
  142. $asset->setAttribute('child_id',$assets);
  143. return new AssetReportResource($asset);
  144. }
  145. public function tree()
  146. {
  147. $attachmentArray = [];
  148. $assets = Asset::select(['id', 'name', 'parent_id'])->withCount('library')->allowed()->get()
  149. ->each(function ($assets) use (&$attachmentArray) {
  150. $attachmentArray[]=[
  151. 'parent_id'=>$assets->id,
  152. 'type'=>'attachment',
  153. 'name'=>'attachment',
  154. 'uuId'=>'attachment_'.$assets->id,
  155. 'id'=>'asset_id_'.$assets->id,
  156. ];
  157. $assets->type='asset';
  158. $assets->uniId=$assets->type.'_'.$assets->id;
  159. });
  160. return $this->success([
  161. 'data' => make_tree(array_merge($assets->toArray(),$attachmentArray))
  162. ]);
  163. }
  164. public function dynamic(Request $request, string $id)
  165. {
  166. $asset = Asset::allowed($id)->findOrFail($id);
  167. return $this->success([
  168. 'data' => ActionRepository::AssetDynamic($asset, $request->all())
  169. ]);
  170. }
  171. }