示例#1
0
 public void Get_local_file()
 {
     using (var cleaner = new FileCleaner()) {
         var tmpFile  = cleaner.TmpFile();
         var fileInfo = new FileInfo(tmpFile);
         fileInfo.LastWriteTime = DateTime.Now.AddDays(1);
         var source = new CertificateSource {
             DecodeTableUrl = new Uri(fileInfo.FullName).ToString()
         };
         var file = handler.GetCatalogFile(source, cleaner);
         Assert.IsNotNull(file);
     }
 }
        public virtual CertificateCatalogFile GetCatalogFile(CertificateSource source, FileCleaner cleaner)
        {
            var downloader = new FtpDownloader();
            var uri        = new Uri(source.DecodeTableUrl);

            if (uri.Scheme.Match("ftp"))
            {
                var downloadFiles = downloader.DownloadedFiles(uri,
                                                               source.LastDecodeTableDownload.HasValue ? source.LastDecodeTableDownload.Value : DateTime.MinValue,
                                                               DownHandlerPath);

                if (downloadFiles.Count > 0)
                {
                    return(new CertificateCatalogFile(source, downloadFiles[0].FileDate, downloadFiles[0].FileName));
                }
                else
                {
                    _logger.DebugFormat("Файл {0} не найден", uri);
                }
            }
            else if (uri.Scheme.Match("file"))
            {
                var src = new FileInfo(uri.LocalPath);
                if (!src.Exists)
                {
                    return(null);
                }
                //в базе даты хранятся с точнотью до секунды сравнивать их нужно так же
                if (Math.Abs((DateTime.Now - src.LastWriteTime).TotalMilliseconds) > Settings.Default.FileDownloadInterval &&
                    Math.Abs((source.LastDecodeTableDownload.GetValueOrDefault() - src.LastWriteTime).TotalSeconds) > 1)
                {
                    var dst = src.CopyTo(cleaner.TmpFile(), true);
                    return(new CertificateCatalogFile(source, src.LastWriteTime, dst.FullName));
                }
            }

            return(null);
        }
示例#3
0
        private static void ProcessUser(ISession session, uint userId)
        {
            log.Debug($"Обработка пользователя {userId}");
            var config = session.Query <FtpConfig>().Where(x => x.User.Id == userId).ToList();

            foreach (var priceConfig in config)
            {
                using (var cleaner = new FileCleaner())
                    using (var client = new FtpClient()) {
                        var url = new Uri(priceConfig.OrderUrl);
                        FtpExportJob.OpenFtp(url, client);
                        var dir   = url.GetComponents(UriComponents.Path, UriFormat.Unescaped);
                        var files = client.GetListing(dir);
                        foreach (var file in files)
                        {
                            var tmp        = cleaner.TmpFile();
                            var targetFile = Path.Combine(dir, file.Name);
                            using (var dst = File.OpenWrite(tmp))
                                using (var src = client.OpenRead(targetFile))
                                    src.CopyTo(dst);

                            try {
                                using (var trx = session.BeginTransaction()) {
                                    Protocols.DbfAsna.OrderImport(userId, tmp);
                                    client.DeleteFile(targetFile);
                                    trx.Commit();
                                }
                            } catch (Exception e) {
                                log.Error($"Не удалось обработать файл {file.Name} из {url}", e);
#if DEBUG
                                throw;
#endif
                            }
                        }
                    }
            }
        }
示例#4
0
        private void ProcessUser(ISession session, uint userId)
        {
            log.Debug($"Обработка пользователя {userId}");
            IList <NamedOffer> offers;

            using (var trx = session.BeginTransaction()) {
                offers = PriceHelper.QueryOffers(session, userId);
                trx.Commit();
            }
            var config = session.Query <FtpConfig>().Where(x => x.User.Id == userId).ToList();

            foreach (var supplierConfig in config)
            {
                using (var cleander = new FileCleaner())
                    using (var client = new FtpClient()) {
                        var url = supplierConfig.PriceUrl;
                        try {
                            if (!String.IsNullOrEmpty(url))
                            {
                                OpenFtp(new Uri(url), client);
                                var priceOffers = offers.Where(x => x.PriceList.Id.Price.Supplier.Id == supplierConfig.Supplier.Id)
                                                  .ToArray();
                                var tmp = cleander.TmpFile();
                                Dbf2.SaveAsDbf4(DbfAsna.Price(priceOffers), tmp);

                                UploadFile(client, tmp, new Uri(url));
                            }
                        } catch (Exception e) {
                            log.Error($"Не удалось выгрузить прайс-лист поставщика {supplierConfig.Supplier.Name} ({supplierConfig.Supplier.Id}) в {url}", e);
#if DEBUG
                            throw;
#endif
                        }

                        url = supplierConfig.WaybillUrl;
                        if (!String.IsNullOrEmpty(url))
                        {
                            try {
                                var waybillIds = session.CreateSQLQuery(@"
select dh.Id, d.RowId, ds.Id as SendLogId
from Logs.DocumentSendLogs ds
	join Logs.Document_logs d on d.RowId = ds.DocumentId
	join Documents.DocumentHeaders dh on dh.DownloadId = d.RowId
where ds.UserId = :userId
	and ds.Committed in (0, 2)
	and d.FirmCode = :supplierId
order by d.LogTime desc
limit 400;")
                                                 .SetParameter("userId", userId)
                                                 .SetParameter("supplierId", supplierConfig.Supplier.Id)
                                                 .List <object[]>();
                                foreach (var pair in waybillIds)
                                {
                                    var doc = session.Load <Document>(Convert.ToUInt32(pair[0]));
                                    var tmp = cleander.TmpFile();
                                    Dbf2.SaveAsDbf4(DbfAsna.Waybill(session, doc), tmp);
                                    var part = url;
                                    if (!part.EndsWith("/"))
                                    {
                                        part += "/";
                                    }
                                    UploadFile(client, tmp, new Uri(part + pair[1] + ".dbf"));
                                    using (var trx = session.BeginTransaction()) {
                                        var sendLog = session.Load <DocumentSendLog>(Convert.ToUInt32(pair[2]));
                                        sendLog.Commit();
                                        session.Flush();
                                        trx.Commit();
                                    }
                                }
                            } catch (Exception e) {
                                log.Error($"Не удалось выгрузить накладную поставщика {supplierConfig.Supplier.Name} ({supplierConfig.Supplier.Id}) в {url}", e);
#if DEBUG
                                throw;
#endif
                            }
                        }
                    }
            }
        }