<?php

namespace App\Http\Controllers;

use App\Http\Resources\PermissionResource;
use App\Http\Requests\UsersRequest;
use App\Http\Resources\UserResource;
use App\Laravue\JsonResponse;
use App\Laravue\Models\Device;
use Illuminate\Http\Request;
use Illuminate\Http\Resources\Json\ResourceCollection;
use Illuminate\Support\Arr;
use Illuminate\Support\Facades\Auth;
use Illuminate\Support\Facades\Config;
use Illuminate\Support\Facades\DB;
use Illuminate\Support\Facades\Hash;
use Illuminate\Support\Facades\Redis;
use Validator;

class HuinappHomeController extends controller
{
    //设备详情
    public function deviceDataInfo($id){
        $data = DB::table('device as d')
            ->join('device_type as t','d.dtype','=','t.tid')
            ->join('gas as g','d.status','=','g.id')
            ->join('danwei as  w','d.devicemonad','=','w.id')
            ->join('status as s','d.devicepolice','=','s.id')
            ->join('users as u','d.uid','=','u.id')
            ->where('d.id','=',$id)
            ->select('d.*','t.tname','g.gas','w.danwei','s.status_name','u.username','u.isadmin')
            ->first();
        return $this->jsonSuccessData($data);
    }
    //返回设备类型和单位和介质
    public function devicetype()
    {
        $gas = DB::table('gas')->get()->toArray();
        $danwei = DB::table('danwei')->get()->toArray();
        $devicetypecount = DB::table('device_type as t')
            ->leftjoin('device as d', 'd.dtype', '=', 't.tid')
            ->selectRaw('t.*,count(d.id) as counnum')
            ->groupBy('t.tid')
            ->get()->toArray();
        return $this->jsonSuccessData(['devicetype' => $devicetypecount, 'gas' => $gas, 'danwei' => $danwei]);
    }
}

