Browse Source

优化workman

master
wanghongjun 3 years ago
parent
commit
febc0c828c
  1. 24
      source/application/api/controller/library/Pass.php
  2. 233
      source/application/api/controller/pass/Passcc.php
  3. 26
      source/application/api/controller/stadium/Param.php
  4. 11
      source/application/api/controller/stadium/Pass.php
  5. 317
      source/application/common/logic/PassFlow.php
  6. 144
      web/ws_task.php

24
source/application/api/controller/library/Pass.php

@ -3,6 +3,7 @@ namespace app\api\controller\library;
use app\api\controller\Controller;
use app\common\dm\Dm;
use app\common\logic\PassFlow;
class Pass extends Controller
{
@ -18,28 +19,7 @@ class Pass extends Controller
*/
public function allFlowTrends()
{
$dm = new Dm();
$res = $dm->find('bt_library',['group_id' => 'lib001']);
$data = [
'day30' => [
'incount' => isset($res['day30_incount']) ? $res['day30_incount'] : 0,
'outcount' => isset($res['day30_outcount']) ? $res['day30_outcount'] : 0
],
'month' => [
'incount' => isset($res['month_incount']) ? $res['month_incount'] : 0,
'outcount' => isset($res['month_outcount']) ? $res['month_outcount'] : 0
],
'today' => [
'incount' => isset($res['today_incount']) ? $res['today_incount'] : 0,
'outcount' => isset($res['today_outcount']) ? $res['today_outcount'] : 0
],
'year' => [
'incount' => isset($res['year_incount']) ? $res['year_incount'] : 0,
'outcount' => isset($res['year_outcount']) ? $res['year_outcount'] : 0
],
];
$data = PassFlow::allFlowTrends();
return $this->renderSuccess(compact('data'));
}

233
source/application/api/controller/pass/Passcc.php

@ -5,6 +5,7 @@ namespace app\api\controller\pass;
use app\api\controller\Controller;
use app\common\dm\Dm;
use app\common\logic\PassFlow;
use app\common\model\Pass;
use think\Db;
@ -22,124 +23,14 @@ class Passcc extends Controller
* @throws \think\db\exception\ModelNotFoundException
* @throws \think\exception\DbException
*/
public function allGroupTimeNum() {
public function allGroupTimeNum()
{
$granularity = $this->request->param('granularity');
$groupsId = $this->request->param('groupsId');
if (empty($granularity)) return $this->renderError('date参数不能为空');
$where = '';
if(!empty($groupsId)){
$garr = explode("|",$groupsId);
$where .= ' "groupId" in(' . "'" . implode("','",$garr) . "')";
}
//if (!empty($groupsId)) $where .= ' "groupId" 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];
$data = PassFlow::allGroupTimeNum(['granularity' => $granularity, 'groupsId' => $groupsId]);
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
@ -147,50 +38,9 @@ class Passcc extends Controller
* @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']);
}
public function allGroupNum()
{
$returnData = PassFlow::allGroupNum();
return $this->renderSuccess(compact('returnData'));
}
@ -203,42 +53,7 @@ class Passcc extends Controller
*/
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 = $dm->select('bt_library',['group_id' => ['baz001','BAF055']]);
foreach ($libData as $libRow) {
$libProportion = $libRow['today_incount'] > 0 ? round($libRow['today_incount']/$threshold,2) : 0;
$returnData[] = [
'allEnter' => $libRow['today_incount'] ?: 0,
'proportion' => ($libProportion * 100) . "%",
'congestion' => Pass::getPassType($libProportion),
'groupId' => $libRow['group_id'],
'groupName' => $libRow['group_name']
];
}
$returnData = PassFlow::realTimeData();
return $this->renderSuccess(compact('returnData'));
}
@ -251,37 +66,7 @@ class Passcc extends Controller
*/
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) {
$listData = [];
foreach ($item as $itemKey => $itemRow) {
$listData[] = [
'time' => $itemKey,
'count' => $itemRow
];
}
$data[] = [
'groupName' => $groupName,
'list' => $listData
];
}
$data = PassFlow::toDayGroupsEnterNum();
return $this->renderSuccess(compact('data'));
}

26
source/application/api/controller/stadium/Param.php

@ -1,26 +0,0 @@
<?php
namespace app\api\controller\stadium;
use app\api\controller\Controller;
class Param extends Controller
{
public function paramInfo()
{
$config = config('api_config');
$returnArr = [
'host' => $config['host'],
'port' => $config['port'],
'appkey' => $config['cid'],
'secret' => $config['cskey']
];
$base64 = base64_encode(json_encode($returnArr)) . "==";
$base64Two = base64_encode($base64);
return $this->renderSuccess(compact('base64Two'));
}
}

11
source/application/api/controller/stadium/Pass.php

@ -1,11 +0,0 @@
<?php
namespace app\api\controller\stadium;
use app\api\controller\Controller;
class Pass extends Controller
{
}

317
source/application/common/logic/PassFlow.php

@ -0,0 +1,317 @@
<?php
namespace app\common\logic;
use app\common\dm\Dm;
use app\common\model\Pass;
class PassFlow
{
/**
* 分时时间段接口 <首页-客流趋势>
* @return array
* @throws \think\db\exception\DataNotFoundException
* @throws \think\db\exception\ModelNotFoundException
* @throws \think\exception\DbException
*/
public static function allGroupTimeNum($param,$dm = null) {
$granularity = $param['granularity'];
$groupsId = $param['groupsId'];
if (empty($granularity)) return ['code' => 0,'msg' => '缺少参数:granularity'];
$where = '';
if(!empty($groupsId)){
$garr = explode("|",$groupsId);
$where .= ' "groupId" in(' . "'" . implode("','",$garr) . "')";
}
$allTimeData = [];
$groupsData = [];
$dkeys=[];
if (empty($dm)) $dm = new Dm();
if ($granularity == 'daily') {
$where .= $where ? ' and "granularity" = ' . "'hourly'" : ' "granularity" = ' . "'hourly'";
$list = $dm->select('bt_passenger_flow',$where);
$data = self::handleGroupData($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 = self::handleGroupData($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 = self::handleGroupData($list,'m');
$allTimeData = $data['allTimeData'];
$groupsData = $data['groupsData'];
$dkeys = $data['dkeys'];
}
$data = ['allTimeData' => $allTimeData, 'groupsData' => $groupsData, 'dkeys' => $dkeys];
return ['code' => 1, 'msg' => 'success', 'data' => $data];
}
/**
* 特殊处理
* @param $list
* @param $date_str
* @return array
*/
protected static 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 static function allGroupNum($dm = null) {
$dateArr = ['day' => 'daily', 'week' => 'daily', 'month' => 'monthly', 'year' => 'yearly'];
$returnData = [];
$y = date("Y");
# 总进馆人数
$yearSum = 0;
if (empty($dm)) $dm = new Dm();
foreach ($dateArr as $date => $granularity) {
$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;
foreach ($returnData as &$val) {
$val['noRepeatInNum'] = formatNumber($val['noRepeatInNum']);
}
return $returnData;
}
/**
* 查询最新客流统计组实时数据 <各场管实施实时进馆人数>
* @return array
* @throws \think\db\exception\DataNotFoundException
* @throws \think\db\exception\ModelNotFoundException
* @throws \think\exception\DbException
*/
public static function realTimeData($dm = null)
{
if (empty($dm)) $dm = new Dm();
$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 = $dm->select('bt_library',['group_id' => ['baz001','BAF055']]);
foreach ($libData as $libRow) {
$libProportion = $libRow['today_incount'] > 0 ? round($libRow['today_incount']/$threshold,2) : 0;
$returnData[] = [
'allEnter' => $libRow['today_incount'] ?: 0,
'proportion' => ($libProportion * 100) . "%",
'congestion' => self::getPassType($libProportion),
'groupId' => $libRow['group_id'],
'groupName' => $libRow['group_name']
];
}
return $returnData;
}
protected static function getPassType($proportion)
{
# 舒适:阈值<80%
if ($proportion < 0.8) {
return '舒适';
}
# 饱和:80%<阈值<100%
if (0.8 < $proportion || $proportion < 1) {
return '饱和';
}
# 拥挤:阈值=100%
if ($proportion == 1) {
return '拥挤';
}
# 拥挤:阈值=100%
if ($proportion >= 1.2) {
return '爆满';
}
return '';
}
/**
* 今日各场馆人数趋势
* @return array
* @throws \think\db\exception\DataNotFoundException
* @throws \think\db\exception\ModelNotFoundException
* @throws \think\exception\DbException
*/
public static function toDayGroupsEnterNum($dm = null)
{
if (empty($dm)) $dm = new Dm();
$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) {
$listData = [];
foreach ($item as $itemKey => $itemRow) {
$listData[] = [
'time' => $itemKey,
'count' => $itemRow
];
}
$data[] = [
'groupName' => $groupName,
'list' => $listData
];
}
return $data;
}
/**
* 图书管数据接口
* @return array
* 返回数据:today.incount=今天进 month.incount=本月进 year.incount=今年进 day30.incount=近30天近 outcount=出
* 不传参数就是所有摄像头,传cameraid就是按传的摄像头ID
* cameraid = 宝安图书馆9,7,6,4,2,1,67,3 1990分馆134、 135
*/
public static function allFlowTrends($dm = null)
{
if (empty($dm)) $dm = new Dm();
$res = $dm->find('bt_library',['group_id' => 'lib001']);
$data = [
'day30' => [
'incount' => isset($res['day30_incount']) ? $res['day30_incount'] : 0,
'outcount' => isset($res['day30_outcount']) ? $res['day30_outcount'] : 0
],
'month' => [
'incount' => isset($res['month_incount']) ? $res['month_incount'] : 0,
'outcount' => isset($res['month_outcount']) ? $res['month_outcount'] : 0
],
'today' => [
'incount' => isset($res['today_incount']) ? $res['today_incount'] : 0,
'outcount' => isset($res['today_outcount']) ? $res['today_outcount'] : 0
],
'year' => [
'incount' => isset($res['year_incount']) ? $res['year_incount'] : 0,
'outcount' => isset($res['year_outcount']) ? $res['year_outcount'] : 0
],
];
return $data;
}
}

144
web/ws_task.php

@ -2,8 +2,10 @@
use Workerman\Worker;
use Workerman\Connection\TcpConnection;
use app\common\dm\Dm;
use app\common\logic\PassFlow;
require_once __DIR__ . '/workman/vendor/autoload.php';
require_once __DIR__ . '../../source/application/common/dm/Dm.php';
require_once __DIR__ . '../../source/application/common/logic/PassFlow.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';
@ -26,8 +28,28 @@ $ws_worker->onMessage = function(TcpConnection $connection, $data) use($dm) {
switch ($type) {
case 'allGroupTimeNum':
$passData = all_group_time_num($request['granularity'],$request['groupsId'],$dm);
if (isset($passData['msg'])) throw new Exception($passData['msg']);
$passData = PassFlow::allGroupTimeNum(['granularity' => $request['granularity'] ,'groupsId' => $request['groupsId']],$dm);
if (empty($passData['code'])) throw new Exception($passData['msg']);
$returnData['data'] = $passData;
break;
case 'allGroupNum':
$passData = PassFlow::allGroupNum($dm);
if (empty($passData['code'])) throw new Exception($passData['msg']);
$returnData['data'] = $passData;
break;
case 'realTimeData':
$passData = PassFlow::realTimeData($dm);
if (empty($passData['code'])) throw new Exception($passData['msg']);
$returnData['data'] = $passData;
break;
case 'toDayGroupsEnterNum':
$passData = PassFlow::toDayGroupsEnterNum($dm);
if (empty($passData['code'])) throw new Exception($passData['msg']);
$returnData['data'] = $passData;
break;
case 'allFlowTrends':
$passData = PassFlow::allFlowTrends($dm);
if (empty($passData['code'])) throw new Exception($passData['msg']);
$returnData['data'] = $passData;
break;
default:
@ -43,120 +65,4 @@ $ws_worker->onMessage = function(TcpConnection $connection, $data) use($dm) {
};
// 运行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
];
}
Worker::runAll();
Loading…
Cancel
Save