40000,# 图书馆 'BAF055' => 1000,# 文化馆 '110' => 10000,# 体育场 '210' => 10000,# 体育馆 '310' => 1000,# 游泳馆 '510' => 1000 # 博物馆 ]; protected static function getGroupIds($dm,$type = 1) { $groupIdArr = []; $result = $dm->select('bt_passenger_monitor_group',['type' => $type]); foreach ($result as $row) { $groupIdArr[] = $row['groupId']; } return $groupIdArr; } /** * 分时时间段接口 <首页-客流趋势> * @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']; $type = $param['type']; if (empty($granularity)) return ['code' => 0, 'msg' => '缺少参数:granularity']; if (!in_array($granularity,self::$granularity)) return ['code' => 0, 'msg' => '参数错误:granularity']; $where = ''; $groupsId = ''; if (empty($dm)) $dm = new Dm(); if (!empty($type)) { if (!in_array($type,self::$groupType)) return ['code' => 0, 'msg' => 'type字段参数错误']; $groupsIdArr = self::getGroupIds($dm,$type); $groupsId = implode("|",$groupsIdArr); $where .= ' "groupId" in(' . "'" . implode("','", $groupsIdArr) . "')"; } $allTimeData = []; $groupsData = []; $dkeys = []; 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,'H:00',$dm,$groupsId); $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',$dm,$groupsId); $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',$dm,$groupsId); $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("this week")) . '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',$dm = null,$groupsId = '') { $allTimeData = []; $groupsData = []; $dkey=[]; if ($date_str == 'H:00') { $hour = (int) date('H'); for ($i = 0; $i <= $hour; $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; $groupsIdArr = []; foreach ($list as $value) { $groupsIdArr[] = $value['groupId']; $sumNoRepeatInNum += $value['flowInNum']; // 进馆总人数 if (!isset($groupsData[$value['groupName']]['noRepeatInNum'])) $groupsData[$value['groupName']]['noRepeatInNum'] = 0; $groupsData[$value['groupName']]['noRepeatInNum'] += $value['flowInNum']; // 分租进馆人数 $date = date($date_str,strtotime($value['statTime'])); // 各时间端进馆人数 if (!isset($allTimeData[$date])) continue; $allTimeData[$date]['num'] += $value['flowInNum']; } ksort($allTimeData); $groupsIdParamArr = []; # 总览-合并文化数据 if (empty($groupsId)) { $res = $dm->select('bt_library',['group_id' => ['baz001','BAF055']]); foreach ($res as $row) { if (!isset($groupsData[$row['group_name']]['noRepeatInNum'])) $groupsData[$row['group_name']]['noRepeatInNum'] = 0; if ($date_str == 'H:00') { $sumNoRepeatInNum += $row['today_incount']; $groupsData[$row['group_name']]['noRepeatInNum'] = $row['today_incount']?:0; } elseif ($date_str == 'd') { $sumNoRepeatInNum += $row['month_incount']; $groupsData[$row['group_name']]['noRepeatInNum'] = $row['month_incount']?:0; } elseif ($date_str == 'm') { $sumNoRepeatInNum += $row['year_incount']; $groupsData[$row['group_name']]['noRepeatInNum'] = $row['year_incount']?:0; } } $whgData = []; if ($date_str == 'H:00') { $whgData = $dm->select('bt_library_hours',['create_time' => ['>=',date("Y-m-d 00:00:00")],'group_id' => 'lib001']); } elseif ($date_str == 'd') { $whgData = $dm->select('bt_library_data',['create_time' => ['>=',date("Y-m-01 00:00:00")],'group_id' => 'lib001','date' => 'day']); } elseif ($date_str == 'm') { $whgData = $dm->select('bt_library_data',['create_time' => ['>=',date("Y-01-01 00:00:00")],'group_id' => 'lib001','date' => 'month']); } # 文化馆客流柱状图数据 foreach ($whgData as $whgRow) { if ($date_str == 'H:00') { $date = $whgRow['hour_time']; } else { $date = date($date_str,strtotime($whgRow['create_time'])); // 各时间端进馆人数 } if (!isset($allTimeData[$date])) continue; $allTimeData[$date]['num'] += $whgRow['incount']; } } else { $groupsIdParamArr = explode("|",$groupsId); } // ------------------ 组数据不存在赋值为0 ------------------ // # 去重存在的统计组id $groupsIdArr = array_unique($groupsIdArr); # type 1-体育 2-文化 $type = [1,2]; if ($groupsIdParamArr) { $type = [1]; # 和传值参数 分组取相同值 $groupsIdArr = array_intersect($groupsIdArr,$groupsIdParamArr); } # 如果为空 $groupsIdArr = $groupsIdArr ?: [0]; # 查询分组中没有数据的统计组 $groupWhere = ' "groupId" not in (' . "'" . implode("','",$groupsIdArr) ."')"; $groupWhere .= ' and "type" in (' ."'" . implode("','",$type) . "')"; $groupRes = $dm->select("bt_passenger_monitor_group",$groupWhere); foreach ($groupRes as $groupRow) { $groupsData[$groupRow['groupName']]['noRepeatInNum'] = 0; } // ------------------ 组数据不存在赋值 ------------------ // // 计算各组占比 foreach ($groupsData as &$val) { $val['proportion'] = $val['noRepeatInNum'] > 0 ? (round($val['noRepeatInNum']/$sumNoRepeatInNum,4) * 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"); if (empty($dm)) $dm = new Dm(); $paramWhere = ''; if (!empty($param)) { if (!empty($param['type'])) { if (!in_array($param['type'],self::$groupType)) return ['code' => 0, 'msg' => 'type字段参数错误']; $groupsId = self::getGroupIds($dm,$param['type']); $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') { $yDate = date('Y',ceil($value['createTime'] / 1000)); // 各今年端进馆人数 if ($y == $yDate) $returnData[$date]['noRepeatInNum'] += $value['flowInNum']; } else { $returnData[$date]['noRepeatInNum'] += $value['flowInNum']; } } } #累计进馆人数 $sumWhere = ' "granularity" = ' . "'yearly'" . ' and "date" = ' . "'year' {$paramWhere}"; $sumFind = $dm->find('bt_passenger_flow_all', $sumWhere,'SUM("flowInNum") as NUM'); $returnData['sumYear']['noRepeatInNum'] = isset($sumFind['NUM']) ? $sumFind['NUM'] : 0; if (empty($groupsId)) { # 文化数据 $mData = $dm->find('bt_library',['group_id' => 'lib001']); $returnData['day']['noRepeatInNum'] += isset($mData['today_incount']) ? $mData['today_incount'] : 0; $returnData['month']['noRepeatInNum'] += isset($mData['month_incount']) ? $mData['month_incount'] : 0; $returnData['year']['noRepeatInNum'] += isset($mData['year_incount']) ? $mData['year_incount'] : 0; // $returnData['sumYear']['noRepeatInNum'] += isset($mData['year_incount']) ? $mData['year_incount'] : 0; # 文化总年度 $libraryData = $dm->find('bt_library_data',['group_id' => 'lib001','date' => 'year'],'SUM("incount") as NUM'); $returnData['sumYear']['noRepeatInNum'] += isset($libraryData['NUM']) ? $libraryData['NUM'] : 0; $week_start = date("Y-m-d 00:00:00",strtotime('this week')); $week_end = date("Y-m-d 23:59:59",strtotime("-".(date("w") - 7) . "day")); $weekDateWhere = ' "group_id" =' . "'lib001'" . ' and "create_time" >= ' . "'{$week_start}'" . ' and "create_time" <= ' . "'{$week_end}'"; $weekDate = $dm->find('bt_library_hours',$weekDateWhere,'SUM("incount") as NUM'); $returnData['week']['noRepeatInNum'] += isset($weekDate['NUM']) ? $weekDate['NUM'] : 0; } 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']; if (!isset($data[$value['groupId']]['holdValue'])) $data[$value['groupId']]['holdValue'] = 0; $data[$value['groupId']]['holdValue'] += $value['holdValue']; } $groupThreshold = self::$groupThreshold; $returnData = []; foreach ($data as $groupId => $val) { $proportion = $val['holdValue'] > 0 ? round($val['holdValue']/$groupThreshold[$groupId],4) : 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'] ?: ''; $val['sort']=intval($getGroup['sort']); $returnData[] = $val; } $libData = $dm->select('bt_library',['group_id' => ['baz001','BAF055']]); foreach ($libData as $libRow) { $holdValue = $libRow['today_incount'] - $libRow['today_outcount']; $libProportion = $holdValue > 0 ? round($holdValue/$groupThreshold[$libRow['group_id']],4) : 0; // 文化馆排 4# 图书馆 2# $isort = $libRow['group_id']=='baz001'?2:4; $returnData[] = [ 'allEnter' => $libRow['today_incount'] ?: 0, 'proportion' => ($libProportion * 100) . "%", 'congestion' => self::getPassType($libProportion), 'groupId' => $libRow['group_id'], 'groupName' => $libRow['group_name'], 'sort' => $isort, ]; } // 排序 $tmparr =[]; foreach($returnData as $k=>$rv){ $tmparr[$k]=$rv['sort']; } array_multisort($tmparr,SORT_ASC,$returnData); return $returnData; } protected static function getPassType($proportion) { # 舒适:阈值<80% if ($proportion <= 0.5) { return '舒适'; } # 饱和:80%<阈值<100% // if (0.8 < $proportion || $proportion < 1) { // return '饱和'; // } # 拥挤:阈值=100% if ($proportion > 0.5 && $proportion <= 1) { return '拥挤'; } # 拥挤:阈值=100% if ($proportion > 1) { 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' "; $groupsWhere = null; $groupsId = []; if (!empty($param)) { if (!empty($param['type'])) { if (!in_array($param['type'],self::$groupType)) return ['code' => 0, 'msg' => 'type字段参数错误']; $groupsId = self::getGroupIds($dm,$param['type']); $groupsWhere = ['groupId' => $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 = []; # 获取各组初始数值 $groupData = $dm->select('bt_passenger_monitor_group',$groupsWhere); foreach ($groupData as $groupDataRow) { foreach ($allTimeData as $date => $dateValue) { $returnData[$groupDataRow['groupId']][$date] = $dateValue; } } foreach ($list as $value) { $date = date('H:00',ceil($value['createTime'] / 1000)); // 各时间端进馆人数 if (!isset($allTimeData[$date])) continue; if (!isset($returnData[$value['groupId']][$allTimeData[$date]])) $returnData[$value['groupId']][$date] = 0; $returnData[$value['groupId']][$date] += $value['flowInNum']; } $data = []; foreach ($returnData as $groupName => $item) { $listData = []; foreach ($item as $itemKey => $itemRow) { /*$listData[] = [ 'time' => $itemKey, 'count' => $itemRow ];*/ $listData['dkeys'][] = $itemKey; $listData['dvalue'][] = $itemRow; } if (is_numeric($groupName)) { $groupData = $dm->find('bt_passenger_monitor_group',['groupId' => $groupName]); $groupName = isset($groupData['groupName']) ? $groupData['groupName'] : $groupName; } $data[] = [ 'groupName' => $groupName, 'list' => $listData ]; } if(empty($groupsId)){ $data = self::getLibraryGroupHoursList($dm,false,$data); } 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(); # 累计进馆人数 $sum_all_incount = 0; # 宝安图书馆数据 $ts_res = $dm->find('bt_library',['group_id' => 'baz001']); $ts_today_incount = isset($ts_res['today_incount']) ? $ts_res['today_incount'] : 0; $ts_month_incount = isset($ts_res['month_incount']) ? $ts_res['month_incount'] : 0; $ts_year_incount = isset($ts_res['year_incount']) ? $ts_res['year_incount'] : 0; # 劳务博物馆数据 $dateArr = ['day' => 'daily', 'month' => 'monthly', 'year' => 'yearly']; $dateData = self::getDateData(); $bw_data = []; 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("flowInNum") as NUM'); $bw_data[$date] = $museumRes['NUM'] ?: 0; } $bw_today_incount = isset($bw_data['day']) ? $bw_data['day'] : 0; $bw_month_incount = isset($bw_data['month']) ? $bw_data['month'] : 0; $bw_year_incount = isset($bw_data['year']) ? $bw_data['year'] : 0; # 文化馆数据 $wh_res = $dm->find('bt_library',['group_id' => 'BAF055']); $wh_today_incount = isset($wh_res['today_incount']) ? $wh_res['today_incount'] : 0; $wh_month_incount = isset($wh_res['month_incount']) ? $wh_res['month_incount'] : 0; $wh_year_incount = isset($wh_res['year_incount']) ? $wh_res['year_incount'] : 0; # 总年、月、日总数 $sum_today_incount = $ts_today_incount + $bw_today_incount + $wh_today_incount; $sum_month_incount = $ts_month_incount + $bw_month_incount + $wh_month_incount; $sum_year_incount = $ts_year_incount + $bw_year_incount + $wh_year_incount; # 累计进馆人数 - 博物馆 $sumAllWhere = ' "granularity" = ' . "'yearly'" . ' and "date" = ' . "'year' " . ' and "groupId"='."'510'"; $sumAllFind = $dm->find('bt_passenger_flow_all', $sumAllWhere,'SUM("flowInNum") as NUM'); $sum_all_incount += isset($sumAllFind['NUM']) ? $sumAllFind['NUM'] : 0; # 累计进馆人数 - 文化、图书 $libData = $dm->find('bt_library_data',['group_id' => 'lib001','date' => 'year'],' SUM("incount") as SUM'); $sum_all_incount += $libData ? $libData['SUM'] : 0; $data = [ 'month' => [ 'incount' => $sum_month_incount ], 'today' => [ 'incount' => $sum_today_incount ], 'year' => [ 'incount' => $sum_year_incount ], 'sum_year' => [ 'incount' => $sum_all_incount ], 'hour24' => [ [ 'groupName' => '图书馆', 'incount' => $ts_today_incount ], [ 'groupName' => '博物馆', 'incount' => $bw_today_incount ], [ 'groupName' => '文化馆', 'incount' => $wh_today_incount ], ] ]; 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['type'])) { if (!in_array($param['type'], self::$groupType)) return ['code' => 0, 'msg' => 'type字段参数错误']; $groupsId = self::getGroupIds($dm, $param['type']); $where .= ' and "groupId" in (' . "'" . implode("','",$groupsId) . "')"; } } #$date = self::getDateData('day'); $start_time = date("Y-m-d\TH",strtotime('-23 hours')) . ':00:00.000+08:00'; $end_time = date("Y-m-d\TH") . ':59:59.000+08:00'; $where .= ' and "statTime" >= ' . "'{$start_time}'" . ' and "statTime" <= ' . "'{$end_time}'"; $list = $dm->select('bt_passenger_flow',$where); $allTimeData = []; $returnData = []; $hoursArr = self::getHoursArr(); foreach ($hoursArr as $hoursVal) { $str = $hoursVal . ':00'; $allTimeData[$str] = 0; if (!isset($returnData[$str])) $returnData[$str] = 0; } foreach ($list as $value) { $date = date('H:00',strtotime($value['statTime'])); // 各时间端进馆人数 if (!isset($allTimeData[$date])) continue; $returnData[$date] += $value['flowInNum']; } $data = []; foreach ($returnData as $date => $item) { $data['dkeys'][] = $date; $data['dvalue'][] = $item; } return $data; } /** * 获取24小时字段 * @return array */ protected static function getHoursArr() { $hours = array(); // 创建空数组 // 获取当前时间戳和当前小时数 $current_time = time(); $current_hour = date('H', $current_time); // 循环向前获取23个小时的小时数,并添加到数组中 for ($i = 23; $i >= 1; $i--) { $hour = date('H', strtotime("-{$i} hour", $current_time)); array_push($hours, $hour); } // 将当前小时数添加到数组末尾 array_push($hours, $current_hour); return $hours; } /** * 文化 24小时数据 柱状图 * @param $dm * @return array */ public static function getLibraryHours24List($dm = null) { if (empty($dm)) $dm = new Dm(); # 获取时间参数 $start_time = date("Y-m-d\TH",strtotime('-23 hours')) . ':00:00.000+08:00'; $end_time = date("Y-m-d\TH") . ':59:59.000+08:00'; # 获取文化组id $groupRes = $dm->select('bt_passenger_monitor_group',['type' => 2],'"groupId","groupName"'); $groupsId = []; foreach ($groupRes as $groupRow) $groupsId[] = $groupRow['groupId']; # 查询文化组 24小时数据 $where = ' "granularity"='."'hourly' " . ' and "statTime" >= ' . "'{$start_time}'" . ' and "statTime" <= ' . "'{$end_time}'"; $where .= ' and "groupId" in (' . "'" . implode("','",$groupsId) . "')"; $list = $dm->select('bt_passenger_flow',$where); # 收集参数 $allTimeData = []; $returnData = []; $hoursArr = self::getHoursArr(); foreach ($hoursArr as $hoursVal) { $str = $hoursVal . ':00'; $allTimeData[$str] = 0; if (!isset($returnData[$str])) $returnData[$str] = 0; } foreach ($list as $value) { $date = date('H:00',ceil($value['createTime'] / 1000)); // 各时间端进馆人数 if (!isset($allTimeData[$date])) continue; $returnData[$date] += $value['flowInNum']; } # 图书、文化馆 $start_create_time = date("Y-m-d H:00:00",strtotime('-23 hours')); $end_create_time = date("Y-m-d H:59:59"); $hoursWhere = ' "group_id" in (' . "'baz001','BAF055')" . ' and "create_time" >= ' . "'{$start_create_time}'" . ' and "create_time" <= ' . "'{$end_create_time}'" ; $hoursRes = $dm->select('bt_library_hours',$hoursWhere); foreach ($hoursRes as $hoursRow) { $date = $hoursRow['hour_time']; if (!isset($allTimeData[$date])) continue; $returnData[$date] += $hoursRow['incount']; } $data = []; foreach ($returnData as $date => $item) { $data['dkeys'][] = $date; $data['dvalue'][] = $item; } return $data; } /** * 文化 今日各场馆数据 * @param $dm * @return array * @throws \think\db\exception\DataNotFoundException * @throws \think\db\exception\ModelNotFoundException * @throws \think\exception\DbException */ public static function getLibraryGroupHoursList($dm = null,$is_group = true,$data = []) { if (empty($dm)) $dm = new Dm(); # 获取文化组id if ($is_group) { $groupRes = $dm->select('bt_passenger_monitor_group',['type' => 2],'"groupId","groupName"'); $groupsId = []; foreach ($groupRes as $groupRow) $groupsId[] = $groupRow['groupId']; $data = self::toDayGroupsEnterNum($dm,['type' => 2]); } # 图书、文化馆 $start_create_time = date("Y-m-d 00:00:00"); $end_create_time = date("Y-m-d 23:59:59"); $allTimeData = []; for ($i = 0; $i <= 23; $i++) { $str = strlen($i) == 1 ? '0'.$i.':00' : $i.':00'; $allTimeData[$str] = 0; } $returnData = []; # 获取各组初始数值 $groupData = [['groupName' => '图书馆'], ['groupName' => '文化馆']]; foreach ($groupData as $groupDataRow) { foreach ($allTimeData as $date => $dateValue) { $returnData[$groupDataRow['groupName']][$date] = $dateValue; } } $hoursWhere = ' "group_id" in (' . "'baz001','BAF055')" . ' and "create_time" >= ' . "'{$start_create_time}'" . ' and "create_time" <= ' . "'{$end_create_time}'" ; $hoursRes = $dm->select('bt_library_hours',$hoursWhere); foreach ($hoursRes as $hoursRow) { $date = $hoursRow['hour_time']; // 各时间端进馆人数 if (!isset($allTimeData[$date])) continue; if (!isset($returnData[$hoursRow['group_name']][$date])) $returnData[$hoursRow['group_name']][$date] = 0; $returnData[$hoursRow['group_name']][$date] += $hoursRow['incount']; } foreach ($returnData as $groupName => $item) { $listData = []; foreach ($item as $itemKey => $itemRow) { $listData['dkeys'][] = $itemKey; $listData['dvalue'][] = $itemRow; } $data[] = [ 'groupName' => $groupName, 'list' => $listData ]; } return $data; } /** * 获取今日图书馆借阅、办证数据 * @param $dm * @return int[] */ public static function getLibraryLeaseData($dm = null) { if (empty($dm)) $dm = new Dm(); $today = date("Y-m-d 00:00:00"); $LibLeaseRes = $dm->find('bt_library_lease',['library' => '044007','create_time' => ['>=',$today]]); $data = [ 'newreader' => 0, 'servcount' => 0, 'returncount' => 0 ]; if ($LibLeaseRes) { $data['newreader'] = $LibLeaseRes['newreader']; # 今日办证册数 $data['servcount'] = $LibLeaseRes['servcount']; # 今日借书人数 $data['returncount'] = $LibLeaseRes['returncount']; # 今日还书册数 } return $data; } }