1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140
| public interface IDownloadJobService : ISyncJobService { void SyncProcess(CommCode jobSetting); }
public class DownloadJobService : IDownloadJobService { private FtpServiceResolver _ftpServiceResolver; private IFtpService _ftpService; private readonly IFilePathService _filePathService; private string _localPath; private readonly IBusinessDateService _businessDateService; private readonly SqlConnectionFactory _factory; public DownloadJobService(FtpServiceResolver ftpServiceResolver , IFilePathService filePathService , IBusinessDateService businessDateService, SqlConnectionFactory factory) { _ftpServiceResolver = ftpServiceResolver; _filePathService = filePathService; _businessDateService = businessDateService; _factory = factory; } public void SyncProcess(CommCode jobSetting) {
var ftpDownloadConfigSetting = jobSetting.CodeVal3.Split(";"); _ftpService = _ftpServiceResolver(ftpDownloadConfigSetting[0]); var ftpDownloadConfigBaseModel = new FTPConfigBaseModel() { Server = ftpDownloadConfigSetting[1], Account = ftpDownloadConfigSetting[2], Password = ftpDownloadConfigSetting[3], RemotePath = ftpDownloadConfigSetting[4], LocalPath = ftpDownloadConfigSetting[5] }; _localPath = _filePathService.GetDirectoryPath(ftpDownloadConfigBaseModel.LocalPath); var fileName = _businessDateService.GetBusinessDateWithParameter((ftpDownloadConfigSetting[6], "yyyyMMdd"));
if (string.IsNullOrEmpty(ftpDownloadConfigSetting[8]) == false) { var dataDate = _businessDateService.GetBusinessDateWithParameter((ftpDownloadConfigSetting[7], "yyyy/MM/dd"));
var fileInputs = new List<FileInput>(); var fName = ftpDownloadConfigSetting[8]; var encodingName = ftpDownloadConfigSetting[10]; var encoding = string.IsNullOrEmpty(encodingName) ? Encoding.UTF8 : Encoding.GetEncoding(encodingName);
using (var reader = new StreamReader(_filePathService.GetFile(_localPath, fileName), encoding)) { while (reader.Peek() >= 0) { var content = reader.ReadLine().Trim(); if (string.IsNullOrEmpty(content)) continue; fileInputs.Add(new FileInput() { Fname = fName, DataDate = dataDate, DataStr = content, AuditUser = "SYS", AuditTime = DateTime.Now }); } }
using (var _transaction = new TransactionScope()) { using (var _conn = _factory.GetConnection()) { _conn.Open(); _conn.Execute(@"DELETE FileInput WHERE DataDate = @DataDate AND FName = @FName" , new { DataDate = dataDate, FName = fName });
using (SqlBulkCopy bulkCopy = new SqlBulkCopy((SqlConnection)(_conn as SKBSqlConnection))) { using (var reader = ObjectReader.Create(fileInputs , new string[] { "SeqNo", "Fname", "DataDate", "DataStr", "AuditUser", "AuditTime" })) { bulkCopy.DestinationTableName = "dbo.FileInput"; bulkCopy.WriteToServer(reader); } } _transaction.Complete(); } } }
if (string.IsNullOrEmpty(ftpDownloadConfigSetting[9]) == false) { using (var _conn = _factory.GetConnection()) { var spExecuteSql = _businessDateService.GetBusinessDateWithParameter( (ftpDownloadConfigSetting[9], "yyyy/MM/dd")); if (string.IsNullOrEmpty(jobSetting.CodeVal4)) { _conn.Execute(spExecuteSql); } else { var result = _conn.Query<FileUploadResponseModel>(spExecuteSql).ToList(); var ftpUploadConfigSetting = jobSetting.CodeVal4.Split(";"); var ftpUploadConfigBaseModel = new FTPConfigBaseModel() { Server = ftpUploadConfigSetting[1], Account = ftpUploadConfigSetting[2], Password = ftpUploadConfigSetting[3], RemotePath = ftpUploadConfigSetting[4], LocalPath = ftpUploadConfigSetting[5] }; _ftpService.UploadData(ftpUploadConfigBaseModel, result); } } } } }
|