infoalloggi-monorepo/apps/backend/miogest_parsing.go
2026-05-22 15:49:28 +02:00

182 lines
5 KiB
Go

package main
import (
"backend/parser"
"backend/typesdefs"
"backend/utils"
"fmt"
"log"
"reflect"
"regexp"
"slices"
"strconv"
"strings"
)
var re = regexp.MustCompile(`[^+\d]`)
func boolUpd(b bool) *bool {
return &b
}
func strUpd(s string) *string {
return &s
}
func flatClienteTipologia(t []string) string {
if len(t) == 0 {
return ""
}
if slices.Contains(t, "Proprietario") {
return "proprietario"
} else if slices.Contains(t, "Delegato") {
return "delegato"
} else if slices.Contains(t, "Cliente") {
return "inquilino"
}
return ""
}
func tipologiaPriority(tipologie []string) int {
if slices.Contains(tipologie, "Delegato") {
return 3
}
if slices.Contains(tipologie, "Cliente") {
return 2
}
if slices.Contains(tipologie, "Proprietario") {
return 1
}
return 0
}
func selectTargetCliente(annuncio *typesdefs.AnnuncioXML) (int, bool) {
if len(annuncio.Clienti) == 0 {
return 0, false
}
best := 0
bestPriority := tipologiaPriority(annuncio.Clienti[0].Tipologie)
for i := 1; i < len(annuncio.Clienti); i++ {
if p := tipologiaPriority(annuncio.Clienti[i].Tipologie); p > bestPriority {
bestPriority = p
best = i
}
}
return best, true
}
func processLocatore(tmp *typesdefs.AnnuncioParsed, annuncio *typesdefs.AnnuncioXML) {
idx, ok := selectTargetCliente(annuncio)
if !ok {
log.Printf("Annuncio %s has no client information", utils.GetStringValue(annuncio.Codice))
return
}
trg := annuncio.Clienti[idx]
tmp.Email = parser.ParseEmail(trg.Email1)
var number string
for _, tel := range []*string{trg.Tel1, trg.Tel2, trg.Tel3} {
if tel != nil {
cleanedTel := re.ReplaceAllString(*tel, "")
if cleanedTel != "" {
number = cleanedTel
break
}
}
}
tmp.Numero = &number
if trg.Cognome != nil && trg.Nome != nil {
rawLoc := fmt.Sprintf("%s %s", utils.MakeUppercase(*trg.Cognome), utils.MakeUppercase(*trg.Nome))
rawLoc = strings.TrimSpace(rawLoc)
rawLoc = regexp.MustCompile(`\s+`).ReplaceAllString(rawLoc, " ") // Replace multiple spaces with a single space
tmp.Locatore = strUpd(rawLoc)
}
tmp.Idlocatore = trg.Id
tmp.Tipo_locatore = strUpd(flatClienteTipologia(trg.Tipologie))
}
func processIndirizzo(tmp *typesdefs.AnnuncioParsed, annuncio *typesdefs.AnnuncioXML) {
tmp.Indirizzo = strUpd(utils.MakeUppercase(utils.GetStringValue(annuncio.Indirizzo)))
tmp.Civico = annuncio.Civico
tmp.Comune = strUpd(utils.MakeUppercase(utils.GetStringValue(annuncio.Comune)))
tmp.Cap = annuncio.Cap
tmp.Provincia = annuncio.Provincia
tmp.Regione = annuncio.Regione
tmp.Lat = annuncio.Latitudine
tmp.Lon = annuncio.Longitudine
tmp.Indirizzo_secondario = strUpd(utils.MakeUppercase(utils.GetStringValue(annuncio.IndirizzoSecondario)))
tmp.Civico_secondario = annuncio.CivicoSecondario
tmp.Lat_secondario = annuncio.LatitudineSecondario
tmp.Lon_secondario = annuncio.LongitudineSecondario
}
func processCategorie(tmp *typesdefs.AnnuncioParsed, annuncio *typesdefs.AnnuncioXML, def_cats *[]typesdefs.Categoria) {
if annuncio.Categoria == nil {
return
}
resultArray := make([]string, 0)
cats := *annuncio.Categoria
for _, cat := range cats {
for _, catdef := range *def_cats {
if cat == catdef.Id {
resultArray = append(resultArray, catdef.Nome)
}
}
}
if tmp.Categorie == nil {
tmp.Categorie = &[]string{}
}
*tmp.Categorie = resultArray
}
func processDatiImmobile(tmp *typesdefs.AnnuncioParsed, annuncio *typesdefs.AnnuncioXML) {
tmpanno := utils.GetStringValue(annuncio.Anno)
if tmpanno != "0" && tmpanno != "" {
tmp.Anno = &tmpanno
}
tmp.Prezzo = parser.ParsePrezzo(annuncio.Prezzo)
tmp.Classe = annuncio.Classe
if annuncio.Mq != nil {
replaced := strings.ReplaceAll(*annuncio.Mq, ",", ".")
if s, err := strconv.ParseFloat(replaced, 64); err == nil {
tmp.Mq = &s
}
}
tmp.Piano = annuncio.Piano
tmp.Piano_palazzo = annuncio.PianiCondominio
tmp.Unita_condominio = annuncio.UnitaCondominio
tmp.Numero_vani = annuncio.Vani
tmp.Numero_camere = annuncio.Camere
tmp.Numero_bagni = annuncio.Bagni
tmp.Numero_balconi = annuncio.Balconi
tmp.Numero_terrazzi = annuncio.Terrazzi
tmp.Numero_box = annuncio.Box
tmp.Numero_postiauto = annuncio.PostiAuto
}
func parseCaratteristiche(wrkrecord *typesdefs.AnnuncioXML, caratt *typesdefs.ListaCaratteristiche) map[string]any {
if reflect.TypeFor[*typesdefs.AnnuncioXML]().Kind() != reflect.Pointer || reflect.ValueOf(wrkrecord).Elem().Kind() != reflect.Struct {
return nil
}
tmpCaratteristiche := make(map[string]any)
val := reflect.ValueOf(wrkrecord).Elem()
numField := val.NumField()
for i := range numField {
if strings.HasPrefix(val.Type().Field(i).Name, "Scheda_") {
nameField := val.Type().Field(i).Name
valueField := val.Field(i)
if valueField.IsValid() && !valueField.IsNil() {
tmpCaratteristiche[nameField] = valueField.Interface()
} else {
tmpCaratteristiche[nameField] = nil
}
}
}
for _, scheda := range caratt.Caratteristiche {
if _, ok := tmpCaratteristiche[scheda.Tagxml]; !ok {
tmpCaratteristiche[scheda.Tagxml] = nil
}
}
return tmpCaratteristiche
}