private void UpdateDB(List <Department> fromClient, List <Department> fromDB) { if (fromClient == null || fromDB == null || fromClient.Count == 0) { return; } // we parse all the items and populate the status field // we do this if for optimization if (fromClient.Count > fromDB.Count) { foreach (Department dept in fromClient) { if (dept == null) { continue; } Department dbDept = fromDB.Where(dDB => dDB.Id == dept.Id).FirstOrDefault(); if (dbDept == null) { dept.Status = ElementStatus.New; int errorCount = DeptDAL.InsertDepartment(dept); _job.IncrementError(errorCount); _job.IncrementNew(errorCount); continue; } if (dept.DifferFrom(dbDept)) { dbDept.Status = dept.Status = ElementStatus.Update; int errorCount = DeptDAL.UpdateDepartment(dept, dbDept); _job.IncrementError(errorCount); _job.IncrementUpdated(errorCount); continue; } else { dbDept.Status = dept.Status = ElementStatus.Unchanged; continue; } } fromDB .Where(d => d.Status == null) .ToList() .ForEach(d => { if (d.DateRemoved == null) { d.Status = ElementStatus.Delete; int errorCount = DeptDAL.DeleteDepartment(d.RetailerId, d.Id); _job.IncrementError(errorCount); _job.IncrementUpdated(errorCount); } }); } else { foreach (Department dept in fromDB) { Department clientDept = fromClient.Where(d => d.Id == dept.Id).FirstOrDefault(); if (clientDept == null) { if (dept.DateRemoved == null) { dept.Status = ElementStatus.Delete; int errorCount = DeptDAL.DeleteDepartment(dept.RetailerId, dept.Id); _job.IncrementError(errorCount); _job.IncrementDeleted(errorCount); } continue; } if (dept.DifferFrom(clientDept)) { clientDept.Status = dept.Status = ElementStatus.Update; int errorCount = DeptDAL.UpdateDepartment(clientDept, dept); _job.IncrementError(errorCount); _job.IncrementUpdated(errorCount); continue; } else { clientDept.Status = dept.Status = ElementStatus.Unchanged; continue; } } fromClient .Where(d => d.Status == null) .ToList() .ForEach(d => { d.Status = ElementStatus.New; int errorCount = DeptDAL.InsertDepartment(d); _job.IncrementError(errorCount); _job.IncrementNew(errorCount); }); } }
public bool ProcessTransactions(JobAudit job) { try { int totalItems = 0; _job = job; _job.JobStatus = BOFileStatus.Processing; JobAuditDAL.UpdateJobAudit(_job); DateTime startTime = DateTime.Now; List <byte[]> files = CompressHelper.DeCompress(_job.FilePath); foreach (byte[] file in files) { Transactions receivedTransactions = file.Deserialize <Transactions>(); if (receivedTransactions == null || receivedTransactions.SaleTransactions == null || receivedTransactions.SaleTransactions.Count == 0) { continue; } foreach (SalesTransaction trans in receivedTransactions.SaleTransactions) { if (trans.TransactionDetails == null) { continue; } foreach (SalesTransactionDetail item in trans.TransactionDetails) { totalItems++; Transaction transaction = item.ToDALTransaction(job.RetailerId, trans); bool transactionExists = TransDAL.TransactionExists(transaction); if (transaction == null || transactionExists) { continue; } int errorCount = TransDAL.InsertTransaction(transaction); _job.ErrorCount += errorCount; _job.IncrementNew(errorCount); } } } _job.ItemsProcessed = totalItems; _job.ProcessTime = (int)DateTime.Now.Subtract(startTime).TotalSeconds; _job.JobStatus = BOFileStatus.Done; JobAuditDAL.UpdateJobAudit(_job); return(true); } catch (Exception ex) { Logger.LogException(ex); return(false); } }
private void UpdateDB(List <Item> fromClient, List <Item> fromDB) { if (fromClient == null || fromDB == null || fromClient.Count == 0) { return; } // we parse all the items and populate the status field // we do this if for optimization if (fromClient.Count > fromDB.Count) { foreach (Item item in fromClient) { if (item == null) { continue; } Item dbItem = fromDB.Where(iDB => iDB.SKU == item.SKU).FirstOrDefault(); if (dbItem == null) { item.Status = ElementStatus.New; int errorCount = ItmDAL.InsertItem(item); _job.IncrementError(errorCount); _job.IncrementNew(errorCount); continue; } if (item.DifferFrom(dbItem)) { dbItem.Status = item.Status = ElementStatus.Update; int errorCount = ItmDAL.UpdateItem(item, dbItem); _job.IncrementError(errorCount); _job.IncrementUpdated(errorCount); continue; } else { dbItem.Status = item.Status = ElementStatus.Unchanged; continue; } } fromDB .Where(i => i.Status == null) .ToList() .ForEach(i => { if (i.DateRemoved == null) { i.Status = ElementStatus.Delete; int errorCount = ItmDAL.DeleteItem(i.RetailerId, i.SKU); _job.IncrementError(errorCount); _job.IncrementDeleted(errorCount); } }); } else { foreach (Item item in fromDB) { Item clientItem = fromClient.Where(i => i != null && item != null && i.SKU == item.SKU).FirstOrDefault(); if (clientItem == null) { if (item.DateRemoved == null) { item.Status = ElementStatus.Delete; int errorCount = ItmDAL.DeleteItem(item.RetailerId, item.SKU); _job.IncrementError(errorCount); _job.IncrementDeleted(errorCount); } continue; } if (item.DifferFrom(clientItem)) { clientItem.Status = item.Status = ElementStatus.Update; int errorCount = ItmDAL.UpdateItem(clientItem, item); _job.IncrementError(errorCount); _job.IncrementUpdated(errorCount); continue; } else { clientItem.Status = item.Status = ElementStatus.Unchanged; continue; } } fromClient .Where(i => i != null && i.Status == null) .ToList() .ForEach(i => { i.Status = ElementStatus.New; int errorCount = ItmDAL.InsertItem(i); _job.IncrementError(errorCount); _job.IncrementNew(errorCount); }); } }