diff --git a/tools/wsndata.php b/tools/wsndata.php index d112896d..670578e1 100644 --- a/tools/wsndata.php +++ b/tools/wsndata.php @@ -64,7 +64,9 @@ ini_set('error_log','/tmp/wsn.log'); } } } else { - $sql_user = "SELECT distinct uid,site,variable,starttime,endtime from users_pm_down where starttime is not null and result=-1 and type='down' and uid=$uid"; + $sql_user = "SELECT distinct u.uid,u.site,u.starttime,u.endtime ,s.fld_name,case when (u.variable=-1) then '-1' else '1' end as variable + from users_pm_down u left join tbl_site_info s on u.site=s.fld_site_id + where starttime is not null and result=-1 and type='down' and uid=$uid"; $sth_user = $db->query($sql_user); $rows_user = $sth_user->fetchAll(); @@ -84,10 +86,7 @@ ini_set('error_log','/tmp/wsn.log'); $days=round((strtotime($end)-strtotime($start))/3600/24)+1; //获取观测点的名称 - $sql_site= "SELECT fld_name from tbl_site_info where fld_site_id=$site"; - $sth_site = $db->query($sql_site); - $rows_site = $sth_site->fetch(); - $sitename= $rows_site['fld_name']; + $sitename= $v['fld_name']; //先为该用户创建一个文件夹 $aimDir_user=$basepath.'wsn_'.$uid; @@ -103,84 +102,77 @@ ini_set('error_log','/tmp/wsn.log'); $result = mkdir($aimDir_site); } - //选择全部变量 - if($variable==-1) + //获取变量名称 + $sql="select distinct va.fld_name from tbl_value v left join tbl_variable_info va on v.fld_variable_id=va.fld_variable_id + WHERE v.fld_site_id=$site AND v.fld_time>='$start' AND v.fld_time<'$end'"; + if ($variable==1) { - //获取数据 - $sql2= "SELECT v.fld_time as time,v.fld_value as value,va.fld_name as variablename FROM tbl_value v left join tbl_variable_info va on v.fld_variable_id=va.fld_variable_id WHERE v.fld_site_id=$site AND v.fld_time>='$start' AND v.fld_time<'$end' ORDER BY time "; - $sth2 = $db->query($sql2); - //$rows2 = $sth2->fetchAll(); - $endtime=date("Y-m-d",strtotime($start)+24*3600); - $head = "Time,Variable,Value, \n"; - $html=""; - while ($v=$sth2->fetch()) - { - //组装数据 - if ($v['time']>=$start && $v['time']<$endtime) - { - $html.=$v['time'].",".$v['variablename'].",".$v['value']."\n"; - } elseif ($v['time']>=$endtime) { - //写入文件 - $resulttime=date("Ymd",strtotime($start)); - $fp = fopen($aimDir_site.DIRECTORY_SEPARATOR.$sitename."_all_".$resulttime.".CSV","w+"); - fwrite($fp,$head.$html); //写入数据 - fclose($fp); //关闭文件句柄 - $start=$endtime; - $endtime=date("Y-m-d",strtotime($start)+24*3600); - $html=$v['time'].",".$v['variablename'].",".$v['value']."\n"; - } - } - //写入文件 - $resulttime=date("Ymd",strtotime($start)); - $fp = fopen($aimDir_site.DIRECTORY_SEPARATOR.$sitename."_all_".$resulttime.".CSV","w+"); - fwrite($fp,$head.$html); //写入数据 - fclose($fp); //关闭文件句柄 - print $sitename."\r\n"; - $sql_up= "update users_pm_down set result=0 WHERE site=$site and variable=-1 and uid=$uid and result=-1 and type='down'"; - $db->query($sql_up); - } else { - //获取观测变量的名称 - $sql3= "SELECT fld_name from tbl_variable_info where fld_variable_id=$variable"; - $sth3 = $db->query($sql3); - $rows3 = $sth3->fetch(); - $varname=$rows3['fld_name']; - //获取数据 - $endtime=date("Y-m-d",strtotime($start)+24*3600); - $sql2= "SELECT v.fld_time as time,v.fld_value as value,va.fld_name as variablename FROM tbl_value v left join tbl_variable_info va on v.fld_variable_id=va.fld_variable_id WHERE v.fld_variable_id=$variable and v.fld_site_id=$site AND v.fld_time>='$start' AND v.fld_time<='$end' ORDER BY time "; - $sth2 = $db->query($sql2); - //$rows2 = $sth2->fetchAll(); - $head = "Time,Variable,Value, \n"; - $html=""; - while ($v = $sth2->fetch()) - { - //组装数据 - if ($v['time']>=$start && $v['time']<$endtime) - { - $html.=$v['time'].",".$v['variablename'].",".$v['value']."\n"; - } elseif ($v['time']>=$endtime) { - //写入文件 - $resulttime=date("Ymd",strtotime($start)); - $fp = fopen($aimDir_site.DIRECTORY_SEPARATOR.$sitename."_".$varname."_".$resulttime.".CSV","w+"); - fwrite($fp,$head.$html); //写入数据 - fclose($fp); //关闭文件句柄 - $start=$endtime; - $endtime=date("Y-m-d",strtotime($start)+24*3600); - $html=$v['time'].",".$v['variablename'].",".$v['value']."\n"; - } - } - //写入文件 - $resulttime=date("Ymd",strtotime($start)); - $fp = fopen($aimDir_site.DIRECTORY_SEPARATOR.$sitename."_".$varname."_".$resulttime.".CSV","w+"); - fwrite($fp,$head.$html); //写入数据 - fclose($fp); //关闭文件句柄 - print $sitename.'--'.$varname."\r\n"; - //给处理的数据做个标记result=0 - $sql_up= "update users_pm_down set result=0 WHERE site=$site and variable=$variable and uid=$uid and result=-1 and type='down'"; - $db->query($sql_up); + $sql.=" and va.fld_variable_id in (select distinct variable from users_pm_down where starttime is not null and result=-1 and type='down' and uid=$uid) "; } - + $sql.=" order by va.fld_name"; + $sth=$db->query($sql); + $rs=$sth->fetchAll(); + $head = "Time"; + $sql2= "select * from crosstab('SELECT v.fld_time as time,va.fld_name as variablename,v.fld_value as value + FROM tbl_value v left join tbl_variable_info va on v.fld_variable_id=va.fld_variable_id + WHERE v.fld_site_id=$site AND v.fld_time>=''$start'' AND v.fld_time<''$end'' "; + if ($variable==1) + { + $sql2.=" and va.fld_variable_id in (select distinct variable from users_pm_down where starttime is not null and result=-1 and type=''down'' and uid=$uid) "; + } + $sql2.=" ORDER BY time', + 'select distinct va.fld_name from tbl_variable_info va left join tbl_value v on v.fld_variable_id=va.fld_variable_id + WHERE v.fld_site_id=$site AND v.fld_time>=''$start'' AND v.fld_time<''$end''"; + if ($variable==1) + { + $sql2.=" and va.fld_variable_id in (select distinct variable from users_pm_down where starttime is not null and result=-1 and type=''down'' and uid=$uid) "; + } + $sql2.=" order by va.fld_name') + as ct(time timestamp without time zone"; + foreach($rs as $k=>$var) + { + $head.=", ".$var['fld_name']; + $sql2.=",v".$k." real"; + } + $head.="\n"; + $sql2.=")"; + + //获取数据 + $sth2 = $db->query($sql2); + //$rows2 = $sth2->fetchAll(); + $endtime=date("Y-m-d",strtotime($start)+24*3600); + $html=""; + while ($v=$sth2->fetch()) + { + //组装数据 + if ($v['time']>=$start && $v['time']<$endtime) + { + $html.=$v['time']; + foreach ($rs as $k=>$var) + $html.=",".$v['v'.$k]; + $html.="\n"; + } elseif ($v['time']>=$endtime) { + //写入文件 + $resulttime=date("Ymd",strtotime($start)); + $fp = fopen($aimDir_site.DIRECTORY_SEPARATOR.$sitename.(($variable==-1)?"_all_":"_selected_").$resulttime.".CSV","w+"); + fwrite($fp,$head.$html); //写入数据 + fclose($fp); //关闭文件句柄 + $start=$endtime; + $endtime=date("Y-m-d",strtotime($start)+24*3600); + $html=$v['time']; + foreach ($rs as $k=>$var) + $html.=",".$v['v'.$k]; + $html.="\n"; + } + } + //写入文件 + $resulttime=date("Ymd",strtotime($start)); + $fp = fopen($aimDir_site.DIRECTORY_SEPARATOR.$sitename.(($variable==-1)?"_all_":"_selected_").$resulttime.".CSV","w+"); + fwrite($fp,$head.$html); //写入数据 + fclose($fp); //关闭文件句柄 + print $sitename."\r\n"; + $sql_up= "update users_pm_down set result=0 WHERE site=$site and uid=$uid and result=-1 and type='down'"; + $db->query($sql_up); } - } - - } + } \ No newline at end of file