123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384 |
- <?php
- /**
- * Created by IntelliJ IDEA.
- * User: kelyliang
- * Date: 2024/3/4
- * Time: 下午 03:09
- */
- namespace App\Http\Resources\API;
- use App\Models\Approval;
- use App\Models\Asset;
- use App\Models\AssetGroup;
- use App\Models\Requirement;
- use App\Models\User;
- use Carbon\Carbon;
- use Illuminate\Http\Request;
- use Illuminate\Http\Resources\Json\JsonResource;
- use Illuminate\Support\Facades\Auth;
- use function Nette\Utils\data;
- class AssetReportResource extends JsonResource
- {
- public function toArray(Request $request): array
- {
- $now = Carbon::now();
- $whitelist=make_array_list($this->whitelist??'');
- $whitelistName=User::query()->whereIn('id',$whitelist)->get();
- $approvalCounts = Approval::query()
- ->allowed(true)
- ->selectRaw('sum(case when status = \'doing\' then 1 else 0 end) as approval_doing_count,
- sum(case when status = \'canceled\' then 1 else 0 end) as approval_canceled_count,
- sum(case when status = \'rejected\' then 1 else 0 end) as approval_rejected_count')
- ->first();
- return[
- 'requirement_total' => Requirement::query()->whereIn('asset_id',$this->child_id)->count(),
- 'plan_total' => $this->plans()->count(),
- 'project_total' => $this->projects()->count(),
- 'plan_unexpired_total' => $planTotalCount = $this->plans()->where('end', '>=', $now)
- ->get()
- ->count(),
- 'project_unexpired_total' => $planTotalCount = $this->projects()->where('end', '>=', $now)
- ->get()
- ->count(),
- 'asset_total'=>Asset::query()->allowed()->count(),
- 'asset_closed_total'=>Asset::query()->allowed()->where('status','closed')->count(),
- 'id' => $this->id,
- 'name' => $this->name,
- 'code' => $this->code,
- 'description' => $this->description?(new \App\Services\File\ImageUrlService)->getImageUrl($this->description):null,
- 'status' => $this->status,
- 'created_by' =>new UserProfileResource($this->createdBy),
- 'owner' => new UserProfileResource($this->byOwner),
- 'address' => $this->address,
- 'group_id' => new AssetGroupResource($this->assetGroup),
- 'geo_address_code' => $this->geo_address_code,
- 'acl' => $this->acl,
- 'latitude' => $this->latitude,
- 'longitude' => $this->longitude,
- 'whitelist'=>$whitelist,
- 'whitelist_name' => UserProfileResource::collection($whitelistName),
- 'parent_asset' => new AssetResource($this->parent),
- 'created_at' => (string)$this->created_at,
- 'equity_interest'=>$this->equity_interest,
- 'developer' =>$this->developer,
- 'date_completed' =>$this->date_completed,
- 'total_floor_area' => $this->total_floor_area,
- 'contact_person'=>$this->contact_person,
- 'contact_phone'=>$this->contact_phone,
- 'contact_email'=>$this->contact_email,
- 'property'=> $this->property,
- 'building_type_description'=>$this->building_type_description,
- 'children' => $this->when($this->children->isNotEmpty(), function () {
- return $this->children->map(function ($child) {
- return new AssetResource($child, $this->level);
- })->all();
- }),
- 'area_unit'=>$this->area_unit,
- 'approval_doing_count' => $approvalCounts->approval_doing_count,
- 'approval_canceled_count' => $approvalCounts->approval_canceled_count,
- 'approval_rejected_count' => $approvalCounts->approval_rejected_count,
- ];
- }
- }
|