getTypesByCode('StationType'); } /** * 获取所有用户设备类型 */ public function getDeviceType() { return (new SysDictDataModel())->getTypesByCode('DeviceType'); } /** * 基站设备增长趋势数据 */ 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'=>'液压扳手'), ); // var_dump($device_types); $data = []; while ($start_time <= $end_time) { foreach($device_types as $type) { if (!isset($data[$type['text']]['name'])) { $data[$type['text']]['name'] = $type['text']; } $count=mt_rand(1,10); $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, ]; } }