getTypesByCode('StationType'); } /** * 获取所有用户设备类型 */ public function getDeviceType() { return (new SysDictDataModel())->getTypesByCode('DeviceType'); } /** * 基站数量数据 */ public function getStationCountData() { $station_type_arr = []; // 基站类型数组 $total_count = 0; // 基站总数量 $online_count = 0; // 基站总在线数量 $yesterday_add_count = 0; // 基站昨日新增总数 $today_add_count = 0; // 基站今日新增总数 $station_type_data = []; $time = time(); $today_time = strtotime(date('Y-m-d')); // 获取基站类型 $station_types = $this->getStationType(); foreach ($station_types as $type) { $station_type_arr[$type['value']] = $type; } // 基站离线时间间隔 $offline_interval = SysConfig::getConfigValueBy('station_offline_interval', 'station_config') ?? 86400; // 基站总数据 $cursor = (new StationModel)->dataRange()->field('station_mac, station_type, online_time, created_at')->cursor(); foreach ($cursor as $station) { $total_count++; // 按基站类型分类 if (isset($station_type_arr[$station['station_type']])) { $key = $station_type_arr[$station['station_type']]['text']; } else { $key = '未知类型'; } if (!isset($station_type_data[$key])) { $station_type_data[$key] = isset($station_type_arr[$station['station_type']]) ? $station_type_arr[$station['station_type']] : []; } // 总数量 $station_type_data[$key]['total'] = isset($station_type_data[$key]['total']) ? ($station_type_data[$key]['total'] + 1) : 1; // 昨日新增 $create_time = $station->getData('created_at'); if ($today_time - $create_time > 0 && $today_time - $create_time < 86400) { $yesterday_add_count++; $station_type_data[$key]['yesterday_add'] = isset($station_type_data[$key]['yesterday_add']) ? ($station_type_data[$key]['yesterday_add'] + 1) : 1; } // 今日新增 if ($today_time - $create_time <= 0) { $today_add_count++; $station_type_data[$key]['today_add'] = isset($station_type_data[$key]['today_add']) ? ($station_type_data[$key]['today_add'] + 1) : 1; } // 在线数 if ($time - intval(strtotime($station['online_time'])) > $offline_interval) { // 离线时间,秒 $online_count++; $station_type_data[$key]['online'] = isset($station_type_data[$key]['online']) ? ($station_type_data[$key]['online'] + 1) : 1; } } // 转化为输出数据 $type_data = []; foreach ($station_type_data as $data) { $data['yesterday_add'] = isset($data['yesterday_add']) ? $data['yesterday_add'] : 0; $data['today_add'] = isset($data['today_add']) ? $data['today_add'] : 0; $data['online'] = isset($data['online']) ? $data['online'] : 0; $data['offline'] = $data['total'] - $data['online']; $type_data[] = $data; } return [ 'station_total_count' => $total_count, 'station_online_count' => $online_count, 'station_offline_count' => $total_count - $online_count, 'station_yesterday_add_count' => $yesterday_add_count, 'station_today_add_count' => $today_add_count, 'station_type_data' => $type_data, ]; } /** * 基站设备增长趋势数据 */ public function getStationGrowthTrendData($start_time = '', $end_time = '') { // 默认最近1周 if (!$start_time) { $start_time = date('Y-m-d', strtotime('-6 day')); } if (!$end_time) { $end_time = date('Y-m-d'); } // 获取基站类型 $station_types = $this->getStationType(); $data = []; $station_model = new StationModel(); while ($start_time <= $end_time) { foreach($station_types as $type) { if (!isset($data[$type['text']]['name'])) { $data[$type['text']]['name'] = $type['text']; } $count = $station_model->dataRange() ->where('station_type', $type['value']) ->whereBetweenTime('created_at', 0, date('Y-m-d 23:59:59', strtotime($start_time))) ->count(); // var_dump($station_model->getLastSql()); $data[$type['text']]['data'][$start_time] = $count; } $start_time = date('Y-m-d', strtotime($start_time . ' +1 day')); } return array_values($data); } /** * 用户设备增长趋势数据 */ public function getDeviceGrowthTrendData($start_time = '', $end_time = '') { // 默认最近1周 if (!$start_time) { $start_time = date('Y-m-d', strtotime('-6 day')); } if (!$end_time) { $end_time = date('Y-m-d'); } // 获取设备类型 $device_types = array( array('text'=>'法兰'), array('text'=>'液压泵'), array('text'=>'液压扳手'), ); // var_dump($device_types); $data = []; $device_model = new DeviceModel(); while ($start_time <= $end_time) { foreach($device_types as $type) { if (!isset($data[$type['text']]['name'])) { $data[$type['text']]['name'] = $type['text']; } // $count = $device_model->dataRange() // ->where('device_type', $type['value']) // ->whereBetweenTime('created_at', 0, date('Y-m-d 23:59:59', strtotime($start_time))) // ->count(); $count=mt_rand(1,10); // var_dump($device_model->getLastSql()); $data[$type['text']]['data'][$start_time] = $count; } $start_time = date('Y-m-d', strtotime($start_time . ' +1 day')); } return array_values($data); } /** * 部门数量数据:统计含本部门、及子部门数量 */ public function getDepartmentCountData() { $depart_ids = DepartmentModel::getChildrenDepartmentIds(request()->user()->department_id); // 去掉部门为0的 $depart_ids = array_filter($depart_ids); return [ 'department_count' => count($depart_ids), ]; } /** * 设备用户数量统计 */ public function getDeviceUserCountData() { // 当前时间 $now_time = time(); // 离线时间间隔 $offline_interval = SysConfig::getConfigValueBy('card_offline_interval', 'basic_config') ?: 86400; $offline_time = date('Y-m-d H:i:s', $now_time - $offline_interval); // 离线时间 // 设备类型 $device_types = $this->getDeviceType(); $deviceModel = (new \catchAdmin\device\model\Device()); $devices = $deviceModel->dataRange() ->field('id,device_type,alarm_state,online_time,wifi_online_time') ->append(['last_online_time']) ->select(); // 总设备数 $totle = 0; // 总在线设备数 $totle_online = 0; // 按设备类型分组 $device_type_data = []; foreach ($device_types as $type) { // 数量 $type_count = $devices->where('device_type', $type['value'])->count(); // 在线数量 $type_online_count = $devices->where('device_type', $type['value'])->where('last_online_time', '>', $offline_time)->count(); // 告警数量 $type_alarm_count = $devices->where('device_type', $type['value'])->where('alarm_state', '>', 0)->count(); $device_type_data[] = [ 'text' => $type['text'], // 设备类型文本 'value' => $type['value'], // 设备类型值 'total' => $type_count, 'online' => $type_online_count, 'alarm' => $type_alarm_count, ]; // 计算总数 $totle += $type_count; // 计算总在线数 $totle_online += $type_online_count; } return [ 'device_total_count' => $totle, 'device_online_count' => $totle_online, 'device_type_data' => $device_type_data, ]; } }