infoalloggi-monorepo/apps/backend/from_bkp.go

278 lines
6.6 KiB
Go

package main
import (
"backend/parser"
"backend/typesdefs"
"backend/utils"
"encoding/xml"
"fmt"
"io"
"os"
"slices"
"strconv"
"strings"
"sync"
"time"
)
func From_Backup() typesdefs.XmlBkp {
//open the bkp.xml file
xmlFile, err := os.Open("bkp.xml")
if err != nil {
fmt.Println(err)
}
defer xmlFile.Close()
byteValue, err := io.ReadAll(xmlFile)
if err != nil {
fmt.Println(err)
}
var miogest typesdefs.XmlBkp
xml.Unmarshal(byteValue, &miogest)
return miogest
}
func ParseUpdateBkp(m *MiogestHandler) typesdefs.AnnunciParsed {
miogest := From_Backup()
var AnnunciArray typesdefs.AnnunciParsed
extractedData := make(chan typesdefs.AnnuncioParsed)
defaultCaratteristiche := m.GetCaratteristiche()
defaultCategorie := m.GetCategorie()
var wg sync.WaitGroup
wg.Add(len(miogest.Annunci.Record))
for i := 0; i < len(miogest.Annunci.Record); i++ {
go func(i int) {
defer wg.Done()
extractData_bkp(miogest.Annunci.Record[i], extractedData, defaultCaratteristiche, defaultCategorie)
}(i)
}
go func() {
wg.Wait()
close(extractedData)
}()
for result := range extractedData {
AnnunciArray.Annuncio = append(AnnunciArray.Annuncio, result)
}
return AnnunciArray
}
func extractData_bkp(annuncio typesdefs.AnnuncioBKP, extractedData chan typesdefs.AnnuncioParsed, defaultCaratteristiche typesdefs.ListaCaratteristiche, defaultCategorie []typesdefs.Categoria) {
var tmp typesdefs.AnnuncioParsed = typesdefs.AnnuncioParsed{}
codice := utils.RemoveWhitespace(utils.GetStringValue(annuncio.Codice))
tmp.Codice = codice
tmp.Email = nil
if annuncio.Proprietario != nil && annuncio.Proprietario.Cliente != nil && annuncio.Proprietario.Cliente.Nome != nil {
tmp.Locatore = annuncio.Proprietario.Cliente.Nome
}
tmp.Numero = nil
if annuncio.Proprietario != nil && annuncio.Proprietario.Cliente != nil && annuncio.Proprietario.Cliente.Nome != nil {
tmp.Idlocatore = annuncio.Proprietario.Cliente.ID
}
upperIndirizzo := utils.MakeUppercase(utils.GetStringValue(annuncio.Indirizzo))
tmp.Indirizzo = &upperIndirizzo
tmp.Civico = annuncio.Civico
upperComune := utils.MakeUppercase(utils.GetStringValue(annuncio.Comune))
tmp.Comune = &upperComune
tmp.Cap = annuncio.Cap
tmp.Provincia = annuncio.Provincia
tmp.Regione = annuncio.Regione
tmp.Lat = utils.CommaToDot(annuncio.Lat)
tmp.Lon = utils.CommaToDot(annuncio.Lon)
tmp.Indirizzo_secondario = nil
tmp.Civico_secondario = nil
tmp.Lat_secondario = nil
tmp.Lon_secondario = nil
var tipo = tipologiaParser(*annuncio.Tipo)
tmp.Tipo = &tipo
tmpanno := utils.GetStringValue(annuncio.Anno)
if tmpanno != "0" && tmpanno != "" {
tmp.Anno = &tmpanno
}
tmp.Prezzo = parser.ParsePrezzo(annuncio.Prezzo)
tmp.Consegna = parser.ParseConsegna(annuncio.Consegna)
tmp.Classe = annuncio.Classe
if annuncio.Categorie != nil && annuncio.Categorie.Categoria != nil {
resultArray := make([]string, 0)
cats := *annuncio.Categorie.Categoria
for _, cat := range cats {
for _, catdef := range defaultCategorie {
if cat.ID == catdef.Id {
resultArray = append(resultArray, catdef.Nome)
}
}
}
if tmp.Categorie == nil {
tmp.Categorie = &[]string{}
}
*tmp.Categorie = resultArray
}
if annuncio.Mq != nil {
replaced := strings.ReplaceAll(*annuncio.Mq, ",", ".")
if s, err := strconv.ParseFloat(replaced, 64); err == nil {
fmt.Printf("%T, %v\n", s, s)
tmp.Mq = &s
}
}
tmp.Piano = annuncio.Piano
tmp.Piano_palazzo = annuncio.PianoPalazzo
tmp.Unita_condominio = annuncio.UnitaImmobiliari
tmp.Numero_vani = annuncio.NumeroVani
tmp.Numero_camere = annuncio.NumeroCamere
tmp.Numero_bagni = annuncio.NumeroBagni
tmp.Numero_balconi = annuncio.NumeroBalconi
tmp.Numero_terrazzi = annuncio.NumeroTerrazzi
tmp.Numero_box = annuncio.NumeroBox
tmp.Numero_postiauto = annuncio.NumeroPostiauto
tmpstato := utils.GetStringValue(annuncio.Stato)
var tmpstatoString string
if tmpstato == "1" {
tmpstatoString = "Attivo"
}
if tmpstato == "2" {
tmpstatoString = "Trattativa"
} else {
tmpstatoString = "Sospeso"
}
tmp.Stato = &tmpstatoString
home := false
tmp.Homepage = &home
layout := "20060102150405"
if annuncio.Inserito != nil {
t, err := time.Parse(layout, *annuncio.Inserito)
if err != nil {
tmp.Creato_il = nil
} else {
tmp.Creato_il = &t
}
}
tmp.Modificato_il = nil
var itlingua string = "it"
var enlingua string = "en"
var tmpTitoloIt *string = nil
var tmpTitoloEn *string = nil
var tmpDescIt *string = nil
var tmpDescEn *string = nil
if annuncio.Descrizioni != nil {
for _, desc := range *annuncio.Descrizioni.Descrizione {
if desc.Lingua != nil {
if *desc.Lingua == itlingua {
tmpTitoloIt = desc.Titolo
tmpDescIt = desc.Testo
}
if *desc.Lingua == enlingua {
tmpTitoloEn = desc.Titolo
tmpDescEn = desc.Testo
}
}
}
}
tmp.Titolo_it = tmpTitoloIt
tmp.Titolo_en = tmpTitoloEn
tmp.Desc_it = tmpDescIt
tmp.Desc_en = tmpDescEn
w := false
tmp.Web = &w
tmp.External_videos = nil
if annuncio.Accessori != nil && annuncio.Accessori.Accessorio != nil {
tmpAcc := make([]string, 0)
for _, acc := range *annuncio.Accessori.Accessorio {
if acc.Nome != nil {
tmpAcc = append(tmpAcc, *acc.Nome)
}
}
if tmp.Accessori == nil {
tmp.Accessori = &[]string{}
}
*tmp.Accessori = tmpAcc
}
tmpCaratteristiche := make(map[string]interface{})
if annuncio.Schede != nil && annuncio.Schede.Scheda != nil {
for _, scheda := range *annuncio.Schede.Scheda {
if scheda.Valore != nil {
var cossriponding_name *string = nil
for _, def := range defaultCaratteristiche.Caratteristiche {
if scheda.ID == def.Id {
cossriponding_name = &def.Tagxml
}
}
if cossriponding_name != nil {
tmpCaratteristiche[*cossriponding_name] = *scheda.Valore
}
}
}
}
for _, def := range defaultCaratteristiche.Caratteristiche {
if _, ok := tmpCaratteristiche[def.Tagxml]; !ok {
tmpCaratteristiche[def.Tagxml] = nil
}
}
tmp.Caratteristiche = &tmpCaratteristiche
tmp.External_videos = nil
extractedData <- tmp
}
func TestTipologieBkp() {
bkp := From_Backup()
var tipologie []string
for _, r := range bkp.Annunci.Record {
if r.Tipo != nil {
if !slices.Contains(tipologie, *r.Tipo) {
tipologie = append(tipologie, *r.Tipo)
}
}
}
fmt.Println(strings.Join(tipologie, ","))
}
func tipologiaParser(tipo string) string {
var result string
switch tipo {
case "Affitto Residenziale", "Affitto Turistico":
result = "Transitorio"
case "Vendita Residenziale", "Vendita Commerciale":
result = "Vendita"
case "Affitto Commerciale", "Affitto":
result = "Stabile"
case "Cessione Commerciale", "Cessione":
result = "Cessione"
default:
result = "Errore"
}
return result
}