486 lines
14 KiB
Go
486 lines
14 KiB
Go
|
package repository
|
||
|
|
||
|
import (
|
||
|
"context"
|
||
|
"strings"
|
||
|
"time"
|
||
|
|
||
|
"git.nochill.in/nochill/excel_import_playground/model"
|
||
|
"git.nochill.in/nochill/excel_import_playground/util"
|
||
|
"github.com/henvic/pgq"
|
||
|
"github.com/jackc/pgx/v5"
|
||
|
"github.com/jackc/pgx/v5/pgtype"
|
||
|
)
|
||
|
|
||
|
func (q *Queries) FindLastPatientInCertainFasyankes(ctx context.Context, fasyankes_id int32) (model.Patient, error) {
|
||
|
findLastPasienQuery := `
|
||
|
SELECT * FROM "Pasien" p
|
||
|
WHERE fasyankes_id = $1
|
||
|
ORDER BY p.id DESC
|
||
|
LIMIT(1)
|
||
|
`
|
||
|
row, _ := q.db.Query(ctx, findLastPasienQuery, fasyankes_id)
|
||
|
result, err := pgx.CollectExactlyOneRow[model.Patient](row, pgx.RowToStructByName[model.Patient])
|
||
|
return result, err
|
||
|
}
|
||
|
|
||
|
func (q *Queries) FindPatientByBPJSCode(ctx context.Context, BPJSCode string) (model.Patient, error) {
|
||
|
findPatientByBPJSCode := `
|
||
|
SELECT * FROM "Pasien" p
|
||
|
WHERE no_kartu_peserta_bpjs = $1
|
||
|
`
|
||
|
|
||
|
row, _ := q.db.Query(ctx, findPatientByBPJSCode, BPJSCode)
|
||
|
result, err := pgx.CollectExactlyOneRow[model.Patient](row, pgx.RowToStructByName[model.Patient])
|
||
|
|
||
|
return result, err
|
||
|
}
|
||
|
|
||
|
func (q *Queries) FindPatientByNoRm(ctx context.Context, noRm string) (model.Patient, error) {
|
||
|
findPasienByNoRmQuery := `
|
||
|
SELECT * FROM "Pasien" p
|
||
|
WHERE no_rm = $1
|
||
|
`
|
||
|
row, _ := q.db.Query(ctx, findPasienByNoRmQuery, noRm)
|
||
|
result, err := pgx.CollectExactlyOneRow[model.Patient](row, pgx.RowToStructByName[model.Patient])
|
||
|
return result, err
|
||
|
}
|
||
|
|
||
|
func (q *Queries) FindPatientByNik(ctx context.Context, nik string) (model.Patient, error) {
|
||
|
findPasienByNikQuery := `
|
||
|
SELECT * FROM "Pasien" p
|
||
|
WHERE nik = $1
|
||
|
`
|
||
|
row, _ := q.db.Query(ctx, findPasienByNikQuery, nik)
|
||
|
result, err := pgx.CollectExactlyOneRow[model.Patient](row, pgx.RowToStructByName[model.Patient])
|
||
|
return result, err
|
||
|
}
|
||
|
|
||
|
type UpdatePatientParams struct {
|
||
|
NoRm UpdateableField[string] `json:"no_rm" db:"no_rm"`
|
||
|
NamaPasien UpdateableField[string] `json:"nama_pasien" db:"nama_pasien"`
|
||
|
JenisIdentitas UpdateableField[pgtype.Int4] `json:"jenis_identitas" db:"jenis_identitas"`
|
||
|
NoIdentitas UpdateableField[pgtype.Text] `json:"no_identitas" db:"no_identitas"`
|
||
|
FotoProfil UpdateableField[pgtype.Text] `json:"foto_profil" db:"foto_profil"`
|
||
|
FotoKtp UpdateableField[pgtype.Text] `json:"foto_ktp" db:"foto_ktp"`
|
||
|
KotaLahir UpdateableField[pgtype.Int4] `json:"kota_lahir" db:"kota_lahir"`
|
||
|
TanggalLahir UpdateableField[time.Time] `json:"tanggal_lahir" db:"tanggal_lahir"`
|
||
|
JenisKelamin UpdateableField[pgtype.Int4] `json:"jenis_kelamin" db:"jenis_kelamin"`
|
||
|
Suku UpdateableField[pgtype.Int4] `json:"suku" db:"suku"`
|
||
|
Agama UpdateableField[pgtype.Int4] `json:"agama" db:"agama"`
|
||
|
Kebangsaan UpdateableField[pgtype.Int4] `json:"kebangsaan" db:"kebangsaan"`
|
||
|
Bahasa UpdateableField[pgtype.Int4] `json:"bahasa" db:"bahasa"`
|
||
|
Pendidikan UpdateableField[pgtype.Int4] `json:"pendidikan" db:"pendidikan"`
|
||
|
StatusPerkawinan UpdateableField[pgtype.Int4] `json:"status_perkawinan" db:"status_perkawinan"`
|
||
|
Email UpdateableField[pgtype.Text] `json:"email" db:"email"`
|
||
|
Pekerjaan UpdateableField[pgtype.Int4] `json:"pekerjaan" db:"pekerjaan"`
|
||
|
NoHp UpdateableField[pgtype.Text] `json:"no_hp" db:"no_hp"`
|
||
|
IsDeleted UpdateableField[bool] `json:"is_deleted" db:"is_deleted"`
|
||
|
FasyankesID UpdateableField[int] `json:"fasyankes_id" db:"fasyankes_id"`
|
||
|
Nik UpdateableField[pgtype.Text] `json:"nik" db:"nik"`
|
||
|
NoKk UpdateableField[pgtype.Text] `json:"no_kk" db:"no_kk"`
|
||
|
NoKartuPesertaBPJS UpdateableField[pgtype.Text] `json:"no_kartu_peserta_bpjs" db:"no_kartu_peserta_bpjs"`
|
||
|
UpdateBy int `json:"update_by" db:"update_by"`
|
||
|
}
|
||
|
|
||
|
func (q *Queries) UpdatePatient(ctx context.Context, arg UpdatePatientParams, patientId int32) (model.Patient, error) {
|
||
|
builder := pgq.Update("\"Pasien\"").Set("update_by", arg.UpdateBy).Set("updated_at", time.Now().UTC())
|
||
|
|
||
|
if arg.NoRm.IsFilled {
|
||
|
builder.Set("no_rm", arg.NoRm.Value)
|
||
|
}
|
||
|
if arg.NamaPasien.IsFilled {
|
||
|
builder = builder.Set("nama_pasien", arg.NamaPasien.Value)
|
||
|
}
|
||
|
if arg.JenisIdentitas.IsFilled {
|
||
|
builder = builder.Set("jenis_identitas", arg.JenisIdentitas.Value)
|
||
|
}
|
||
|
if arg.NoIdentitas.IsFilled {
|
||
|
builder = builder.Set("no_identitas", arg.NoIdentitas.Value)
|
||
|
}
|
||
|
if arg.FotoProfil.IsFilled {
|
||
|
builder = builder.Set("foto_profil", arg.FotoProfil.Value)
|
||
|
}
|
||
|
if arg.FotoKtp.IsFilled {
|
||
|
builder = builder.Set("foto_ktp", arg.FotoKtp.Value)
|
||
|
}
|
||
|
if arg.KotaLahir.IsFilled {
|
||
|
builder = builder.Set("kota_lahir", arg.KotaLahir.Value)
|
||
|
}
|
||
|
if arg.TanggalLahir.IsFilled {
|
||
|
builder = builder.Set("tanggal_lahir", arg.TanggalLahir.Value)
|
||
|
}
|
||
|
if arg.JenisKelamin.IsFilled {
|
||
|
builder = builder.Set("jenis_kelamin", arg.JenisKelamin.Value)
|
||
|
}
|
||
|
if arg.Suku.IsFilled {
|
||
|
builder = builder.Set("suku", arg.Suku.Value)
|
||
|
}
|
||
|
if arg.Agama.IsFilled {
|
||
|
builder = builder.Set("agama", arg.Agama.Value)
|
||
|
}
|
||
|
if arg.Kebangsaan.IsFilled {
|
||
|
builder = builder.Set("kebangsaan", arg.Kebangsaan.Value)
|
||
|
}
|
||
|
if arg.Bahasa.IsFilled {
|
||
|
builder = builder.Set("bahasa", arg.Bahasa.Value)
|
||
|
}
|
||
|
if arg.Pendidikan.IsFilled {
|
||
|
builder = builder.Set("pendidikan", arg.Pendidikan.Value)
|
||
|
}
|
||
|
if arg.StatusPerkawinan.IsFilled {
|
||
|
builder = builder.Set("status_perkawinan", arg.StatusPerkawinan.Value)
|
||
|
}
|
||
|
if arg.Email.IsFilled {
|
||
|
builder = builder.Set("email", arg.Email.Value)
|
||
|
}
|
||
|
if arg.Pekerjaan.IsFilled {
|
||
|
builder = builder.Set("pekerjaan", arg.Pekerjaan.Value)
|
||
|
}
|
||
|
if arg.NoHp.IsFilled {
|
||
|
builder = builder.Set("no_hp", arg.NoHp.Value)
|
||
|
}
|
||
|
if arg.IsDeleted.IsFilled {
|
||
|
builder = builder.Set("is_deleted", arg.IsDeleted.Value)
|
||
|
}
|
||
|
if arg.FasyankesID.IsFilled {
|
||
|
builder = builder.Set("fasyankes_id", arg.FasyankesID.Value)
|
||
|
}
|
||
|
if arg.Nik.IsFilled {
|
||
|
builder = builder.Set("nik", arg.Nik.Value)
|
||
|
}
|
||
|
if arg.NoKk.IsFilled {
|
||
|
builder = builder.Set("no_kk", arg.NoKk.Value)
|
||
|
}
|
||
|
if arg.NoKartuPesertaBPJS.IsFilled {
|
||
|
builder = builder.Set("no_kartu_peserta_bpjs", arg.NoKartuPesertaBPJS.Value)
|
||
|
}
|
||
|
|
||
|
query, args, err := builder.Where(pgq.Eq{"id": patientId}).Suffix("RETURNING *").SQL()
|
||
|
|
||
|
if err != nil {
|
||
|
return model.Patient{}, err
|
||
|
}
|
||
|
|
||
|
row, _ := q.db.Query(ctx, query, args...)
|
||
|
result, err := pgx.CollectExactlyOneRow[model.Patient](row, pgx.RowToStructByName[model.Patient])
|
||
|
return result, err
|
||
|
}
|
||
|
|
||
|
func (q *Queries) UpdatePatientNik(ctx context.Context, nik *string, patientId int32) error {
|
||
|
updatePatientQuery := `
|
||
|
UPDATE "Pasien" p
|
||
|
SET nik = $1
|
||
|
WHERE id = $2
|
||
|
`
|
||
|
|
||
|
_, err := q.db.Exec(ctx, updatePatientQuery, nik, patientId)
|
||
|
return err
|
||
|
|
||
|
}
|
||
|
|
||
|
// type UpdatePatientParams struct {
|
||
|
// NoRm string `json:"no_rm" db:"no_rm"`
|
||
|
// NamaPasien string `json:"nama_pasien" db:"nama_pasien"`
|
||
|
// JenisIdentitas pgtype.Int4 `json:"jenis_identitas" db:"jenis_identitas"`
|
||
|
// NoIdentitas pgtype.Text `json:"no_identitas" db:"no_identitas"`
|
||
|
// FotoProfil pgtype.Text `json:"foto_profil" db:"foto_profil"`
|
||
|
// FotoKtp pgtype.Text `json:"foto_ktp" db:"foto_ktp"`
|
||
|
// KotaLahir pgtype.Int4 `json:"kota_lahir" db:"kota_lahir"`
|
||
|
// TanggalLahir *time.Time `json:"tanggal_lahir" db:"tanggal_lahir"`
|
||
|
// JenisKelamin pgtype.Int4 `json:"jenis_kelamin" db:"jenis_kelamin"`
|
||
|
// Suku pgtype.Int4 `json:"suku" db:"suku"`
|
||
|
// Agama pgtype.Int4 `json:"agama" db:"agama"`
|
||
|
// Kebangsaan pgtype.Int4 `json:"kebangsaan" db:"kebangsaan"`
|
||
|
// Bahasa pgtype.Int4 `json:"bahasa" db:"bahasa"`
|
||
|
// Pendidikan pgtype.Int4 `json:"pendidikan" db:"pendidikan"`
|
||
|
// StatusPerkawinan pgtype.Int4 `json:"status_perkawinan" db:"status_perkawinan"`
|
||
|
// Email pgtype.Text `json:"email" db:"email"`
|
||
|
// Pekerjaan pgtype.Int4 `json:"pekerjaan" db:"pekerjaan"`
|
||
|
// NoHp pgtype.Text `json:"no_hp" db:"no_hp"`
|
||
|
// IsDeleted *bool `json:"is_deleted" db:"is_deleted"`
|
||
|
// FasyankesID int `json:"fasyankes_id" db:"fasyankes_id"`
|
||
|
// Nik pgtype.Text `json:"nik" db:"nik"`
|
||
|
// NoKk pgtype.Text `json:"no_kk" db:"no_kk"`
|
||
|
// NoKartuPesertaBPJS pgtype.Text `json:"no_kartu_peserta_bpjs" db:"no_kartu_peserta_bpjs"`
|
||
|
// UpdateBy int `json:"update_by" db:"update_by"`
|
||
|
// UpdatedAt time.Time `json:"updated_at" db:"updated_at"`
|
||
|
// }
|
||
|
|
||
|
// func (q *Queries) UpdatePatient(ctx context.Context, patientId int32, arg UpdatePatientParams) (model.Patient, error) {
|
||
|
// updatePatientQuery := `
|
||
|
// UPDATE "Pasien" p
|
||
|
// SET
|
||
|
// no_rm = $1,
|
||
|
// nama_pasien = $2,
|
||
|
// jenis_identitas = $3,
|
||
|
// no_identitas = $4,
|
||
|
// foto_profil = $5,
|
||
|
// foto_ktp = $6,
|
||
|
// kota_lahir = $7,
|
||
|
// tanggal_lahir = $8,
|
||
|
// jenis_kelamin = $9,
|
||
|
// suku = $10,
|
||
|
// agama = $11,
|
||
|
// kebangsaan = $12,
|
||
|
// bahasa = $13,
|
||
|
// pendidikan = $14,
|
||
|
// status_perkawinan = $15,
|
||
|
// email = $16,
|
||
|
// pekerjaan = $17,
|
||
|
// no_hp = $18,
|
||
|
// is_deleted = $19,
|
||
|
// fasyankes_id = $20,
|
||
|
// nik = $21,
|
||
|
// no_kk = $22,
|
||
|
// no_kartu_peserta_bpjs = $23,
|
||
|
// update_by = $24,
|
||
|
// updated_at = $25
|
||
|
// WHERE id = $26
|
||
|
// `
|
||
|
|
||
|
// row, _ := q.db.Query(ctx, updatePatientQuery,
|
||
|
// arg.NoRm,
|
||
|
// arg.NamaPasien,
|
||
|
// arg.JenisIdentitas,
|
||
|
// arg.NoIdentitas,
|
||
|
// arg.FotoProfil,
|
||
|
// arg.FotoKtp,
|
||
|
// arg.KotaLahir,
|
||
|
// arg.TanggalLahir,
|
||
|
// arg.JenisKelamin,
|
||
|
// arg.Suku,
|
||
|
// arg.Agama,
|
||
|
// arg.Kebangsaan,
|
||
|
// arg.Bahasa,
|
||
|
// arg.Pendidikan,
|
||
|
// arg.StatusPerkawinan,
|
||
|
// arg.Email,
|
||
|
// arg.Pekerjaan,
|
||
|
// arg.NoHp,
|
||
|
// arg.IsDeleted,
|
||
|
// arg.FasyankesID,
|
||
|
// arg.Nik,
|
||
|
// arg.NoKk,
|
||
|
// arg.NoKartuPesertaBPJS,
|
||
|
// arg.UpdateBy,
|
||
|
// arg.UpdatedAt,
|
||
|
// patientId,
|
||
|
// )
|
||
|
|
||
|
// result, err := pgx.CollectExactlyOneRow[model.Patient](row, pgx.RowToStructByName[model.Patient])
|
||
|
|
||
|
// return result, err
|
||
|
// }
|
||
|
|
||
|
func (q *Queries) CreatePatient(ctx context.Context, arg model.Patient) (model.Patient, error) {
|
||
|
createPasienQuery := `
|
||
|
INSERT INTO "Pasien"(
|
||
|
no_rm,
|
||
|
nama_pasien,
|
||
|
jenis_identitas,
|
||
|
no_identitas,
|
||
|
foto_profil,
|
||
|
foto_ktp,
|
||
|
kota_lahir,
|
||
|
tanggal_lahir,
|
||
|
jenis_kelamin,
|
||
|
suku,
|
||
|
agama,
|
||
|
kebangsaan,
|
||
|
bahasa,
|
||
|
pendidikan,
|
||
|
status_perkawinan,
|
||
|
email,
|
||
|
pekerjaan,
|
||
|
no_hp,
|
||
|
is_deleted,
|
||
|
fasyankes_id,
|
||
|
nik,
|
||
|
no_kk,
|
||
|
no_kartu_peserta_bpjs,
|
||
|
create_by,
|
||
|
update_by
|
||
|
) VALUES ($1, $2, $3, $4, $5, $6, $7, $8, $9, $10, $11, $12, $13, $14, $15, $16)
|
||
|
RETURNING *;
|
||
|
`
|
||
|
|
||
|
row, _ := q.db.Query(ctx, createPasienQuery,
|
||
|
arg.NoRm,
|
||
|
arg.NamaPasien,
|
||
|
arg.JenisIdentitas,
|
||
|
arg.NoIdentitas,
|
||
|
arg.FotoProfil,
|
||
|
arg.FotoKtp,
|
||
|
arg.KotaLahir,
|
||
|
arg.TanggalLahir,
|
||
|
arg.JenisKelamin,
|
||
|
arg.Suku,
|
||
|
arg.Agama,
|
||
|
arg.Kebangsaan,
|
||
|
arg.Bahasa,
|
||
|
arg.Pendidikan,
|
||
|
arg.StatusPerkawinan,
|
||
|
arg.Email,
|
||
|
arg.Pekerjaan,
|
||
|
arg.NoHp,
|
||
|
arg.IsDeleted,
|
||
|
arg.FasyankesID,
|
||
|
arg.Nik,
|
||
|
arg.NoKk,
|
||
|
arg.NoKartuPesertaBPJS,
|
||
|
arg.CreateBy,
|
||
|
arg.UpdateBy,
|
||
|
arg.CreatedAt,
|
||
|
arg.UpdatedAt,
|
||
|
)
|
||
|
|
||
|
result, err := pgx.CollectExactlyOneRow[model.Patient](row, pgx.RowToStructByName[model.Patient])
|
||
|
|
||
|
return result, err
|
||
|
}
|
||
|
|
||
|
type ImportDataPasienParams struct {
|
||
|
NoRekamMedis string
|
||
|
NamaPasien string
|
||
|
Nik pgtype.Text
|
||
|
NoBPJS pgtype.Text
|
||
|
TpLahir pgtype.Text
|
||
|
TglLahir time.Time
|
||
|
Kelamin int8
|
||
|
Kebangsaan *int8
|
||
|
Agama *int8
|
||
|
Suku *int32
|
||
|
Pendidikan *int8
|
||
|
Pekerjaan *int8
|
||
|
Hp pgtype.Text
|
||
|
Email pgtype.Text
|
||
|
StatusNikah *int8
|
||
|
Provinsi *string
|
||
|
Kabupaten *int32
|
||
|
Kecamatan *int32
|
||
|
Kelurahan *int32
|
||
|
Kodepos *int32
|
||
|
NamaJalan pgtype.Text
|
||
|
HpPenjamin pgtype.Text
|
||
|
NamaPenjamin pgtype.Text
|
||
|
KtpPenjamin pgtype.Text
|
||
|
HubunganPenjamin *int8
|
||
|
PendidikanPenjamin *int8
|
||
|
AlamatPenjamin pgtype.Text
|
||
|
}
|
||
|
|
||
|
func (store *SQLStore) ImportPatientTx(ctx context.Context, params ImportDataPasienParams) error {
|
||
|
err := store.execTx(ctx, func(q *Queries) error {
|
||
|
var err error
|
||
|
|
||
|
tempNilInt := int8(util.NilIntVal)
|
||
|
|
||
|
if params.Pendidikan == nil {
|
||
|
params.Pendidikan = &tempNilInt
|
||
|
}
|
||
|
|
||
|
if params.Agama == nil {
|
||
|
params.Agama = &tempNilInt
|
||
|
}
|
||
|
|
||
|
if params.Pekerjaan == nil {
|
||
|
params.Pekerjaan = &tempNilInt
|
||
|
}
|
||
|
|
||
|
if params.StatusNikah == nil {
|
||
|
params.StatusNikah = &tempNilInt
|
||
|
}
|
||
|
|
||
|
patient, err := q.CreatePatient(ctx, model.Patient{
|
||
|
NoRm: params.NoRekamMedis,
|
||
|
NamaPasien: params.NamaPasien,
|
||
|
Pendidikan: pgtype.Int4{Valid: params.Pendidikan != nil, Int32: int32(*params.Pendidikan)},
|
||
|
TanggalLahir: params.TglLahir,
|
||
|
Nik: params.Nik,
|
||
|
Agama: pgtype.Int4{Valid: params.Agama != nil, Int32: int32(*params.Agama)},
|
||
|
NoHp: params.Hp,
|
||
|
Pekerjaan: pgtype.Int4{Valid: params.Pekerjaan != nil, Int32: int32(*params.Agama)},
|
||
|
Email: params.Email,
|
||
|
NoKartuPesertaBPJS: params.NoBPJS,
|
||
|
JenisKelamin: params.Kelamin,
|
||
|
StatusPerkawinan: pgtype.Int4{Valid: params.StatusNikah != nil, Int32: int32(*params.StatusNikah)},
|
||
|
IsDeleted: false,
|
||
|
})
|
||
|
|
||
|
if err != nil {
|
||
|
return err
|
||
|
}
|
||
|
|
||
|
if params.NamaJalan.Valid {
|
||
|
var provinsi_id *int32
|
||
|
provinsi, err := q.FindWilayahByName(ctx, *params.Provinsi)
|
||
|
|
||
|
if err != nil {
|
||
|
return err
|
||
|
}
|
||
|
|
||
|
if provinsi.Id == 0 {
|
||
|
provinsi_id = nil
|
||
|
} else {
|
||
|
provinsi_id = &provinsi.Id
|
||
|
}
|
||
|
|
||
|
_, err = q.CreateAlamat(ctx, model.Alamat{
|
||
|
Provinsi: provinsi_id,
|
||
|
Nama_jalan: ¶ms.NamaJalan.String,
|
||
|
Pasien_id: int32(patient.ID),
|
||
|
})
|
||
|
|
||
|
if err != nil {
|
||
|
return err
|
||
|
}
|
||
|
|
||
|
}
|
||
|
|
||
|
if params.HubunganPenjamin != nil && params.NamaPenjamin.Valid {
|
||
|
_, err = q.CreateKeluarga(ctx, model.Keluarga{
|
||
|
Id: int32(patient.ID),
|
||
|
Nama: ¶ms.NamaPenjamin.String,
|
||
|
Hubungan: params.HubunganPenjamin,
|
||
|
})
|
||
|
|
||
|
if err != nil {
|
||
|
return err
|
||
|
}
|
||
|
}
|
||
|
|
||
|
return err
|
||
|
})
|
||
|
|
||
|
return err
|
||
|
}
|
||
|
|
||
|
func (q *Queries) GenerateNoRm(ctx context.Context, fasyankesId int32) (string, error) {
|
||
|
lastPasien, err := q.FindLastPatientInCertainFasyankes(ctx, fasyankesId)
|
||
|
|
||
|
if err != nil {
|
||
|
return "", err
|
||
|
}
|
||
|
|
||
|
return genNoRm(&lastPasien.NoRm), nil
|
||
|
}
|
||
|
|
||
|
func genNoRm(lastNoRm *string) string {
|
||
|
var tempNoRm string
|
||
|
|
||
|
if lastNoRm != nil {
|
||
|
tempNoRm = *lastNoRm
|
||
|
} else {
|
||
|
tempNoRm = "1"
|
||
|
}
|
||
|
|
||
|
length := strings.IndexFunc(tempNoRm, func(r rune) bool {
|
||
|
return r != '0'
|
||
|
})
|
||
|
|
||
|
return util.Pad(tempNoRm, int32(length))
|
||
|
}
|
||
|
|
||
|
// func FindPatientBy
|