* @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') { $date = self::getDateData('day'); $start_time = $date['day']['start_time']; $end_time = $date['day']['end_time']; $where .= $where ? ' and "granularity" = ' . "'hourly'" : ' "granularity" = ' . "'hourly'"; $where .= ' and "statTime" >= ' . "'{$start_time}'" . ' and "statTime" <= ' . "'{$end_time}'"; $list = $dm->select('bt_passenger_flow', $where); $data = self::handleGroupData($list); $allTimeData = $data['allTimeData']; $groupsData = $data['groupsData']; $dkeys = $data['dkeys']; } elseif ($granularity == 'monthly') { $date = self::getDateData('month'); $start_time = $date['month']['start_time']; $end_time = $date['month']['end_time']; $where .= $where ? ' and "granularity" = ' . "'daily'" : ' "granularity" = ' . "'daily'"; $where .= ' and "statTime" >= ' . "'{$start_time}'" . ' and "statTime" <= ' . "'{$end_time}'"; $list = $dm->select('bt_passenger_flow', $where); $data = self::handleGroupData($list, 'd'); $allTimeData = $data['allTimeData']; $groupsData = $data['groupsData']; $dkeys = $data['dkeys']; } elseif ($granularity == 'yearly') { $date = self::getDateData('year'); $start_time = $date['year']['start_time']; $end_time = $date['year']['end_time']; $where .= $where ? ' and "granularity" = ' . "'monthly'" : ' "granularity" = ' . "'monthly'"; $where .= ' and "statTime" >= ' . "'{$start_time}'" . ' and "statTime" <= ' . "'{$end_time}'"; $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 $date * @return array */ protected static function getDateData($date = 'all') { $data = []; if ($date == 'day' || $date == 'all') { $data['day'] = [ 'start_time' => date("Y-m-d") . 'T00:00:00.000+08:00', 'end_time' => date("Y-m-d") . 'T23:59:59.000+08:00' ]; } if ($date == 'week' || $date == 'all') { $data['week'] = [ 'start_time' => date("Y-m-d",strtotime("+".(date("w") - 7) . "day")) . 'T00:00:00.000+08:00', 'end_time' => date("Y-m-d",strtotime("-".(date("w") - 7) . "day")) . 'T23:59:59.000+08:00' ]; } if ($date == 'month' || $date == 'all') { $data['month'] = [ 'start_time' => date("Y-m-01") . 'T00:00:00.000+08:00', 'end_time' => date("Y-m-d", strtotime('-1 day', strtotime(date('Y-m-01', strtotime('+1 month'))))) . 'T23:59:59.000+08:00' ]; } if ($date == 'year' || $date == 'all') { $data['year'] = [ 'start_time' => date("Y-01-01") . 'T00:00:00.000+08:00', 'end_time' => date("Y-m-d", strtotime('-1 day', strtotime(date('Y-01-01', strtotime('+1 year'))))) . 'T23:59:59.000+08:00' ]; } return $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,$param = []) { $dateArr = ['day' => 'daily', 'week' => 'daily', 'month' => 'monthly', 'year' => 'yearly']; $returnData = []; $y = date("Y"); # 总进馆人数 $yearSum = 0; if (empty($dm)) $dm = new Dm(); $paramWhere = ''; if (!empty($param)) { if (!empty($param['groupsId'])) { $groupsId = explode("|",$param['groupsId']); $paramWhere .= ' and "groupId" in (' . "'" . implode("','",$groupsId) . "')"; } } $dateData = self::getDateData(); foreach ($dateArr as $date => $granularity) { $where = ' "granularity" = ' . "'{$granularity}'" . ' and "date" = ' . "'{$date}' {$paramWhere}"; $where .= ' and "statTime" >= ' . "'{$dateData[$date]['start_time']}'" . ' and "statTime" <= ' . "'{$dateData[$date]['end_time']}'"; $list = $dm->select( 'bt_passenger_flow_all', $where ); $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,$param = []) { if (empty($dm)) $dm = new Dm(); $where = ' "granularity"='."'hourly' "; if (!empty($param)) { if (!empty($param['groupsId'])) { $groupsId = explode("|",$param['groupsId']); $where .= ' and "groupId" in (' . "'" . implode("','",$groupsId) . "')"; } } $date = self::getDateData('day'); $start_time = $date['day']['start_time']; $end_time = $date['day']['end_time']; $where .= ' and "statTime" >= ' . "'{$start_time}'" . ' and "statTime" <= ' . "'{$end_time}'"; $list = $dm->select('bt_passenger_flow',$where); $allTimeData = []; for ($i = 0; $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']); $sum_month_incount = $month_incount = isset($res['month_incount']) ? $res['month_incount'] : 0; $sum_today_incount = $today_incount = isset($res['today_incount']) ? $res['today_incount'] : 0; $sum_year_incount = $year_incount = isset($res['year_incount']) ? $res['year_incount'] : 0; # 劳务博物馆数据 $dateArr = ['day' => 'daily', 'month' => 'monthly', 'year' => 'yearly']; $dateData = self::getDateData(); $museumData = []; foreach ($dateArr as $date => $granularity) { $museumWhere = ' "groupId"='."'510'".' and "granularity" = ' . "'{$granularity}'" . ' and "date" = ' . "'{$date}'"; $museumWhere .= ' and "statTime" >= ' . "'{$dateData[$date]['start_time']}'" . ' and "statTime" <= ' . "'{$dateData[$date]['end_time']}'"; $museumRes = $dm->find('bt_passenger_flow_all',$museumWhere,' SUM("noRepeatInNum") as NUM'); $museumData[$date] = $museumRes['NUM'] ?: 0; } $museum_today_incount = isset($museumData['day']) ? $museumData['day'] : 0; $sum_today_incount += $museum_today_incount; $museum_month_incount = isset($museumData['month']) ? $museumData['month'] : 0; $sum_month_incount += $museum_month_incount; $museum_year_incount = isset($museumData['year']) ? $museumData['year'] : 0; $sum_year_incount += $museum_year_incount; # 文化馆 $data = [ 'month' => [ 'incount' => $sum_month_incount ], 'today' => [ 'incount' => $sum_today_incount ], 'year' => [ 'incount' => $sum_year_incount ], 'hour24' => [ [ 'groupName' => '图书馆', 'incount' => $today_incount ], [ 'groupName' => '博物馆', 'incount' => $museum_today_incount ], [ 'groupName' => '文化馆', 'incount' => 0 ], ] ]; return $data; } /** * 24小时各场馆数据 * @return array */ public static function getPassHoursData($dm = null,$param = []) { if (empty($dm)) $dm = new Dm(); $where = ' "granularity"='."'hourly' "; if (!empty($param)) { if (!empty($param['groupsId'])) { $groupsId = explode("|",$param['groupsId']); $where .= ' and "groupId" in (' . "'" . implode("','",$groupsId) . "')"; } } $date = self::getDateData('day'); $start_time = $date['day']['start_time']; $end_time = $date['day']['end_time']; $where .= ' and "statTime" >= ' . "'{$start_time}'" . ' and "statTime" <= ' . "'{$end_time}'"; $list = $dm->select('bt_passenger_flow',$where); $allTimeData = []; $returnData = []; for ($i = 0; $i <= 23; $i++) { $str = strlen($i) == 1 ? '0'.$i.':00' : $i.':00'; $allTimeData[$str] = 0; if (!isset($returnData[$str])) $returnData[$str] = 0; } foreach ($list as $value) { $date = date('H:00',strtotime($value['statTime']) + (8*3600)); // 各时间端进馆人数 if (!isset($allTimeData[$date])) continue; $returnData[$date] += $value['noRepeatInNum']; } $data = []; foreach ($returnData as $date => $item) { $data['dkeys'][] = $date; $data['dvalue'][] = $item; } return $data; } }