From 33a87c6398162f7d5ff14e102b1c6f55eb7baf4c Mon Sep 17 00:00:00 2001 From: wanghongjun <1445693971@qq,com> Date: Tue, 27 Jun 2023 14:50:45 +0800 Subject: [PATCH] =?UTF-8?q?=E4=BD=93=E8=82=B2=E9=A6=86=E6=95=B0=E6=8D=AE?= =?UTF-8?q?=E6=96=B0=E5=A2=9E=E8=84=9A=E6=9C=AC=E5=8F=8A=E6=8E=A5=E5=8F=A3?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../service/PassengerFlow/PassengerFlow.php | 194 ++++++++++++++++++ .../behavior/passengerFlow/PassengerFlow.php | 83 ++++++++ .../passengerFlow/PassengerFlowAll.php | 89 ++++++++ .../passengerFlow/PassengerFlowReal.php | 59 ++++++ 4 files changed, 425 insertions(+) create mode 100644 source/application/api/service/PassengerFlow/PassengerFlow.php create mode 100644 source/application/task/behavior/passengerFlow/PassengerFlow.php create mode 100644 source/application/task/behavior/passengerFlow/PassengerFlowAll.php create mode 100644 source/application/task/behavior/passengerFlow/PassengerFlowReal.php diff --git a/source/application/api/service/PassengerFlow/PassengerFlow.php b/source/application/api/service/PassengerFlow/PassengerFlow.php new file mode 100644 index 0000000..7932984 --- /dev/null +++ b/source/application/api/service/PassengerFlow/PassengerFlow.php @@ -0,0 +1,194 @@ + + * @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 + ); + } +} \ No newline at end of file diff --git a/source/application/task/behavior/passengerFlow/PassengerFlow.php b/source/application/task/behavior/passengerFlow/PassengerFlow.php new file mode 100644 index 0000000..6ddc89b --- /dev/null +++ b/source/application/task/behavior/passengerFlow/PassengerFlow.php @@ -0,0 +1,83 @@ + [ + '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' // --- --- // + ]; + + $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; + } +} \ No newline at end of file diff --git a/source/application/task/behavior/passengerFlow/PassengerFlowAll.php b/source/application/task/behavior/passengerFlow/PassengerFlowAll.php new file mode 100644 index 0000000..84247d2 --- /dev/null +++ b/source/application/task/behavior/passengerFlow/PassengerFlowAll.php @@ -0,0 +1,89 @@ + [ + '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' // --- --- // + ]; + + $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; + } +} \ No newline at end of file diff --git a/source/application/task/behavior/passengerFlow/PassengerFlowReal.php b/source/application/task/behavior/passengerFlow/PassengerFlowReal.php new file mode 100644 index 0000000..1d04751 --- /dev/null +++ b/source/application/task/behavior/passengerFlow/PassengerFlowReal.php @@ -0,0 +1,59 @@ +where('id','>',1)->delete(); + + try{ + + $url = $config['host_url'] . "/api/cfas/v3/passenger/realTime"; + $dataArr = [ + '01' // --- --- // + ]; + + $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; + } +} \ No newline at end of file