Files

296 lines
15 KiB
PHP

<?php
session_start();
if( !defined('IN') ) die('bad request!');
include_once( AROOT . 'controller' . DS . 'app.class.php');
header("Content-Type: text/html; charset=utf-8");
class eventController extends appController
{
public function __construct()
{
parent::__construct();
}
// 发送错误结果集
public function send_error($number,$msg)
{
$obj = array();
$obj['code'] = intval($number);
$obj['result'] = $msg;
if( g('API_EMBED_MODE') == 1 ){
return json_encode($obj);
}else{
header("Content-Type: application/json");
die(json_encode($obj));
}
}
// 发送结果集
public function send_result($data)
{
$obj = array();
$obj['rows'] = $data;
// $obj['result'] = 'success';
$obj['total'] = 1731;
if( g('API_EMBED_MODE') == 1 ){
return json_encode($obj);
}else{
header("Content-Type: application/json");
die(json_encode($obj));
}
}
// 发送结果集
public function send_resu($data)
{
$obj = array();
$obj['code'] = 0;
$obj['message'] = 'success';
$obj['data'] = $data;
if( g('API_EMBED_MODE') == 1 ){
return json_encode($obj);
}else{
header("Content-Type: application/json");
die(json_encode($obj));
}
}
//设备占比
public function eventPie(){
$module_hanyupinyin = v("id");
$project_id = $_SESSION['project_id'];
if($project_id == '0'){
if(empty($module_hanyupinyin)){
$sql_deviceType = "select CASE WHEN type =
1 THEN '控制柜' WHEN type = 2 THEN '曝气机' WHEN type = 3 THEN '管网监测' else '摄像头' END as name,count(id) as value from device group by type";
}else{
$sql_deviceType = prepare("select CASE WHEN type =
1 THEN '控制柜' WHEN type = 2 THEN '曝气机' WHEN type = 3 THEN '管网监测' else '摄像头' END as name,count(id) as value from device where project_id=?i group by type",array($module_hanyupinyin));
}
}else{
if(empty($module_hanyupinyin)){
$sql_deviceType = prepare("select CASE WHEN type =
1 THEN '控制柜' WHEN type = 2 THEN '曝气机' WHEN type = 3 THEN '管网监测' else '摄像头' END as name,count(id) as value from device where project_id=?i group by type",array($project_id));
}else{
$sql_deviceType = prepare("select CASE WHEN type =
1 THEN '控制柜' WHEN type = 2 THEN '曝气机' WHEN type = 3 THEN '管网监测' else '摄像头' END as name,count(id) as value from device where project_id=?i group by type",array($module_hanyupinyin));
}
}
// $project_id = $_SESSION['project_id'];
$deviceType = get_data($sql_deviceType);
// $data['type']=$deviceType;
return self::send_resu($deviceType);
}
//变化趋势 15天的变化趋势
public function eventline(){
$module_hanyupinyin = v("id");
$project_id = $_SESSION['project_id'];
$starttime = date("Y-m-d",strtotime('-7 day'));
$endtime = date("Y-m-d",time());
$start = ' 00:00:00';
$end = ' 23:59:59';
$times = $this -> getDate($starttime,$endtime);
if($project_id == '0'){
if(empty($module_hanyupinyin)){
for($i=0;$i<count($times);$i++){
$created_at_start = $times[$i].$start;
$created_at_end = $times[$i].$end;
$sql = prepare("select count(event.id) from event,device where event.device_id=device.id and event.created_at >= ?s and event.created_at <= ?s",array($created_at_start,$created_at_end));
$count = get_var($sql);
$data['times'][] = substr($times[$i],5,5);
$data['line'][] = $count;
}
}else{
for($i=0;$i<count($times);$i++){
$created_at_start = $times[$i].$start;
$created_at_end = $times[$i].$end;
$sql = prepare("select count(event.id) from event,device where event.device_id=device.id and device.project_id=?i and event.created_at >= ?s and event.created_at <= ?s",array($module_hanyupinyin,$created_at_start,$created_at_end));
$count = get_var($sql);
$data['times'][] = substr($times[$i],5,5);
$data['line'][] = $count;
}
}
}else{
if(empty($module_hanyupinyin)){
for($i=0;$i<count($times);$i++){
$created_at_start = $times[$i].$start;
$created_at_end = $times[$i].$end;
$sql = prepare("select count(event.id) from event,device where event.device_id=device.id and device.project_id=?i and event.created_at >= ?s and event.created_at <= ?s",array($project_id,$created_at_start,$created_at_end));
$count = get_var($sql);
$data['times'][] = substr($times[$i],5,5);
$data['line'][] = intval($count);
}
}else{
for($i=0;$i<count($times);$i++){
$created_at_start = $times[$i].$start;
$created_at_end = $times[$i].$end;
$sql = prepare("select count(event.id) from event,device where event.device_id=device.id and device.project_id=?i and event.created_at >= ?s and event.created_at <= ?s",array($module_hanyupinyin,$created_at_start,$created_at_end));
$count = get_var($sql);
$data['times'][] = substr($times[$i],5,5);
$data['line'][] = intval($count);
}
}
}
// $deviceType = get_data($sql_deviceType);
// $data['type']=$deviceType;
return self::send_resu($data);
}
public function eventDataTable(){
$module_hanyupinyin = v("id");
$name = v("name");
$terminal = v("terminal");
$project_id = $_SESSION['project_id'];
if($project_id == '0'){
if(empty($module_hanyupinyin)){
if(empty($terminal) && empty($name)){
$sql ="select project.name as project,terminal.name as terminal,device.name,event.id,event.event,event.created_at,event.event_description from event,device,terminal,project where event.device_id=device.id and terminal.id=device.terminal_id and device.project_id=project.id order by event.created_at desc";
}else if(empty($terminal) && !empty($name)){
$sql = prepare("select project.name as project,terminal.name as terminal,device.name,event.id,event.event,event.created_at,event.event_description from event,device,terminal,project where event.device_id=device.id and terminal.id=device.terminal_id and device.project_id=project.id and device.name=?s order by event.created_at desc",array($name));
}else if(!empty($terminal) && empty($name)){
$sql = prepare("select project.name as project,terminal.name as terminal,device.name,event.id,event.event,event.created_at,event.event_description from event,device,terminal,project where event.device_id=device.id and terminal.id=device.terminal_id and device.project_id=project.id and terminal.name=?s order by event.created_at desc",array($terminal));
}else{
$sql = prepare("select project.name as project,terminal.name as terminal,device.name,event.id,event.event,event.created_at,event.event_description from event,device,terminal,project where event.device_id=device.id and terminal.id=device.terminal_id and device.project_id=project.id and terminal.name=?s and device.name=?s order by event.created_at desc",array($terminal,$name));
}
}else{
if(empty($terminal) && empty($name)){
$sql =prepare("select project.name as project,terminal.name as terminal,device.name,event.id,event.event,event.created_at,event.event_description from event,device,terminal,project where event.device_id=device.id and terminal.id=device.terminal_id and device.project_id=project.id and device.project_id=?i order by event.created_at desc",array($module_hanyupinyin));
}else if(empty($terminal) && !empty($name)){
$sql =prepare("select project.name as project,terminal.name as terminal,device.name,event.id,event.event,event.created_at,event.event_description from event,device,terminal,project where event.device_id=device.id and terminal.id=device.terminal_id and device.project_id=project.id and device.name=?s and device.project_id=?i order by event.created_at desc",array($name,$module_hanyupinyin));
}else if(!empty($terminal) && empty($name)){
$sql =prepare("select project.name as project,terminal.name as terminal,device.name,event.id,event.event,event.created_at,event.event_description from event,device,terminal,project where event.device_id=device.id and terminal.id=device.terminal_id and device.project_id=project.id and terminal.name=?s and device.project_id=?i order by event.created_at desc",array($terminal,$module_hanyupinyin));
}else{
$sql =prepare("select project.name as project,terminal.name as terminal,device.name,event.id,event.event,event.created_at,event.event_description from event,device,terminal,project where event.device_id=device.id and terminal.id=device.terminal_id and device.project_id=project.id and terminal.name=?s and device.name=?s and device.project_id=?i order by event.created_at desc",array($terminal,$name,$module_hanyupinyin));
}
}
}else{
if(empty($module_hanyupinyin)){
if(empty($terminal) && empty($name)){
$sql =prepare("select project.name as project,terminal.name as terminal,device.name,event.id,event.event,event.created_at,event.event_description from event,device,terminal,project where event.device_id=device.id and terminal.id=device.terminal_id and device.project_id=project.id and device.project_id=?i order by event.created_at desc",array($project_id));
}else if(empty($terminal) && !empty($name)){
$sql =prepare("select project.name as project,terminal.name as terminal,device.name,event.id,event.event,event.created_at,event.event_description from event,device,terminal,project where event.device_id=device.id and terminal.id=device.terminal_id and device.project_id=project.id and device.name=?s and device.project_id=?i order by event.created_at desc",array($name,$project_id));
}else if(!empty($terminal) && empty($name)){
$sql =prepare("select project.name as project,terminal.name as terminal,device.name,event.id,event.event,event.created_at,event.event_description from event,device,terminal,project where event.device_id=device.id and terminal.id=device.terminal_id and device.project_id=project.id and terminal.name=?s and device.project_id=?i order by event.created_at desc",array($terminal,$project_id));
}else{
$sql =prepare("select project.name as project,terminal.name as terminal,device.name,event.id,event.event,event.created_at,event.event_description from event,device,terminal,project where event.device_id=device.id and terminal.id=device.terminal_id and device.project_id=project.id and terminal.name=?s and device.name=?s and device.project_id=?i order by event.created_at desc",array($terminal,$name,$project_id));
}
}else{
if(empty($terminal) && empty($name)){
$sql =prepare("select project.name as project,terminal.name as terminal,device.name,event.id,event.event,event.created_at,event.event_description from event,device,terminal,project where event.device_id=device.id and terminal.id=device.terminal_id and device.project_id=project.id and device.project_id=?i order by event.created_at desc",array($module_hanyupinyin));
}else if(empty($terminal) && !empty($name)){
$sql =prepare("select project.name as project,terminal.name as terminal,device.name,event.id,event.event,event.created_at,event.event_description from event,device,terminal,project where event.device_id=device.id and terminal.id=device.terminal_id and device.project_id=project.id and device.name=?s and device.project_id=?i order by event.created_at desc",array($name,$module_hanyupinyin));
}else if(!empty($terminal) && empty($name)){
$sql =prepare("select project.name as project,terminal.name as terminal,device.name,event.id,event.event,event.created_at,event.event_description from event,device,terminal,project where event.device_id=device.id and terminal.id=device.terminal_id and device.project_id=project.id and terminal.name=?s and device.project_id=?i order by event.created_at desc",array($terminal,$module_hanyupinyin));
}else{
$sql =prepare("select project.name as project,terminal.name as terminal,device.name,event.id,event.event,event.created_at,event.event_description from event,device,terminal,project where event.device_id=device.id and terminal.id=device.terminal_id and device.project_id=project.id and terminal.name=?s and device.name=?s and device.project_id=?i order by event.created_at desc",array($terminal,$name,$module_hanyupinyin));
}
}
}
$data = get_data($sql);
return self::send_result($data);
}
// 贡献图
public function getHeatMap()
{
$project_id = $_SESSION['project_id'];
$startdate = date('Y-01-01', strtotime(date('Y-m-d')));
$enddate = date('Y-12-31', strtotime(date('Y-m-d')));
$times = $this -> getDate($startdate,$enddate);
if($project_id == '0' && empty($module_hanyupinyin)){
for($i=0;$i<count($times);$i++){
$start_time = $times[$i]." 00:00:00";
$end_time = $times[$i]." 23:59:59";
$sql = prepare("select COALESCE(COUNT(event.id), 0) from event,device where event.device_id=device.id and event.created_at>=?s and event.created_at<=?s",array($start_time,$end_time));
$data = intval(get_var($sql));
$resu[] = array($data);
}
}elseif($project_id == '0' && !empty($module_hanyupinyin)){
for($i=0;$i<count($times);$i++){
$start_time = $times[$i]." 00:00:00";
$end_time = $times[$i]." 23:59:59";
$sql = prepare("select COALESCE(COUNT(event.id), 0) from event,device where event.device_id=device.id and device.project_id=?i and event.created_at>=?s and event.created_at<=?s",array($module_hanyupinyin,$start_time,$end_time));
$data = intval(get_var($sql));
$resu[] = array($data);
}
}elseif($project_id != '0' && empty($module_hanyupinyin)){
for($i=0;$i<count($times);$i++){
$start_time = $times[$i]." 00:00:00";
$end_time = $times[$i]." 23:59:59";
$sql = prepare("select COALESCE(COUNT(event.id), 0) from event,device where event.device_id=device.id and device.project_id=?i and event.created_at>=?s and event.created_at<=?s",array($project_id,$start_time,$end_time));
$data = intval(get_var($sql));
$resu[] = array($data);
}
}elseif($project_id != '0' && !empty($module_hanyupinyin)){
for($i=0;$i<count($times);$i++){
$start_time = $times[$i]." 00:00:00";
$end_time = $times[$i]." 23:59:59";
$sql = prepare("select COALESCE(COUNT(event.id), 0) from event,device where event.device_id=device.id and device.project_id=?i and event.created_at>=?s and event.created_at<=?s",array($module_hanyupinyin,$start_time,$end_time));
$data = intval(get_var($sql));
$resu[] = array($data);
}
}
return self::send_resu($resu);
}
// 通过时间参数 获取整个区域的医废类型分布图
public function getAreaDashboardMwTypePie()
{
$result = array(368,81);
return self::send_result($result);
}
/**
* 获取两个指定日期之间的每一天
*/
// 获取指定日期之间的所有日期
public function getDate($startdate,$enddate){
$stimestamp = strtotime($startdate);
$etimestamp = strtotime($enddate);
// 计算日期段内有多少天
$days = ($etimestamp-$stimestamp)/86400+1;
// 保存每天日期
$date = array();
for($i=0; $i<$days; $i++){
$date[] = date('Y-m-d', $stimestamp+(86400*$i));
}
return $date;
}
/**
* 获取本年的12个月
*/
public function getYearMonthTime()
{
for ($i=1; $i < 13; $i++) {
$month = date("Y",time())."-".$i;
$array[] = date("Y-m",strtotime($month));
}
return $array;
}
}