Batch 6a: application controllers base
This commit is contained in:
198
application/controllers/hs/Test_sample.php
Normal file
198
application/controllers/hs/Test_sample.php
Normal file
@@ -0,0 +1,198 @@
|
||||
<?php
|
||||
|
||||
class Test_sample extends MY_Controller
|
||||
{
|
||||
function __construct()
|
||||
{
|
||||
parent::__construct();
|
||||
}
|
||||
function now()
|
||||
{
|
||||
return Date("Y-m-d H:i:s");
|
||||
}
|
||||
function log($msg)
|
||||
{
|
||||
echo "{$this->now()} $msg\n";
|
||||
}
|
||||
/*
|
||||
create table test_sample (
|
||||
test_SampleID int not null auto_increment primary key,
|
||||
test_SampleT_TestID int,
|
||||
test_SampleT_SampleTypeID int,
|
||||
test_SampleIsActive varchar(1) default 'Y',
|
||||
key (test_SampleT_TestID),
|
||||
key (test_SampleT_SampleTypeID)
|
||||
)
|
||||
*/
|
||||
function generate_master()
|
||||
{
|
||||
$sql = "select count(*) from one_hs.test_sample";
|
||||
$qry = $this->db->query($sql);
|
||||
if (!$qry) {
|
||||
if ($this->db->error()["code"] == "1146") {
|
||||
$sql = "
|
||||
create table one_hs.test_sample (
|
||||
test_SampleID int not null auto_increment primary key,
|
||||
test_SampleT_TestID int,
|
||||
test_SampleT_SampleTypeID int,
|
||||
test_SampleIsActive varchar(1) default 'Y',
|
||||
key (test_SampleT_TestID),
|
||||
key (test_SampleT_SampleTypeID)
|
||||
)";
|
||||
$qry = $this->db->query($sql);
|
||||
if (!$qry) {
|
||||
$this->log(
|
||||
"Error Create table one_hs.test_sample : " .
|
||||
$this->db->error()["message"]
|
||||
);
|
||||
exit();
|
||||
}
|
||||
} else {
|
||||
$this->log("Error : " . $this->db->error()["message"]);
|
||||
exit();
|
||||
}
|
||||
}
|
||||
// mark isActive as 'N' if not here
|
||||
$sql = "update one_hs.test_sample
|
||||
set test_SampleIsActive = 'N'
|
||||
where test_SampleT_TestID not in (
|
||||
select T_TestID
|
||||
from t_test
|
||||
where T_TestIsActive = 'Y' and T_TestIsPrice = 'Y'
|
||||
)";
|
||||
$qry = $this->db->query($sql);
|
||||
if (!$qry) {
|
||||
$this->log(
|
||||
"Error Set NonActive one_hs.test_sample : " .
|
||||
$this->db->error()["message"]
|
||||
);
|
||||
exit();
|
||||
}
|
||||
|
||||
$sql = "select distinct T_TestSasCode,
|
||||
if (T_SpecimenLocalIsActive is null or T_SpecimenLocalIsActive = 'N', T_TestT_SampleTypeID, T_SpecimenLocalT_SampleTypeID)
|
||||
newT_SampleTypeID
|
||||
from t_test
|
||||
join t_sampletype on T_TestT_SampleTypeID = T_SampleTypeID
|
||||
and T_TestIsResult = 'Y'
|
||||
and T_TestIsActive = 'Y' and T_SampleTypeIsActive = 'Y'
|
||||
left join t_specimenlocal on T_TestNat_TestID = T_SpecimenLocalNat_TestID";
|
||||
|
||||
$qry = $this->db->query($sql);
|
||||
if (!$qry) {
|
||||
$this->log(
|
||||
"Error Set NonActive one_hs.test_sample : " .
|
||||
$this->db->error()["message"]
|
||||
);
|
||||
exit();
|
||||
}
|
||||
$arr_sample = $qry->result_array();
|
||||
$sql =
|
||||
"select T_TestID,T_TestSasCode from t_test where T_TestIsActive='Y' and T_TestIsPrice='Y'";
|
||||
|
||||
$qry = $this->db->query($sql);
|
||||
if (!$qry) {
|
||||
$this->log(
|
||||
"Error Set NonActive one_hs.test_sample : " .
|
||||
$this->db->error()["message"]
|
||||
);
|
||||
exit();
|
||||
}
|
||||
$rows = $qry->result_array();
|
||||
$result = [];
|
||||
foreach ($rows as $r) {
|
||||
$arr_target = array_filter($arr_sample, function ($s) use ($r) {
|
||||
$parentCode = $r["T_TestSasCode"];
|
||||
$childCode = $s["T_TestSasCode"];
|
||||
return $parentCode == substr($childCode, 0, strlen($childCode));
|
||||
});
|
||||
foreach ($arr_target as $t) {
|
||||
$arr = [
|
||||
"test_SampleT_TestID" => $r["T_TestID"],
|
||||
"test_SampleT_SampleTypeID" => $t["newT_SampleTypeID"],
|
||||
];
|
||||
$resp = $this->insert_or_update("one_hs.test_sample", $arr, [
|
||||
"test_SampleT_TestID",
|
||||
"test_SampleT_SampleTypeID",
|
||||
]);
|
||||
if ($resp["status"] == "ERR") {
|
||||
$this->log("ERR : " . $resp["message"]);
|
||||
exit();
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
function insert_or_update($table, $dt, $keys)
|
||||
{
|
||||
$s_where = "";
|
||||
$param = [];
|
||||
foreach ($keys as $k) {
|
||||
if ($s_where != "") {
|
||||
$s_where .= " and ";
|
||||
}
|
||||
$s_where .= " $k = ?";
|
||||
$param[] = $dt[$k];
|
||||
}
|
||||
$sql = "select count(*) as total
|
||||
from $table
|
||||
where $s_where ";
|
||||
$qry = $this->db->query($sql, $param);
|
||||
if (!$qry) {
|
||||
return [
|
||||
"status" => "ERR",
|
||||
"message" =>
|
||||
$this->db->error()["message"] .
|
||||
"|" .
|
||||
$this->db->last_query(),
|
||||
];
|
||||
}
|
||||
$rows = $qry->result_array();
|
||||
$status = "Insert";
|
||||
if (count($rows) > 0) {
|
||||
if ($rows[0]["total"] > 0) {
|
||||
foreach ($keys as $k) {
|
||||
$this->db->where($k, $dt[$k]);
|
||||
}
|
||||
$qry = $this->db->update($table, $dt);
|
||||
if (!$qry) {
|
||||
return [
|
||||
"status" => "ERR",
|
||||
"message" =>
|
||||
"ERR Update : " .
|
||||
$this->db->error()["message"] .
|
||||
"|" .
|
||||
$this->db->last_query(),
|
||||
];
|
||||
}
|
||||
$status = "Update";
|
||||
} else {
|
||||
//insert
|
||||
$qry = $this->db->insert($table, $dt);
|
||||
if (!$qry) {
|
||||
return [
|
||||
"status" => "ERR",
|
||||
"message" =>
|
||||
"ERR Insert : " .
|
||||
$this->db->error()["message"] .
|
||||
"|" .
|
||||
$this->db->last_query(),
|
||||
];
|
||||
}
|
||||
}
|
||||
} else {
|
||||
//insert
|
||||
$qry = $this->db->insert($table, $dt);
|
||||
if (!$qry) {
|
||||
return [
|
||||
"status" => "ERR",
|
||||
"message" =>
|
||||
"ERR Insert : " .
|
||||
$this->db->error()["message"] .
|
||||
"|" .
|
||||
$this->db->last_query(),
|
||||
];
|
||||
}
|
||||
}
|
||||
return ["status" => "OK", "message" => $status];
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user