1 changed files with 180 additions and 0 deletions
@ -0,0 +1,180 @@ |
|||||
|
<?php |
||||
|
|
||||
|
namespace app\api\controller\passenger; |
||||
|
|
||||
|
use app\api\controller\Controller; |
||||
|
|
||||
|
|
||||
|
class PassengerFlow extends Controller |
||||
|
{ |
||||
|
|
||||
|
/** |
||||
|
* 分时时间段接口 <首页-客流趋势> |
||||
|
* @return false|string |
||||
|
* @throws \think\db\exception\DataNotFoundException |
||||
|
* @throws \think\db\exception\ModelNotFoundException |
||||
|
* @throws \think\exception\DbException |
||||
|
*/ |
||||
|
public function allGroupTimeNum($granularity) { |
||||
|
|
||||
|
$granularity = $this->postData('granularity'); |
||||
|
if (empty($granularity)) $this->renderError('date参数不能为空'); |
||||
|
|
||||
|
$allTimeData = []; |
||||
|
$groupsData = []; |
||||
|
if ($granularity == 'daily') { |
||||
|
$list = Db::table('bt_passenger_flow')->where(['granularity' => 'hourly'])->select()->toArray(); |
||||
|
$data = $this->handleGroupData($list); |
||||
|
$allTimeData = $data['allTimeData']; |
||||
|
$groupsData = $data['groupsData']; |
||||
|
} elseif ($granularity == 'monthly') { |
||||
|
$list = Db::table('bt_passenger_flow')->where(['granularity' => 'daily'])->select()->toArray(); |
||||
|
$data = $this->handleGroupData($list,'d'); |
||||
|
$allTimeData = $data['allTimeData']; |
||||
|
$groupsData = $data['groupsData']; |
||||
|
} elseif ($granularity == 'yearly') { |
||||
|
$list = Db::table('bt_passenger_flow')->where(['granularity' => 'monthly'])->select()->toArray(); |
||||
|
$data = $this->handleGroupData($list,'m'); |
||||
|
$allTimeData = $data['allTimeData']; |
||||
|
$groupsData = $data['groupsData']; |
||||
|
} |
||||
|
|
||||
|
$this->renderSuccess(['allTimeData' => $allTimeData, 'groupsData' => $groupsData]); |
||||
|
} |
||||
|
|
||||
|
/** |
||||
|
* 特殊处理 |
||||
|
* @param $list |
||||
|
* @param $date_str |
||||
|
* @return array |
||||
|
*/ |
||||
|
protected function handleGroupData($list,$date_str = 'H:00') { |
||||
|
$allTimeData = []; |
||||
|
$groupsData = []; |
||||
|
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; |
||||
|
} |
||||
|
} 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; |
||||
|
} |
||||
|
} 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; |
||||
|
} |
||||
|
} |
||||
|
$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'] = (round($val['noRepeatInNum']/$sumNoRepeatInNum,2) * 100) ."%"; |
||||
|
} |
||||
|
|
||||
|
return [ |
||||
|
'allTimeData' => $allTimeData, // 总客流趋势时间段 |
||||
|
'groupsData' => $groupsData // 各组总数及占比 |
||||
|
]; |
||||
|
} |
||||
|
|
||||
|
/** |
||||
|
* 总览总数据接口 <首页-中间部分总览> |
||||
|
* @return false|string |
||||
|
* @throws \think\db\exception\DataNotFoundException |
||||
|
* @throws \think\db\exception\ModelNotFoundException |
||||
|
* @throws \think\exception\DbException |
||||
|
*/ |
||||
|
public function allGroupNum() { |
||||
|
|
||||
|
$dateArr = ['day' => 'daily', 'week' => 'daily', 'month' => 'monthly']; |
||||
|
|
||||
|
$returnData = []; |
||||
|
foreach ($dateArr as $date => $granularity) { |
||||
|
|
||||
|
$list = Db::table('bt_passenger_flow_all') |
||||
|
->where(['granularity' => $granularity, 'date' => $date])->select()->toArray(); |
||||
|
|
||||
|
$returnData[$date]['noRepeatInNum'] = 0; |
||||
|
foreach ($list as $value) { |
||||
|
$returnData[$date]['noRepeatInNum'] += $value['noRepeatInNum']; |
||||
|
} |
||||
|
} |
||||
|
|
||||
|
foreach ($returnData as &$val) { |
||||
|
$val['noRepeatInNum'] = formatNumber($val['noRepeatInNum']); |
||||
|
} |
||||
|
|
||||
|
$this->renderSuccess($returnData); |
||||
|
} |
||||
|
|
||||
|
/** |
||||
|
* 查询最新客流统计组实时数据 <各场管实施实时进馆人数> |
||||
|
* @return false|string |
||||
|
* @throws \think\db\exception\DataNotFoundException |
||||
|
* @throws \think\db\exception\ModelNotFoundException |
||||
|
* @throws \think\exception\DbException |
||||
|
*/ |
||||
|
public function realTimeData() { |
||||
|
|
||||
|
$list = Db::table('bt_passenger_flow_real')->select()->toArray(); |
||||
|
$returnData = []; |
||||
|
|
||||
|
$sumAllEnter = 0; |
||||
|
foreach ($list as $value) { |
||||
|
if (!isset($returnData[$value['groupId']]['allEnter'])) $returnData[$value['groupId']]['allEnter'] = 0; |
||||
|
$returnData[$value['groupId']]['allEnter'] += $value['allEnter']; |
||||
|
$sumAllEnter += $value['allEnter']; |
||||
|
} |
||||
|
|
||||
|
foreach ($returnData as &$val) { |
||||
|
$val['proportion'] = (round($val['allEnter']/$sumAllEnter,2) * 100) ."%"; |
||||
|
} |
||||
|
|
||||
|
$this->renderSuccess($returnData); |
||||
|
} |
||||
|
|
||||
|
/** |
||||
|
* 今日各场馆人数趋势 |
||||
|
* @return false|string |
||||
|
* @throws \think\db\exception\DataNotFoundException |
||||
|
* @throws \think\db\exception\ModelNotFoundException |
||||
|
* @throws \think\exception\DbException |
||||
|
*/ |
||||
|
public function toDayGroupsEnterNum() |
||||
|
{ |
||||
|
$list = Db::table('bt_passenger_flow')->where(['granularity' => 'hourly'])->select()->toArray(); |
||||
|
$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['flowInNum']; |
||||
|
} |
||||
|
$this->renderSuccess($returnData); |
||||
|
} |
||||
|
|
||||
|
} |
||||
Loading…
Reference in new issue