* @return array * @throws \think\db\exception\DataNotFoundException * @throws \think\db\exception\ModelNotFoundException * @throws \think\exception\DbException */ public static function allGroupTimeNum($param,$dm = null) { $granularity = $param['granularity']; $groupsId = $param['groupsId']; if (empty($granularity)) return ['code' => 0,'msg' => '缺少参数:granularity']; $where = ''; if(!empty($groupsId)){ $garr = explode("|",$groupsId); $where .= ' "groupId" in(' . "'" . implode("','",$garr) . "')"; } $allTimeData = []; $groupsData = []; $dkeys=[]; if (empty($dm)) $dm = new Dm(); if ($granularity == 'daily') { $where .= $where ? ' and "granularity" = ' . "'hourly'" : ' "granularity" = ' . "'hourly'"; $list = $dm->select('bt_passenger_flow',$where); $data = self::handleGroupData($list); $allTimeData = $data['allTimeData']; $groupsData = $data['groupsData']; $dkeys = $data['dkeys']; } elseif ($granularity == 'monthly') { $where .= $where ? ' and "granularity" = ' . "'daily'" : ' "granularity" = ' . "'daily'"; $list = $dm->select('bt_passenger_flow',$where); $data = self::handleGroupData($list,'d'); $allTimeData = $data['allTimeData']; $groupsData = $data['groupsData']; $dkeys = $data['dkeys']; } elseif ($granularity == 'yearly') { $where .= $where ? ' and "granularity" = ' . "'monthly'" : ' "granularity" = ' . "'monthly'"; $list = $dm->select('bt_passenger_flow',$where); $data = self::handleGroupData($list,'m'); $allTimeData = $data['allTimeData']; $groupsData = $data['groupsData']; $dkeys = $data['dkeys']; } $data = ['allTimeData' => $allTimeData, 'groupsData' => $groupsData, 'dkeys' => $dkeys]; return ['code' => 1, 'msg' => 'success', 'data' => $data]; } /** * 特殊处理 * @param $list * @param $date_str * @return array */ protected static function handleGroupData($list,$date_str = 'H:00') { $allTimeData = []; $groupsData = []; $dkey=[]; if ($date_str == 'H:00') { for ($i = 9; $i < 23; $i++) { $str = strlen($i) == 1 ? '0'.$i.':00' : $i.':00'; $allTimeData[$str]['num'] = 0; $allTimeData[$str]['title'] = $str; $dkey[]=$str; } } elseif ($date_str == 'd') { $d = date('d') * 1; $date = $d - 12 > 0 ? $d - 12 : 1; for ($i = $date; $i <= $d; $i++) { $str = strlen($i) == 1 ? '0'.$i : $i; $allTimeData[$str]['num'] = 0; $allTimeData[$str]['title'] = $str; $dkey[]=$str; } } elseif ($date_str == 'm') { $m = date('m') * 1; $date = $m - 12 > 0 ? $m - 12 : 1; for ($i = $date; $i <= $m; $i++) { $str = strlen($i) == 1 ? '0'.$i : $i; $allTimeData[$str]['num'] = 0; $allTimeData[$str]['title'] = $str; $dkey[]=$str; } } $sumNoRepeatInNum = 0; foreach ($list as $value) { $sumNoRepeatInNum += $value['noRepeatInNum']; // 进馆总人数 if (!isset($groupsData[$value['groupName']]['noRepeatInNum'])) $groupsData[$value['groupName']]['noRepeatInNum'] = 0; $groupsData[$value['groupName']]['noRepeatInNum'] += $value['noRepeatInNum']; // 分租进馆人数 $date = date($date_str,ceil($value['createTime'] / 1000) + (8*3600)); // 各时间端进馆人数 if (!isset($allTimeData[$date])) continue; $allTimeData[$date]['num'] += $value['noRepeatInNum']; } ksort($allTimeData); // 计算各组占比 foreach ($groupsData as &$val) { $val['proportion'] = $val['noRepeatInNum'] > 0 ? (round($val['noRepeatInNum']/$sumNoRepeatInNum,2) * 100) ."%" : '0%'; } $groupData = []; foreach ($groupsData as $groupName => $groupValue) { $groupData[] = [ 'groupName' => $groupName, 'noRepeatInNum' => $groupValue['noRepeatInNum'], 'proportion' => $groupValue['proportion'] ]; } return [ 'allTimeData' => $allTimeData, // 总客流趋势时间段 'groupsData' => $groupData, // 各组总数及占比 'dkeys' =>$dkey ]; } /** * 总览总数据接口 <首页-中间部分总览> * @return array * @throws \think\db\exception\DataNotFoundException * @throws \think\db\exception\ModelNotFoundException * @throws \think\exception\DbException */ public static function allGroupNum($dm = null) { $dateArr = ['day' => 'daily', 'week' => 'daily', 'month' => 'monthly', 'year' => 'yearly']; $returnData = []; $y = date("Y"); # 总进馆人数 $yearSum = 0; if (empty($dm)) $dm = new Dm(); foreach ($dateArr as $date => $granularity) { $list = $dm->select( 'bt_passenger_flow_all', ' "granularity" = ' . "'{$granularity}'" . ' and "date" = ' . "'{$date}'" ); $returnData[$date]['noRepeatInNum'] = 0; foreach ($list as $value) { if ($date == 'year') { $yearSum += $value['noRepeatInNum']; $yDate = date('Y',ceil($value['createTime'] / 1000)); // 各今年端进馆人数 if ($y == $yDate) $returnData[$date]['noRepeatInNum'] += $value['noRepeatInNum']; } else { $returnData[$date]['noRepeatInNum'] += $value['noRepeatInNum']; } } } $returnData['sumYear']['noRepeatInNum'] = $yearSum; foreach ($returnData as &$val) { $val['noRepeatInNum'] = formatNumber($val['noRepeatInNum']); } return $returnData; } /** * 查询最新客流统计组实时数据 <各场管实施实时进馆人数> * @return array * @throws \think\db\exception\DataNotFoundException * @throws \think\db\exception\ModelNotFoundException * @throws \think\exception\DbException */ public static function realTimeData($dm = null) { if (empty($dm)) $dm = new Dm(); $list = $dm->select('bt_passenger_flow_real'); $data = []; $configRes = $dm->find("bt_config",' "key" = ' . "'threshold'"); $threshold = $configRes['value'] ?: 0; foreach ($list as $value) { if (!isset($data[$value['groupId']]['allEnter'])) $data[$value['groupId']]['allEnter'] = 0; $data[$value['groupId']]['allEnter'] += $value['allEnter']; } $returnData = []; foreach ($data as $groupId => $val) { $proportion = $val['allEnter'] > 0 ? round($val['allEnter']/$threshold,2) : 0; $val['proportion'] = ($proportion * 100) . "%"; $val['congestion'] = self::getPassType($proportion); $val['groupId'] = $groupId; $getGroup = $dm->find('bt_passenger_monitor_group',' "groupId" = ' . "'{$groupId}'"); $val['groupName'] = $getGroup['groupName'] ?: ''; $returnData[] = $val; } $libData = $dm->select('bt_library',['group_id' => ['baz001','BAF055']]); foreach ($libData as $libRow) { $libProportion = $libRow['today_incount'] > 0 ? round($libRow['today_incount']/$threshold,2) : 0; $returnData[] = [ 'allEnter' => $libRow['today_incount'] ?: 0, 'proportion' => ($libProportion * 100) . "%", 'congestion' => self::getPassType($libProportion), 'groupId' => $libRow['group_id'], 'groupName' => $libRow['group_name'] ]; } return $returnData; } protected static function getPassType($proportion) { # 舒适:阈值<80% if ($proportion < 0.8) { return '舒适'; } # 饱和:80%<阈值<100% if (0.8 < $proportion || $proportion < 1) { return '饱和'; } # 拥挤:阈值=100% if ($proportion == 1) { return '拥挤'; } # 拥挤:阈值=100% if ($proportion >= 1.2) { return '爆满'; } return ''; } /** * 今日各场馆人数趋势 * @return array * @throws \think\db\exception\DataNotFoundException * @throws \think\db\exception\ModelNotFoundException * @throws \think\exception\DbException */ public static function toDayGroupsEnterNum($dm = null) { if (empty($dm)) $dm = new Dm(); $list = $dm->select('bt_passenger_flow',' "granularity"='."'hourly'"); $allTimeData = []; for ($i = 9; $i < 23; $i++) { $str = strlen($i) == 1 ? '0'.$i.':00' : $i.':00'; $allTimeData[$str] = 0; } $returnData = []; foreach ($list as $value) { $date = date('H:00',ceil($value['createTime'] / 1000)); // 各时间端进馆人数 if (!isset($allTimeData[$date])) continue; if (!isset($returnData[$value['groupName']][$allTimeData[$date]])) $returnData[$value['groupName']][$date] = 0; $returnData[$value['groupName']][$date] += $value['noRepeatInNum']; } $data = []; foreach ($returnData as $groupName => $item) { $listData = []; foreach ($item as $itemKey => $itemRow) { /*$listData[] = [ 'time' => $itemKey, 'count' => $itemRow ];*/ $listData['dkeys'][] = $itemKey; $listData['dvalue'][] = $itemRow; } $data[] = [ 'groupName' => $groupName, 'list' => $listData ]; } return $data; } /** * 图书管数据接口 * @return array * 返回数据:today.incount=今天进 month.incount=本月进 year.incount=今年进 day30.incount=近30天近 outcount=出 * 不传参数就是所有摄像头,传cameraid就是按传的摄像头ID * cameraid = 宝安图书馆9,7,6,4,2,1,67,3 1990分馆134、 135 */ public static function allFlowTrends($dm = null) { if (empty($dm)) $dm = new Dm(); $res = $dm->find('bt_library',['group_id' => 'lib001']); $data = [ 'day30' => [ 'incount' => isset($res['day30_incount']) ? $res['day30_incount'] : 0, 'outcount' => isset($res['day30_outcount']) ? $res['day30_outcount'] : 0 ], 'month' => [ 'incount' => isset($res['month_incount']) ? $res['month_incount'] : 0, 'outcount' => isset($res['month_outcount']) ? $res['month_outcount'] : 0 ], 'today' => [ 'incount' => isset($res['today_incount']) ? $res['today_incount'] : 0, 'outcount' => isset($res['today_outcount']) ? $res['today_outcount'] : 0 ], 'year' => [ 'incount' => isset($res['year_incount']) ? $res['year_incount'] : 0, 'outcount' => isset($res['year_outcount']) ? $res['year_outcount'] : 0 ], ]; return $data; } }