コード例 #1
0
        private async Task <int> ProcessFileDataAsync(int fileId, byte[] inputStream)
        {
            try
            {
                var watch          = Stopwatch.StartNew();
                var processedCount = 0;

                using (var streamReader = new StreamReader(new MemoryStream(inputStream)))
                {
                    var headerLine        = streamReader.ReadLine();
                    var processedValues   = new StringBuilder();
                    var unProcessedValues = new StringBuilder();
                    var parsedCount       = 1;
                    var unProcessedCount  = 0;

                    string currentLine;
                    while ((currentLine = streamReader.ReadLine()) != null)
                    {
                        var lineItems = currentLine.Split('|');

                        if (lineItems.Count() != 4)
                        {
                            unProcessedValues.Append("('");
                            unProcessedValues.Append(fileId.ToString());
                            unProcessedValues.Append("', '");
                            unProcessedValues.Append(currentLine);
                            unProcessedValues.Append("'),");

                            unProcessedCount++;
                        }
                        else
                        {
                            decimal amount;
                            var     validAmount = decimal.TryParse(lineItems[3], out amount);

                            if (!string.IsNullOrWhiteSpace(lineItems[0]) &&
                                !string.IsNullOrWhiteSpace(lineItems[1]) &&
                                _currencyCodeValidator.IsValidCurrencyCode(lineItems[2]) &&
                                validAmount)
                            {
                                processedValues.Append("('");
                                processedValues.Append(fileId.ToString());
                                processedValues.Append("', '");
                                processedValues.Append(lineItems[0]); // Account
                                processedValues.Append("', '");
                                processedValues.Append(lineItems[1]); // Description
                                processedValues.Append("', '");
                                processedValues.Append(_currencyCodeValidator.ToCurrencyCodeEnumValue(lineItems[2]));
                                processedValues.Append("', '");
                                processedValues.Append(amount);
                                processedValues.Append("'),");

                                processedCount++;
                            }
                            else
                            {
                                unProcessedValues.Append("('");
                                unProcessedValues.Append(fileId.ToString());
                                unProcessedValues.Append("', '");
                                unProcessedValues.Append(currentLine);
                                unProcessedValues.Append("'),");

                                unProcessedCount++;
                            }
                        }

                        parsedCount++;

                        if ((processedCount > 0) && (processedCount % 200 == 0))
                        {
                            await _baseRepo.AddTaxDetailAsync(processedValues.ToString().TrimEnd(','));

                            processedValues.Clear();
                        }

                        if ((unProcessedCount > 0) && (unProcessedCount % 200 == 0))
                        {
                            await _baseRepo.AddUnprocessedDetailAsync(unProcessedValues.ToString().TrimEnd(','));

                            unProcessedValues.Clear();
                        }
                    }

                    if (processedValues.Length > 0)
                    {
                        await _baseRepo.AddTaxDetailAsync(processedValues.ToString().TrimEnd(','));
                    }

                    if (unProcessedValues.Length > 0)
                    {
                        await _baseRepo.AddUnprocessedDetailAsync(unProcessedValues.ToString().TrimEnd(','));
                    }
                }

                watch.Stop();

                _logger.LogProcessingTime("ProcessFileDataAsync", watch.ElapsedMilliseconds, null);

                return(processedCount);
            }
            catch (Exception ex)
            {
                _logger.LogException(ex, null);
                return(0);
            }
        }