Initial commit
This commit is contained in:
101
cpone-dashboard/menu/result/query.go
Normal file
101
cpone-dashboard/menu/result/query.go
Normal file
@@ -0,0 +1,101 @@
|
||||
package result
|
||||
|
||||
import (
|
||||
"cpone-dashboard/db"
|
||||
"strings"
|
||||
)
|
||||
|
||||
type ResultRow struct {
|
||||
NIP string
|
||||
Name string
|
||||
Posisi string
|
||||
FileUrl string
|
||||
ReportDate string
|
||||
}
|
||||
|
||||
type ResultSummary struct {
|
||||
Total int
|
||||
HasPDF int
|
||||
}
|
||||
|
||||
func GetResultRows(mcuID int) ([]ResultRow, error) {
|
||||
rows, err := db.DB.Query(`
|
||||
SELECT
|
||||
COALESCE(NULLIF(TRIM(mp.Mcu_PatientNIP), ''), '-') AS nip,
|
||||
COALESCE(NULLIF(TRIM(mp.Mcu_PatientName), ''), '-') AS name,
|
||||
COALESCE(
|
||||
NULLIF(TRIM(mp.Mcu_PatientDepartment), ''),
|
||||
NULLIF(TRIM(mp.Mcu_PatientDivision), ''),
|
||||
NULLIF(TRIM(mp.Mcu_PatientPosisi), ''),
|
||||
'-'
|
||||
) AS posisi,
|
||||
COALESCE(p.Published_McuDasboardFileUrl, '') AS file_url,
|
||||
CASE
|
||||
WHEN p.Published_McuDasboardFileUrl IS NOT NULL
|
||||
AND p.Published_McuDasboardFileUrl != ''
|
||||
THEN COALESCE(CAST(p.Published_McuDasboardLastUpdated AS CHAR), '')
|
||||
ELSE ''
|
||||
END AS report_date
|
||||
FROM mcu_patient mp
|
||||
LEFT JOIN published_mcu_dashboard_sync p
|
||||
ON p.Published_McuDasboardT_OrderHeaderID = mp.Mcu_PatientOrderID
|
||||
WHERE mp.Mcu_PatientMcuID = ?
|
||||
AND mp.Mcu_PatientIsActive = 'Y'
|
||||
ORDER BY
|
||||
(p.Published_McuDasboardFileUrl IS NOT NULL AND p.Published_McuDasboardFileUrl != '') DESC,
|
||||
mp.Mcu_PatientName ASC
|
||||
`, mcuID)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
defer rows.Close()
|
||||
|
||||
var result []ResultRow
|
||||
for rows.Next() {
|
||||
var r ResultRow
|
||||
if err := rows.Scan(&r.NIP, &r.Name, &r.Posisi, &r.FileUrl, &r.ReportDate); err != nil {
|
||||
continue
|
||||
}
|
||||
result = append(result, r)
|
||||
}
|
||||
return result, rows.Err()
|
||||
}
|
||||
|
||||
func BuildResultSummary(rows []ResultRow) ResultSummary {
|
||||
s := ResultSummary{Total: len(rows)}
|
||||
for _, r := range rows {
|
||||
if r.FileUrl != "" {
|
||||
s.HasPDF++
|
||||
}
|
||||
}
|
||||
return s
|
||||
}
|
||||
|
||||
func FilterResultRows(rows []ResultRow, search, filter string) []ResultRow {
|
||||
search = strings.ToLower(strings.TrimSpace(search))
|
||||
filter = strings.TrimSpace(filter)
|
||||
if search == "" && filter == "" {
|
||||
return rows
|
||||
}
|
||||
out := make([]ResultRow, 0, len(rows))
|
||||
for _, r := range rows {
|
||||
switch filter {
|
||||
case "has_pdf":
|
||||
if r.FileUrl == "" {
|
||||
continue
|
||||
}
|
||||
case "no_pdf":
|
||||
if r.FileUrl != "" {
|
||||
continue
|
||||
}
|
||||
}
|
||||
if search != "" {
|
||||
hay := strings.ToLower(r.Name + " " + r.NIP + " " + r.Posisi)
|
||||
if !strings.Contains(hay, search) {
|
||||
continue
|
||||
}
|
||||
}
|
||||
out = append(out, r)
|
||||
}
|
||||
return out
|
||||
}
|
||||
Reference in New Issue
Block a user