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.
162 lines
5.6 KiB
162 lines
5.6 KiB
<?php
|
|
use Workerman\Worker;
|
|
use Workerman\Connection\TcpConnection;
|
|
use app\common\dm\Dm;
|
|
require_once __DIR__ . '/workman/vendor/autoload.php';
|
|
require_once __DIR__ . '../../source/application/common/dm/Dm.php';
|
|
require_once __DIR__ . '../../source/thinkphp/helper.php';
|
|
require_once __DIR__ . '../../source/thinkphp/library/think/Config.php';
|
|
$config = require_once __DIR__ . '../../source/application/database.php';
|
|
|
|
$dm = new Dm($config['connections']['dm']);
|
|
// 注意:这里与上个例子不同,使用的是websocket协议
|
|
$ws_worker = new Worker("websocket://0.0.0.0:2000");
|
|
|
|
// 启动4个进程对外提供服务
|
|
$ws_worker->count = 4;
|
|
// 当收到客户端发来的数据后返回hello $data给客户端
|
|
$ws_worker->onMessage = function(TcpConnection $connection, $data) use($dm) {
|
|
|
|
$param = json_decode(html_entity_decode($data),true);
|
|
$returnData = ['code' => 0, 'msg' => 'error:请求失败'];
|
|
|
|
try {
|
|
$type = $param['type'];
|
|
$request = $param['request'];
|
|
|
|
switch ($type) {
|
|
case 'allGroupTimeNum':
|
|
$passData = all_group_time_num($request['granularity'],$request['groupsId'],$dm);
|
|
if (isset($passData['msg'])) throw new Exception($passData['msg']);
|
|
$returnData['data'] = $passData;
|
|
break;
|
|
default:
|
|
throw new Exception('type:参数数据错误');
|
|
}
|
|
$returnData['code'] = 1;
|
|
$returnData['msg'] = 'success';
|
|
} catch (Exception $e) {
|
|
$returnData['msg'] = $e->getMessage();
|
|
}
|
|
|
|
$connection->send(json_encode($returnData));
|
|
};
|
|
|
|
// 运行worker
|
|
Worker::runAll();
|
|
|
|
|
|
/**
|
|
* 分时时间段接口 <首页-客流趋势>
|
|
* @return array
|
|
*
|
|
*/
|
|
function all_group_time_num($granularity,$groupsId,$dm) {
|
|
|
|
|
|
|
|
if (empty($granularity)) return array('granularity参数不能为空');
|
|
$where = '';
|
|
if(!empty($groupsId)){
|
|
$garr = explode("|",$groupsId);
|
|
$where .= ' "groupId" in(' . "'" . implode("','",$garr) . "')";
|
|
|
|
}
|
|
|
|
$allTimeData = [];
|
|
$groupsData = [];
|
|
$dkeys=[];
|
|
#$dm = new Dm();
|
|
if ($granularity == 'daily') {
|
|
$where .= $where ? ' and "granularity" = ' . "'hourly'" : ' "granularity" = ' . "'hourly'";
|
|
$list = $dm->select('bt_passenger_flow',$where);
|
|
$data = handle_group_data($list);
|
|
$allTimeData = $data['allTimeData'];
|
|
$groupsData = $data['groupsData'];
|
|
$dkeys = $data['dkeys'];
|
|
} elseif ($granularity == 'monthly') {
|
|
$where .= $where ? ' and "granularity" = ' . "'daily'" : ' "granularity" = ' . "'daily'";
|
|
$list = $dm->select('bt_passenger_flow',$where);
|
|
$data = handle_group_data($list,'d');
|
|
$allTimeData = $data['allTimeData'];
|
|
$groupsData = $data['groupsData'];
|
|
$dkeys = $data['dkeys'];
|
|
} elseif ($granularity == 'yearly') {
|
|
$where .= $where ? ' and "granularity" = ' . "'monthly'" : ' "granularity" = ' . "'monthly'";
|
|
$list = $dm->select('bt_passenger_flow',$where);
|
|
$data = handle_group_data($list,'m');
|
|
$allTimeData = $data['allTimeData'];
|
|
$groupsData = $data['groupsData'];
|
|
$dkeys = $data['dkeys'];
|
|
}
|
|
|
|
return ['allTimeData' => $allTimeData, 'groupsData' => $groupsData, 'dkeys' => $dkeys];
|
|
}
|
|
|
|
/**
|
|
* 特殊处理
|
|
* @param $list
|
|
* @param $date_str
|
|
* @return array
|
|
*/
|
|
function handle_group_data($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
|
|
];
|
|
}
|