package main

import (
	"database/sql"
	"fmt"
	"log"

	_ "github.com/microsoft/go-mssqldb"
)

func main() {
	connString := "server=127.0.0.1;user id=sa;password=Ehong@2025;database=EHONGDB;encrypt=disable"
	db, err := sql.Open("sqlserver", connString)
	if err != nil {
		log.Fatal(err)
	}
	defer db.Close()

	// Check one contract from the screenshot (e.g. if I can see the account no)
    // Looking at the screenshot, I can't see the CT_NO, but I can check any contract that has "ไม่มีอาชีพ"
	rows, err := db.Query(`
		SELECT TOP 5 h.ct_no, h.ps_id, ps.CD_CODE, o.OC_NAME
		FROM EHONGDB.dbo.hps_ctmain h
		LEFT JOIN EHONGDB.dbo.PERSON ps ON LTRIM(RTRIM(ps.PS_ID)) = LTRIM(RTRIM(h.ps_id))
		LEFT JOIN EHONGDB.dbo.OCCUPATION o ON o.CD_CODE = ps.CD_CODE
		WHERE o.OC_NAME IS NULL OR o.OC_NAME = 'ไม่มีอาชีพ'
	`)
	if err == nil {
		fmt.Println("Contracts with 'No Occupation' or NULL:")
		for rows.Next() {
			var ct, psid, cd, oc sql.NullString
			rows.Scan(&ct, &psid, &cd, &oc)
			fmt.Printf("CT_NO: %s, PS_ID: %s, CD_CODE: %s, OC_NAME: %s\n", ct.String, psid.String, cd.String, oc.String)
		}
		rows.Close()
	}
}
