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

101 lines
2.1 KiB
PHP

<?php
namespace Westdc\Visual;
class Record extends Database
{
public $subdataset,$valueFilter,$vars,$uuid,$xaxis,$field;
private $sql;
function __construct($uuid,$identifier )
{
parent::__construct();
$this->uuid=$uuid;
$this->subdataset = $identifier;
$this->init();
}
public function __invoke()
{
return $this->getData();
}
public function init()
{
$visual = new Visual;
$var_data = $visual->getVisualVars($this->uuid);
$this->vars = $var_data['variable'];
$this->xaxis=$var_data['xaxis'];
$json=json_decode($this->vars,true);
//$subvar=array_filter($json,function($sub) { return $sub['name']==$this->subdataset; });
$subvar=$json[$this->subdataset];
$this->field=$subvar['name'];
if (isset($subvar['sql']) && !empty($subvar['sql']))
$this->sql=$this->makeSql($subvar['sql']);
elseif(isset($var_data['data']) && !empty($var_data['data']))
{
$condition=null;
if (isset($subvar['condition']) && !empty($subvar['condition']))
$condition=$subvar['condition'];
$this->sql = $this->makeSql($var_data['data'],$condition);
}
//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))
{
if ($this->xaxis!='category')
$row[$this->xaxis] = $this->utcMsTime(strtotime($row[$this->xaxis]));
//var_dump($row);
$t=[];
foreach($row as $k=>$v)
$t[]=$v;
$data[] = $t;
}
return $data;
}
public function makeSql($table,$condition=NULL)
{
$wheresql = "";
if ($condition)
{
$filter=explode(';',$condition);
if(!empty($filter))
{
if(is_string($filter))
$wheresql = " WHERE $filter ";
elseif(is_array($filter))
{
$wheresql = ' WHERE '.join(" AND ",$filter);
}
}
}
$sql = "SELECT {$this->xaxis},{$this->field}
FROM ($table) as tbl
$wheresql
ORDER BY {$this->xaxis}";
return $sql;
}
public function utcMsTime($time=''){
if(empty($time))
return (time()+8*3600)*1000;
else
return ($time)*1000;
}
}