宝体数据调用接口
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 
 
 
 
 

531 lines
21 KiB

<?php
namespace app\common\logic;
use app\common\dm\Dm;
class PassFlow
{
/**
* 分时时间段接口 <首页-客流趋势>
* @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,'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("+".(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',$dm = null,$groupsId = '') {
$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;
$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 ($date_str == 'H:00') {
$sumNoRepeatInNum += $row['today_incount'];
$groupsData[$row['group_name']]['noRepeatInNum'] = $row['today_incount'];
} elseif ($date_str == 'd') {
$sumNoRepeatInNum += $row['month_incount'];
$groupsData[$row['group_name']]['noRepeatInNum'] = $row['month_incount'];
} elseif ($date_str == 'm') {
$sumNoRepeatInNum += $row['year_incount'];
$groupsData[$row['group_name']]['noRepeatInNum'] = $row['year_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,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['flowInNum'];
$yDate = date('Y',ceil($value['createTime'] / 1000)); // 各今年端进馆人数
if ($y == $yDate) $returnData[$date]['noRepeatInNum'] += $value['flowInNum'];
} else {
$returnData[$date]['noRepeatInNum'] += $value['flowInNum'];
}
}
}
$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' ";
$groupsWhere = null;
if (!empty($param)) {
if (!empty($param['groupsId'])) {
$groupsId = explode("|",$param['groupsId']);
$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['groupName']][$date] = $dateValue;
}
}
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['flowInNum'];
}
$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("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) {
$allTimeData[$hoursVal] = 0;
if (!isset($returnData[$hoursVal])) $returnData[$hoursVal] = 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;
}
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;
}
}