コード例 #1
0
        /// <summary>
        /// Maps the batch data.
        /// </summary>
        /// <param name="tableData">The table data.</param>
        /// <exception cref="System.NotImplementedException"></exception>
        private void MapBatch(IQueryable <Row> tableData)
        {
            var batchStatusClosed = Rock.Model.BatchStatus.Closed;
            var newBatches        = new List <FinancialBatch>();

            int completed  = 0;
            int totalRows  = tableData.Count();
            int percentage = (totalRows - 1) / 100 + 1;

            ReportProgress(0, string.Format("Verifying batch import ({0:N0} found, {1:N0} already exist).", totalRows, ImportedBatches.Count()));
            foreach (var row in tableData)
            {
                int?batchId = row["BatchID"] as int?;
                if (batchId != null && !ImportedBatches.ContainsKey((int)batchId))
                {
                    var batch = new FinancialBatch();
                    batch.CreatedByPersonAliasId = ImportPersonAlias.Id;
                    batch.ForeignId = batchId.ToString();
                    batch.Status    = batchStatusClosed;

                    string name = row["BatchName"] as string;
                    if (name != null)
                    {
                        name           = name.Trim();
                        batch.Name     = name.Left(50);
                        batch.CampusId = CampusList.Where(c => name.StartsWith(c.Name) || name.StartsWith(c.ShortCode))
                                         .Select(c => (int?)c.Id).FirstOrDefault();
                    }

                    DateTime?batchDate = row["BatchDate"] as DateTime?;
                    if (batchDate != null)
                    {
                        batch.BatchStartDateTime = batchDate;
                        batch.BatchEndDateTime   = batchDate;
                    }

                    decimal?amount = row["BatchAmount"] as decimal?;
                    if (amount != null)
                    {
                        batch.ControlAmount = amount.HasValue ? amount.Value : new decimal();
                    }

                    newBatches.Add(batch);
                    completed++;
                    if (completed % percentage < 1)
                    {
                        int percentComplete = completed / percentage;
                        ReportProgress(percentComplete, string.Format("{0:N0} batches imported ({1}% complete).", completed, percentComplete));
                    }
                    else if (completed % ReportingNumber < 1)
                    {
                        SaveFinancialBatches(newBatches);
                        newBatches.ForEach(b => ImportedBatches.Add(b.ForeignId.AsType <int>(), (int?)b.Id));
                        newBatches.Clear();
                        ReportPartialProgress();
                    }
                }
            }

            if (newBatches.Any())
            {
                SaveFinancialBatches(newBatches);
                newBatches.ForEach(b => ImportedBatches.Add(b.ForeignId.AsType <int>(), (int?)b.Id));
            }

            ReportProgress(100, string.Format("Finished batch import: {0:N0} batches imported.", completed));
        }