AssetController.php 6.4 KB

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