db->query($sql, $param); } else { $qry = $this->db->query($sql); } if (!$qry) { return array("status" => false, "message" => $this->db->error()["message"] . "|" . $this->db->last_query()); } return array("status" => true, "rows" => $qry->result_array()); } function get_one_row($sql, $param = false) { $rs = $this->get_rows($sql, $param); if ($rs["status"] == false) { return array("status" => false, "notfound" => false, "message" => $rs["message"]); } if (count($rs["rows"]) == 0) { return array("status" => false, "notfound" => true, "row" => array()); } return array("status" => true, "row" => $rs["rows"][0]); } function log($msg) { echo date("Y-m-d H:i:s") . " " . $msg . "\n"; } function get_last_rawID($instrumentID, $date) { $sql = "select max(itfQcLastItfRawID) maxRawID from itf_qc where itfQcNat_InstrumentID = ? and itfQcDate = ?"; $rs = $this->get_one_row($sql, array($instrumentID, $date)); if ($rs["status"] === false && $rs["notfound"] === false) { $this->log("Error Get Max RawID : [{$instrumentID}] [{$date}]" . $rs["message"]); exit; } if ($rs["status"] == false) return 0; if ($rs["row"]["maxRawID"] == "") return 0; return $rs["row"]["maxRawID"]; } function parse_qc_alinity($result) { if (strpos($result, "\n") > 0 && strpos($result, "\r") > 0) { $result = str_replace("\n", "", $result); $lines = explode("\r", $result); } else if (strpos($result, "\n") > 0) { $lines = explode("\n", $result); } else { $lines = explode("\r", $result); } $lines = array_map(function ($r) { return explode("|", $r); }, $lines); $lines = array_filter($lines, function ($r) { if ($r[0] == "O" || $r[0] == "R") return true; return false; }); $o_qc = array_filter($lines, function ($l) { if ($l[0] == "O") { if ($l[11] == "Q") { return true; } } }); if (count($o_qc) == 0) { return false; } $number = ""; $result = ""; $arr_result = array(); foreach ($lines as $l) { if ($l[0] == "O") { $number = $l[2]; } else if ($l[0] == "R") { $a_test = explode("^", $l[2]); if (count($a_test) >= 6) { $test = $a_test[3]; if ($a_test[6] == "F") { $result = $l[3]; if ($result == "" || $number == "" || $test == "") { continue; } $arr_result[] = array( "no" => $number, "assay" => $test, "result" => $result ); $number = ""; $test = ""; $result = ""; } } } } return $arr_result; } function parse_qc_cobas_c8000($result) { if (strpos($result, "\n") > 0 && strpos($result, "\r") > 0) { $result = str_replace("\n", "", $result); $lines = explode("\r", $result); } else if (strpos($result, "\n") > 0) { $lines = explode("\n", $result); } else { $lines = explode("\r", $result); } $lines = array_map(function ($r) { return explode("|", $r); }, $lines); $lines = array_filter($lines, function ($r) { if ($r[0] == "O" || $r[0] == "R") return true; return false; }); $o_qc = array_filter($lines, function ($l) { if ($l[0] == "O") { if ($l[11] == "Q") { return true; } } }); if (count($o_qc) == 0) { return false; } $number = ""; $result = ""; $arr_result = array(); foreach ($lines as $l) { if ($l[0] == "O") { $a_number = explode("^",$l[2]); if (count($a_number) >= 1) { $number = $a_number[1]; } } else if ($l[0] == "R") { $a_test = explode("^", $l[2]); if (count($a_test) >= 3) { $test = $a_test[3]; if(strpos($a_test[3],"/") > 0) { $a_test = explode("/",$a_test[3]); $test = $a_test[0]; } $result = $l[3]; if ($result == "" || $number == "" || $test == "") { continue; } $arr_result[] = array( "no" => $number, "assay" => $test, "result" => $result ); $number = ""; $test = ""; $result = ""; } } } return $arr_result; } function parse_qc_xn_1000($result) { if (strpos($result, "\n") > 0 && strpos($result, "\r") > 0) { $result = str_replace("\n", "", $result); $lines = explode("\r", $result); } else if (strpos($result, "\n") > 0) { $lines = explode("\n", $result); } else { $lines = explode("\r", $result); } $lines = array_map(function ($r) { return explode("|", $r); }, $lines); $lines = array_filter($lines, function ($r) { if ($r[0] == "O" || $r[0] == "R") return true; return false; }); $o_qc = array_filter($lines, function ($l) { if ($l[0] == "O") { if ( strpos($l[3],"QC-") > 0) { return true; } } }); if (count($o_qc) == 0) { return false; } print_r($o_qc); exit; $number = ""; $result = ""; $arr_result = array(); foreach ($lines as $l) { if ($l[0] == "O") { $a_number = explode("^",$l[2]); if (count($a_number) >= 1) { $number = $a_number[1]; } } else if ($l[0] == "R") { $a_test = explode("^", $l[2]); if (count($a_test) >= 3) { $test = $a_test[3]; if(strpos($a_test[3],"/") > 0) { $a_test = explode("/",$a_test[3]); $test = $a_test[0]; } $result = $l[3]; if ($result == "" || $number == "" || $test == "") { continue; } $arr_result[] = array( "no" => $number, "assay" => $test, "result" => $result ); $number = ""; $test = ""; $result = ""; } } } return $arr_result; } function parse($instrumentID, $date = "") { //2021-05-27 if ($date == "") $date = date("Y-m-d"); $this->log("Start Parce Qc InstrumentID : {$instrumentID} Date {$date}"); // get 50 rawID $lastRawID = $this->get_last_rawID($instrumentID, $date); //get rawID $sql = "select * from itf_raw where Itf_RawNat_InstrumentID = ? and Itf_RawID > ? and date(Itf_RawDate) = ? limit 0,100"; $rs = $this->get_rows($sql, array($instrumentID, $lastRawID, $date)); if ($rs["status"] === false) { $this->log("Err get Raw Data : " . $rs["message"]); exit; } $rawID = $lastRawID; $this->db->trans_begin(); $arr_qc_ids = array(); $last_qc_id = 0; foreach ($rs["rows"] as $r) { $rawID = $r["Itf_RawID"]; $instrumentID = $r["Itf_RawNat_InstrumentID"]; $qcData = array(); switch ($instrumentID) { case 1: $qcData = $this->parse_qc_alinity($r["Itf_RawData"]); break; case 2: $qcData = $this->parse_qc_cobas_c8000($r["Itf_RawData"]); break; case 5: $qcData = $this->parse_qc_xn_1000($r["Itf_RawData"]); break; } if ($qcData != array()) { // foreach($qcData as $dt) { $data = array( "itfQcNat_InstrumentID" => $instrumentID, "itfQcDate" => $date, "itfQcNumber" => $dt["no"], "itfQcAssay" => $dt["assay"], "itfQcResult" => $dt["result"], "itfQcNat_TestID" => 0, "itfQcLastItfRawID" => $rawID ); $qry = $this->db->insert("itf_qc", $data); if (!$qry) { $this->log("Error save itf_qc : " . $this->db->error()["message"] . "|" . $this->db->last_query()); $this->db->trans_rollback(); exit; } $last_qc_id = $this->db->insert_id(); $arr_qc_ids[] = $last_qc_id; } } } if ($rawID > $lastRawID) { if ($last_qc_id > 0 ) { $sql = "update itf_qc set itfQcLastItfRawID=? where itfQcID=?"; $qry = $this->db->query($sql, array($rawID, $last_qc_id)); } else { $sql = "select max(itfQcID) maxID from itf_qc where itfQcIsActive='Y' and itfQcDate = '$date' and itfQcNat_InstrumentID = $instrumentID"; $res = $this->get_one_row($sql); if (! $res["status"]) { $this->log("Error get max itf_qc : " . $res["message"]); $this->db->trans_rollback(); exit; } if (! $res["notfound"]) { $last_qc_id = 0; } else { $last_qc_id = $res["row"]["maxID"]; if ($last_qc_id == "") $last_qc_id = 0; } if ( $last_qc_id == 0) { $sql = "insert into itf_qc(itfQcNat_InstrumentID,itfQcDate,itfQcNumber,itfQcLastItfRawID) values($instrumentID,'$date','dummy',$rawID)"; $qry = $this->db->query($sql); } else { $sql = "update itf_qc set itfQcLastItfRawID=? where itfQcID=?"; $qry = $this->db->query($sql, array($rawID, $last_qc_id)); } } if (!$qry) { $this->log("Error save itf_qc : " . $this->db->error()["message"] . "|" . $this->db->last_query()); $this->db->trans_rollback(); exit; } } if (count($arr_qc_ids) > 0 ) { $str_qc_id = implode(",",$arr_qc_ids); //update nat_id $sql = "update itf_qc join m_instrumentassay on itfQcID in ($str_qc_id) and itfQcNat_InstrumentID = '$instrumentID' and M_InstrumentAssayNat_InstrumentID = itfQcNat_InstrumentID and M_InstrumentAssayCode = itfQcAssay and M_InstrumentAssayIsActive='Y' set itfQcNat_TestID = M_InstrumentAssayNat_TestID"; $qry = $this->db->query($sql); if (!$qry) { $this->log("Error update itf_qc nat test : " . $this->db->error()["message"] . "|" . $this->db->last_query()); $this->db->trans_rollback(); exit; } } $this->log("Done up to $rawID"); $this->db->trans_commit(); } }