From 3547c5e90750a9a1cc41460edcdb974a945d3b80 Mon Sep 17 00:00:00 2001 From: wanghongjun <1445693971@qq,com> Date: Sun, 30 Jul 2023 16:04:19 +0800 Subject: [PATCH] =?UTF-8?q?=E6=96=87=E5=8C=96=E9=A6=86=E6=8E=A5=E5=8F=A3?= =?UTF-8?q?=EF=BC=8C=E6=96=87=E5=8C=96=E9=A6=86=E5=B0=8F=E6=97=B6=E6=95=B0?= =?UTF-8?q?=E6=8D=AE?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../api/controller/library/Pass.php | 72 ++++++++++ source/application/common/logic/PassFlow.php | 124 +++++++++++++++++- 2 files changed, 194 insertions(+), 2 deletions(-) diff --git a/source/application/api/controller/library/Pass.php b/source/application/api/controller/library/Pass.php index e7ff2a1..0440c29 100644 --- a/source/application/api/controller/library/Pass.php +++ b/source/application/api/controller/library/Pass.php @@ -50,6 +50,8 @@ class Pass extends Controller $data_json = $this->request->param('data'); $data = json_decode(html_entity_decode($data_json),true); + $hour = date("H:"); + $last_hour = date("H",strtotime('-1 hour')); $dm = new Dm(); foreach ($data as $val) { @@ -74,8 +76,78 @@ class Pass extends Controller $opData['create_time'] = date("Y-m-d H:i:s",time()); $dm->insert('bt_library',$opData); } + + // --------------------- 小时数据 --------------------- // + $hoursQueryWhere = ['hour_time' => $hour, 'group_id' => $val['group_id']]; + $hoursQuery = $dm->find("bt_library_hours",$hoursQueryWhere); + + # 默认单天数据 + $incount = 0; + $outcount = 0; + if ($hoursQuery) { + + # 存在的数量 + (这次天数数量 - 上次天数数量) = 当前小时数量 + $incount = $hoursQuery['incount'] + ($val['today_incount'] - $hoursQuery['target_today_incount']); + $outcount = $hoursQuery['outcount'] + ($val['today_outcount'] - $hoursQuery['target_today_outcount']); + + $update = [ + 'incount' => $incount, + 'outcount' => $outcount, + 'target_today_incount' => $val['today_incount'], + 'target_today_outcount' => $val['today_outcount'], + 'update_time' => date("Y-m-d H:i:s",time()) + ]; + + $dm->update('bt_library_hours',$update,['id' => $hoursQuery['id']]); + + } else { + # 查看该组是否存在上一条数据 + $lastHoursWhere = ['hour_time' => $last_hour, 'grouup_id' => $val['group_id']]; + $lastHours = $dm->find('bt_library_hours',$lastHoursWhere); + if ($lastHours) { + $incount = $val['today_incount'] - $lastHours['target_today_incount']; + $outcount = $val['today_outcount'] - $lastHours['target_today_outcount']; + } + # 创建数据 + $insert = [ + 'group_id' => $val['group_id'], + 'group_name' => base64_decode($val['group_name']), + 'incount' => $incount, + 'outcount' => $outcount, + 'target_today_incount' => $val['today_incount'], + 'target_today_outcount' => $val['today_outcount'], + 'create_time' => date("Y-m-d H:i:s",time()) + ]; + + $dm->insert("bt_library_hours",$insert); + } + + // --------------------- end --------------------- // } return $this->renderSuccess(); } + /** + * 文化 - 24小时 柱状图 数据 + * @return array + */ + public function getLibraryHours24List() + { + $data = PassFlow::getLibraryHours24List(); + return $this->renderSuccess(compact('data')); + } + + /** + * 今日个场馆数据 + * @return array + * @throws \think\db\exception\DataNotFoundException + * @throws \think\db\exception\ModelNotFoundException + * @throws \think\exception\DbException + */ + public function getLibraryGroupHoursList() + { + $data = PassFlow::getLibraryGroupHoursList(); + return $this->renderSuccess(compact('data')); + } + } \ No newline at end of file diff --git a/source/application/common/logic/PassFlow.php b/source/application/common/logic/PassFlow.php index 2f13381..f953b26 100644 --- a/source/application/common/logic/PassFlow.php +++ b/source/application/common/logic/PassFlow.php @@ -362,7 +362,7 @@ class PassFlow $where .= ' and "statTime" >= ' . "'{$start_time}'" . ' and "statTime" <= ' . "'{$end_time}'"; $list = $dm->select('bt_passenger_flow',$where); $allTimeData = []; - for ($i = 0; $i < 23; $i++) { + for ($i = 0; $i <= 23; $i++) { $str = strlen($i) == 1 ? '0'.$i.':00' : $i.':00'; $allTimeData[$str] = 0; } @@ -480,7 +480,7 @@ class PassFlow return $data; } /** - * 24小时各场馆数据 + * 体育 24小时各场馆数据 柱状图 * @return array */ public static function getPassHoursData($dm = null,$param = []) @@ -525,6 +525,10 @@ class PassFlow return $data; } + /** + * 获取24小时字段 + * @return array + */ protected static function getHoursArr() { $hours = array(); // 创建空数组 @@ -543,4 +547,120 @@ class PassFlow return $hours; } + + /** + * 文化 24小时数据 柱状图 + * @param $dm + * @return array + */ + public static function getLibraryHours24List($dm = null) + { + if (empty($dm)) $dm = new Dm(); + + # 获取时间参数 + $start_time = date("Y-m-d\TH",strtotime('-23 hours')) . ':00:00.000+08:00'; + $end_time = date("Y-m-d\TH") . ':59:59.000+08:00'; + # 获取文化组id + $groupRes = $dm->select('bt_passenger_monitor_group',['type' => 2],'"groupId","groupName"'); + $groupsId = []; + foreach ($groupRes as $groupRow) $groupsId[] = $groupRow['groupId']; + # 查询文化组 24小时数据 + $where = ' "granularity"='."'hourly' " . ' and "statTime" >= ' . "'{$start_time}'" . ' and "statTime" <= ' . "'{$end_time}'"; + $where .= ' and "groupId" in (' . "'" . implode("','",$groupsId) . "')"; + $list = $dm->select('bt_passenger_flow',$where); + # 收集参数 + $allTimeData = []; + $returnData = []; + $hoursArr = self::getHoursArr(); + foreach ($hoursArr as $hoursVal) { + $str = $hoursVal . ':00'; + $allTimeData[$str] = 0; + if (!isset($returnData[$str])) $returnData[$str] = 0; + } + foreach ($list as $value) { + $date = date('H:00',strtotime($value['statTime'])); // 各时间端进馆人数 + if (!isset($allTimeData[$date])) continue; + $returnData[$date] += $value['flowInNum']; + } + # 图书、文化馆 + $start_create_time = date("Y-m-d H:00:00",strtotime('-23 hours')); + $end_create_time = date("Y-m-d H:59:59"); + $hoursWhere = ' "group_id" = ' . "'lib001'" . ' and "create_time" >= ' . "'{$start_create_time}'" + . ' and "create_time" <= ' . "'{$end_create_time}'" ; + $hoursRes = $dm->select('bt_library_hours',$hoursWhere); + foreach ($hoursRes as $hoursRow) { + $date = $hoursRow['hour_time']; + if (!isset($allTimeData[$date])) continue; + $returnData[$date] += $hoursRow['incount']; + } + + $data = []; + foreach ($returnData as $date => $item) { + + $data['dkeys'][] = $date; + $data['dvalue'][] = $item; + } + return $data; + } + + /** + * 文化 今日各场馆数据 + * @param $dm + * @return array + * @throws \think\db\exception\DataNotFoundException + * @throws \think\db\exception\ModelNotFoundException + * @throws \think\exception\DbException + */ + public static function getLibraryGroupHoursList($dm = null) + { + if (empty($dm)) $dm = new Dm(); + + # 获取文化组id + $groupRes = $dm->select('bt_passenger_monitor_group',['type' => 2],'"groupId","groupName"'); + $groupsId = []; + foreach ($groupRes as $groupRow) $groupsId[] = $groupRow['groupId']; + $data = self::toDayGroupsEnterNum($dm,['groupsId' => implode("|",$groupsId)]); + + # 图书、文化馆 + $start_create_time = date("Y-m-d 00:00:00"); + $end_create_time = date("Y-m-d 23:59:59"); + $allTimeData = []; + for ($i = 0; $i <= 23; $i++) { + $str = strlen($i) == 1 ? '0'.$i.':00' : $i.':00'; + $allTimeData[$str] = 0; + } + $returnData = []; + + # 获取各组初始数值 + $groupData = [['groupName' => '图书馆'], ['groupName' => '文化馆']]; + foreach ($groupData as $groupDataRow) { + foreach ($allTimeData as $date => $dateValue) { + $returnData[$groupDataRow['groupName']][$date] = $dateValue; + } + } + $hoursWhere = ' "group_id" in (' . "'baz001','BAF055')" . ' and "create_time" >= ' . "'{$start_create_time}'" + . ' and "create_time" <= ' . "'{$end_create_time}'" ; + $hoursRes = $dm->select('bt_library_hours',$hoursWhere); + + foreach ($hoursRes as $hoursRow) { + $date = $hoursRow['hour_time']; // 各时间端进馆人数 + if (!isset($allTimeData[$date])) continue; + if (!isset($returnData[$hoursRow['group_name']][$allTimeData[$date]])) $returnData[$hoursRow['group_name']][$date] = 0; + $returnData[$hoursRow['group_name']][$date] += $hoursRow['incount']; + } + + foreach ($returnData as $groupName => $item) { + $listData = []; + foreach ($item as $itemKey => $itemRow) { + $listData['dkeys'][] = $itemKey; + $listData['dvalue'][] = $itemRow; + } + $data[] = [ + 'groupName' => $groupName, + 'list' => $listData + ]; + } + + return $data; + } } \ No newline at end of file