westdc-zf1/application/module/Westdc/Visual/Record.php

155 lines
4.0 KiB
PHP
Raw Normal View History

2014-12-22 06:35:15 +00:00
<?php
namespace Westdc\Visual;
class Record extends Database
{
public $subdataset,$valueFilter;
public $timeFiled = "utctime";
private $sql;
function __construct($uuid = NULL,$identifier = "")
{
parent::__construct();
if(!empty($identifier))
{
$this->subdataset = $identifier;
}
if(!empty($uuid) && !empty($identifier))
{
$this->initVisual($uuid);
}
}
public function __invoke()
{
return $this->getData();
}
public function initVisual($uuid)
{
$visual = new Visual;
$var_data = $visual->getVisualVars($uuid);
if(!empty($var_data['visual_valuefilter']))
$this->valueFilter = (float)$var_data['visual_valuefilter'];
if($var_data['visual_timefield_model'] == 'single') {
$this->timeFiled = '"' . $var_data['visual_timefield'] . '"';
}//single
elseif($var_data['visual_timefield_model'] == 'multiple'){
$timefield_temp = "(";
if(!empty($var_data['visual_timefield_year']))
{
$timefield_temp .= $var_data['visual_timefield_year'] . "::character varying||";
}
$timefield_temp .= "'-'";
if(!empty($var_data['visual_timefield_month']))
{
$timefield_temp .= "||".$var_data['visual_timefield_month']."::character varying||";
}else{
$timefield_temp .= '1';
}
$timefield_temp .= "'-";
if(!empty($var_data['visual_timefield_day']))
{
$timefield_temp .= "'||".$var_data['visual_timefield_day'] . '::character varying';
}else{
$timefield_temp .= "1'";
}
$timefield_temp .= ")";
$timefield_temp .= "::timestamp without time zone";
$this->timeFiled = $timefield_temp;
}//multiple
elseif($var_data['visual_timefield_model'] == 'custom'){
$this->timeFiled = $var_data['visual_timefield_model'];
}//custom
//时间字段为空的bug
if(empty($var_data['visual_timefield_model'])){
$this->timeFiled = "utctime";
}else{
if($var_data['visual_timefield_model'] == 'single' && empty($var_data['visual_timefield'])){
$this->timeFiled = "utctime";
}elseif($var_data['visual_timefield_model'] == 'multiple' && empty($var_data['visual_timefield_year'])){
$this->timeFiled = "utctime";
}elseif($var_data['visual_timefield_model'] == 'custom' && empty($var_data['visual_timefield_custom'])) {
$this->timeFiled = "utctime";
}
}
if(isset($var_data['data']) && !empty($var_data['data']))
$this->sql = $this->makeSql($var_data['data'],$this->subdataset);
else
$this->sql = $this->makeSql($var_data['visual_datatable'],$this->subdataset);
//echo $this->sql;exit();
}
public function getSql()
{
return $this->sql;
}
public function getData()
{
$rs = $this->db->query($this->sql);
$data = [];
while($row = $rs->fetch(\PDO::FETCH_ASSOC))
{
$row['utctime'] = $this->utcMsTime(strtotime($row['utctime']));
//var_dump($row);
$data[] = array(
$row['utctime'],
$row['value']
);
}
return $data;
}
public function makeSql($table,$fieldValue)
{
$wheresql = "";
if(!empty($this->valueFilter))
{
$wheresql = " WHERE \"$fieldValue\"<>".$this->valueFilter .' ';
}
$sql = "SELECT
{$this->timeFiled} as utctime , \"$fieldValue\" as value
FROM $table as tbl
$wheresql
ORDER BY
extract(YEAR from {$this->timeFiled}) ASC,
extract(MONTH from {$this->timeFiled}) ASC,
extract(DAY from {$this->timeFiled}) ASC,
extract(HOUR from {$this->timeFiled}) ASC,
extract(MINUTE from {$this->timeFiled}) ASC,
extract(SECOND from {$this->timeFiled}) ASC
";
return $sql;
}
public function utcMsTime($time=''){
if(empty($time))
return (time()+8*3600)*1000;
else
return ($time)*1000;
}
2014-05-12 06:53:21 +00:00
}