\PDO::FETCH_ASSOC, \PDO::MYSQL_ATTR_INIT_COMMAND => "SET NAMES 'utf8mb4'" ]); // $db->setAttribute(PDO::ATTR_DEFAULT_FETCH_MODE , PDO::FETCH_ASSOC ); $terminals = array(); $ts = time(); $result = array('res' => 0, 'data' => array()); $startTime = isset($_GET['st']) ? intval($_GET['st']) : ($ts - 86400); $endTime = isset($_GET['et']) ? intval($_GET['et']) : $ts; $lineId = isset($_GET['lineId']) ? intval($_GET['lineId']) : 0; $channel = isset($_GET['channel']) ? intval($_GET['channel']) : 0; if ($channel === 0) { $sql = "SELECT term_id,count(*) AS photo_count FROM terminal_photos WHERE photo_time BETWEEN ? AND ? GROUP BY term_id"; } else { $sql = "SELECT term_id,count(*) AS photo_count FROM terminal_photos WHERE photo_time BETWEEN ? AND ? AND channel_id=? GROUP BY term_id"; } $stmt = $db->prepare($sql); $stmt->bindValue(1 , $startTime); $stmt->bindValue(2 , $endTime); if ($channel > 0) { $stmt->bindValue(3, $channel); } $stmt->execute(); $rows = $stmt->fetchAll(); $stmt = null; $terminal_mapper = array(); foreach ($rows as $row) { $terminal_mapper[$row['term_id']] = $row['photo_count']; } if ($channel == 0) { $sql = "SELECT term_id,max(recv_time) AS recv_time,min(photo_time) AS min_photo_time FROM terminal_photos WHERE photo_time BETWEEN ? AND ? AND media_type<>2 GROUP BY term_id"; } else { $sql = "SELECT term_id,max(recv_time) AS recv_time,min(photo_time) AS min_photo_time FROM terminal_photos WHERE photo_time BETWEEN ? AND ? AND channel_id=? AND media_type<>2 GROUP BY term_id"; } $stmt = $db->prepare($sql); $stmt->bindValue(1 , $startTime); $stmt->bindValue(2 , $endTime); if ($channel > 0) { $stmt->bindValue(3, $channel); } $stmt->execute(); $rows = $stmt->fetchAll(); $stmt = null; $recv_time_mapper = array(); foreach ($rows as $row) { $recv_time_mapper[$row['term_id']] = $row; //['recv_time']; } $stmt = $db->query("SELECT term_id,count(*) AS cnt FROM terminal_basic_info_history WHERE update_time BETWEEN " . $startTime . " AND " . $endTime . " GROUP BY term_id"); $rows = $stmt->fetchAll(); foreach ($rows as $row) { $reboot_mapper[$row['term_id']] = $row['cnt']; } $lineCondtion = $lineId ? ' WHERE t3.`id`=' . $lineId : ''; $sql = "SELECT t1.id, t1.cmdid,t1.`display_name`,t1.`protocol`,t2.`name` AS tower_name,t3.`name` AS line_name, FROM_UNIXTIME(t4.last_heartbeat) AS last_heartbeat,FROM_UNIXTIME(t4.boot_time) AS boot_time FROM terminals AS t1 JOIN towers AS t2 ON t1.tower_id=t2.id JOIN `lines` AS t3 ON t2.line_id=t3.id"; $sql .= " LEFT JOIN terminal_status AS t4 ON t1.id=t4.term_id " . $lineCondtion . " ORDER BY t2.line_id,t1.cmdid"; $stmt = $db->query($sql); $rows = $stmt->fetchAll(); $stmt = null; foreach ($rows as $row) { if (!isset($terminal_mapper[$row['id']])) { continue; } $row['cnt'] = $terminal_mapper[$row['id']]; $row['reboot_cnt'] = isset($reboot_mapper[$row['id']]) ? $reboot_mapper[$row['id']] : 0; $row['recv_time'] = isset($recv_time_mapper[$row['id']]) ? date('Y-m-d H:i:s', $recv_time_mapper[$row['id']]['recv_time']) : ''; $row['min_photo_time'] = isset($recv_time_mapper[$row['id']]) ? date('H:i:s', $recv_time_mapper[$row['id']]['min_photo_time']) : ''; $result['data'][] = $row; } header ('Content-type: application/json; charset=utf-8'); echo json_encode($result, JSON_UNESCAPED_UNICODE);