完成自动类型检查和查询功能

This commit is contained in:
Yu Sun
2022-07-12 22:36:14 +08:00
parent 5fdfc04349
commit 68f86c94b7
4 changed files with 113 additions and 31 deletions

View File

@@ -32,7 +32,9 @@ class EDataCapture {
$code_type[$v['code']] = $v['type'];
});
foreach ($this -> post -> param -> data as $row) {
foreach ($this -> post -> param -> data as &$row) {
if (($code_type[$row -> code]) === 'float' and is_int($row -> value))
$row -> value = (float) number_format((float) $row -> value, 1, '.', '');
$check_func = 'is_' . $code_type[$row -> code];
if (!$check_func($row -> value)) {
$this -> check_res = 'MISMATCH_TYPE';
@@ -61,11 +63,11 @@ class EDataCapture {
);
foreach ($chunk as $row) {
$sql_values[] = sprintf(
"('%s', '%s', %s, %s)",
"('%s', '%s', '%s', '%s')",
$code,
$row['value'],
$row['device_code'] ?? 'DEFAULT',
$row['batch'] ?? 'DEFAULT'
$row['value'] === false ? 'f' : $row['value'],
$row['device_code'] ?? NULL,
$row['batch'] ?? NULL
);
}
$res[] = pg_query($this -> dbconn, $sql_head . implode(',', $sql_values));
@@ -85,28 +87,40 @@ class EDataCapture {
}
function get_node_data() {
$name_type = pg_fetch_assoc(pg_query($this -> dbconn, sprintf(
"SELECT name, type
FROM hf_mes_scada_data_capture_node_configure
WHERE working_subclass = '%s'
AND code = '%s'",
$this -> get['working_subclass'],
$this -> get['code']
)));
// 先把code和name的对应关系拿到快于OUTER JOIN
$res = pg_fetch_all(pg_query($this -> dbconn, sprintf(
"SELECT id, v_%s AS value, device_code, batch, capture_time
FROM hf_mes_scada_data_capture_node_data_%s
WHERE code = '%s'",
$name_type['type'],
$this -> get['working_subclass'],
$this -> get['code']
"SELECT code, name
FROM hf_mes_scada_data_capture_node_configure
WHERE working_subclass = '%s'",
$this -> get['working_subclass']
)));
array_walk($res, function(&$v, $k) use ($name_type) {
$v['name'] = $name_type['name'];
$code_name = [];
array_walk($res, function(&$v, $k) use (&$code_name) {
$code_name[$v['code']] = $v['name'];
});
$sql_cmd = sprintf(
"SELECT id, code, COALESCE(v_string, v_int::text, v_float::text, v_bool::text) AS value, device_code, batch, capture_time
FROM hf_mes_scada_data_capture_node_data_%s
WHERE capture_time >= '%s' AND capture_time <= '%s'",
$this -> get['working_subclass'],
date("Y-m-d H:i:s", $this -> get['start_time'] / 1000),
date("Y-m-d H:i:s", $this -> get['end_time'] / 1000)
);
if (isset($this -> get['code']))
$sql_cmd .= sprintf(" AND code = '%s'", $this -> get['code']);
$res = pg_fetch_all(pg_query($this -> dbconn, $sql_cmd));
if (count($res) > 2e6)
return "TOO_MANY";
array_walk($res, function(&$v, $k) use ($code_name) {
$v['name'] = $code_name[$v['code']];
});
return $res;
}
}