Files
ris-backend-go/services/patient/patient.store.go
2024-12-09 09:51:19 +07:00

162 lines
6.4 KiB
Go

package patient
import (
"strings"
"github.com/jmoiron/sqlx"
"sismedika.com/sas/westone/types"
)
type Store struct {
db *sqlx.DB
}
func NewStore(db *sqlx.DB) *Store {
return &Store{db: db}
}
func (s *Store) GetPatientByID(id int) (*types.Patient, error) {
patient := new(types.Patient)
qry := `
SELECT
PatientID
PatientPersonID,
PatientManaginOrganizationID,
PatientHISNumber,
PatientRegNumber
FROM patient
WHERE PatientID = ?
`
if err := s.db.Get(patient, qry, id); err != nil {
return nil, err
}
return patient, nil
}
func (s *Store) SearchPatient(keyword string, page int, perpage int) ([]types.PatientResult, error) {
var list_patient []types.PatientResult
var sqlName, sqlNIK, sqlReg, sqlDob, sqlhp string
// params nama+nik+noreg+dob+nohp
if keyword != "" {
params := strings.Split(keyword, "+")
if len(params) > 0 && params[0] != "" {
// sqlName = " AND PersonName LIKE '%" + params[0] + "%' "
sqlName = " WHERE MATCH(PersonNameFulltext) AGAINST('" + params[0] + "' IN BOOLEAN MODE) "
}
if len(params) > 1 && params[1] != "" {
sqlNIK = " AND PersonIdentifierValue = '" + params[1] + "' "
}
if len(params) > 2 && params[2] != "" {
sqlReg = " AND PatientRegNumber = '" + params[2] + "' "
}
if len(params) > 3 && params[3] != "" {
sqlDob = " AND PersonBirthDate = '" + params[3] + "' "
}
if len(params) > 4 && params[4] != "" {
sqlhp = " AND PersonTelecomValue = '" + params[4] + "' "
}
}
patient_tb := `PatientID, PatientPersonID, PatientManaginOrganizationID, PatientHISNumber, PatientRegNumber`
person_tb := `PersonID, PersonName, PersonSalutation, PersonPrefix, PersonSuffix, PersonGender, PersonBirthPlace, PersonBirthDate,
PersonReligionCode, PersonReligionSystem, PersonBloodTypeCode, PersonBloodTypeSystem, PersonRhesusCode, PersonRhesusSystem,
PersonEducationCode, PersonEducationSystem, PersonJobClassCode, PersonJobClassSystem, PersonEtnicityCode, PersonEtnicitySystem,
PersonMaritalBirth, PersonMaritalStatus`
person_address_tb := `PersonAddressID, PersonAddressPersonID, PersonAddressIsDefault, PersonAddressUse, PersonAddressType, PersonAddressText,
PersonAddressLine1, PersonAddressLine2, PersonAddressRegionalCd, PersonAddressRT, PersonAddressRW, PersonAddressCity,
PersonAddressDistrict, PersonAddressState, PersonAddressCountry`
person_identifier_tb := `PersonIdentifierID, PersonIdentifierPersonID, PersonIdentifierUse, PersonIdentifierTypeCode,
PersonIdentifierTypeSystem, PersonIdentifierCode, PersonIdentifierSystem, PersonIdentifierValue`
person_telecom_tb := `PersonTelecomID, PersonTelecomPersonID, PersonTelecomSystem, PersonTelecomValue, PersonTelecomUse`
offset := (page - 1) * perpage
sql := `SELECT ` + patient_tb + `,` + person_tb + `,` + person_identifier_tb + `,` + person_address_tb + `,` + person_telecom_tb +
` FROM patient
JOIN person ON PersonID = PatientPersonID AND PatientIsActive = 'Y'` + sqlDob + sqlReg + `
JOIN person_identifier ON PersonIdentifierPersonID = PersonID` + sqlNIK + `
JOIN person_telecom ON PersonTelecomPersonID = PersonID` + sqlhp + `
JOIN person_address ON PersonAddressPersonID = PersonID
` + sqlName + `
LIMIT ? OFFSET ?`
if err := s.db.Select(&list_patient, sql, perpage, offset); err != nil {
return nil, err
}
return list_patient, nil
}
func (s *Store) SearchPatientOld(keyword string, page int, perpage int) ([]types.PatientResult, error) {
var list_patient []types.PatientResult
// var sqlFilter []string
var sqlName, sqlNIK, sqlReg, sqlDob, sqlhp string
// params nama+nik+noreg+dob+nohp
if keyword != "" {
params := strings.Split(keyword, "+")
if len(params) > 0 && params[0] != "" {
// sqlFilter = append(sqlFilter, "PersonName LIKE '%"+params[0]+"%'")
sqlName = " AND PersonName LIKE '%" + params[0] + "%' "
}
if len(params) > 1 && params[1] != "" {
// sqlFilter = append(sqlFilter, "PersonIdentifierValue = '"+params[1]+"'")
sqlNIK = " AND PersonIdentifierValue = '" + params[1] + "' "
}
if len(params) > 2 && params[2] != "" {
// sqlFilter = append(sqlFilter, "PatientRegNumber = '"+params[2]+"'")
sqlReg = " AND PatientRegNumber = '" + params[2] + "' "
}
if len(params) > 3 && params[3] != "" {
// sqlFilter = append(sqlFilter, "PersonBirthDate = '"+params[3]+"'")
sqlDob = " AND PersonBirthDate = '" + params[3] + "' "
}
if len(params) > 4 && params[4] != "" {
// sqlFilter = append(sqlFilter, "PersonTelecomValue = '"+params[4]+"'")
sqlhp = " AND PersonTelecomValue = '" + params[4] + "' "
}
}
patient_tb := `PatientID, PatientPersonID, PatientManaginOrganizationID, PatientHISNumber, PatientRegNumber`
person_tb := `PersonID, PersonName, PersonSalutation, PersonPrefix, PersonSuffix, PersonGender, PersonBirthPlace, PersonBirthDate,
PersonReligionCode, PersonReligionSystem, PersonBloodTypeCode, PersonBloodTypeSystem, PersonRhesusCode, PersonRhesusSystem,
PersonEducationCode, PersonEducationSystem, PersonJobClassCode, PersonJobClassSystem, PersonEtnicityCode, PersonEtnicitySystem,
PersonMaritalBirth, PersonMaritalStatus`
person_address_tb := `PersonAddressID, PersonAddressPersonID, PersonAddressIsDefault, PersonAddressUse, PersonAddressType, PersonAddressText,
PersonAddressLine1, PersonAddressLine2, PersonAddressRegionalCd, PersonAddressRT, PersonAddressRW, PersonAddressCity,
PersonAddressDistrict, PersonAddressState, PersonAddressCountry`
person_identifier_tb := `PersonIdentifierID, PersonIdentifierPersonID, PersonIdentifierUse, PersonIdentifierTypeCode,
PersonIdentifierTypeSystem, PersonIdentifierCode, PersonIdentifierSystem, PersonIdentifierValue`
person_telecom_tb := `PersonTelecomID, PersonTelecomPersonID, PersonTelecomSystem, PersonTelecomValue, PersonTelecomUse`
offset := (page - 1) * perpage
sql := `SELECT ` + patient_tb + `,` + person_tb + `,` + person_identifier_tb + `,` + person_address_tb + `,` + person_telecom_tb +
` FROM patient
JOIN person ON PersonID = PatientPersonID AND PatientIsActive = 'Y'` + sqlName + sqlDob + sqlReg + `
JOIN person_identifier ON PersonIdentifierPersonID = PersonID` + sqlNIK + `
JOIN person_telecom ON PersonTelecomPersonID = PersonID` + sqlhp + `
JOIN person_address ON PersonAddressPersonID = PersonID
LIMIT ? OFFSET ?`
// if len(sqlFilter) > 0 {
// sql += " AND " + strings.Join(sqlFilter, " AND ")
// }
// sql += " "
if err := s.db.Select(&list_patient, sql, perpage, offset); err != nil {
return nil, err
}
return list_patient, nil
}