Created
January 10, 2019 09:25
-
-
Save mikote2000/b286c3387126f27613fbe4d4ad2013d4 to your computer and use it in GitHub Desktop.
Ordenar líneas
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
public static List<facturas_lin> ordenarFilas(List<facturas_lin> dr) | |
{ | |
if (opc_orden_lineas == null) | |
{ | |
//opc_orden_lineas es un map (array_asociativo) del estilo: | |
//$_opc_orden_lineas[$_res['comienza_por']] = $_res['posicion']; | |
//Siendo $_res un resulset de esta consulta: 'select * from opc_orden_lineas' | |
opc_orden_lineas = Impextrom.cargadoresBBDD.LoadObjects.cargarOrdenLineas(); | |
} | |
//Aquí inicializo una matriz que tiene como indice 'posicion' y como valor una lista de artículos | |
Dictionary<int, IList<facturas_lin>> array_aux = new Dictionary<int, IList<facturas_lin>>(); | |
foreach (KeyValuePair<string, int> entry in opc_orden_lineas) | |
{ | |
array_aux[entry.Value] = new System.Collections.Generic.List<facturas_lin>(); | |
} | |
//Para los artículos no definidos en la tabla | |
array_aux[POSICION_MAXIMA] = new System.Collections.Generic.List<facturas_lin>(); | |
//Comenzamos el ordenamiento | |
foreach (facturas_lin linea in dr) | |
{ | |
//Simplemente devuelve el valor posición de opc_orden_lineas que le corresponde a la pos_almacen del artículo | |
int n = getNumArray(linea.pos_almacen); | |
if (array_aux[n].Count > 0) | |
{ | |
if(linea.pos_almacen != null){ | |
string[] posiciones_linea = linea.pos_almacen.Split('-'); | |
if (posiciones_linea.Length >= 3) | |
{ | |
//Calculamos el desplazamiento dentro del pasillo | |
try | |
{ | |
int pos_dentro_hilera = int.Parse(posiciones_linea[2]); | |
bool encontrado = false; | |
int contador = 0; | |
//Recorremos para añadir en donde corresponda dentro del listado del índice del MAP. Es decir dentro de la lista que le corresponda, lo posicionamos en orden menor a mayor | |
while (!encontrado && array_aux[n].Count > contador) | |
{ | |
facturas_lin aux_lin = array_aux[n][contador]; | |
//Si tiene posición | |
if(aux_lin.pos_almacen != null){ | |
string[] aux_posiciones_linea = aux_lin.pos_almacen.Split('-'); | |
//Igualamos aux y comprobamos cuál es mayor | |
if (aux_posiciones_linea.Length >= 3) | |
{ | |
try | |
{ | |
int aux_pos_dentro_hilera = int.Parse(aux_posiciones_linea[2]); | |
//Si es mayor, entonces lo tenemos que poner justo antes | |
if (aux_pos_dentro_hilera >= pos_dentro_hilera) | |
{ | |
array_aux[n].Insert(contador, linea); | |
encontrado = true; | |
} | |
} | |
catch (FormatException) | |
{ | |
array_aux[n].Insert(contador, linea); | |
encontrado = true; | |
} | |
} | |
contador++; | |
} | |
//Si el elemento que recorremos no tiene posición, añadimos antes que éste | |
else{ | |
array_aux[n].Insert(contador, linea); | |
encontrado = true; | |
} | |
} | |
if (!encontrado) | |
{ | |
array_aux[n].Add(linea); | |
} | |
} | |
catch (FormatException) | |
{ | |
array_aux[n].Add(linea); | |
} | |
} | |
else | |
{ | |
array_aux[n].Add(linea); | |
} | |
} | |
else{ | |
array_aux[n].Add(linea); | |
} | |
} | |
else | |
{ | |
array_aux[n].Add(linea); | |
} | |
} | |
//Ahora es cuando ordenamos dentro de la lógica de pasillos. Es decir en el primer pasillo voy de menor a mayor, en segundo de mayor a menor, en tercero de menor a mayor, etc. | |
List<facturas_lin> final = new List<facturas_lin>(); | |
bool menor_a_mayor = true; | |
foreach (KeyValuePair<int, IList<facturas_lin>> entry in array_aux) | |
{ | |
IList<facturas_lin> listado = entry.Value; | |
if (listado.Count > 0) | |
{ | |
if(menor_a_mayor){ | |
foreach(facturas_lin fl in listado){ | |
final.Add(fl); | |
} | |
} | |
else{ | |
for(int i = listado.Count - 1; i >= 0 ; i--){ | |
final.Add(listado[i]); | |
} | |
} | |
menor_a_mayor = !menor_a_mayor; | |
} | |
} | |
return final; | |
} | |
private static int getNumArray(string posicion) | |
{ | |
int tamano = (posicion.Length > 8) ? 8 : posicion.Length; | |
while (tamano >= 1) | |
{ | |
string a_buscar = posicion.Substring(0, tamano); | |
foreach (KeyValuePair<string, int> entry in opc_orden_lineas) | |
{ | |
if (a_buscar.Equals(entry.Key, StringComparison.CurrentCultureIgnoreCase)) | |
{ | |
return entry.Value; | |
} | |
} | |
tamano--; | |
} | |
return POSICION_MAXIMA; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment