<?php

namespace App\Http\Controllers\Auth;

use Illuminate\Support\Facades\DB;
use App\Laravue\Models\Users;
use Illuminate\Http\Request;
use App\Http\Controllers\Controller;
use Illuminate\Support\Facades\Auth;
use App\Http\Controllers\DevicesController;
class  SwooleCommandController extends Controller
{
    //返回用户列表
    public function userslist(): string
    {
        //查询用户数量
        $usercount = DB::table('users')->count();//获取用户数量
        //查询设备类型
        $count = DB::table('users as b')
            ->leftjoin('device as d', 'b.id', '=', 'd.uid')
            ->leftjoin('device_type as t', 'd.dtype', '=', 't.tid')
            ->selectRaw('b.name,count(IF(d.dtype=1,true,null)) AS type_1, COUNT(IF(t.tid=2, true, null)) AS type_2,COUNT(IF(t.tid=4, true, null)) AS type_4,COUNT(IF(t.tid=5, true, null)) AS type_5,COUNT(IF(t.tid=6, true, null)) AS type_6,COUNT(IF(t.tid=7, true, null)) AS type_7,COUNT(IF(t.tid=8, true, null)) AS type_8,COUNT(IF(t.tid=9, true, null)) AS type_9,COUNT(IF(t.tid=10, true, null)) AS type_10,COUNT(IF(t.tid=11, true, null)) AS type_11')
            ->groupBy('b.id')
            ->get();
        $countdata = ['usercount' => $usercount, 'userdata' => $count];
        return $this->jsonSuccessData($countdata);
    }

    /**
     * 危化监测
     * @param $data
     * @return false|string
     */
    public function chemicals($data)
    {
        return $this->getDevices($data, [1, 5, 10]);
    }

    /**
     * 消防监测
     * @param $data
     * @return false|string
     */
    public function fire($data)
    {
        return $this->getDevices($data, [2, 4, 6, 7, 8, 9, 11]);
    }

    public function getDevices($data, $type_ids)
    {
        $searchData = json_decode($data, true);
        $id = $searchData['userId'];
        $uid = $this->parseAbilityAndArgumentsadmin($id);

        $where = [];
        $whereIn = $type_ids;
        if(!empty($searchData['deviceNum'])) {
            $where[] = ['devicenum', 'like', "%{$searchData['deviceNum']}%"];
        }
        if(!empty($searchData['deviceName'])) {
            $where[] = ['username', 'like', "%{$searchData['deviceName']}%"];
        }
        if($searchData['deviceType'] != 0) {
            $whereIn = [$searchData['deviceType']];
        }
        if($searchData['deviceStatus'] != 0) {
            $where[] = ['device.devicepolice', '=', $searchData['deviceStatus']];
        }
        $limit = $searchData['limit'];
        $pagenNum = $limit * ($searchData['page'] - 1);//页数

        if($uid == 1){
            $devicelist = DB::table('device')
                ->whereIn('dtype', $whereIn)
                ->leftjoin('device_type as dy', "device.dtype", '=', 'dy.tid')
                ->leftjoin('gas as g', "device.status", '=', 'g.id')
                ->leftjoin('status as p', "device.devicepolice", '=', 'p.id')
                ->leftjoin('danwei as dw', 'device.devicemonad', '=', 'dw.id')
                ->select('dy.tname', 'g.gas', 'device.*', 'p.status_name', 'dw.danwei')
                ->where($where)
                ->offset($pagenNum)
                ->limit($limit)
                ->orderBy('device.id', 'desc')
                ->get()->toArray();
            $count = DB::table('device')
                ->whereIn('dtype', $whereIn)
                ->where($where)
                ->count();
        }else{
            $devicelist = DB::table('device')
                ->whereIn('dtype', $whereIn)
                ->leftjoin('device_type as dy', "device.dtype", '=', 'dy.tid')
                ->leftjoin('gas as g', "device.status", '=', 'g.id')
                ->leftjoin('status as p', "device.devicepolice", '=', 'p.id')
                ->leftjoin('danwei as dw', 'device.devicemonad', '=', 'dw.id')
                ->where('device.uid','=',$id)
                ->where($where)
                ->select('dy.tname', 'g.gas', 'device.*', 'p.status_name', 'dw.danwei')
                ->offset($pagenNum)
                ->limit($limit)
                ->orderBy('device.id', 'desc')
                ->get()->toArray();
            $count = DB::table('device')
                ->where('device.uid','=',$id)
                ->whereIn('dtype', $whereIn)
                ->where($where)
                ->count();
        }
        return $this->jsonSuccessData(['devicelist' => $devicelist, 'count' => $count]);
    }


    //监测是否设备报警
    public function alarm($id)
    {
        $uid = $this->parseAbilityAndArgumentsadmin($id);
        if ($uid == 1) {
            $isalarm = DB::table('reportpolice')
                ->where('status', '=', '1')
                ->where('police', '=', '1')
                ->groupBy('id')
                ->select('devicenumber')
                ->get()->toArray();
        } else {
            $isalarm = DB::table('reportpolice')
                ->join('device as d','reportpolice.devicenumber','=','d.devicenum')
                ->where('reportpolice.status', '=', '1')
                ->where('reportpolice.police', '=', '1')
                ->where('d.uid', '=', $id)
                ->groupBy('reportpolice.id')
                ->select('reportpolice.devicenumber')
                ->get()->toArray();
        }
        if ($isalarm) {
          return $this->jsonSuccessData($isalarm);
        } else {
            return $this->jsonErrorData(105, '当前无报警');
        }
    }
    public function parseAbilityAndArgumentsadmin($id)
    {
        $isadmin = DB::table('users')->where('id','=',$id)->value('isadmin');
        return $isadmin;
    }
}
