init biotest web
This commit is contained in:
279
ss_gw.php
Normal file
279
ss_gw.php
Normal file
@@ -0,0 +1,279 @@
|
||||
<?php
|
||||
if (true) {
|
||||
ini_set('display_errors', 1);
|
||||
ini_set('display_startup_errors', 1);
|
||||
error_reporting(E_ALL);
|
||||
}
|
||||
define('BASEPATH', "/home/smartlab");
|
||||
include_once("./smartlab_biotest/application/config/database.php");
|
||||
|
||||
$dbh = new Database();
|
||||
$dbh->host = $db["default"]["hostname"];
|
||||
$dbh->username = $db["default"]["username"];
|
||||
$dbh->password = $db["default"]["password"];
|
||||
$dbh->database = $db["default"]["database"];
|
||||
$p = $dbh->get_param();
|
||||
|
||||
header("Content-Type: application/json");
|
||||
if ($p["cmd"] == "test") {
|
||||
$resp = $dbh->connect();
|
||||
$dbh->exit_if_error($resp[0], $resp[1]);
|
||||
$sql = "show databases";
|
||||
$resp = $dbh->query($sql);
|
||||
$dbh->exit_if_error($resp[0], $resp[1]);
|
||||
$dbh->jsonReply($resp);
|
||||
|
||||
$sql = "select * from m_branch";
|
||||
$resp = $dbh->query($sql);
|
||||
$dbh->exit_if_error($resp[0], $resp[1]);
|
||||
$dbh->jsonReply($resp);
|
||||
exit;
|
||||
}
|
||||
if ($p["cmd"] == "service_req") {
|
||||
$resp = $dbh->connect();
|
||||
$dbh->exit_if_error($resp[0], $resp[1]);
|
||||
$ids = implode(",", $p["ids"]);
|
||||
if ($ids == "") {
|
||||
$ids = "0";
|
||||
}
|
||||
$sql = "select
|
||||
T_OrderDetailUserID,
|
||||
T_OrderHeaderID,
|
||||
T_OrderDetailID,T_OrderDetailT_TestID,
|
||||
T_OrderDetailT_TestCode,
|
||||
T_OrderDetailT_TestName,
|
||||
T_OrderDetailLastUpdate,
|
||||
T_OrderHeaderDate,
|
||||
concat(T_OrderDetailSamplingDate,' ',T_OrderDetailSamplingTime)
|
||||
T_OrderDetailSamplingDate,
|
||||
M_UserFullName
|
||||
from
|
||||
t_orderdetail
|
||||
join t_orderheader
|
||||
on T_OrderDetailT_OrderHeaderID in ($ids)
|
||||
and T_OrderHeaderID = T_OrderDetailT_OrderHeaderID
|
||||
and T_OrderDetailIsActive='Y'
|
||||
and T_OrderDetailPrice > 0
|
||||
join m_user on T_OrderDetailUserID = M_UserID
|
||||
join t_test on T_OrderDetailT_TestID = T_TestID
|
||||
and T_TestIsActive = 'Y'
|
||||
join t_testgroup on T_TestT_TestGroupID = T_TestGroupID
|
||||
and T_TestGroupT_ServiceID = 1 and T_TestGroupIsActive = 'Y'";
|
||||
$resp = $dbh->query($sql);
|
||||
$dbh->exit_if_error($resp[0], $resp[1]);
|
||||
$dbh->jsonReply($resp, $dbh->info());
|
||||
exit;
|
||||
}
|
||||
if ($p["cmd"] == "encounter") {
|
||||
$resp = $dbh->connect();
|
||||
$dbh->exit_if_error($resp[0], $resp[1]);
|
||||
$sql = "select m_branch.M_BranchID, m_branch.M_BranchCode,
|
||||
t_orderheader.T_OrderHeaderID, t_orderheader.T_OrderHeaderDate,
|
||||
t_orderheader.T_OrderHeaderM_DoctorPJID, t_orderheader.T_OrderHeaderM_PatientID,
|
||||
t_orderheader.T_OrderHeaderLabNumber,
|
||||
m_patient.M_PatientNIK, m_patient.M_PatientName,
|
||||
m_doctor.M_DoctorName
|
||||
from t_orderheader
|
||||
join m_patient on
|
||||
t_orderheader.T_OrderHeaderDate > ? and
|
||||
t_orderheader.T_OrderHeaderDate < ?
|
||||
and t_orderheader.T_OrderHeaderM_PatientID = m_patient.M_PatientID
|
||||
left join m_doctor on T_OrderHeaderM_DoctorPJID = m_doctor.M_DoctorID
|
||||
left join m_branch on m_branch.M_BranchIsDefault = 'Y' and m_branch.M_BranchIsActive = 'Y' ";
|
||||
$date = $p["date"];
|
||||
if ($date == "") {
|
||||
$date = date("Y-m-d");
|
||||
}
|
||||
$sdate = $date . " 00:00:01";
|
||||
$edate = $date . " 23:59:59";
|
||||
$resp = $dbh->query($sql, [$sdate, $edate]);
|
||||
$dbh->exit_if_error($resp[0], $resp[1]);
|
||||
$dbh->jsonReply($resp);
|
||||
exit;
|
||||
}
|
||||
echo json_encode(["status" => "ERR", "message" => "[sys] cmd not found"]);
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
//helper
|
||||
class Database
|
||||
{
|
||||
public $host;
|
||||
public $username;
|
||||
public $password;
|
||||
public $database;
|
||||
private $conn;
|
||||
|
||||
public function __construct()
|
||||
{
|
||||
$this->host = "localhost";
|
||||
$this->username = "root";
|
||||
$this->password = "smb1122";
|
||||
$this->database = "one_health";
|
||||
}
|
||||
public function jsonReply($resp, $sql = "")
|
||||
{
|
||||
$resp = ["status" => "OK", "data" => $resp[1]];
|
||||
if ($sql != "") {
|
||||
$resp["sql"] = $sql;
|
||||
}
|
||||
echo json_encode($resp);
|
||||
}
|
||||
function get_param()
|
||||
{
|
||||
$body = file_get_contents("php://input");
|
||||
$res = json_decode($body, true);
|
||||
if ($res == null) {
|
||||
return ["cmd" => "xx-unknown"];
|
||||
}
|
||||
return $res;
|
||||
}
|
||||
|
||||
function connect()
|
||||
{
|
||||
$this->conn = new mysqli($this->host, $this->username, $this->password, $this->database);
|
||||
if ($this->conn->connect_error) {
|
||||
return [false, "Connection failed: " . $this->conn->connect_error];
|
||||
}
|
||||
return [true, "Connection OK "];
|
||||
}
|
||||
|
||||
public function query($sql, $params = array())
|
||||
{
|
||||
if (!$this->conn) {
|
||||
$this->connect();
|
||||
}
|
||||
|
||||
$statement = $this->conn->prepare($sql);
|
||||
if (!$statement) {
|
||||
return [false, "Error preparing SQL statement: " . $this->conn->error];
|
||||
}
|
||||
if (!empty($params)) {
|
||||
$types = '';
|
||||
foreach ($params as $param) {
|
||||
if (is_int($param)) {
|
||||
$types .= 'i'; // integer
|
||||
} elseif (is_float($param)) {
|
||||
$types .= 'd'; // double
|
||||
} elseif (is_string($param)) {
|
||||
$types .= 's'; // string
|
||||
} else {
|
||||
$types .= 'b'; // blob
|
||||
}
|
||||
}
|
||||
$xparams = [$types];
|
||||
foreach ($params as $param) {
|
||||
$xparams[] = $param;
|
||||
}
|
||||
$tmp = array();
|
||||
foreach ($xparams as $key => $value) $tmp[$key] = &$xparams[$key];
|
||||
call_user_func_array(array($statement, 'bind_param'), $tmp);
|
||||
}
|
||||
|
||||
$result = $statement->execute();
|
||||
if (!$result) {
|
||||
return [false, "Error executing SQL statement: " . $statement->error];
|
||||
}
|
||||
|
||||
// $result = $statement->get_result();
|
||||
// if (!$result) {
|
||||
// return [false, "Error getting result set: " . $statement->error];
|
||||
// }
|
||||
|
||||
$rows = array();
|
||||
$result = $this->get_result($statement);
|
||||
while ($row = array_shift($result)) {
|
||||
$rows[] = $row;
|
||||
}
|
||||
|
||||
$statement->close();
|
||||
return [true, $rows];
|
||||
}
|
||||
function info()
|
||||
{
|
||||
$param = mysqli_info($this->conn);
|
||||
return $param;
|
||||
}
|
||||
|
||||
public function exec($sql, $params = array())
|
||||
{
|
||||
if (!$this->conn) {
|
||||
$this->connect();
|
||||
}
|
||||
|
||||
$statement = $this->conn->prepare($sql);
|
||||
if (!$statement) {
|
||||
return [false, "Error preparing SQL statement: " . $this->conn->error];
|
||||
}
|
||||
|
||||
if (!empty($params)) {
|
||||
$types = '';
|
||||
foreach ($params as $param) {
|
||||
if (is_int($param)) {
|
||||
$types .= 'i'; // integer
|
||||
} elseif (is_float($param)) {
|
||||
$types .= 'd'; // double
|
||||
} elseif (is_string($param)) {
|
||||
$types .= 's'; // string
|
||||
} else {
|
||||
$types .= 'b'; // blob
|
||||
}
|
||||
}
|
||||
|
||||
$xparams = [$types];
|
||||
foreach ($params as $param) {
|
||||
$xparams[] = $param;
|
||||
}
|
||||
$tmp = array();
|
||||
foreach ($xparams as $key => $value) $tmp[$key] = &$xparams[$key];
|
||||
call_user_func_array(array($statement, 'bind_param'), $tmp);
|
||||
}
|
||||
|
||||
$result = $statement->execute();
|
||||
if (!$result) {
|
||||
return [false, "Error executing SQL statement: " . $statement->error];
|
||||
}
|
||||
|
||||
$statement->close();
|
||||
return [true, "Query executed successfully."];
|
||||
}
|
||||
|
||||
public function close()
|
||||
{
|
||||
if ($this->conn) {
|
||||
$this->conn->close();
|
||||
}
|
||||
}
|
||||
public function exit_if_error($status, $data)
|
||||
{
|
||||
if ($status === false) {
|
||||
echo json_encode(["status" => "ERR", "message" => $data]);
|
||||
exit;
|
||||
}
|
||||
}
|
||||
function get_result($statement)
|
||||
{
|
||||
$result = array();
|
||||
$statement->store_result();
|
||||
for ($i = 0; $i < $statement->num_rows; $i++) {
|
||||
$metadata = $statement->result_metadata();
|
||||
$params = array();
|
||||
while ($field = $metadata->fetch_field()) {
|
||||
$params[] = &$result[$i][$field->name];
|
||||
}
|
||||
call_user_func_array(array($statement, 'bind_result'), $params);
|
||||
$statement->fetch();
|
||||
}
|
||||
return $result;
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user