public async Task <ResponseData> Save(CommonMaster data) { try { _logger.LogInformation($"{nameof(Save)} - Called"); var allMasterData = _rsaContext.CommonMasters.AsNoTracking().ToList(); if (data.Id > 0) { if (allMasterData.Any(a => a.DisplayText == data.DisplayText && data.Type == a.Type && a.Id != data.Id)) { return(new ResponseData() { status = ResponseStatus.warning, message = $"The Display Text {data.DisplayText} in master {data.Type} is already exists." }); } _rsaContext.CommonMasters.Update(data); } else { if (allMasterData.Any(a => a.DisplayText == data.DisplayText && data.Type == a.Type)) { return(new ResponseData() { status = ResponseStatus.warning, message = $"The Display Text {data.DisplayText} in master {data.Type} is already exists." }); } _rsaContext.CommonMasters.Add(data); } await _rsaContext.SaveChangesAsync(); _logger.LogInformation($"{nameof(Save)} - completed"); return(new ResponseData() { status = ResponseStatus.success }); } catch (Exception ex) { _logger.LogError($"{nameof(Save)} - Error.", ex); return(new ResponseData() { status = ResponseStatus.error, message = ex.Message }); } }
public async Task <ResponseData> SyncOfflineData(ReportHeader rptHdr, ReportAllDetailsVm reportAllDetails) { try { _logger.LogInformation($"{nameof(SyncOfflineData)} - Called"); var existHeaderData = _rsaContext.ReportHeaders.AsNoTracking().Where(w => w.ReportGuid == rptHdr.ReportGuid).FirstOrDefault(); rptHdr.CreatedOn = DateTime.UtcNow; rptHdr.CreatedBy = rptHdr.CreatedBy; rptHdr.IsSafetyFirstComplete = false; rptHdr.IsCustomerEquipmentComplete = false; rptHdr.IsVibrationAnalysisComplete = false; rptHdr.IsObservationComplete = false; rptHdr.IsRecommendationComplete = false; rptHdr.ApprovedBy = null; rptHdr.DocTriggerFrom = "DRAFT"; if (existHeaderData == null) { _rsaContext.Add(rptHdr); } else { rptHdr.Id = existHeaderData.Id; _rsaContext.Update(rptHdr); } #region SafetyFirstCheck if (reportAllDetails.SafetyFirstCheck != null) { var eixstsSafety = _rsaContext.SafetyFirstChecks.AsNoTracking().FirstOrDefault(a => a.ReportGuid == rptHdr.ReportGuid); if (eixstsSafety != null) { _rsaContext.SafetyFirstChecks.Remove(eixstsSafety); _rsaContext.SafetyFirstCheckDetails.RemoveRange(_rsaContext.SafetyFirstCheckDetails.AsNoTracking().Where(a => a.ReportGuid == rptHdr.ReportGuid)); } reportAllDetails.SafetyFirstCheck.ReportGuid = rptHdr.ReportGuid; reportAllDetails.SafetyFirstCheck.Id = 0; foreach (var item in reportAllDetails.SafetyFirstCheck.SafetyFirstCheckDetails) { item.Id = 0; item.ReportGuid = rptHdr.ReportGuid; } _rsaContext.Add(reportAllDetails.SafetyFirstCheck); } #endregion #region CustomerEquipmentActivity if (reportAllDetails.CustomerEquipmentActivity != null) { var custEquipAct = _rsaContext.CustomerEquipmentActivities.AsNoTracking().FirstOrDefault(a => a.ReportGuid == rptHdr.ReportGuid); if (custEquipAct != null) { _rsaContext.CustomerEquipmentActivities.Remove(custEquipAct); } reportAllDetails.CustomerEquipmentActivity.ReportGuid = rptHdr.ReportGuid; reportAllDetails.CustomerEquipmentActivity.Id = 0; _rsaContext.Add(reportAllDetails.CustomerEquipmentActivity); } #endregion #region VibrationAnalysisHeader if (reportAllDetails.VibrationAnalysisHeader != null) { var existVibAna = _rsaContext.VibrationAnalysisHeaders.AsNoTracking().FirstOrDefault(a => a.ReportGuid == rptHdr.ReportGuid); if (existVibAna != null) { _rsaContext.VibrationAnalysisHeaders.Remove(existVibAna); var exisVibAnaDetails = _rsaContext.VibrationAnalysis.AsNoTracking().Where(a => a.ReportGuid == rptHdr.ReportGuid); if (exisVibAnaDetails.Any()) { _rsaContext.VibrationAnalysis.RemoveRange(exisVibAnaDetails); } } reportAllDetails.VibrationAnalysisHeader.ReportGuid = rptHdr.ReportGuid; reportAllDetails.VibrationAnalysisHeader.Id = 0; foreach (var item in reportAllDetails.VibrationAnalysisHeader.VibrationAnalysis) { item.Id = 0; item.ReportGuid = rptHdr.ReportGuid; } _rsaContext.Add(reportAllDetails.VibrationAnalysisHeader); } #endregion #region Observations if (reportAllDetails.Observations != null) { var existObs = _rsaContext.Observations.AsNoTracking().Where(w => w.ReportGuid == rptHdr.ReportGuid); if (existObs.Any()) { _rsaContext.Observations.RemoveRange(existObs); } foreach (var obs in reportAllDetails.Observations) { obs.ReportGuid = rptHdr.ReportGuid; obs.Id = 0; _rsaContext.Add(obs); } } #endregion #region Recommendations if (reportAllDetails.Recommendations != null) { var existRecomm = _rsaContext.Recommendations.AsNoTracking().Where(w => w.ReportGuid == rptHdr.ReportGuid); if (existRecomm.Any()) { _rsaContext.Recommendations.RemoveRange(existRecomm); } foreach (var recomm in reportAllDetails.Recommendations) { recomm.ReportGuid = rptHdr.ReportGuid; recomm.Id = 0; _rsaContext.Add(recomm); } } #endregion #region SpareParts if (reportAllDetails.SpareParts != null) { var existSparts = _rsaContext.SpareParts.AsNoTracking().Where(w => w.ReportGuid == rptHdr.ReportGuid); if (existSparts.Any()) { _rsaContext.SpareParts.RemoveRange(existSparts); } foreach (var sp in reportAllDetails.SpareParts) { sp.ReportGuid = rptHdr.ReportGuid; sp.Id = 0; _rsaContext.Add(sp); } } #endregion if (reportAllDetails.Misc != null) { var exisMisc = _rsaContext.Miscs.AsNoTracking().FirstOrDefault(w => w.ReportGuid == rptHdr.ReportGuid); if (exisMisc != null) { _rsaContext.Miscs.Remove(exisMisc); } reportAllDetails.Misc.Id = 0; reportAllDetails.Misc.ReportGuid = rptHdr.ReportGuid; reportAllDetails.Misc.CustomerDate = DateTime.Now; reportAllDetails.Misc.FirmDate = DateTime.Now; _rsaContext.Add(reportAllDetails.Misc); } if (await _rsaContext.SaveChangesAsync() > 0) { return(new ResponseData() { status = ResponseStatus.success, data = rptHdr.ReportGuid, message = "Report Synchronized Successfully." }); } return(new ResponseData() { status = ResponseStatus.warning, data = rptHdr.ReportGuid, message = "Report Synchronization Failed." }); } catch (Exception ex) { _logger.LogError($"{nameof(SyncOfflineData)} - Error", ex); return(new ResponseData() { status = ResponseStatus.error, data = rptHdr.ReportGuid, message = "Report Synchronization Failed." }); } }