419 lines
13 KiB
Go
419 lines
13 KiB
Go
package repository
|
|
|
|
import (
|
|
"context"
|
|
"strconv"
|
|
"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.CollectOneRow[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 createPatientParams 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 int8 `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"`
|
|
CreateBy int `json:"create_by" db:"create_by"`
|
|
UpdateBy int `json:"update_by" db:"update_by"`
|
|
UpdatedAt time.Time `json:"updated_at" db:"updated_at"`
|
|
}
|
|
|
|
func (q *Queries) CreatePatient(ctx context.Context, arg createPatientParams) (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,
|
|
updated_at
|
|
) VALUES ( $1, $2, $3, $4, $5, $6, $7, $8, $9, $10, $11, $12, $13, $14, $15, $16, $17, $18, $19, $20, $21, $22, $23, $24, $25, $26)
|
|
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.UpdatedAt,
|
|
)
|
|
|
|
result, err := pgx.CollectExactlyOneRow[model.Patient](row, pgx.RowToStructByName[model.Patient])
|
|
|
|
return result, err
|
|
}
|
|
|
|
type ImportDataPasienParams struct {
|
|
NoRekamMedis string
|
|
NamaPasien string
|
|
NoKTP pgtype.Text
|
|
NoBPJS pgtype.Text
|
|
TempatLahir pgtype.Int4
|
|
TglLahir time.Time
|
|
Kelamin int8
|
|
Kebangsaan pgtype.Int4
|
|
Agama pgtype.Int4
|
|
Suku pgtype.Int4
|
|
Pendidikan pgtype.Int4
|
|
Pekerjaan pgtype.Int4
|
|
Hp pgtype.Text
|
|
Email pgtype.Text
|
|
StatusKawin pgtype.Int4
|
|
Provinsi pgtype.Text
|
|
NamaJalan pgtype.Text
|
|
NamaPenjamin pgtype.Text
|
|
NikPenjamin pgtype.Text
|
|
HubunganPenjamin pgtype.Int2
|
|
HpPenjamin pgtype.Text
|
|
}
|
|
|
|
func (store *SQLStore) ImportPatientTx(ctx context.Context, params ImportDataPasienParams, createdBy int32, fasyankesId int32) error {
|
|
err := store.execTx(ctx, func(q *Queries) error {
|
|
var err error
|
|
|
|
patient, err := q.CreatePatient(ctx, createPatientParams{
|
|
NoRm: params.NoRekamMedis,
|
|
NamaPasien: params.NamaPasien,
|
|
Nik: params.NoKTP,
|
|
NoKartuPesertaBPJS: params.NoBPJS,
|
|
KotaLahir: params.TempatLahir,
|
|
TanggalLahir: params.TglLahir,
|
|
JenisKelamin: params.Kelamin,
|
|
Kebangsaan: params.Kebangsaan,
|
|
Agama: params.Agama,
|
|
Suku: params.Suku,
|
|
Pendidikan: params.Pendidikan,
|
|
Pekerjaan: params.Pekerjaan,
|
|
NoHp: params.Hp,
|
|
Email: params.Email,
|
|
StatusPerkawinan: params.StatusKawin,
|
|
FasyankesID: int(fasyankesId),
|
|
UpdatedAt: time.Now().UTC(),
|
|
})
|
|
|
|
if err != nil {
|
|
return err
|
|
}
|
|
|
|
if params.NamaJalan.Valid {
|
|
var provinsi_id *int32
|
|
provinsi, err := q.FindWilayahByName(ctx, params.Provinsi.String)
|
|
|
|
if err != nil {
|
|
if err != pgx.ErrNoRows {
|
|
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),
|
|
Kategori: nil,
|
|
Negara: nil,
|
|
Kabupaten: nil,
|
|
Kecamatan: nil,
|
|
Kelurahan: nil,
|
|
Kode_pos: nil,
|
|
Rt: nil,
|
|
Rw: nil,
|
|
No_hp: nil,
|
|
No_telpon: nil,
|
|
Status: nil,
|
|
})
|
|
|
|
if err != nil {
|
|
return err
|
|
}
|
|
|
|
}
|
|
|
|
if params.HubunganPenjamin.Valid && params.NamaPenjamin.Valid {
|
|
_, err = q.CreateKeluarga(ctx, model.Keluarga{
|
|
Id: int32(patient.ID),
|
|
Nama: params.NamaPenjamin,
|
|
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 = "0"
|
|
}
|
|
|
|
length := strings.IndexFunc(tempNoRm, func(r rune) bool {
|
|
return r != '0'
|
|
})
|
|
|
|
if lastNoRm == nil {
|
|
length = 6
|
|
}
|
|
|
|
tempNoRmNum, _ := strconv.ParseInt(tempNoRm, 10, 32)
|
|
|
|
return util.PadWithZeros(int(tempNoRmNum)+1, int(length))
|
|
}
|