更新三江源子平台可视化模块文件

This commit is contained in:
Li Jianxuan 2014-06-11 08:12:05 +00:00
parent a7b9e63e70
commit f0f0e639b5
7 changed files with 388 additions and 236 deletions

View File

@ -2,42 +2,63 @@
namespace Westdc\Visual\Handle;
use \Helpers\View as view;
use \Helpers\dbh;
use \Helpers\Table;
//事件中存在的操作
class VisualHandler
class VisualHandle extends \Westdc\Visual\VariableEncoder
{
private $db; //传入PDO对象误
private $config; //全局配置
public $table;
public $tbl_maillog = ""; //邮件日志表
function __construct()
{
$this->db = \Zend_Registry::get('db');
$this->config = \Zend_Registry::get('config');
$this->table = new Table();
}
public function deleteAuthor(\Zend_EventManager_Event $e)
public function checkParam(\Zend_EventManager_Event $e)
{
$data = $e->getParam('data');
if(empty($data['uuid']))
{
return "请填写数据UUID";
}
if(!is_array($data['vars']) || count($data['vars']) < 1)
{
return "参数错误";
}
return true;
}
public function processData(\Zend_EventManager_Event $e)
{
$data = $e->getParam('data');
$data['vars'] = $this->encode($data['vars']);
if(!isset($data['status']))
{
$data['status'] = 1;
}
return $data;
}
public function recordPosted(\Zend_EventManager_Event $e)
{
$id = $e->getParam('id');
$ref = new Reference();
return $ref->deleteReferenceAuthor($id);
}
public function deleteTag(\Zend_EventManager_Event $e)
public function recordChanged(\Zend_EventManager_Event $e)
{
$id = $e->getParam('id');
$ref = new Reference();
return $ref->deleteReferenceTag($id);
}
}

View File

@ -1,6 +1,8 @@
<?php
namespace Westdc\Visual\Listener;
use Westdc\Visual\Handle\VisualHandle;
class VisualListener implements \Zend_EventManager_ListenerAggregate
{
private $event;
@ -12,9 +14,9 @@ class VisualListener implements \Zend_EventManager_ListenerAggregate
public function attach(\Zend_EventManager_EventCollection $events)
{
$Handler = new ReferenceHandler();
$events->attach('submit.checkParam', array($Handler, 'checkReferenceParam'), 100);
$events->attach('submit.processData', array($Handler, 'processReferenceData'), 100);
$Handler = new VisualHandle();
$events->attach('submit.checkParam', array($Handler, 'checkParam'), 100);
$events->attach('submit.processData', array($Handler, 'processData'), 100);
$events->attach('submit.recordPosted', array($Handler, 'recordPosted'), 100);
$events->attach('submit.recordChanged', array($Handler, 'recordChanged'), 100);
}

View File

@ -5,13 +5,15 @@ namespace Westdc\Visual\Reader;
use Westdc\Visual\Record;
use Westdc\Visual\RecordInterface;
class SoilMoisture extends Record implements RecordInterface
abstract class SoilMoisture extends Record implements RecordInterface
{
function __construct()
{
parent::__construct(__CLASS__);
}
public $tablename;
public $subset;
public function switchDataset()
@ -47,7 +49,7 @@ class SoilMoisture extends Record implements RecordInterface
{
$this->switchDataset();
$sql = "SELECT * FROM data_watercontent
$sql = "SELECT * FROM {$this->tablename}
WHERE
$this->subset > 0
ORDER BY

View File

@ -6,9 +6,30 @@ class Record
public $db;
function __construct($recordType)
public $subdataset;
public $timeFiled = "utctime";
private $sql;
function __construct($uuid = NULL,$identifier = "")
{
$this->initDatabase();
if(!empty($identifier))
{
$this->subdataset = $identifier;
}
if(!empty($uuid) && !empty($identifier))
{
$this->initVisual($uuid);
}
}
public function __invoke()
{
return $this->getData();
}
public function initDatabase()
@ -24,19 +45,56 @@ class Record
$this->db = new \PDO($dsn);
}
public function makeSql($table,$index,$fieldValue,$fieldTime)
public function initVisual($uuid)
{
$visual = new Visual;
$var_data = $visual->getVisualVars($uuid);
$this->sql = $this->makeSql($var_data['data'],$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)
{
$sql = "SELECT
\"$index\" as \"index\", \"$fieldValue\" as \"value\", \"$fieldTime\" as \"time\"
FROM \"$table\"
{$this->timeFiled} as utctime , \"$fieldValue\" as value
FROM $table
ORDER BY
extract(year from \"$fieldTime\") ASC,
extract(month from \"$fieldTime\") ASC,
extract(day from \"$fieldTime\") ASC,
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
";
$rs = $this->db->query($sql);
return $rs;
return $sql;
}
public function utcMsTime($time=''){

View File

@ -0,0 +1,44 @@
<?php
namespace Westdc\Visual;
class VariableEncoder
{
function __construct()
{
}
public function __invoke($json)
{
$this->decode($json);
}
public function encode($vars)
{
$arr = [];
foreach($vars as $k=>$v)
{
$names = explode("|",$v);
$arr[] = [ 'dataset' => $names[0], 'subdataset' => $names[1],'seriename' => $names[2]];
}
return json_encode($arr);
}
public function decode($json)
{
$arr = json_decode($json,true);
//var_dump($arr);
$data = [];
foreach($arr as $k=>$v)
{
$data[$k] = join("|",$v);
}
return $data;
}
public function normaldecode($json)
{
return json_decode($json,true);
}
}

View File

@ -1,18 +1,22 @@
<?php
namespace Westdc\Visual;
use Westdc\Visual\VisualListener;
use Westdc\Visual\Listener\VisualListener as Listener;
use Helpers\dbh;
class Visual
{
public $db;
protected $events;
function __construct($recordType)
function __construct()
{
$this->db = \Zend_Registry::get('db');
$this->mainTable = "datavisual";
$Listener = new Listener();
@$this->getEventManager()->attachAggregate($Listener);
}
public function getEventManager(\Zend_EventManager_EventCollection $events = NULL)
@ -26,7 +30,7 @@ class Visual
}
//添加
public function add($data,$id = 0)
public function add($data)
{
$params = compact('data');
$results = $this->getEventManager()->trigger('submit.checkParam', $this, $params);
@ -42,11 +46,13 @@ class Visual
$dbh = new dbh();
if(empty($id))
{
$id = $dbh->insert($this->mainTable,$data,true);
$record = $this->getVisualVars($data['uuid']);
if(!empty($id) && is_numeric($id))
if(empty($record))
{
$id = $dbh->insert($this->mainTable,$data);
if($id)
{
$this->getEventManager()->trigger('submit.recordPosted', $this, $params);
return true;
@ -61,7 +67,7 @@ class Visual
}//add
else{
if(!$dbh->update($this->mainTable,$data," id=$id ",true))
if(!$dbh->update($this->mainTable,$data," uuid='{$data['uuid']}' "))
{
$this->getEventManager()->trigger('submit.recordChanged', $this, $params);
return "修改失败!请重试";
@ -73,14 +79,33 @@ class Visual
}// add()
//删除
public function del($id)
public function del($uuid)
{
if(!is_numeric($id) || empty($id) || $id< 0)
{
return false;
}
$sql = "DELETE FROM {$this->mainTable} WHERE id=$id";
$sql = "DELETE FROM {$this->mainTable} WHERE uuid='$uuid'";
return $this->db->exec($sql);
}
//获得可视化变量
public function getVisualVars($uuid)
{
$sql = "SELECT * FROM {$this->mainTable} WHERE uuid='$uuid' LIMIT 1";
$rs = $this->db->query($sql);
return $rs->fetch(\PDO::FETCH_ASSOC);
}
//获得可以可视化的元数据
public function getVisualMetadata()
{
$sql = "SELECT * FROM metadata md
LEFT JOIN {$this->mainTable} v ON v.uuid = md.uuid
WHERE v.uuid IS NOT NULL";
$rs = $this->db->query($sql);
return $rs->fetchAll(\PDO::FETCH_ASSOC);
}
}