<?php
/**
 * Created by IntelliJ IDEA.
 * User: kelyliang
 * Date: 2024/3/4
 * Time: 下午 03:09
 */

namespace App\Http\Resources\API;

use App\Models\Asset;
use App\Models\AssetGroup;
use App\Models\Requirement;
use Carbon\Carbon;
use Illuminate\Http\Request;
use Illuminate\Http\Resources\Json\JsonResource;
use Illuminate\Support\Facades\Auth;

class AssetReportResource  extends JsonResource
{
    public function toArray(Request $request): array
    {
        $now = Carbon::now();
        return[
            'requirement_total' => Requirement::query()->whereIn('asset_id',$this->child_id)->count(),
            'plan_total' => $this->plans()->count(),
            'prject_total' => $this->projects()->count(),
            'plan_unexpired_total' => $planTotalCount = $this->plans()->where('begin', '<=', $now)
                ->where('end', '>=', $now)
                ->get()
                ->count(),
            'project_unexpired_total' => $planTotalCount = $this->projects()->where('begin', '<=', $now)
                ->where('end', '>=', $now)
                ->get()
                ->count(),
            'asset_total'=>Asset::query()->where('company_id',Auth::user()->company_id)->where('parent_id',0)->count(),
            'asset_closed_total'=>Asset::query()->where('company_id',Auth::user()->company_id)->where('parent_id',0)->where('status','closed')->count(),
            'id' => $this->id,
            'name' => $this->name,
            'code' => $this->code,
            'description' => $this->description,
            'status' => $this->status,
            'created_by' => $this->created_by,
            'owner' => $this->owner,
            'address' => $this->address,
            'group_id' => $this->group_id,
            'geo_address_code' => $this->geo_address_code,
            'acl' => $this->acl,
            'whitelist' => $this->whitelist,
            'latitude' => $this->latitude,
            'longitude' => $this->longitude,
            'parent_id' => $this->parent_id,
            'created_at' => $this->created_at,
        ];
    }
}