db_regional = $this->load->database("regional", true); } function index() { echo "Api: Download Pesan Khusus"; } function download() { try { $prm = $this->get_param(); $limit = 20; if (isset($prm['limit'])) { $limit = trim($prm['limit']); } $branchId = $prm['branchId']; $branchCode = $prm['branchCode']; // $sql = "SELECT distinct // DoctorMessageOrderPatientID as OrderPatientID, // downloadDoctorMessageLogM_BranchID, // downloadDoctorMessageLogM_BranchCode, // downloadDoctorMessageLogIsDownloaded, // downloadDoctorMessageLogRetry // from one_doctor.doctor_message // join one_doctor.order_patient // ON DoctorMessageOrderPatientID = OrderPatientID // AND DoctorMessageIsActive = 'Y' // AND DoctorMessageStatusDownload = 'N' // JOIN one_doctor.order_patient_details // ON OrderPatientID = OrderPatientDetailsOrderPatientID // AND OrderPatientDetailsIsActive = 'Y' // JOIN m_mou on OrderPatientM_MouID = M_MouID // AND M_MouIsActive = 'Y' // LEFT JOIN one_doctor.download_doctor_message_log // ON OrderPatientID = downloadDoctorMessageLogOrderPatientID // WHERE OrderPatientIsActive = 'Y' // AND ( // ISNULL(downloadDoctorMessageLogM_BranchID) // OR (downloadDoctorMessageLogM_BranchID = ? AND downloadDoctorMessageLogIsDownloaded = 'N') // OR (downloadDoctorMessageLogM_BranchID <> ? ) // ) // AND (ISNULL(downloadDoctorMessageLogRetry) OR downloadDoctorMessageLogRetry < 10) // GROUP BY DoctorMessageOrderPatientID, downloadDoctorMessageLogM_BranchID // ORDER BY DoctorMessageOrderPatientID // LIMIT ?"; $sql = "SELECT distinct DoctorMessageOrderPatientID as OrderPatientID, downloadDoctorMessageLogM_BranchID, downloadDoctorMessageLogM_BranchCode, downloadDoctorMessageLogIsDownloaded, downloadDoctorMessageLogRetry from one_doctor.doctor_message join one_doctor.order_patient ON DoctorMessageOrderPatientID = OrderPatientID AND DoctorMessageIsActive = 'Y' AND DoctorMessageStatusDownload = 'N' JOIN one_doctor.order_patient_details ON OrderPatientID = OrderPatientDetailsOrderPatientID AND OrderPatientDetailsIsActive = 'Y' JOIN m_mou ON (OrderPatientM_MouID = M_MouID AND M_MouIsActive = 'Y' OR OrderPatientM_MouID = 0) LEFT JOIN one_doctor.download_doctor_message_log ON OrderPatientID = downloadDoctorMessageLogOrderPatientID WHERE OrderPatientIsActive = 'Y' AND ( ISNULL(downloadDoctorMessageLogM_BranchID) OR (downloadDoctorMessageLogM_BranchID = ? AND downloadDoctorMessageLogIsDownloaded = 'N') OR (downloadDoctorMessageLogM_BranchID <> ? ) ) AND (ISNULL(downloadDoctorMessageLogRetry) OR downloadDoctorMessageLogRetry < 10) GROUP BY DoctorMessageOrderPatientID, downloadDoctorMessageLogM_BranchID ORDER BY DoctorMessageOrderPatientID LIMIT ?"; $qry = $this->db_regional->query($sql, [ intval($branchId), intval($branchId), intval($limit) ]); if (!$qry) { $response = ["status" => "ERR", "message" => "select doctor message | " . $this->db_regional->error()["message"], "debug" => $this->db_regional->last_query()]; $this->reply_gz($response); exit; } // echo $this->db_regional->last_query(); // exit; $rows = $qry->result_array(); $arrOrderPatientId = array(); foreach ($rows as $key => $val) { $orderPatientId = $val['OrderPatientID']; if (in_array($orderPatientId, $arrOrderPatientId)) { continue; } $arrOrderPatientId[] = $orderPatientId; $this->insertDownloadLog($branchId, $branchCode, $orderPatientId); } if (count($arrOrderPatientId) > 0) { $orderPatientIdAll = implode(',', $arrOrderPatientId); // tnp order nasional $sqlData = "SELECT doctor_message.* , IFNULL(M_MouM_CompanyID,0) as M_MouM_CompanyID from one_doctor.doctor_message join one_doctor.order_patient ON DoctorMessageOrderPatientID = OrderPatientID AND DoctorMessageIsActive = 'Y' LEFT JOIN m_mou ON (OrderPatientM_MouID = M_MouID AND M_MouIsActive = 'Y') WHERE OrderPatientID IN ($orderPatientIdAll) AND OrderPatientIsActive = 'Y'"; // $sqlData = "SELECT doctor_message.* , M_MouM_CompanyID // from one_doctor.doctor_message // join one_doctor.order_patient // ON DoctorMessageOrderPatientID = OrderPatientID // AND DoctorMessageIsActive = 'Y' // JOIN m_mou ON OrderPatientM_MouID = M_MouID // AND M_MouIsActive = 'Y' // WHERE OrderPatientID IN ($orderPatientIdAll) // AND OrderPatientIsActive = 'Y'"; $qryData = $this->db_regional->query($sqlData); if (!$qryData) { $response = ["status" => "ERR", "message" => "select doctor message | " . $this->db_regional->error()["message"], "debug" => $this->db_regional->last_query()]; $this->reply_gz($response); exit; } $rowsData = $qryData->result_array(); // print_r($rowsData); // exit; $result = array( 'status' => 'OK', "data" => $rowsData, ); $this->reply_gz($result); // $this->reply($result); } else { $this->reply_gz("Tidak ada data terbaru"); } } catch (Exception $exc) { $message = $exc->getMessage(); $result = array( 'status' => 'err', "message" => $message, ); $this->reply_gz($result); } } function insertDownloadLog($branchId, $branchCode, $patientId) { $sql = "INSERT INTO one_doctor.download_doctor_message_log (downloadDoctorMessageLogM_BranchID, downloadDoctorMessageLogM_BranchCode, downloadDoctorMessageLogOrderPatientID, downloadDoctorMessageLogRetry) VALUES (?,?,?,0) ON DUPLICATE KEY UPDATE downloadDoctorMessageLogRetry = downloadDoctorMessageLogRetry + 1"; $qry = $this->db_regional->query($sql, [intval($branchId), $branchCode, intval($patientId)]); if (!$qry) { $this->sys_error_db(["status" => "ERR", "message" => "select doctor message | " . $this->db_regional->error()["message"], "debug" => $this->db_regional->last_query()]); exit; } } function updateLog() { try { $prm = $this->get_param_z(); $branchId = $prm['branchId']; // $branchCde = $prm['branchCode']; $arrOrderPatientId = $prm['arrOrderPatientId']; foreach ($arrOrderPatientId as $key => $value) { $sqlUpdate = "UPDATE one_doctor.download_doctor_message_log SET downloadDoctorMessageLogIsDownloaded = 'Y' WHERE downloadDoctorMessageLogOrderPatientID = ? AND downloadDoctorMessageLogM_BranchID = ?"; $qry = $this->db_regional->query($sqlUpdate, [intval($value), intval($branchId)]); if (!$qry) { $this->sys_error_db(["status" => "ERR", "message" => "Insert download doctor message Log | " . $this->db_regional->error()["message"], "debug" => $this->db_regional->last_query()]); exit; } // UPDATE doctor_message status_download jadi Y $sql_update_doctor_message = "UPDATE one_doctor.doctor_message SET DoctorMessageStatusDownload = 'Y' WHERE DoctorMessageOrderPatientID = ?"; $qry_doctor_message = $this->db_regional->query($sql_update_doctor_message, [intval($value)]); if (!$qry_doctor_message) { $this->sys_error_db(["status" => "ERR", "message" => "update doctor message status download N | " . $this->db_regional->error()["message"], "debug" => $this->db_regional->last_query()]); exit; } } $result = array( "message" => 'Berhasil', "sql" => $this->db_regional->last_query() ); $this->sys_ok($result); } catch (Exception $exc) { $message = $exc->getMessage(); $this->sys_error($message); } } function reply($resp) { echo json_encode($resp); } function reply_gz($resp) { echo gzcompress(json_encode($resp)); } function get_param() { $body = file_get_contents("php://input"); return json_decode($body, true); } function get_param_z() { $body_z = file_get_contents("php://input"); $body = gzuncompress($body_z); return json_decode($body, true); } }