<?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;

class  SwooleCommandController extends Controller
{
    //返回用户列表
    public function userslist(): string
    {
        //查询用户数量
        $usercount = DB::table('BackgroundUser')->count();//获取用户数量
        //查询设备类型
        $device_type = DB::table('device_type')->get()->toArray();
        //量产
        $userdata = DB::table('BackgroundUser as b')
            ->leftjoin('device as de', 'b.id', '=', 'de.uid')
            ->where('b.state', '=', '2')
            ->select('b.id', 'b.username', 'b.nickname', 'de.dtype', 'de.uid')
            ->groupBy('b.id')
            ->get()->toArray();
        $data = [];
        foreach (array_column($device_type, 'tid') as $key => $value) {
            foreach (array_column($device_type, 'tname') as $k => $v) {
                foreach (array_column($userdata, 'id') as $uk => $uv){
                    foreach (array_column($userdata, 'username') as $usk => $usv)
                    if ($key == $k && $uk == $usk){
                        $countdevice_type = DB::table('device')
                            ->where('uid','=',$uv)
                            ->where('dtype', '=', $value)
                            ->count();
                        $username['username'] = $usv;
                        $username['tname'] = $v;
                        $username['count_dtype'] = $countdevice_type;
                        array_push($data,$username);
                    }
                }
            }
        }

        $countdata = ['usercount' => $usercount, 'userdata' => $data];
        return $this->jsonSuccessData($countdata);
    }

}