宝体数据调用接口
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.
 
 
 
 
 
 

167 lines
5.5 KiB

<?php
namespace app\api\controller\pass;
use app\api\controller\Controller;
use app\common\dm\Dm;
class Statistics extends Controller
{
private $password = 'batyg@2023';
private function validateUserToken($token) {
$xtToken = md5(base64_encode($this->password));
return $xtToken === $token;
}
/**
* 获取年各场馆统计数据
* @return array
*/
public function getYearVenueData()
{
$year = $this->request->param('year') ?: date('Y');
$token = $this->request->param('token');
if (!is_numeric($year)) return $this->renderError('请求失败');
if (empty($token)) return $this->renderError('请求失败');
if (!$this->validateUserToken($token)) return $this->renderError('请求验证不通过');
$dm = new Dm();
$start_time = "{$year}-01-01" . 'T00:00:00.000+08:00';
$end_time = "{$year}-12-31" . 'T23:59:59.000+08:00';
$yearWhere = '"date" =' . "'month'"
. ' and "granularity"=' . "'monthly'"
. ' and "statTime">=' . "'{$start_time}'"
. ' and "statTime"<=' . "'{$end_time}'";
# 体育馆 体育场 劳务博物馆 游泳场馆
$tyData = [];
$tyResult = $dm->select('bt_passenger_flow_all',$yearWhere,'"groupId","flowInNum","statTime"');
foreach ($tyResult as $item) {
$groupId = $item['groupId'];
$month = date("m", strtotime($item['statTime']));
if (!isset($tyData[$groupId]['sum_year'])) $tyData[$groupId]['sum_year'] = 0;
$tyData[$groupId]['sum_year'] += $item['flowInNum'];
if (!isset($tyData[$groupId]['month'][$month])) $tyData[$groupId]['month'][$month] = 0;
$tyData[$groupId]['month'][$month] += $item['flowInNum'];
}
$data = [
'yearDate' => $year,
'dataResult' => []
];
$whgData = isset($tyData['610']) ? $tyData['610'] : ['sum_year' => 0, 'month' => []];
unset($tyData['610']);
foreach ($tyData as $groupId => $monthData) {
$getGroup = $dm->find('bt_passenger_monitor_group',' "groupId" = ' . "'{$groupId}'");
$groupName = $getGroup['groupName'] ?: '';
$tempData = [
'name' => $groupName,
'yearAmount' => $monthData['sum_year'],
'monthDate' => []
];
ksort($monthData['month']);
foreach ($monthData['month'] as $month => $value) {
$tempData['monthDate'][] = [
'month' => $month,
'value' => $value
];
}
$data['dataResult'][] = $tempData;
}
# 文化馆 图书馆
$start_time = "{$year}-01-01 00:00:00";
$end_time = "{$year}-12-31 23:59:59";
$whWhere = '"date"=' . "'month'"
.' and "group_id"<>' . "'lib001'"
.' and "create_time">=' . "'{$start_time}'"
.' and "create_time"<=' . "'{$end_time}'";
$whResult = $dm->select('bt_library_data',$whWhere,'"group_id","group_name","incount","create_time"');
$whData = [];
$whGroupName = [];
foreach ($whResult as $item) {
$groupId = $item['group_id'];
$month = date("m", strtotime($item['create_time']));
$whGroupName[$groupId] = $item['group_name'];
if (!isset($whData[$groupId]['sum_year'])) $whData[$groupId]['sum_year'] = 0;
$whData[$groupId]['sum_year'] += $item['incount'];
if (!isset($whData[$groupId]['month'][$month])) $whData[$groupId]['month'][$month] = 0;
$whData[$groupId]['month'][$month] += $item['incount'];
}
$whg2Data = isset($whData['BAF055']) ? $whData['BAF055'] : ['sum_year' => 0, 'month' => []];
unset($whData['BAF055']);
foreach ($whData as $groupId => $monthData) {
$groupName = isset($whGroupName[$groupId]) ? $whGroupName[$groupId] : '';
$tempData = [
'name' => $groupName,
'yearAmount' => $monthData['sum_year'],
'monthDate' => []
];
ksort($monthData['month']);
foreach ($monthData['month'] as $month => $value) {
$tempData['monthDate'][] = [
'month' => $month,
'value' => $value
];
}
$data['dataResult'][] = $tempData;
}
// 文化馆两数据合并
$getGroup = $dm->find('bt_passenger_monitor_group',' "groupId" = ' . "'610'");
$groupName = $getGroup['groupName'] ?: '';
$tempData = [
'name' => $groupName,
'yearAmount' => $whgData['sum_year'] + $whg2Data['sum_year'],
'monthDate' => []
];
$whgMonthKeyArr = array_keys($whgData['month']);
foreach ($whg2Data['month'] as $key => $value) {
if (in_array($key, $whgMonthKeyArr)) {
$whgData['month'][$key] += $value;
unset($whg2Data['month'][$key]);
}
}
if (!empty($whg2Data['month'])) {
$whgData['month'] = array_merge($whgData['month'], $whg2Data['month']);
}
ksort($whgData['month']);
foreach ($whgData['month'] as $month => $value) {
$tempData['monthDate'][] = [
'month' => $month,
'value' => $value
];
}
$data['dataResult'][] = $tempData;
return $this->renderSuccess($data);
}
}