4 changed files with 425 additions and 0 deletions
@ -0,0 +1,194 @@ |
|||||
|
<?php |
||||
|
namespace app\api\service\PassengerFlow; |
||||
|
|
||||
|
use think\Db; |
||||
|
|
||||
|
class PassengerFlow |
||||
|
{ |
||||
|
|
||||
|
/** |
||||
|
* 分时时间段接口 <首页-客流趋势> |
||||
|
* @return false|string |
||||
|
* @throws \think\db\exception\DataNotFoundException |
||||
|
* @throws \think\db\exception\ModelNotFoundException |
||||
|
* @throws \think\exception\DbException |
||||
|
*/ |
||||
|
public function allGroupTimeNum($granularity) { |
||||
|
|
||||
|
if (empty($granularity)) return json_encode(['code' => 403, 'date参数不能为空'],JSON_UNESCAPED_UNICODE); |
||||
|
|
||||
|
$dateData = getTargetDate(); |
||||
|
$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']; |
||||
|
} |
||||
|
|
||||
|
return json_encode(['code' => 0, 'msg' => '成功', 'data' => [ |
||||
|
'allTimeData' => $allTimeData, |
||||
|
'groupsData' => $groupsData |
||||
|
]],JSON_UNESCAPED_UNICODE); |
||||
|
} |
||||
|
|
||||
|
/** |
||||
|
* 特殊处理 |
||||
|
* @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']); |
||||
|
} |
||||
|
|
||||
|
$returnArr = [ |
||||
|
'code' => 0, |
||||
|
'msg' => '成功', |
||||
|
'data' => $returnData |
||||
|
]; |
||||
|
return json_encode($returnArr,JSON_UNESCAPED_UNICODE); |
||||
|
} |
||||
|
|
||||
|
/** |
||||
|
* 查询最新客流统计组实时数据 <各场管实施实时进馆人数> |
||||
|
* @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) ."%"; |
||||
|
} |
||||
|
|
||||
|
return json_encode([ |
||||
|
'code' => 0, |
||||
|
'msg' => '成功', |
||||
|
'data' => $returnData |
||||
|
],JSON_UNESCAPED_UNICODE); |
||||
|
} |
||||
|
|
||||
|
/** |
||||
|
* 今日各场馆人数趋势 |
||||
|
* @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']; |
||||
|
} |
||||
|
return json_encode([ |
||||
|
'code' => 0, |
||||
|
'msg' => '成功', |
||||
|
'data' => $returnData], |
||||
|
JSON_UNESCAPED_UNICODE |
||||
|
); |
||||
|
} |
||||
|
} |
||||
@ -0,0 +1,83 @@ |
|||||
|
<?php |
||||
|
|
||||
|
namespace app\task\behavior\passengerFlow; |
||||
|
|
||||
|
use think\Db; |
||||
|
|
||||
|
class PassengerFlow |
||||
|
{ |
||||
|
/** |
||||
|
* 执行函数 |
||||
|
* @return bool |
||||
|
* @throws \think\Exception |
||||
|
* @throws \think\exception\PDOException |
||||
|
*/ |
||||
|
public function run() |
||||
|
{ |
||||
|
$config = config('api_config'); |
||||
|
$dateData = getTargetDate(); |
||||
|
$token = getAccessToken(); |
||||
|
|
||||
|
$arr = [ |
||||
|
'hourly' => [ |
||||
|
'startTime' => $dateData['day']['c_start_time'], |
||||
|
'endTime' => $dateData['day']['c_end_time'], |
||||
|
], |
||||
|
'daily' => [ |
||||
|
'startTime' => $dateData['month']['c_start_time'], |
||||
|
'endTime' => $dateData['month']['c_end_time'], |
||||
|
], |
||||
|
'monthly' => [ |
||||
|
'startTime' => $dateData['year']['c_start_time'], |
||||
|
'endTime' => date('c'), |
||||
|
] |
||||
|
]; |
||||
|
|
||||
|
$url = $config['host_url'] . "/api/cfas/v2/passengerFlow/groups"; |
||||
|
|
||||
|
|
||||
|
$delete = Db::table('bt_passenger_flow')->where('id','>',1)->delete(); |
||||
|
|
||||
|
try { |
||||
|
foreach ($arr as $granularity => $value) { |
||||
|
|
||||
|
$dataArr = [ |
||||
|
'granularity' => $granularity, |
||||
|
'startTime' => $value['startTime'], |
||||
|
'endTime' => $value['endTime'], |
||||
|
'ids' => '01' // --- <replace> --- // |
||||
|
]; |
||||
|
|
||||
|
$json_data = json_encode($dataArr); |
||||
|
$result = postToken($url,$json_data,false,[],$token); |
||||
|
|
||||
|
$res_data = json_decode($result,true); |
||||
|
if ($res_data['code'] != 0) throw new \Exception('请求失败'); |
||||
|
$list = (array)$res_data['data']['list']; |
||||
|
|
||||
|
foreach ($list as $val) { |
||||
|
$insert_arr = [ |
||||
|
'groupId' => $val['groupId'], |
||||
|
'groupName' => $val['groupName'], |
||||
|
'flowInNum' => $val['flowInNum'], |
||||
|
'flowOutNum' => $val['flowOutNum'], |
||||
|
'noRepeatInNum' => $val['noRepeatInNum'], |
||||
|
'noRepeatOutNum' => $val['noRepeatOutNum'], |
||||
|
'holdValue' => $val['holdValue'], |
||||
|
'createTime' => $val['createTime'], |
||||
|
'updateTime' => $val['updateTime'], |
||||
|
'netValue' => $val['netValue'], |
||||
|
'statTime' => $val['statTime'], |
||||
|
'granularity' => $granularity, |
||||
|
]; |
||||
|
|
||||
|
Db::table('bt_passenger_flow')->insert($insert_arr); |
||||
|
} |
||||
|
|
||||
|
} |
||||
|
} catch (\Exception $e) { |
||||
|
} |
||||
|
|
||||
|
return true; |
||||
|
} |
||||
|
} |
||||
@ -0,0 +1,89 @@ |
|||||
|
<?php |
||||
|
|
||||
|
namespace app\task\behavior\passengerFlow; |
||||
|
|
||||
|
use think\Db; |
||||
|
|
||||
|
class PassengerFlowAll |
||||
|
{ |
||||
|
/** |
||||
|
* 执行函数 |
||||
|
* @return bool |
||||
|
* @throws \think\Exception |
||||
|
* @throws \think\exception\PDOException |
||||
|
*/ |
||||
|
public function run() |
||||
|
{ |
||||
|
$config = config('api_config'); |
||||
|
$dateData = getTargetDate(); |
||||
|
$token = getAccessToken(); |
||||
|
|
||||
|
$dateArr = [ |
||||
|
'day' => [ |
||||
|
'startTime' => $dateData['day']['c_start_time'],// 当天开始时间 |
||||
|
'endTime' => date("c"),// 当天结束时间 |
||||
|
'granularity' => 'daily' |
||||
|
], |
||||
|
'week' => [ |
||||
|
'startTime' => $dateData['week']['c_start_time'], |
||||
|
'endTime' => date("c"), |
||||
|
'granularity' => 'daily' |
||||
|
], |
||||
|
'month' =>[ |
||||
|
'startTime' => $dateData['month']['c_start_time'], |
||||
|
'endTime' => date("c"), |
||||
|
'granularity' => 'monthly' |
||||
|
] |
||||
|
|
||||
|
]; |
||||
|
|
||||
|
$delete = Db::table('bt_passenger_flow_all')->where('id','>',1)->delete(); |
||||
|
|
||||
|
try { |
||||
|
|
||||
|
foreach ($dateArr as $date => $value) { |
||||
|
|
||||
|
$url = $config['host_url'] . "/api/cfas/v2/passengerFlow/groups"; |
||||
|
$granularity = $value['granularity']; |
||||
|
|
||||
|
$dataArr = [ |
||||
|
'granularity' => $granularity, |
||||
|
'startTime' => $value['startTime'], |
||||
|
'endTime' => $value['endTime'], |
||||
|
'ids' => '01' // --- <replace> --- // |
||||
|
]; |
||||
|
|
||||
|
$json_data = json_encode($dataArr); |
||||
|
$result = postToken($url,$json_data,false,[],$token); |
||||
|
|
||||
|
$res_data = json_decode($result,true); |
||||
|
if ($res_data['code'] != 0) throw new \Exception('请求失败'); |
||||
|
$list = (array)$res_data['data']['list']; |
||||
|
foreach ($list as $val) { |
||||
|
$insert_arr = [ |
||||
|
'groupId' => $val['groupId'], |
||||
|
'groupName' => $val['groupName'], |
||||
|
'flowInNum' => $val['flowInNum'], |
||||
|
'flowOutNum' => $val['flowOutNum'], |
||||
|
'noRepeatInNum' => $val['noRepeatInNum'], |
||||
|
'noRepeatOutNum' => $val['noRepeatOutNum'], |
||||
|
'holdValue' => $val['holdValue'], |
||||
|
'createTime' => $val['createTime'], |
||||
|
'updateTime' => $val['updateTime'], |
||||
|
'netValue' => $val['netValue'], |
||||
|
'statTime' => $val['statTime'], |
||||
|
'granularity' => $granularity, |
||||
|
'date' => $date, |
||||
|
]; |
||||
|
|
||||
|
Db::table('bt_passenger_flow_all')->insert($insert_arr); |
||||
|
} |
||||
|
} |
||||
|
|
||||
|
} catch (\Exception $e) { |
||||
|
|
||||
|
} |
||||
|
|
||||
|
return true; |
||||
|
} |
||||
|
} |
||||
@ -0,0 +1,59 @@ |
|||||
|
<?php |
||||
|
|
||||
|
namespace app\task\behavior\passengerFlow; |
||||
|
|
||||
|
use think\Db; |
||||
|
|
||||
|
class PassengerFlowReal |
||||
|
{ |
||||
|
/** |
||||
|
* 执行函数 |
||||
|
* @return bool |
||||
|
* @throws \think\Exception |
||||
|
* @throws \think\exception\PDOException |
||||
|
*/ |
||||
|
public function run() |
||||
|
{ |
||||
|
$config = config('api_config'); |
||||
|
|
||||
|
$delete = Db::table('bt_passenger_flow_real')->where('id','>',1)->delete(); |
||||
|
|
||||
|
try{ |
||||
|
|
||||
|
$url = $config['host_url'] . "/api/cfas/v3/passenger/realTime"; |
||||
|
$dataArr = [ |
||||
|
'01' // --- <replace> --- // |
||||
|
]; |
||||
|
|
||||
|
$token = getAccessToken(); |
||||
|
$json_data = json_encode($dataArr); |
||||
|
$result = postToken($url,$json_data,false,[],$token); |
||||
|
|
||||
|
$res_data = json_decode($result,true); |
||||
|
|
||||
|
$list = (array) $res_data['data']; |
||||
|
|
||||
|
foreach ($list as $value) { |
||||
|
|
||||
|
$insert_arr = [ |
||||
|
'groupId' => $value['groupId'], |
||||
|
'regionId' => $value['regionId'], |
||||
|
'statisticsTime' => $value['statisticsTime'], |
||||
|
'enter' => $value['enter'], |
||||
|
'exit' => $value['exit'], |
||||
|
'pass' => $value['pass'], |
||||
|
'holdValue' => $value['holdValue'], |
||||
|
'allEnter' => $value['allEnter'], |
||||
|
'allExit' => $value['allExit'] |
||||
|
]; |
||||
|
|
||||
|
Db::table('bt_passenger_flow_real')->insert($insert_arr); |
||||
|
} |
||||
|
|
||||
|
} catch (\Exception $e) { |
||||
|
|
||||
|
} |
||||
|
|
||||
|
return true; |
||||
|
} |
||||
|
} |
||||
Loading…
Reference in new issue