Agregar archivos de proyecto.
This commit is contained in:
252
bdGrupoSanchoToro/extensiones/municipios.cs
Normal file
252
bdGrupoSanchoToro/extensiones/municipios.cs
Normal file
@@ -0,0 +1,252 @@
|
||||
using Microsoft.EntityFrameworkCore;
|
||||
using Microsoft.VisualBasic;
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Data;
|
||||
using System.Linq;
|
||||
using tsUtilidades.Extensiones;
|
||||
|
||||
namespace bdGrupoSanchoToro.db
|
||||
{
|
||||
|
||||
|
||||
public partial class municipios
|
||||
{
|
||||
public provincias? provincias
|
||||
{
|
||||
get
|
||||
{
|
||||
return this.CodigoProvinciaNavigation;
|
||||
}
|
||||
}
|
||||
|
||||
public static List<municipios> ListaPoblaciones { get; set; }
|
||||
public string PoblacionYProvincia
|
||||
{
|
||||
get
|
||||
{
|
||||
try
|
||||
{
|
||||
if ((this.Nombre ?? "") == (this.provincias.Nombre ?? ""))
|
||||
{
|
||||
return this.Nombre;
|
||||
}
|
||||
else
|
||||
{
|
||||
return this.Nombre + " (" + this.provincias.Nombre + ")";
|
||||
}
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
return "";
|
||||
}
|
||||
}
|
||||
}
|
||||
public static string ObtienePoblacion(string CodigoPoblacion)
|
||||
{
|
||||
if (ListaPoblaciones is null)
|
||||
{
|
||||
tscGrupoSanchoToro bd = tscGrupoSanchoToro.NuevoContexto();
|
||||
ListaPoblaciones = bd.municipios.Include(x=> x.CodigoProvinciaNavigation).ToList();
|
||||
}
|
||||
if (ListaPoblaciones.Any(x => (x.CodigoMunicipio ?? "") == (CodigoPoblacion ?? "")))
|
||||
{
|
||||
return ListaPoblaciones.First(x => (x.CodigoMunicipio ?? "") == (CodigoPoblacion ?? "")).Nombre;
|
||||
}
|
||||
else
|
||||
{
|
||||
return "";
|
||||
}
|
||||
}
|
||||
public static string ObtieneProvincia(string CodigoPoblacion)
|
||||
{
|
||||
if (ListaPoblaciones is null)
|
||||
{
|
||||
tscGrupoSanchoToro bd = tscGrupoSanchoToro.NuevoContexto();
|
||||
ListaPoblaciones = bd.municipios.Include(x => x.CodigoProvinciaNavigation).ToList();
|
||||
}
|
||||
if (ListaPoblaciones.Any(x => (x.CodigoMunicipio ?? "") == (CodigoPoblacion ?? "")))
|
||||
{
|
||||
return ListaPoblaciones.First(x => (x.CodigoMunicipio ?? "") == (CodigoPoblacion ?? "")).provincias.Nombre;
|
||||
}
|
||||
else
|
||||
{
|
||||
return "";
|
||||
}
|
||||
}
|
||||
public static List<municipios> ObtieneListaPoblaciones()
|
||||
{
|
||||
if (ListaPoblaciones is null)
|
||||
{
|
||||
tscGrupoSanchoToro bd = tscGrupoSanchoToro.NuevoContexto();
|
||||
ListaPoblaciones = bd.municipios.Include(x => x.CodigoProvinciaNavigation).OrderBy(x => x.Nombre).ToList();
|
||||
}
|
||||
return ListaPoblaciones;
|
||||
}
|
||||
|
||||
public static string ObtieneCodigoMunicipioMasCoincidente( tscGrupoSanchoToro bd, string CodigoPostal, string Poblacion)
|
||||
{
|
||||
try
|
||||
{
|
||||
if (Poblacion.NothingAVacio() == "")
|
||||
return "";
|
||||
else
|
||||
{
|
||||
var municipios = bd.codigospostales.Where(x => x.CodigoPostal == CodigoPostal & x.CodigoMunicipioNavigation.Nombre == Poblacion).ToList();
|
||||
if (municipios.Count == 1)
|
||||
return municipios[0].CodigoMunicipio;
|
||||
else if (CodigoPostal.Length == 5)
|
||||
{
|
||||
string codigoprovincia = CodigoPostal.Substring(0, 2);
|
||||
municipios = bd.municipios.Where(x => x.CodigoProvincia == codigoprovincia).SelectMany(x => x.codigospostales).ToList();
|
||||
int IndiceMasCoincidente = 0;
|
||||
int Puntuacion = 0;
|
||||
if (municipios.Count == 0)
|
||||
return "";
|
||||
else
|
||||
for (var i = 0; i <= municipios.Count - 1; i++)
|
||||
{
|
||||
// Dim Porcentaje As Double = 0
|
||||
int puntuaciontmp = ComparaPalabras(municipios[i].CodigoMunicipioNavigation.Nombre, Poblacion);
|
||||
if (i == 0 || Puntuacion < puntuaciontmp)
|
||||
{
|
||||
IndiceMasCoincidente = i;
|
||||
Puntuacion = puntuaciontmp;
|
||||
}
|
||||
}
|
||||
if (Puntuacion == 100)
|
||||
{
|
||||
CrearNuevoCodigoPostal(CodigoPostal, municipios[IndiceMasCoincidente].CodigoMunicipio);
|
||||
return municipios[IndiceMasCoincidente].CodigoMunicipio;
|
||||
}
|
||||
else
|
||||
return CrearNuevoMunicipio(CodigoPostal, Poblacion);
|
||||
}
|
||||
else
|
||||
return "";
|
||||
}
|
||||
}
|
||||
catch (Exception EX)
|
||||
{
|
||||
throw new Exception(EX.Message, EX);
|
||||
}
|
||||
}
|
||||
public static string ObtieneCodigoMunicipioMasCoincidente(tscGrupoSanchoToro bd,List<codigospostales> CodigosPostales, List<municipios> LMunicipios, string CodigoPostal, string Poblacion)
|
||||
{
|
||||
try
|
||||
{
|
||||
List<codigospostales> municipios = bd.codigospostales.Where(x => x.CodigoPostal == CodigoPostal).ToList();
|
||||
if (municipios.Count == 1)
|
||||
return municipios[0].CodigoMunicipio;
|
||||
else
|
||||
{
|
||||
if (municipios.Count == 0 && CodigoPostal.Length == 5)
|
||||
{
|
||||
var codigoprovincia = CodigoPostal.Substring(0, 2);
|
||||
municipios = LMunicipios.Where(x => x.CodigoProvincia == codigoprovincia).SelectMany(x => x.codigospostales).ToList();
|
||||
}
|
||||
if (bd.codigospostales.Any(x => x.CodigoPostal == CodigoPostal & x.CodigoMunicipioNavigation.Nombre == Poblacion))
|
||||
return CodigosPostales.First(x => x.CodigoPostal == CodigoPostal & x.CodigoMunicipioNavigation.Nombre == Poblacion).CodigoMunicipio;
|
||||
else
|
||||
{
|
||||
int IndiceMasCoincidente = 0;
|
||||
int Puntuacion = 0;
|
||||
if (municipios.Count == 0)
|
||||
return "";
|
||||
else
|
||||
for (var i = 0; i <= municipios.Count - 1; i++)
|
||||
{
|
||||
var puntuaciontmp = ComparaPalabras(municipios[i].CodigoMunicipioNavigation.Nombre, Poblacion);
|
||||
if (i == 0 || Puntuacion < puntuaciontmp)
|
||||
{
|
||||
IndiceMasCoincidente = i;
|
||||
Puntuacion = puntuaciontmp;
|
||||
}
|
||||
}
|
||||
if (Puntuacion > 49)
|
||||
return municipios[IndiceMasCoincidente].CodigoMunicipio;
|
||||
else
|
||||
return CrearNuevoMunicipio(CodigoPostal, Poblacion);
|
||||
}
|
||||
}
|
||||
}
|
||||
catch (Exception EX)
|
||||
{
|
||||
throw new Exception(EX.Message, EX);
|
||||
}
|
||||
}
|
||||
private static string CrearNuevoMunicipio(string CodigoPostal, string Municipio)
|
||||
{
|
||||
if (Municipio.NothingAVacio() == "")
|
||||
return "";
|
||||
else
|
||||
{
|
||||
tscGrupoSanchoToro bd = tscGrupoSanchoToro.NuevoContexto();
|
||||
// municipios nm = new municipios();
|
||||
string codpro = CodigoPostal.Substring(0, 2);
|
||||
municipios ultmun = bd.municipios.Where(x => x.CodigoProvincia == codpro).OrderByDescending(x => x.CodigoMunicipio).First();
|
||||
char Letra;
|
||||
int Numero = 0;
|
||||
if ("0123456789".Contains(ultmun.CodigoMunicipio.Substring(2, 1)))
|
||||
{
|
||||
Letra = 'A';
|
||||
Numero = 0;
|
||||
}
|
||||
else
|
||||
{
|
||||
Letra = char.Parse(ultmun.CodigoMunicipio.Substring(2, 1));
|
||||
Numero = int.Parse(ultmun.CodigoMunicipio.Substring(3)) + 1;
|
||||
if (Numero > 99)
|
||||
{
|
||||
Letra = Strings.ChrW(Strings.AscW(Letra) + 1);
|
||||
Numero = 0;
|
||||
}
|
||||
}
|
||||
string Nombre = Municipio;
|
||||
string CodigoMunicipio = codpro + Letra + Numero.ToString().PadLeft(2, char.Parse("0"));
|
||||
int rmun = bd.Database.ExecuteSqlRaw("INSERT INTO municipios (`CodigoMunicipio`, `CodigoProvincia`, `Nombre`) VALUES ('" + codpro + "','" + CodigoMunicipio + "','" + Nombre + "')");
|
||||
int rcp = bd.Database.ExecuteSqlRaw("INSERT INTO codigospostales (`CodigoMunicipio`, `CodigoPostal`) VALUES ('" + CodigoMunicipio + "','" + CodigoPostal +"')");
|
||||
return CodigoMunicipio;
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
private static void CrearNuevoCodigoPostal(string CodigoPostal, string CodigoMunicipio)
|
||||
{
|
||||
tscGrupoSanchoToro bd = tscGrupoSanchoToro.NuevoContexto();
|
||||
if (bd.codigospostales.Any(x => x.CodigoMunicipio == CodigoMunicipio & x.CodigoPostal == CodigoPostal) == false)
|
||||
{
|
||||
int rcp = bd.Database.ExecuteSqlRaw("INSERT INTO codigospostales (`CodigoMunicipio`, `CodigoPostal`, `Nombre`) VALUES ('" + CodigoMunicipio + "','" + CodigoPostal + "')");
|
||||
}
|
||||
}
|
||||
|
||||
private static int ComparaPalabras(string Cadena1, string Cadena2)
|
||||
{
|
||||
try
|
||||
{
|
||||
Cadena1 = Cadena1.ToLower().Replace("á", "a").Replace("é", "e").Replace("í", "i").Replace("ó", "o").Replace("ú", "u").Replace("Ñ", "ñ").Replace("(", " ").Replace(")", " ").Replace("\"", "").Replace("-", " ").Replace(",",string.Empty).Trim();
|
||||
Cadena2 = Cadena2.ToLower().Replace("á", "a").Replace("é", "e").Replace("í", "i").Replace("ó", "o").Replace("ú", "u").Replace("Ñ", "ñ").Replace("(", " ").Replace(")", " ").Replace("\"", "").Replace("-", " ").Replace(",", string.Empty).Trim();
|
||||
var Cadenas1 = Cadena1.Split(" ").Where(X => X != "");
|
||||
var Cadenas2 = Cadena2.Split(" ").Where(X => X != "");
|
||||
int Coincidente = 0;
|
||||
foreach (var c in Cadenas1)
|
||||
{
|
||||
if (Cadenas2.Contains(c))
|
||||
Coincidente += 1;
|
||||
}
|
||||
foreach (var c in Cadenas2)
|
||||
{
|
||||
if (Cadenas1.Contains(c))
|
||||
Coincidente += 1;
|
||||
}
|
||||
return (Coincidente * 100 / (int)(Cadenas1.Count() + Cadenas2.Count()));
|
||||
}
|
||||
// Return Coincidente
|
||||
catch (Exception ex)
|
||||
{
|
||||
throw new Exception(ex.Message, ex);
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user