/// <summary> /// Executes the queue. /// </summary> /// <param name="filePath">The file path.</param> private void ExecuteQueue(string filePath) { Logger.Info("Verificando si existen procesos en cola..."); while (Lst_Error_Invoice.Count > 0) { try { var item = Lst_Error_Invoice.Peek(); using (StreamWriter oSW = File.AppendText(filePath)) { oSW.AutoFlush = true; var message = GeneratFilesbyInvoice(item); if (message.Result != null) { oSW.Write(message?.Result); oSW.Flush(); } } Logger.Info($"Factura: {item}, #Proceso: en cola."); } catch (Exception ex) { Logger.Error($"Error en proceso en cola {ex.Message}"); } finally { Lst_Error_Invoice.Dequeue(); } } }
/// <summary> /// Generats the filesby invoice. /// </summary> /// <param name="PK_Invoice">The pk invoice.</param> /// <returns></returns> private async Task <KeyValuePair <string, string> > GeneratFilesbyInvoice(string PK_Invoice) { StringBuilder sbFile = new StringBuilder(); string carrierCode = string.Empty; try { await FillDataSetAsync(ConfigurationManager.ConnectionStrings["DefaultConnection"].ConnectionString, PK_Invoice).ContinueWith(task => { DataSet ds1 = task.Result; carrierCode = ds1.Tables[0].Rows[0]["PNR"].ToString(); for (int table = 0; table <= ds1.Tables.Count - 1; table++) { foreach (DataRow row in ds1.Tables[table].Rows) { if (table == 2) { var rowAsString1 = string.Join("|", row.ItemArray.Take(11).ToArray()) + "\r\n"; sbFile.Append(rowAsString1); if (row["FlgIMPLFAC"].ToString() == "1") { var rowAsString2 = string.Join("|", row.ItemArray.Select(x => x.ToString().Trim()).Skip(12).Take(9)) + "\r\n"; sbFile.Append(rowAsString2); } else if (row["FlagTP"].ToString() == "1") { var rowAsString3 = string.Join("|", row.ItemArray.Select(x => x.ToString().Trim()).Skip(22).Take(14)) + "\r\n"; sbFile.Append(rowAsString3); } } else { var rowAsString = string.Join("|", row.ItemArray) + "\r\n"; sbFile.Append(rowAsString); } } } }); return(new KeyValuePair <string, string>(carrierCode, sbFile.ToString())); //var ds1 = await FillDataSetAsync(ConfigurationManager.ConnectionStrings["DefaultConnection"].ConnectionString, PK_Invoice); //var ds1 = FillDataSet(ConfigurationManager.ConnectionStrings["DefaultConnection"].ConnectionString, PK_Invoice); } catch (Exception ex) { if (ex is SqlException) { Lst_Error_Invoice.Enqueue(PK_Invoice.Trim()); } Logger.Info($"Error en Factura: {PK_Invoice}, Error: {ex.Message}"); return(new KeyValuePair <string, string>(carrierCode, string.Empty)); } }
/// <summary> /// Executes the queue. /// </summary> /// <param name="filePath">The file path.</param> private void ExecuteQueue(string filePath, int countFile) { Logger.Info("Verificando si existen procesos en cola..."); while (Lst_Error_Invoice.Count > 0) { try { var item = Lst_Error_Invoice.Peek(); var message = GeneratFilesbyInvoice(item); var fileName = filePath; if (message.Result.Value != null) { fileName = filePath.Replace("PNR", message?.Result.Key); fileName = filePath.Replace("##", countFile++.ToString()); FileStream fs = new FileStream(fileName, FileMode.Create); using (StreamWriter oSW = new StreamWriter(fs, Encoding.UTF8, 160012)) { oSW.AutoFlush = true; oSW.Write(message?.Result); oSW.Flush(); } if (fs != null) { fs.Dispose(); } } Logger.Info($"Factura: {item}, #Proceso: en cola."); } catch (Exception ex) { Logger.Error($"Error en proceso en cola {ex.Message}"); } finally { Lst_Error_Invoice.Dequeue(); } } }