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

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; namespace Westdc\Visual\Handle;
use \Helpers\View as view; use \Helpers\View as view;
use \Helpers\dbh;
use \Helpers\Table; use \Helpers\Table;
//事件中存在的操作 //事件中存在的操作
class VisualHandler class VisualHandle extends \Westdc\Visual\VariableEncoder
{ {
private $db; //传入PDO对象误 private $db; //传入PDO对象误
private $config; //全局配置 private $config; //全局配置
public $table;
public $tbl_maillog = ""; //邮件日志表
function __construct() function __construct()
{ {
$this->db = \Zend_Registry::get('db'); $this->db = \Zend_Registry::get('db');
$this->config = \Zend_Registry::get('config'); $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'); $id = $e->getParam('id');
$ref = new Reference();
return $ref->deleteReferenceAuthor($id);
} }
public function recordChanged(\Zend_EventManager_Event $e)
public function deleteTag(\Zend_EventManager_Event $e)
{ {
$id = $e->getParam('id'); $id = $e->getParam('id');
$ref = new Reference();
return $ref->deleteReferenceTag($id);
} }
} }

View File

@ -1,6 +1,8 @@
<?php <?php
namespace Westdc\Visual\Listener; namespace Westdc\Visual\Listener;
use Westdc\Visual\Handle\VisualHandle;
class VisualListener implements \Zend_EventManager_ListenerAggregate class VisualListener implements \Zend_EventManager_ListenerAggregate
{ {
private $event; private $event;
@ -12,9 +14,9 @@ class VisualListener implements \Zend_EventManager_ListenerAggregate
public function attach(\Zend_EventManager_EventCollection $events) public function attach(\Zend_EventManager_EventCollection $events)
{ {
$Handler = new ReferenceHandler(); $Handler = new VisualHandle();
$events->attach('submit.checkParam', array($Handler, 'checkReferenceParam'), 100); $events->attach('submit.checkParam', array($Handler, 'checkParam'), 100);
$events->attach('submit.processData', array($Handler, 'processReferenceData'), 100); $events->attach('submit.processData', array($Handler, 'processData'), 100);
$events->attach('submit.recordPosted', array($Handler, 'recordPosted'), 100); $events->attach('submit.recordPosted', array($Handler, 'recordPosted'), 100);
$events->attach('submit.recordChanged', array($Handler, 'recordChanged'), 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\Record;
use Westdc\Visual\RecordInterface; use Westdc\Visual\RecordInterface;
class SoilMoisture extends Record implements RecordInterface abstract class SoilMoisture extends Record implements RecordInterface
{ {
function __construct() function __construct()
{ {
parent::__construct(__CLASS__); parent::__construct(__CLASS__);
} }
public $tablename;
public $subset; public $subset;
public function switchDataset() public function switchDataset()
@ -47,7 +49,7 @@ class SoilMoisture extends Record implements RecordInterface
{ {
$this->switchDataset(); $this->switchDataset();
$sql = "SELECT * FROM data_watercontent $sql = "SELECT * FROM {$this->tablename}
WHERE WHERE
$this->subset > 0 $this->subset > 0
ORDER BY ORDER BY

View File

@ -6,9 +6,30 @@ class Record
public $db; public $db;
function __construct($recordType) public $subdataset;
public $timeFiled = "utctime";
private $sql;
function __construct($uuid = NULL,$identifier = "")
{ {
$this->initDatabase(); $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() public function initDatabase()
@ -24,19 +45,56 @@ class Record
$this->db = new \PDO($dsn); $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 $sql = "SELECT
\"$index\" as \"index\", \"$fieldValue\" as \"value\", \"$fieldTime\" as \"time\" {$this->timeFiled} as utctime , \"$fieldValue\" as value
FROM \"$table\" FROM $table
ORDER BY ORDER BY
extract(year from \"$fieldTime\") ASC, extract(YEAR from \"{$this->timeFiled}\") ASC,
extract(month from \"$fieldTime\") ASC, extract(MONTH from \"{$this->timeFiled}\") ASC,
extract(day from \"$fieldTime\") 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 $sql;
return $rs;
} }
public function utcMsTime($time=''){ 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 <?php
namespace Westdc\Visual; namespace Westdc\Visual;
use Westdc\Visual\VisualListener; use Westdc\Visual\Listener\VisualListener as Listener;
use Helpers\dbh;
class Visual class Visual
{ {
public $db; public $db;
protected $events;
function __construct($recordType) function __construct()
{ {
$this->db = \Zend_Registry::get('db'); $this->db = \Zend_Registry::get('db');
$this->mainTable = "datavisual";
$Listener = new Listener(); $Listener = new Listener();
@$this->getEventManager()->attachAggregate($Listener); @$this->getEventManager()->attachAggregate($Listener);
} }
public function getEventManager(\Zend_EventManager_EventCollection $events = NULL) 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'); $params = compact('data');
$results = $this->getEventManager()->trigger('submit.checkParam', $this, $params); $results = $this->getEventManager()->trigger('submit.checkParam', $this, $params);
@ -42,11 +46,13 @@ class Visual
$dbh = new dbh(); $dbh = new dbh();
if(empty($id)) $record = $this->getVisualVars($data['uuid']);
{
$id = $dbh->insert($this->mainTable,$data,true);
if(!empty($id) && is_numeric($id)) if(empty($record))
{
$id = $dbh->insert($this->mainTable,$data);
if($id)
{ {
$this->getEventManager()->trigger('submit.recordPosted', $this, $params); $this->getEventManager()->trigger('submit.recordPosted', $this, $params);
return true; return true;
@ -61,7 +67,7 @@ class Visual
}//add }//add
else{ 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); $this->getEventManager()->trigger('submit.recordChanged', $this, $params);
return "修改失败!请重试"; return "修改失败!请重试";
@ -73,14 +79,33 @@ class Visual
}// add() }// add()
//删除 //删除
public function del($id) public function del($uuid)
{ {
if(!is_numeric($id) || empty($id) || $id< 0) if(!is_numeric($id) || empty($id) || $id< 0)
{ {
return false; return false;
} }
$sql = "DELETE FROM {$this->mainTable} WHERE id=$id"; $sql = "DELETE FROM {$this->mainTable} WHERE uuid='$uuid'";
return $this->db->exec($sql); 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);
}
} }