AssetReportResource.php 2.0 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556
  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\Asset;
  10. use App\Models\AssetGroup;
  11. use App\Models\Requirement;
  12. use Carbon\Carbon;
  13. use Illuminate\Http\Request;
  14. use Illuminate\Http\Resources\Json\JsonResource;
  15. use Illuminate\Support\Facades\Auth;
  16. class AssetReportResource extends JsonResource
  17. {
  18. public function toArray(Request $request): array
  19. {
  20. $now = Carbon::now();
  21. return[
  22. 'requirement_total' => Requirement::query()->whereIn('asset_id',$this->child_id)->count(),
  23. 'plan_total' => $this->plans()->count(),
  24. 'prject_total' => $this->projects()->count(),
  25. 'plan_unexpired_total' => $planTotalCount = $this->plans()->where('begin', '<=', $now)
  26. ->where('end', '>=', $now)
  27. ->get()
  28. ->count(),
  29. 'project_unexpired_total' => $planTotalCount = $this->projects()->where('begin', '<=', $now)
  30. ->where('end', '>=', $now)
  31. ->get()
  32. ->count(),
  33. 'asset_total'=>Asset::query()->where('company_id',Auth::user()->company_id)->where('parent_id',0)->count(),
  34. 'asset_closed_total'=>Asset::query()->where('company_id',Auth::user()->company_id)->where('parent_id',0)->where('status','closed')->count(),
  35. 'id' => $this->id,
  36. 'name' => $this->name,
  37. 'code' => $this->code,
  38. 'description' => $this->description,
  39. 'status' => $this->status,
  40. 'created_by' => $this->created_by,
  41. 'owner' => $this->owner,
  42. 'address' => $this->address,
  43. 'group_id' => $this->group_id,
  44. 'geo_address_code' => $this->geo_address_code,
  45. 'acl' => $this->acl,
  46. 'whitelist' => $this->whitelist,
  47. 'latitude' => $this->latitude,
  48. 'longitude' => $this->longitude,
  49. 'parent_id' => $this->parent_id,
  50. 'created_at' => $this->created_at,
  51. ];
  52. }
  53. }