AssetReportResource.php 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384
  1. <?php
  2. /**
  3. * Created by IntelliJ IDEA.
  4. * User: kelyliang
  5. * Date: 2024/3/4
  6. * Time: 下午 03:09
  7. */
  8. namespace App\Http\Resources\API;
  9. use App\Models\Approval;
  10. use App\Models\Asset;
  11. use App\Models\AssetGroup;
  12. use App\Models\Requirement;
  13. use App\Models\User;
  14. use Carbon\Carbon;
  15. use Illuminate\Http\Request;
  16. use Illuminate\Http\Resources\Json\JsonResource;
  17. use Illuminate\Support\Facades\Auth;
  18. use function Nette\Utils\data;
  19. class AssetReportResource extends JsonResource
  20. {
  21. public function toArray(Request $request): array
  22. {
  23. $now = Carbon::now();
  24. $whitelist=make_array_list($this->whitelist??'');
  25. $whitelistName=User::query()->whereIn('id',$whitelist)->get();
  26. $approvalCounts = Approval::query()
  27. ->allowed(true)
  28. ->selectRaw('sum(case when status = \'doing\' then 1 else 0 end) as approval_doing_count,
  29. sum(case when status = \'canceled\' then 1 else 0 end) as approval_canceled_count,
  30. sum(case when status = \'rejected\' then 1 else 0 end) as approval_rejected_count')
  31. ->first();
  32. return[
  33. 'requirement_total' => Requirement::query()->whereIn('asset_id',$this->child_id)->count(),
  34. 'plan_total' => $this->plans()->count(),
  35. 'project_total' => $this->projects()->count(),
  36. 'plan_unexpired_total' => $planTotalCount = $this->plans()->where('end', '>=', $now)
  37. ->get()
  38. ->count(),
  39. 'project_unexpired_total' => $planTotalCount = $this->projects()->where('end', '>=', $now)
  40. ->get()
  41. ->count(),
  42. 'asset_total'=>Asset::query()->allowed()->count(),
  43. 'asset_closed_total'=>Asset::query()->allowed()->where('status','closed')->count(),
  44. 'id' => $this->id,
  45. 'name' => $this->name,
  46. 'code' => $this->code,
  47. 'description' => $this->description?(new \App\Services\File\ImageUrlService)->getImageUrl($this->description):null,
  48. 'status' => $this->status,
  49. 'created_by' =>new UserProfileResource($this->createdBy),
  50. 'owner' => new UserProfileResource($this->byOwner),
  51. 'address' => $this->address,
  52. 'group_id' => new AssetGroupResource($this->assetGroup),
  53. 'geo_address_code' => $this->geo_address_code,
  54. 'acl' => $this->acl,
  55. 'latitude' => $this->latitude,
  56. 'longitude' => $this->longitude,
  57. 'whitelist'=>$whitelist,
  58. 'whitelist_name' => UserProfileResource::collection($whitelistName),
  59. 'parent_asset' => new AssetResource($this->parent),
  60. 'created_at' => (string)$this->created_at,
  61. 'equity_interest'=>$this->equity_interest,
  62. 'developer' =>$this->developer,
  63. 'date_completed' =>$this->date_completed,
  64. 'total_floor_area' => $this->total_floor_area,
  65. 'contact_person'=>$this->contact_person,
  66. 'contact_phone'=>$this->contact_phone,
  67. 'contact_email'=>$this->contact_email,
  68. 'property'=> $this->property,
  69. 'building_type_description'=>$this->building_type_description,
  70. 'children' => $this->when($this->children->isNotEmpty(), function () {
  71. return $this->children->map(function ($child) {
  72. return new AssetResource($child, $this->level);
  73. })->all();
  74. }),
  75. 'area_unit'=>$this->area_unit,
  76. 'approval_doing_count' => $approvalCounts->approval_doing_count,
  77. 'approval_canceled_count' => $approvalCounts->approval_canceled_count,
  78. 'approval_rejected_count' => $approvalCounts->approval_rejected_count,
  79. ];
  80. }
  81. }