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.
279 lines
11 KiB
279 lines
11 KiB
<?php
|
|
|
|
namespace app\api\controller\pass;
|
|
|
|
use app\api\controller\Controller;
|
|
|
|
use app\common\dm\Dm;
|
|
use app\common\model\Pass;
|
|
use think\Db;
|
|
|
|
class Passcc extends Controller
|
|
{
|
|
|
|
public function test() {
|
|
echo 1111;
|
|
}
|
|
|
|
/**
|
|
* 分时时间段接口 <首页-客流趋势>
|
|
* @return array
|
|
* @throws \think\db\exception\DataNotFoundException
|
|
* @throws \think\db\exception\ModelNotFoundException
|
|
* @throws \think\exception\DbException
|
|
*/
|
|
public function allGroupTimeNum() {
|
|
|
|
$granularity = $this->request->param('granularity');
|
|
$groupsId = $this->request->param('groupsId');
|
|
|
|
if (empty($granularity)) return $this->renderError('date参数不能为空');
|
|
$where = '';
|
|
if (!empty($groupsId)) $where .= ' "groups_id" in(' . "'" . implode("','",$groupsId) . "')";
|
|
$allTimeData = [];
|
|
$groupsData = [];
|
|
$dkeys=[];
|
|
$dm = new Dm();
|
|
if ($granularity == 'daily') {
|
|
// $where['granularity'] = 'hourly';
|
|
// $list = Db::table('bt_passenger_flow')->where($where)->select()->toArray();
|
|
$where .= $where ? ' and "granularity" = ' . "'hourly'" : ' "granularity" = ' . "'hourly'";
|
|
$list = $dm->select('bt_passenger_flow',$where);
|
|
$data = $this->handleGroupData($list);
|
|
$allTimeData = $data['allTimeData'];
|
|
$groupsData = $data['groupsData'];
|
|
$dkeys = $data['dkeys'];
|
|
} elseif ($granularity == 'monthly') {
|
|
// $where['granularity'] = 'daily';
|
|
// $list = Db::table('bt_passenger_flow')->where($where)->select()->toArray();
|
|
$where .= $where ? ' and "granularity" = ' . "'daily'" : ' "granularity" = ' . "'daily'";
|
|
$list = $dm->select('bt_passenger_flow',$where);
|
|
$data = $this->handleGroupData($list,'d');
|
|
$allTimeData = $data['allTimeData'];
|
|
$groupsData = $data['groupsData'];
|
|
$dkeys = $data['dkeys'];
|
|
} elseif ($granularity == 'yearly') {
|
|
// $where['granularity'] = 'monthly';
|
|
// $list = Db::table('bt_passenger_flow')->where($where)->select()->toArray();
|
|
$where .= $where ? ' and "granularity" = ' . "'monthly'" : ' "granularity" = ' . "'monthly'";
|
|
$list = $dm->select('bt_passenger_flow',$where);
|
|
$data = $this->handleGroupData($list,'m');
|
|
$allTimeData = $data['allTimeData'];
|
|
$groupsData = $data['groupsData'];
|
|
$dkeys = $data['dkeys'];
|
|
}
|
|
|
|
$data = ['allTimeData' => $allTimeData, 'groupsData' => $groupsData, 'dkeys' => $dkeys];
|
|
return $this->renderSuccess(compact('data'));
|
|
}
|
|
|
|
/**
|
|
* 特殊处理
|
|
* @param $list
|
|
* @param $date_str
|
|
* @return array
|
|
*/
|
|
protected 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 function allGroupNum() {
|
|
|
|
$dateArr = ['day' => 'daily', 'week' => 'daily', 'month' => 'monthly', 'year' => 'yearly'];
|
|
|
|
$returnData = [];
|
|
$y = date("Y");
|
|
# 总进馆人数
|
|
$yearSum = 0;
|
|
$dm = new Dm();
|
|
foreach ($dateArr as $date => $granularity) {
|
|
|
|
// $list = Db::table('bt_passenger_flow_all')
|
|
// ->where(['granularity' => $granularity, 'date' => $date])->select()->toArray();
|
|
$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;
|
|
|
|
$libData = Pass::getBaoAnLibData(true);
|
|
foreach ($libData as $libRow) {
|
|
$returnData['day']['noRepeatInNum'] += $libRow['data']['today']['incount'];
|
|
$returnData['week']['noRepeatInNum'] += $libRow['data']['today']['incount'];
|
|
$returnData['month']['noRepeatInNum'] += $libRow['data']['month']['incount'];
|
|
$returnData['year']['noRepeatInNum'] += $libRow['data']['year']['incount'];
|
|
$returnData['sumYear']['noRepeatInNum'] += $libRow['data']['year']['incount'];
|
|
}
|
|
|
|
foreach ($returnData as &$val) {
|
|
$val['noRepeatInNum'] = formatNumber($val['noRepeatInNum']);
|
|
}
|
|
|
|
return $this->renderSuccess(compact('returnData'));
|
|
}
|
|
|
|
/**
|
|
* 查询最新客流统计组实时数据 <各场管实施实时进馆人数>
|
|
* @return array
|
|
* @throws \think\db\exception\DataNotFoundException
|
|
* @throws \think\db\exception\ModelNotFoundException
|
|
* @throws \think\exception\DbException
|
|
*/
|
|
public function realTimeData()
|
|
{
|
|
$dm = new Dm();
|
|
// $list = Db::table('bt_passenger_flow_real')->select()->toArray();
|
|
$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'] = Pass::getPassType($proportion);
|
|
$val['groupId'] = $groupId;
|
|
|
|
$getGroup = $dm->find('bt_passenger_monitor_group',' "groupId" = ' . "'{$groupId}'");
|
|
$val['groupName'] = $getGroup['groupName'] ?: '';
|
|
$returnData[] = $val;
|
|
}
|
|
|
|
$libData = Pass::getBaoAnLibData(true);
|
|
|
|
foreach ($libData as $libRow) {
|
|
|
|
$libProportion = $libRow['data']['today']['incount'] > 0 ? round($libRow['data']['today']['incount']/$threshold,2) : 0;
|
|
$returnData[] = [
|
|
'allEnter' => $libRow['data']['today']['incount'],
|
|
'proportion' => ($libProportion * 100) . "%",
|
|
'congestion' => Pass::getPassType($libProportion),
|
|
'groupId' => $libRow['groupId'],
|
|
'groupName' => $libRow['name']
|
|
];
|
|
}
|
|
|
|
return $this->renderSuccess(compact('returnData'));
|
|
}
|
|
|
|
/**
|
|
* 今日各场馆人数趋势
|
|
* @return array
|
|
* @throws \think\db\exception\DataNotFoundException
|
|
* @throws \think\db\exception\ModelNotFoundException
|
|
* @throws \think\exception\DbException
|
|
*/
|
|
public function toDayGroupsEnterNum()
|
|
{
|
|
$dm = new Dm();
|
|
// $list = Db::table('bt_passenger_flow')->where(['granularity' => 'hourly'])->select()->toArray();
|
|
$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) {
|
|
$data[] = [
|
|
'groupName' => $groupName,
|
|
'list' => $item
|
|
];
|
|
}
|
|
|
|
return $this->renderSuccess(compact('data'));
|
|
}
|
|
|
|
}
|