public override void ProcessData() { //набор строк похожих источников FillSourcesTable(); while (dtSources.Rows.Count > 0) { DataRow[] drLS = null; var currentSource = dtSources.Rows[0]; var priceSource = new PriceSource(currentSource); if (!IsReadyForDownload(priceSource)) { currentSource.Delete(); dtSources.AcceptChanges(); continue; } try { drLS = GetLikeSources(priceSource); try { CurrFileName = String.Empty; GetFileFromSource(priceSource); priceSource.UpdateLastCheck(); } catch (PathSourceHandlerException pathException) { FailedSources.Add(priceSource.PriceItemId); DownloadLogEntity.Log(priceSource.SourceTypeId, priceSource.PriceItemId, pathException.ToString(), pathException.ErrorMessage); } catch (Exception e) { FailedSources.Add(priceSource.PriceItemId); DownloadLogEntity.Log(priceSource.SourceTypeId, priceSource.PriceItemId, e.ToString()); } if (!String.IsNullOrEmpty(CurrFileName)) { var correctArchive = FileHelper.ProcessArchiveIfNeeded(CurrFileName, ExtrDirSuffix, priceSource.ArchivePassword); foreach (var drS in drLS) { SetCurrentPriceCode(drS); string extractFile = null; try { if (!correctArchive) { throw new PricePreprocessingException("Не удалось распаковать файл '" + Path.GetFileName(CurrFileName) + "'. Файл поврежден", CurrFileName); } if (!ProcessPriceFile(CurrFileName, out extractFile, priceSource.SourceTypeId)) { throw new PricePreprocessingException("Не удалось обработать файл '" + Path.GetFileName(CurrFileName) + "'", CurrFileName); } LogDownloadedPrice(priceSource.SourceTypeId, Path.GetFileName(CurrFileName), extractFile); FileProcessed(); } catch (PricePreprocessingException e) { LogDownloaderFail(priceSource.SourceTypeId, e.Message, e.FileName); FileProcessed(); } catch (Exception e) { LogDownloaderFail(priceSource.SourceTypeId, e.Message, extractFile); } finally { drS.Delete(); } } Cleanup(); } else { foreach (var drDel in drLS) { drDel.Delete(); } } } catch (Exception ex) { var error = String.Empty; if (drLS != null && drLS.Length > 1) { error += String.Join(", ", drLS.Select(r => r[SourcesTableColumns.colPriceCode].ToString()).ToArray()); drLS.Each(r => FileHelper.Safe(r.Delete)); error = "Источники : " + error; } else { error = String.Format("Источник : {0}", currentSource[SourcesTableColumns.colPriceCode]); FileHelper.Safe(currentSource.Delete); } Log(ex, error); } finally { FileHelper.Safe(() => dtSources.AcceptChanges()); } } }
public void ResendPrice(WcfCallParameter paramDownlogId) { var downlogId = Convert.ToUInt64(paramDownlogId.Value); var drFocused = MySqlHelper.ExecuteDataRow(ConnectionHelper.GetConnectionString(), @" SELECT distinct logs.RowID as DRowID, logs.LogTime as DLogTime, logs.Addition as DAddition, logs.ArchFileName as DArchFileName, logs.ExtrFileName as DExtrFileName, sp.Name as DFirmName, r.Region as DRegion, if(pd.CostType = 1, concat('[Колонка] ', pc.CostName), pd.PriceName) as DPriceName, pim.Id as DPriceItemId, pd.PriceCode as DPriceCode, pd.ParentSynonym, if(pd.CostType = 1, pc.CostCode, null) DCostCode, st.Type as DSourceType, s.PricePath as DPricePath, s.EmailTo as DEmailTo, s.EmailFrom as DEmailFrom, s.ArchivePassword, pricefmts.FileExtention as DFileExtention FROM logs.downlogs as logs, Customers.Suppliers sp, usersettings.pricesdata pd, usersettings.pricescosts pc, usersettings.PriceItems pim, farm.regions r, farm.sources s, farm.Sourcetypes st, farm.formrules fr, farm.pricefmts WHERE pim.Id = logs.PriceItemId and pc.PriceItemId = pim.Id and pc.PriceCode = pd.PriceCode and ((pd.CostType = 1) OR (exists(select * from userSettings.pricesregionaldata prd where prd.PriceCode = pd.PriceCode and prd.BaseCost=pc.CostCode))) and sp.Id = pd.firmcode and r.regioncode = sp.HomeRegion and s.Id = pim.SourceId and st.Id = s.SourceTypeId and logs.ResultCode in (2, 3) and fr.Id = pim.FormRuleId and pricefmts.id = fr.PriceFormatId and logs.Rowid = ?DownLogId", new MySqlParameter("?DownLogId", downlogId)); var filename = GetFileFromArhive(downlogId); var sourceArchiveFileName = filename; var archFileName = drFocused["DArchFileName"].ToString(); var externalFileName = drFocused["DExtrFileName"].ToString(); var extractedFile = string.Empty; try { if (drFocused["DSourceType"].ToString().Equals("EMAIL", StringComparison.OrdinalIgnoreCase)) { // Если файл пришел по e-mail, то это должен быть файл *.eml, открываем его на чтение filename = ExtractFileFromAttachment(filename, archFileName, externalFileName); extractedFile = filename; } var tempDirectory = Path.Combine(Path.GetTempPath(), Path.GetFileNameWithoutExtension(archFileName)); if (ArchiveHelper.IsArchive(filename)) { if (File.Exists(tempDirectory)) { File.Delete(tempDirectory); } if (Directory.Exists(tempDirectory)) { Directory.Delete(tempDirectory, true); } Directory.CreateDirectory(tempDirectory); ArchiveHelper.Extract(filename, externalFileName, tempDirectory, drFocused["ArchivePassword"].ToString()); filename = FileHelper.FindFromArhive(tempDirectory, externalFileName); if (String.IsNullOrEmpty(filename)) { var errorMessage = String.Format( "Невозможно найти файл {0} в распакованном архиве!", externalFileName); throw new FaultException <string>(errorMessage, new FaultReason(errorMessage)); } } if (String.IsNullOrEmpty(filename)) { return; } var priceExtention = drFocused["DFileExtention"].ToString(); var destinationFile = Path.Combine(Settings.Default.InboundPath, "d" + drFocused["DPriceItemId"] + "_" + downlogId + priceExtention); if (File.Exists(destinationFile)) { throw new FaultException <string>(MessagePriceInQueue, new FaultReason(MessagePriceInQueue)); } File.Copy(filename, destinationFile); var item = new PriceProcessItem(true, Convert.ToUInt64(drFocused["DPriceCode"].ToString()), (drFocused["DCostCode"] is DBNull) ? null : (ulong?)Convert.ToUInt64(drFocused["DCostCode"].ToString()), Convert.ToUInt64(drFocused["DPriceItemId"].ToString()), destinationFile, (drFocused["ParentSynonym"] is DBNull) ? null : (ulong?)Convert.ToUInt64(drFocused["ParentSynonym"].ToString())); PriceItemList.AddItem(item); var priceItemId = Convert.ToUInt64(drFocused["DPriceItemId"]); downlogId = LogResendPriceAsDownload(priceItemId, archFileName, externalFileName, paramDownlogId.LogInformation); if (downlogId > 0) { destinationFile = Path.Combine(Settings.Default.HistoryPath, downlogId + Path.GetExtension(sourceArchiveFileName)); File.Copy(sourceArchiveFileName, destinationFile); } if (Directory.Exists(tempDirectory)) { FileHelper.Safe(() => Directory.Delete(tempDirectory, true)); } } finally { if (File.Exists(extractedFile)) { File.Delete(extractedFile); } } }
protected override void CopyToHistory(UInt64 downloadLogId) { var historyFileName = Path.Combine(DownHistoryPath, downloadLogId + Path.GetExtension(CurrFileName)); FileHelper.Safe(() => File.Copy(CurrFileName, historyFileName)); }