AssetReportResource.php 2.0 KB

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