2025-08-22 10:11:07 +02:00
|
|
|
package queries
|
|
|
|
|
|
|
|
|
|
import (
|
2025-08-22 12:57:33 +02:00
|
|
|
"backend/typesdefs"
|
2025-08-22 10:11:07 +02:00
|
|
|
"fmt"
|
2025-10-15 14:27:06 +02:00
|
|
|
"slices"
|
2025-08-22 10:11:07 +02:00
|
|
|
|
|
|
|
|
"github.com/georgysavva/scany/v2/pgxscan"
|
|
|
|
|
"github.com/jackc/pgx/v5"
|
|
|
|
|
)
|
|
|
|
|
|
|
|
|
|
func AnnunciGetActive() ([]string, error) {
|
|
|
|
|
var annunci []string
|
|
|
|
|
err := pgxscan.Select(Db.Ctx, Db.Dbpool, &annunci, "SELECT codice FROM public.annunci WHERE web = true and stato = 'Attivo' ")
|
|
|
|
|
if err != nil && err != pgx.ErrNoRows {
|
|
|
|
|
return nil, fmt.Errorf("failed to get active annunci: %w", err)
|
|
|
|
|
}
|
|
|
|
|
return annunci, nil
|
|
|
|
|
}
|
|
|
|
|
|
2025-08-22 12:57:33 +02:00
|
|
|
func AnnunciInsert(annunci typesdefs.AnnunciParsed) error {
|
2025-08-22 10:11:07 +02:00
|
|
|
|
|
|
|
|
//SET ALL WEB TO FALSE
|
2025-08-29 16:36:23 +02:00
|
|
|
_, err := Db.Dbpool.Exec(Db.Ctx, "UPDATE public.annunci SET web = false, homepage = false, stato='Sospeso'")
|
2025-08-22 10:11:07 +02:00
|
|
|
if err != nil {
|
|
|
|
|
return fmt.Errorf("failed to set all web to false: %w", err)
|
|
|
|
|
|
|
|
|
|
}
|
|
|
|
|
//GET ALL CODICI
|
|
|
|
|
var codici []string
|
|
|
|
|
err = pgxscan.Select(Db.Ctx, Db.Dbpool, &codici, "SELECT DISTINCT codice FROM public.annunci")
|
|
|
|
|
if err != nil {
|
|
|
|
|
return fmt.Errorf("failed to get all codici: %w", err)
|
|
|
|
|
}
|
|
|
|
|
batch := &pgx.Batch{}
|
|
|
|
|
for _, annuncio := range annunci.Annuncio {
|
|
|
|
|
|
2025-10-15 14:27:06 +02:00
|
|
|
if slices.Contains(codici, annuncio.Codice) {
|
2025-08-22 10:11:07 +02:00
|
|
|
batch.Queue(`
|
|
|
|
|
UPDATE public.annunci
|
|
|
|
|
SET locatore = $1,
|
|
|
|
|
numero = $2,
|
|
|
|
|
idlocatore = $3,
|
|
|
|
|
email = $4,
|
|
|
|
|
creato_il = $5,
|
|
|
|
|
modificato_il = $6,
|
|
|
|
|
indirizzo = $7,
|
|
|
|
|
civico = $8,
|
|
|
|
|
comune = $9,
|
|
|
|
|
cap = $10,
|
|
|
|
|
provincia = $11,
|
|
|
|
|
regione = $12,
|
|
|
|
|
lat = $13,
|
|
|
|
|
lon = $14,
|
|
|
|
|
indirizzo_secondario = $15,
|
|
|
|
|
civico_secondario = $16,
|
|
|
|
|
lat_secondario = $17,
|
|
|
|
|
lon_secondario = $18,
|
|
|
|
|
tipo = $19,
|
|
|
|
|
categorie = $20,
|
|
|
|
|
prezzo = $21,
|
|
|
|
|
anno = $22,
|
|
|
|
|
consegna = $23,
|
|
|
|
|
classe = $24,
|
|
|
|
|
mq = $25,
|
|
|
|
|
piano = $26,
|
|
|
|
|
piano_palazzo = $27,
|
|
|
|
|
unita_condominio = $28,
|
|
|
|
|
numero_vani = $29,
|
|
|
|
|
numero_camere = $30,
|
|
|
|
|
numero_bagni = $31,
|
|
|
|
|
numero_balconi = $32,
|
|
|
|
|
numero_terrazzi = $33,
|
|
|
|
|
numero_box = $34,
|
|
|
|
|
numero_postiauto = $35,
|
|
|
|
|
caratteristiche = $36,
|
|
|
|
|
accessori = $37,
|
|
|
|
|
titolo_it = $38,
|
|
|
|
|
desc_it = $39,
|
|
|
|
|
titolo_en = $40,
|
|
|
|
|
desc_en = $41,
|
|
|
|
|
stato = $42,
|
|
|
|
|
web = $43,
|
|
|
|
|
homepage = $44,
|
|
|
|
|
url_immagini = $45,
|
|
|
|
|
url_video = $46,
|
2025-08-27 17:03:57 +02:00
|
|
|
og_url = $48,
|
|
|
|
|
disponibile_da = $49,
|
|
|
|
|
permanenza = $50,
|
|
|
|
|
persone = $51
|
2025-08-22 10:11:07 +02:00
|
|
|
WHERE codice = $47
|
|
|
|
|
`, annuncio.Locatore, annuncio.Numero, annuncio.Idlocatore,
|
|
|
|
|
annuncio.Email, annuncio.Creato_il, annuncio.Modificato_il,
|
|
|
|
|
annuncio.Indirizzo, annuncio.Civico, annuncio.Comune, annuncio.Cap,
|
|
|
|
|
annuncio.Provincia, annuncio.Regione, annuncio.Lat, annuncio.Lon,
|
|
|
|
|
annuncio.Indirizzo_secondario, annuncio.Civico_secondario, annuncio.Lat_secondario,
|
|
|
|
|
annuncio.Lon_secondario, annuncio.Tipo, annuncio.Categorie, annuncio.Prezzo, annuncio.Anno,
|
|
|
|
|
annuncio.Consegna, annuncio.Classe, annuncio.Mq, annuncio.Piano, annuncio.Piano_palazzo,
|
|
|
|
|
annuncio.Unita_condominio, annuncio.Numero_vani, annuncio.Numero_camere, annuncio.Numero_bagni,
|
|
|
|
|
annuncio.Numero_balconi, annuncio.Numero_terrazzi, annuncio.Numero_box, annuncio.Numero_postiauto,
|
|
|
|
|
annuncio.Caratteristiche, annuncio.Accessori, annuncio.Titolo_it, annuncio.Desc_it, annuncio.Titolo_en,
|
|
|
|
|
annuncio.Desc_en, annuncio.Stato, annuncio.Web, annuncio.Homepage, annuncio.Url_foto,
|
2025-08-27 17:03:57 +02:00
|
|
|
annuncio.Url_video, annuncio.Codice, annuncio.Og_url, annuncio.Disponibile_da, annuncio.Permanenza, annuncio.Persone)
|
2025-08-22 10:11:07 +02:00
|
|
|
|
|
|
|
|
} else {
|
|
|
|
|
batch.Queue(`
|
|
|
|
|
INSERT INTO public.annunci(
|
|
|
|
|
locatore,
|
|
|
|
|
numero,
|
|
|
|
|
idlocatore,
|
|
|
|
|
email,
|
|
|
|
|
creato_il,
|
|
|
|
|
modificato_il,
|
|
|
|
|
indirizzo,
|
|
|
|
|
civico,
|
|
|
|
|
comune,
|
|
|
|
|
cap,
|
|
|
|
|
provincia,
|
|
|
|
|
regione,
|
|
|
|
|
lat,
|
|
|
|
|
lon,
|
|
|
|
|
indirizzo_secondario,
|
|
|
|
|
civico_secondario,
|
|
|
|
|
lat_secondario,
|
|
|
|
|
lon_secondario,
|
|
|
|
|
tipo,
|
|
|
|
|
categorie,
|
|
|
|
|
prezzo,
|
|
|
|
|
anno,
|
|
|
|
|
consegna,
|
|
|
|
|
classe,
|
|
|
|
|
mq,
|
|
|
|
|
piano,
|
|
|
|
|
piano_palazzo,
|
|
|
|
|
unita_condominio,
|
|
|
|
|
numero_vani,
|
|
|
|
|
numero_camere,
|
|
|
|
|
numero_bagni,
|
|
|
|
|
numero_balconi,
|
|
|
|
|
numero_terrazzi,
|
|
|
|
|
numero_box,
|
|
|
|
|
numero_postiauto,
|
|
|
|
|
caratteristiche,
|
|
|
|
|
accessori,
|
|
|
|
|
titolo_it,
|
|
|
|
|
desc_it,
|
|
|
|
|
titolo_en,
|
|
|
|
|
desc_en,
|
|
|
|
|
stato,
|
|
|
|
|
web,
|
|
|
|
|
homepage,
|
|
|
|
|
url_immagini,
|
|
|
|
|
url_video,
|
|
|
|
|
codice,
|
2025-08-27 17:03:57 +02:00
|
|
|
og_url,
|
|
|
|
|
disponibile_da,
|
|
|
|
|
permanenza,
|
|
|
|
|
persone
|
2025-08-22 10:11:07 +02:00
|
|
|
)
|
2025-08-27 17:03:57 +02:00
|
|
|
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, $27, $28, $29, $30, $31, $32, $33, $34, $35, $36, $37, $38, $39, $40, $41, $42, $43, $44, $45, $46, $47, $48, $49, $50, $51)
|
2025-08-22 10:11:07 +02:00
|
|
|
`, annuncio.Locatore, annuncio.Numero, annuncio.Idlocatore,
|
|
|
|
|
annuncio.Email, annuncio.Creato_il, annuncio.Modificato_il,
|
|
|
|
|
annuncio.Indirizzo, annuncio.Civico, annuncio.Comune, annuncio.Cap,
|
|
|
|
|
annuncio.Provincia, annuncio.Regione, annuncio.Lat, annuncio.Lon,
|
|
|
|
|
annuncio.Indirizzo_secondario, annuncio.Civico_secondario, annuncio.Lat_secondario,
|
|
|
|
|
annuncio.Lon_secondario, annuncio.Tipo, annuncio.Categorie, annuncio.Prezzo, annuncio.Anno,
|
|
|
|
|
annuncio.Consegna, annuncio.Classe, annuncio.Mq, annuncio.Piano, annuncio.Piano_palazzo,
|
|
|
|
|
annuncio.Unita_condominio, annuncio.Numero_vani, annuncio.Numero_camere, annuncio.Numero_bagni,
|
|
|
|
|
annuncio.Numero_balconi, annuncio.Numero_terrazzi, annuncio.Numero_box, annuncio.Numero_postiauto,
|
|
|
|
|
annuncio.Caratteristiche, annuncio.Accessori, annuncio.Titolo_it, annuncio.Desc_it, annuncio.Titolo_en,
|
|
|
|
|
annuncio.Desc_en, annuncio.Stato, annuncio.Web, annuncio.Homepage, annuncio.Url_foto,
|
2025-08-27 17:03:57 +02:00
|
|
|
annuncio.Url_video, annuncio.Codice, annuncio.Og_url, annuncio.Disponibile_da, annuncio.Permanenza, annuncio.Persone)
|
2025-08-22 10:11:07 +02:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
err = Db.Dbpool.SendBatch(Db.Ctx, batch).Close()
|
|
|
|
|
if err != nil {
|
|
|
|
|
return fmt.Errorf("failed to insert/update annunci batch: %w", err)
|
|
|
|
|
}
|
|
|
|
|
fmt.Println("Annunci insert/update done")
|
|
|
|
|
return nil
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
func clearRecords() {
|
|
|
|
|
|
|
|
|
|
//SET ALL WEB TO FALSE
|
|
|
|
|
_, err := Db.Dbpool.Exec(Db.Ctx, "DELETE FROM public.annunci")
|
|
|
|
|
if err != nil {
|
|
|
|
|
panic(err)
|
|
|
|
|
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2025-08-22 12:57:33 +02:00
|
|
|
func SetFromBkp(annunci typesdefs.AnnunciParsed) error {
|
2025-08-22 10:11:07 +02:00
|
|
|
clearRecords()
|
|
|
|
|
|
|
|
|
|
batch := &pgx.Batch{}
|
|
|
|
|
for _, annuncio := range annunci.Annuncio {
|
|
|
|
|
batch.Queue(`
|
|
|
|
|
INSERT INTO public.annunci(
|
|
|
|
|
locatore,
|
|
|
|
|
numero,
|
|
|
|
|
idlocatore,
|
|
|
|
|
email,
|
|
|
|
|
creato_il,
|
|
|
|
|
modificato_il,
|
|
|
|
|
indirizzo,
|
|
|
|
|
civico,
|
|
|
|
|
comune,
|
|
|
|
|
cap,
|
|
|
|
|
provincia,
|
|
|
|
|
regione,
|
|
|
|
|
lat,
|
|
|
|
|
lon,
|
|
|
|
|
indirizzo_secondario,
|
|
|
|
|
civico_secondario,
|
|
|
|
|
lat_secondario,
|
|
|
|
|
lon_secondario,
|
|
|
|
|
tipo,
|
|
|
|
|
categorie,
|
|
|
|
|
prezzo,
|
|
|
|
|
anno,
|
|
|
|
|
consegna,
|
|
|
|
|
classe,
|
|
|
|
|
mq,
|
|
|
|
|
piano,
|
|
|
|
|
piano_palazzo,
|
|
|
|
|
unita_condominio,
|
|
|
|
|
numero_vani,
|
|
|
|
|
numero_camere,
|
|
|
|
|
numero_bagni,
|
|
|
|
|
numero_balconi,
|
|
|
|
|
numero_terrazzi,
|
|
|
|
|
numero_box,
|
|
|
|
|
numero_postiauto,
|
|
|
|
|
caratteristiche,
|
|
|
|
|
accessori,
|
|
|
|
|
titolo_it,
|
|
|
|
|
desc_it,
|
|
|
|
|
titolo_en,
|
|
|
|
|
desc_en,
|
|
|
|
|
stato,
|
|
|
|
|
web,
|
|
|
|
|
homepage,
|
|
|
|
|
url_immagini,
|
|
|
|
|
url_video,
|
|
|
|
|
codice
|
|
|
|
|
)
|
|
|
|
|
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, $27, $28, $29, $30, $31, $32, $33, $34, $35, $36, $37, $38, $39, $40, $41, $42, $43, $44, $45, $46, $47)
|
|
|
|
|
`, annuncio.Locatore, annuncio.Numero, annuncio.Idlocatore,
|
|
|
|
|
annuncio.Email, annuncio.Creato_il, annuncio.Modificato_il,
|
|
|
|
|
annuncio.Indirizzo, annuncio.Civico, annuncio.Comune, annuncio.Cap,
|
|
|
|
|
annuncio.Provincia, annuncio.Regione, annuncio.Lat, annuncio.Lon,
|
|
|
|
|
annuncio.Indirizzo_secondario, annuncio.Civico_secondario, annuncio.Lat_secondario,
|
|
|
|
|
annuncio.Lon_secondario, annuncio.Tipo, annuncio.Categorie, annuncio.Prezzo, annuncio.Anno,
|
|
|
|
|
annuncio.Consegna, annuncio.Classe, annuncio.Mq, annuncio.Piano, annuncio.Piano_palazzo,
|
|
|
|
|
annuncio.Unita_condominio, annuncio.Numero_vani, annuncio.Numero_camere, annuncio.Numero_bagni,
|
|
|
|
|
annuncio.Numero_balconi, annuncio.Numero_terrazzi, annuncio.Numero_box, annuncio.Numero_postiauto,
|
|
|
|
|
annuncio.Caratteristiche, annuncio.Accessori, annuncio.Titolo_it, annuncio.Desc_it, annuncio.Titolo_en,
|
|
|
|
|
annuncio.Desc_en, annuncio.Stato, annuncio.Web, annuncio.Homepage, annuncio.Url_foto,
|
|
|
|
|
annuncio.Url_video, annuncio.Codice)
|
|
|
|
|
|
|
|
|
|
}
|
|
|
|
|
err := Db.Dbpool.SendBatch(Db.Ctx, batch).Close()
|
|
|
|
|
if err != nil {
|
|
|
|
|
return fmt.Errorf("failed to insert/update annunci batch: %w", err)
|
|
|
|
|
}
|
|
|
|
|
fmt.Println("Annunci insert/update done")
|
|
|
|
|
return nil
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
func AnnunciResetImages() error {
|
|
|
|
|
_, err := Db.Dbpool.Exec(Db.Ctx, "UPDATE public.annunci SET url_immagini = NULL, url_video = NULL")
|
|
|
|
|
if err != nil {
|
|
|
|
|
return fmt.Errorf("failed to reset images: %w", err)
|
|
|
|
|
}
|
|
|
|
|
return nil
|
|
|
|
|
}
|