public static void SaveTagBalance(String file, SuncorProductionFile pf, List <TagBalance> tb)
 {
     using (DBContextWithConnectionString context = new DBContextWithConnectionString()) {
         Batch batch = new Batch();
         batch.Id        = pf.BatchId.ToString();
         batch.Created   = DateTime.Now;
         batch.CreatedBy = "System";
         batch.Filename  = file;
         context.Batches.Add(batch);
         foreach (var item in tb)
         {
             TagBalance found = context.TagBalances.Find(new object[] { item.Tag, item.BalanceDate });
             if (found == null)
             {
                 batch.TagBalances.Add(item);
             }
             else
             {
                 UpdateTagBalance(found, item);
             }
         }
         context.SaveChanges();
         pf.SavedRecords = tb;
     }
 }
        public void ProcessFile(ILogger log, string version)
        {
            SuncorProductionFile.SetLogFileWriter(LogHelper.WriteLogFile);
            this.ProducitionFile = null;
            DateTime day = GetCurrentDay(this.PlantName);

            if (this.IsHoneywellPB)
            {
                this.ProducitionFile = new HoneywellPBParser().LoadFile(this.TempFileName, this.PlantName, day);
            }
            if (this.IsMontrealSulphur)
            {
                this.ProducitionFile = new MontrealSulphurParser().LoadFile(this.TempFileName, this.PlantName, this.ProductCode, day);
            }
            if (this.IsDPS)
            {
                this.ProducitionFile = new DPSParser().LoadFile(this.TempFileName, this.PlantName, day);
            }
            if (this.IsDenver)
            {
                this.ProducitionFile = new SigmafineParser().LoadExcel(this.TempFileName, this.PlantName, day);
            }
            if (this.IsTerraNova)
            {
                this.ProducitionFile = new TerraNovaParser().LoadFile(this.TempFileName, this.PlantName, day);
            }
            if (this.IsSarnia)
            {
            }
            if (this.ProducitionFile != null)
            {
                this.ProducitionFile.SaveRecords();
                this.SuccessfulRecords = this.ProducitionFile.SavedRecords.Count;
                this.FailedRecords     = this.ProducitionFile.FailedRecords.Count;
                if (this.ProducitionFile.SavedRecords.Count > 0)
                {
                    string json = this.ProducitionFile.ExportR2PJson();
                    if (!MulesoftPush.PostProduction(json))
                    {
                        LogHelper.LogSystemError(log, version, "Json NOT sent to Mulesoft");
                        this.ProducitionFile.Warnings.Add(new WarningMessage("Json NOT sent to Mulesoft"));
                    }
                    AzureFileHelper.WriteFile(this.AzureFullPathName.Replace("immediateScan", "tempJsonOutput") + ".json", json, false);
                }
            }
        }
        public static void RecordStats(SuncorProductionFile pf, string filename, List <WarningMessage> warnings)
        {
            using (DBContextWithConnectionString context = new DBContextWithConnectionString()) {
                TransactionEvent te = new TransactionEvent()
                {
                    Plant = pf.Plant, Filename = filename, SuccessfulRecordCount = pf.SavedRecords.Count, FailedRecordCount = pf.FailedRecords.Count
                };

                foreach (var item in warnings.Select(y => new { Tag = y.Tag, Message = y.Message }).Distinct())
                {
                    te.TransactionEventDetails.Add(new TransactionEventDetail()
                    {
                        Tag = item.Tag, ErrorMessage = item.Message
                    });
                }
                List <WarningMessage> noMappings  = warnings.Where(t => t.Message == "No TagMapping").ToList();
                List <WarningMessage> otherErrors = warnings.Where(t => t.Message != "No TagMapping").ToList();
                te.ErrorMessage = "";
                if (otherErrors.Count > 0)
                {
                    te.ErrorMessage = String.Join(",", otherErrors.Select(y => y.Message).Distinct().ToArray());
                }
                if (noMappings.Count > 0)
                {
                    te.ErrorMessage += (te.ErrorMessage.Length > 0 ? " and " : "") + noMappings.Count + " records with no tag mappings";
                }
                if (pf.SavedRecords.Count == 0 && te.ErrorMessage == "")
                {
                    te.ErrorMessage = "File rejected due to no successfuly records";
                }
                if (te.ErrorMessage == "")
                {
                    te.ErrorMessage = "File completed successfully";
                }
                context.TransactionEvents.Add(te);
                context.SaveChanges();
            }
        }