for Push.

This commit is contained in:
nobobo
2026-06-01 13:23:34 +09:00
parent 9e093ea99f
commit a6550e9928
93 changed files with 10393 additions and 0 deletions
+51
View File
@@ -0,0 +1,51 @@
using KssSmaPlaLib.Batch.Interface;
using KssSmaPlaLib.Cleanup;
using KssSmaPlaLib.Commons;
using KssSmaPlaLib.Ini;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using static KssSmaPlaLib.Batch.Sftp.SftpStepHandlerUxer;
namespace KssSmaPlaLib.Batch.Cleanup
{
public class CleanupStepHandlerUxer
{
public FuncCleanupByIniResultDto TryFuncCleanupByIni(
string iniFilePath,
string secsionName)
{
const string KEY_FOLDER_PATH = "FolderPath";
var result = new FuncCleanupByIniResultDto();
var cuDic = IniReferenceResolveUxer.ReadSectionWithReference(iniFilePath, secsionName);
var folderPath = DictionaryUxer.GetValueOrDefault(cuDic, KEY_FOLDER_PATH, string.Empty);
if (string.IsNullOrEmpty(folderPath))
{
LoggerUxer.Info($"フォルダパスが未指定のため、クリーンアップ機能をスキップしました。[キー:{KEY_FOLDER_PATH}]");
result.Succeeded = true;
return result;
}
if (!FileCleanupUxer.TryFileCleanupByDic(cuDic, out var messageForMe))
{
result.ResultMessage = $"ファイルのクリーンナップに失敗しました。[エラー内容:{messageForMe}]";
result.ExitCode = (int)RetCodeUxer.RetCode.CleanupError;
result.Succeeded = false;
return result;
}
result.Succeeded = true;
return result;
}
public class FuncCleanupByIniResultDto : IFuncResultDto
{
public bool Succeeded { get; set; } = false;
public int ExitCode { get; set; } = 0;
public string ResultMessage { get; set; } = string.Empty;
}
}
}
+96
View File
@@ -0,0 +1,96 @@
using KssSmaPlaLib.Commons;
using System;
using System.Collections.Generic;
using System.Globalization;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace KssSmaPlaLib.Batch.Commons
{
public static class CommonUxer
{
public const string DIC_KEY_TITLE = "Title";
public const string DIC_KEY_DAYS_TO_START_OFFSET = "DaysToStartOffset";
public const string DIC_KEY_DAYS_TO_END_OFFSET = "DaysToEndOffset";
public static bool TryGetStartDate(Dictionary<string, string> iniDic, out DateTime startDate, out string resultMessage)
{
startDate = DateTime.Today;
resultMessage = string.Empty;
// 開始日
DateTime? fromDate = null;
if (VariableUxer.Args.Count >= 2)
{
if (DateTime.TryParseExact(
VariableUxer.Args[1].Substring(1),
"yyyy/MM/dd",
CultureInfo.InvariantCulture,
DateTimeStyles.None,
out var d) == false)
{
resultMessage = $"起動時パラメータの開始日(第2引数)が不正です。[起動時パラメータ:{string.Join(",", VariableUxer.Args)}]";
return false;
}
fromDate = d;
}
// 起動時パラメータで開始日指定あり
if (fromDate != null)
{
startDate = (DateTime)fromDate;
return true;
}
// 起動時パラメータで開始日指定なし
// オフセット日数取得
var daysToStartOffset = DictionaryUxer.GetValueOrDefault(iniDic, DIC_KEY_DAYS_TO_START_OFFSET, "0");
if (int.TryParse(daysToStartOffset, out var days) == false)
{
// 加算日数エラー
resultMessage = $"設定ファイルの開始日オフセット(加算日数)の設定値が不正です。[{daysToStartOffset}]";
return false;
}
// システム日付(本日日)に加算日数を加算して開始日とする
startDate = DateTime.Today.AddDays(days);
return true;
}
/// <summary>
/// 終了日のオフセット指定があるときだけ、終了日を取得する
/// </summary>
/// <param name="iniDic"></param>
/// <param name="startDate"></param>
/// <param name="endDate"></param>
/// <param name="resultMessage"></param>
/// <returns></returns>
public static bool TryGetEndDate(Dictionary<string, string> iniDic, DateTime startDate, out DateTime? endDate, out string resultMessage)
{
endDate = null;
resultMessage = string.Empty;
// オフセット日数取得
var daysToEndOffset = DictionaryUxer.GetValueOrDefault(iniDic, DIC_KEY_DAYS_TO_END_OFFSET, string.Empty);
if (string.IsNullOrEmpty(daysToEndOffset))
{
// オフセット日数なしのとき、終了日なし
return true;
}
if (int.TryParse(daysToEndOffset, out var days) == false)
{
// 加算日数エラー
resultMessage = $"設定ファイルの終了日オフセット(加算日数)の設定値が不正です。[{daysToEndOffset}]";
return false;
}
// 開始日に加算日数を加算して終了日とする
endDate = startDate.AddDays(days);
return true;
}
}
}
+14
View File
@@ -0,0 +1,14 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace KssSmaPlaLib.Batch.DTO
{
public class StepDto
{
public string StepNo { get; set; } = string.Empty;
public string StepSignature { get; set; } = string.Empty;
}
}
+14
View File
@@ -0,0 +1,14 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace KssSmaPlaLib.Batch.DTO
{
public class StepInfoDto
{
public string Title { get; set; } = string.Empty;
public List<StepDto> StepList { get; set; } = new List<StepDto>();
}
}
+56
View File
@@ -0,0 +1,56 @@
using KssSmaPlaLib.AutoTest.Script;
using Serilog.Core;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using KssSmaPlaLib.Batch.Cleanup;
using KssSmaPlaLib.Batch.Sftp;
using KssSmaPlaLib.Batch.Interface;
using KssSmaPlaLib.Batch.Backup;
using KssSmaPlaLib.Batch.Exchange;
using KssSmaPlaLib.Batch.IO;
namespace KssSmaPlaLib.Batch.Dispatch
{
public class StepDispatcherUxer
{
// 利用可能ステップハンドラー
private readonly Dictionary<string, Func<string, string, IFuncResultDto>> _handlers;
public StepDispatcherUxer()
{
_handlers = new Dictionary<string, Func<string, string, IFuncResultDto>>()
{
{ "Cleanup", (iniPath, section) => new CleanupStepHandlerUxer().TryFuncCleanupByIni(iniPath,section) },
{ "SFTPUpload", (iniPath, section) => new SftpStepHandlerUxer().TryFuncSftpUploadByIni(iniPath,section) },
{ "SFTPDownload", (iniPath, section) => new SftpStepHandlerUxer().TryFuncSftpDownloadByIni(iniPath,section) },
{ "SFTPDelete", (iniPath, section) => new SftpStepHandlerUxer().TryFuncSftpDeleteByIni(iniPath,section) },
{ "Backup", (iniPath, section) => new BackupStepHandlerUxer().TryFuncBackupByIni(iniPath,section) },
{ "CsvToDb", (iniPath, section) => new CsvToDbStepHandlerUxer().TryFuncCsvToDbByIni(iniPath,section) },
{ "Delete", (iniPath, section) => new FileStepHandlerUxer().TryFuncFileDeleteByIni(iniPath,section) },
};
}
public bool TryDispatch(string stepName, string iniPath, string sectionName, out string resultMessage, out IFuncResultDto funcResultDto)
{
resultMessage = string.Empty;
funcResultDto = null;
if (_handlers.TryGetValue(stepName, out var func))
{
funcResultDto = func(iniPath,sectionName);
resultMessage = funcResultDto.ResultMessage;
return funcResultDto.Succeeded;
}
resultMessage = $"機能名が登録されていません。[機能名:{stepName}]";
return false;
}
}
public class TryDispatchResultDto : IFuncResultDto
{
public string ResultMessage { get; set; } = string.Empty;
public int ExitCode { get; set; } = 0;
public bool Succeeded { get; set; } = false;
}
}
+357
View File
@@ -0,0 +1,357 @@
using KssSmaPlaLib.Batch.Commons;
using KssSmaPlaLib.Batch.Interface;
using KssSmaPlaLib.Commons;
using KssSmaPlaLib.Exchange;
using KssSmaPlaLib.Ini;
using KssSmaPlaLib.IO.Database;
using KssSmaPlaLib.IO.File;
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Runtime.InteropServices.ComTypes;
using System.Text;
using System.Threading.Tasks;
namespace KssSmaPlaLib.Batch.Exchange
{
public class CsvToDbStepHandlerUxer
{
public FuncCsvToDbByIniResultDto TryFuncCsvToDbByIni(
string iniFilePath,
string sectionName)
{
const string KEY_SQL_FOLDER_PATH = "SqlFolderPath";
const string KEY_CSV_FILE_NAME_PATTERNS = "CsvFileNamePatterns";
const string KEY_INSERT_SQL_FILES = "InsertSqlFiles";
const string KEY_DUP_CHECK_SQL_FILES = "DupCheckSqlFiles";
const string KEY_DELETE_BEFORE_INSERT_SQL_FILES = "DeleteBeforeInsertSqlFiles";
const string KEY_CSV_ENCODING = "CsvEncoding";
const string KEY_DB_PROVIDER_NAME = "DbProviderName";
const string KEY_DB_CONNECTION_STRING = "DbConnectionString";
const string KEY_CSV_FOLDER_PATH = "CsvFolderPath";
var result = new FuncCsvToDbByIniResultDto();
var iniDic = IniReferenceResolveUxer.ReadSectionWithReference(iniFilePath, sectionName);
// 開始日
if (!CommonUxer.TryGetStartDate(iniDic, out var startDate, out var messageForMe))
{
var msg = $"開始日の取得に失敗しました。[エラー内容:{messageForMe}]";
LoggerUxer.Error(msg);
result.Succeeded = false;
result.ExitCode = (int)RetCodeUxer.RetCode.StartDateError;
result.ResultMessage = msg;
return result;
}
// DB接続確認
if (DbUxer.TestConnectByDic(iniDic, out messageForMe) == false)
{
var msg = $"DB接続確認で失敗しました。[エラー内容:{messageForMe}]";
LoggerUxer.Error(msg);
result.Succeeded = false;
result.ExitCode = (int)RetCodeUxer.RetCode.DbConnectError;
result.ResultMessage = msg;
return result;
}
LoggerUxer.Info($"DB接続確認が成功しました。[プロバイダ名:{DictionaryUxer.GetValueOrDefault(iniDic, KEY_DB_PROVIDER_NAME, string.Empty)},接続文字列:{DictionaryUxer.GetValueOrDefault(iniDic, KEY_DB_CONNECTION_STRING, string.Empty)}]");
if (!string.IsNullOrEmpty(messageForMe))
{
// Infoメッセージがあれば出力
LoggerUxer.Info(messageForMe);
}
// SQLフォルダパス
var sqlFolderPath = DictionaryUxer.GetValueOrDefault(iniDic, KEY_SQL_FOLDER_PATH, string.Empty);
if (string.IsNullOrEmpty(sqlFolderPath))
{
var msg = $"設定ファイルにSQLフォルダパスが設定されていません。[キー:{KEY_SQL_FOLDER_PATH}]";
LoggerUxer.Error(msg);
result.Succeeded = false;
result.ExitCode = (int)RetCodeUxer.RetCode.SqlFolderPathNoSetting;
result.ResultMessage = msg;
return result;
}
// SQLのロード
if (!DbUxer.TryLoadSqlFromText(sqlFolderPath, out messageForMe))
{
var msg = $"SQLのロードに失敗しました。[フォルダパス:SQL,エラー内容:{messageForMe}";
LoggerUxer.Error(msg);
result.Succeeded = false;
result.ExitCode = (int)RetCodeUxer.RetCode.SqlFileLoadError;
result.ResultMessage = msg;
return result;
}
LoggerUxer.Info(messageForMe); // 正常メッセージ
// CSVファイル名パターン群
var csvPatterns = DictionaryUxer.GetValueOrDefault(iniDic, KEY_CSV_FILE_NAME_PATTERNS, string.Empty);
if (string.IsNullOrEmpty(csvPatterns))
{
var msg = $"設定ファイルのCSVファイル名パターンが設定されていません。[キー:{KEY_CSV_FILE_NAME_PATTERNS}]";
LoggerUxer.Error(msg);
result.Succeeded = false;
result.ExitCode = (int)RetCodeUxer.RetCode.CsvFileNamePatternsNotSetting;
result.ResultMessage = msg;
return result;
}
var csvPatternArray = csvPatterns.Split(',');
// INSERT文SQLファイル群
var insertSqlFiles = DictionaryUxer.GetValueOrDefault(iniDic, KEY_INSERT_SQL_FILES, string.Empty);
var insertSqlFilesArray = insertSqlFiles.Split(',');
// 重複チェックSELECT文SQLファイル群
var dupCheckSqlFiles = DictionaryUxer.GetValueOrDefault(iniDic, KEY_DUP_CHECK_SQL_FILES, string.Empty);
var dupCheckSqlFilesArray = dupCheckSqlFiles.Split(',');
// 挿入前の削除SQLファイル群
var deleteBeforeInsertSqlFiles = DictionaryUxer.GetValueOrDefault(iniDic, KEY_DELETE_BEFORE_INSERT_SQL_FILES, string.Empty);
var deleteBeforeInsertSqlFilesArray = deleteBeforeInsertSqlFiles.Split(',');
// 要素数チェック
if (csvPatternArray.Length != insertSqlFilesArray.Length ||
csvPatternArray.Length != dupCheckSqlFilesArray.Length ||
csvPatternArray.Length != deleteBeforeInsertSqlFilesArray.Length)
{
var msg = $"設定ファイルの要素数が異なります。[{KEY_CSV_FILE_NAME_PATTERNS},{KEY_INSERT_SQL_FILES},{KEY_DUP_CHECK_SQL_FILES},{KEY_DELETE_BEFORE_INSERT_SQL_FILES}]";
LoggerUxer.Error(msg);
result.Succeeded = false;
result.ExitCode = (int)RetCodeUxer.RetCode.IniFileItemCountError;
result.ResultMessage = msg;
return result;
}
LoggerUxer.Info($"設定ファイルの要素数が一致しています。[要素数:{csvPatternArray.Length},キー:{KEY_CSV_FILE_NAME_PATTERNS},{KEY_INSERT_SQL_FILES},{KEY_DUP_CHECK_SQL_FILES},{KEY_DELETE_BEFORE_INSERT_SQL_FILES}]");
var encodingTypeName = DictionaryUxer.GetValueOrDefault(iniDic, KEY_CSV_ENCODING, "utf8");
var encodingType = EncodingUxer.GetEncodingType(encodingTypeName);
if (encodingType == null)
{
encodingType = EncodingUxer.EncodingType.Utf8WithoutBom;
LoggerUxer.Warn($"CSVエンコーディングタイプ名が不正です。UFT-8で処理します。[キー:{KEY_CSV_ENCODING},値:{encodingTypeName}]");
}
try
{
// DB接続
if (!DbUxer.TryCreateByDic(iniDic, out DbUxer con, out messageForMe, false/* ★★★ トランザクション有無:true */))
{
LoggerUxer.Error(messageForMe);
result.Succeeded = false;
result.ExitCode = (int)RetCodeUxer.RetCode.DbConnectError;
result.ResultMessage = messageForMe;
return result;
}
LoggerUxer.Info($"DBに接続しました。[プロバイダ名:{DictionaryUxer.GetValueOrDefault(iniDic, KEY_DB_PROVIDER_NAME, string.Empty)},接続文字列:{DictionaryUxer.GetValueOrDefault(iniDic, KEY_DB_CONNECTION_STRING, string.Empty)}]");
var csvFolderPath = DictionaryUxer.GetValueOrDefault(iniDic, KEY_CSV_FOLDER_PATH, ".");
LoggerUxer.Info($"[CSVフォルダパス:{csvFolderPath}]");
using (con)
{
// CSVパターン毎
for (var cnt = 0; cnt < csvPatternArray.Length; cnt++)
{
LoggerUxer.Info($"[CSVパターン{cnt + 1}:{csvPatternArray[cnt]}]");
var insertSqlFile = insertSqlFilesArray[cnt];
var csvFileNamePattern = csvPatternArray[cnt];
var dupCheckSqlFile = dupCheckSqlFilesArray[cnt];
var deleteBeforeInsertSqlFile = deleteBeforeInsertSqlFilesArray[cnt];
LoggerUxer.Info($"[該当ファイル数:{Directory.GetFiles(csvFolderPath, csvFileNamePattern).Length}]");
// ファイル毎
foreach (var path in Directory.GetFiles(csvFolderPath, csvFileNamePattern))
{
if (deleteBeforeInsertSqlFile == "DeleteBeforeInsertWeOrder")
{
// 溶接専用!🔥🌊
// 最小日の取得(溶接専用ロジック)
var minDic = new Dictionary<int, DateTime>();
// CSV読み込み+最小値取得
if (!GetMinValueByKeyFromCsvFiles<int, DateTime>(path, "line_id", "work_date", ref minDic, out var messageForMe0))
{
LoggerUxer.Error(messageForMe0);
result.Succeeded = false;
result.ExitCode = (int)RetCodeUxer.RetCode.InsertDbFromCsvFilesError;
result.ResultMessage = messageForMe;
return result;
}
LoggerUxer.Info($"ライン毎の最小日の取得を完了");
if (!string.IsNullOrEmpty(deleteBeforeInsertSqlFile))
{
// 挿入前の削除SQLを実行
LoggerUxer.Info($"挿入前の削除SQLを実行[SQLファイル:{deleteBeforeInsertSqlFile}]");
var totalResultCount = 0;
foreach (var kv in minDic)
{
var resultCount = 0;
if (!con.ExecuteNonQuery(
deleteBeforeInsertSqlFile,
new Dictionary<string, object>()
{
{ "LINE_ID", kv.Key },
{ "START_DATE", kv.Value }
},
out resultCount,
out messageForMe))
{
var msg = $"挿入前の削除が失敗しました。[エラー内容:{messageForMe}]";
LoggerUxer.Error(msg);
result.Succeeded = false;
result.ExitCode = (int)RetCodeUxer.RetCode.DeleteBeforeInsertException;
result.ResultMessage = msg;
return result;
}
totalResultCount += resultCount;
LoggerUxer.Info($"挿入前の削除成功[ラインID:{kv.Key},開始日:{kv.Value},削除件数:{resultCount}]");
}
LoggerUxer.Info($"挿入前の削除SQLを完了[更新件数:{totalResultCount}]");
}
else
{
LoggerUxer.Info($"挿入前の削除処理はSQLが未指定のためスキップしました。[キー:{KEY_DELETE_BEFORE_INSERT_SQL_FILES}]");
}
}
else
{
if (!string.IsNullOrEmpty(deleteBeforeInsertSqlFile))
{
// 挿入前の削除SQLを実行
LoggerUxer.Info($"挿入前の削除SQLを実行[SQLファイル:{deleteBeforeInsertSqlFile}]");
var totalResultCount = 0;
if (!con.ExecuteNonQuery(
deleteBeforeInsertSqlFile,
new Dictionary<string, object>()
{
{ "START_DATE", startDate }
},
out var resultCount,
out messageForMe))
{
var msg = $"挿入前の削除が失敗しました。[エラー内容:{messageForMe}]";
LoggerUxer.Error(msg);
result.Succeeded = false;
result.ExitCode = (int)RetCodeUxer.RetCode.DeleteBeforeInsertException;
result.ResultMessage = msg;
return result;
}
totalResultCount += resultCount;
LoggerUxer.Info($"挿入前の削除SQLを完了[更新件数:{totalResultCount}]");
}
else
{
LoggerUxer.Info($"挿入前の削除処理はSQLが未指定のためスキップしました。[キー:{KEY_DELETE_BEFORE_INSERT_SQL_FILES}]");
}
}
// CSV読み込み+DB挿入
LoggerUxer.Info($"CSV読み込み+DB挿入を実行[ファイルパス:{path}]");
if (!CsvDbSyncUxer.InsertDbFromCsvFiles(iniDic, con, path, dupCheckSqlFile, insertSqlFile, out messageForMe, (EncodingUxer.EncodingType)encodingType))
{
LoggerUxer.Error(messageForMe);
result.Succeeded = false;
result.ExitCode = (int)RetCodeUxer.RetCode.InsertDbFromCsvFilesError;
result.ResultMessage= messageForMe;
return result;
}
LoggerUxer.Info($"CSV読み込み+DB挿入を完了");
}
}
// コミット
con.Commit();
}
LoggerUxer.Info($"DB処理が完了し、DB接続を終了しました。");
}
catch (Exception ex)
{
var msg = $"DB処理にてエラーが発生しました。[エラー内容:{ex.Message}]";
LoggerUxer.Error(msg);
result.Succeeded = false;
result.ExitCode = (int)RetCodeUxer.RetCode.CsvToDbException;
result.ResultMessage = msg;
return result;
}
result.Succeeded = true;
return result;
}
/// <summary>
/// CSVよりキー毎に最小値を取得し辞書で返す
/// </summary>
/// <typeparam name="TKeyType"></typeparam>
/// <typeparam name="TValueType"></typeparam>
/// <param name="csvFilePath"></param>
/// <param name="keyColName"></param>
/// <param name="valueColName"></param>
/// <param name="minDic"></param>
/// <param name="resultMessage"></param>
/// <param name="encodingType"></param>
/// <returns></returns>
public static bool GetMinValueByKeyFromCsvFiles<TKeyType, TValueType>(
string csvFilePath,
string keyColName,
string valueColName,
ref Dictionary<TKeyType, TValueType> minDic,
out string resultMessage,
EncodingUxer.EncodingType encodingType = EncodingUxer.EncodingType.Utf8WithoutBom)
where TKeyType : IComparable<TKeyType>
where TValueType : IComparable<TValueType>
{
// 👇参照型(追記とする)ゆえやめた
//minDic = new Dictionary<TKeyType, TValueType>();
resultMessage = string.Empty;
try
{
// CSV読み込み
if (!CsvUxer.ImportFromCsv(csvFilePath, out var rs, out var messageForMe, encodingType))
{
resultMessage = messageForMe;
return false;
}
//LoggerUxer.Info($"[CSV件数:{rs.Count}]");
foreach (var rec in rs)
{
TKeyType key;
TValueType val;
try { key = (TKeyType)Convert.ChangeType(rec[keyColName], typeof(TKeyType)); }
catch (Exception) { continue; };
try { val = (TValueType)Convert.ChangeType(rec[valueColName], typeof(TValueType)); }
catch (Exception) { continue; }
if (minDic.ContainsKey(key))
{
minDic[key] = Comparer<TValueType>.Default.Compare(val, minDic[key]) < 0 ? val : minDic[key];
}
else
{
minDic[key] = val;
}
}
return true;
}
catch (Exception ex)
{
resultMessage = $"最小日時取得時にエラーが発生しました。[エラー内容:{ex.Message}]";
return false;
}
}
}
public class FuncCsvToDbByIniResultDto : IFuncResultDto
{
public bool Succeeded { get; set; } = false;
public int ExitCode { get; set; } = 0;
public string ResultMessage { get; set; } = string.Empty;
}
}
+87
View File
@@ -0,0 +1,87 @@
using KssSmaPlaLib.Batch.Interface;
using KssSmaPlaLib.Commons;
using KssSmaPlaLib.Ini;
using KssSmaPlaLib.IO.File;
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using static System.Net.WebRequestMethods;
namespace KssSmaPlaLib.Batch.IO
{
public class FileStepHandlerUxer
{
public FuncFileDeleteByIniResultDto TryFuncFileDeleteByIni(
string iniFilePath,
string sectionName)
{
const string KEY_FOLDER_PATH = "FolderPath";
const string KEY_FILE_NAME_PATTERNS = "FileNamePatterns";
FuncFileDeleteByIniResultDto result = new FuncFileDeleteByIniResultDto();
var iniDic = IniReferenceResolveUxer.ReadSectionWithReference(iniFilePath, sectionName);
var folderPath = DictionaryUxer.GetValueOrDefault(iniDic, KEY_FOLDER_PATH, string.Empty);
if (string.IsNullOrEmpty(folderPath))
{
var msg = $"設定ファイルのフォルダパスが指定されていません。[キー:{KEY_FOLDER_PATH}]";
LoggerUxer.Error(msg);
result.Succeeded = false;
result.ExitCode = (int)RetCodeUxer.RetCode.FileDeleteFolderPathNotSetting;
result.ResultMessage = msg;
return result;
}
if (!Directory.Exists(folderPath))
{
var msg = $"設定ファイルのフォルダパスが存在しません。[キー:{KEY_FOLDER_PATH},値:{folderPath}]";
LoggerUxer.Error(msg);
result.Succeeded = false;
result.ExitCode = (int)RetCodeUxer.RetCode.FileDeleteFolderPathNotExist;
result.ResultMessage = msg;
return result;
}
var fileNamePatterns = DictionaryUxer.GetValueOrDefault(iniDic, KEY_FILE_NAME_PATTERNS, string.Empty);
if (string.IsNullOrEmpty(fileNamePatterns))
{
var msg = $"設定ファイルのファイル名パターンが指定されていません。[キー:{KEY_FILE_NAME_PATTERNS}]";
LoggerUxer.Error(msg);
result.Succeeded = false;
result.ExitCode = (int)RetCodeUxer.RetCode.FileDeleteFileNamePatternNotSetting;
result.ResultMessage = msg;
return result;
}
var fileNamePatternsArray = fileNamePatterns.Split(',');
// ローカルのCSVファイルの削除
LoggerUxer.Info($"ファイルの削除を実行");
for (var cnt = 0; cnt < fileNamePatternsArray.Length; cnt++)
{
var fileNamePattern = fileNamePatternsArray[cnt];
LoggerUxer.Info($"[ファイル名パターン:{fileNamePattern}]");
if (!FileUxer.TryDeletePatternFiles(folderPath, fileNamePattern, out var fileCount, out var messageForMe))
{
LoggerUxer.Error(messageForMe);
result.Succeeded = false;
result.ExitCode = (int)RetCodeUxer.RetCode.FileDeletePatternFilesError;
result.ResultMessage = messageForMe;
return result;
}
LoggerUxer.Info($"[削除件数:{fileCount}]");
}
LoggerUxer.Info($"ローカルのCSVファイルの削除を完了");
result.Succeeded = true;
return result;
}
}
public class FuncFileDeleteByIniResultDto : IFuncResultDto
{
public bool Succeeded { get; set; } = false;
public int ExitCode { get; set; } = 0;
public string ResultMessage { get; set; } = string.Empty;
}
}
+15
View File
@@ -0,0 +1,15 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace KssSmaPlaLib.Batch.Interface
{
public interface IFuncResultDto
{
bool Succeeded { get; set; }
int ExitCode { get; set; }
string ResultMessage { get; set; }
}
}
+246
View File
@@ -0,0 +1,246 @@
using KssSmaPlaLib.Batch.Interface;
using KssSmaPlaLib.Commons;
using KssSmaPlaLib.Communications;
using KssSmaPlaLib.Ini;
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Interop;
using static System.Net.WebRequestMethods;
namespace KssSmaPlaLib.Batch.Sftp
{
public class SftpStepHandlerUxer
{
public FuncSftpByIniResultDto TryFuncSftpUploadByIni(
string iniFilePath,
string sessionName)
{
const string KEY_HOST = "Host";
const string KEY_REMOTE_PATH = "RemotePath";
const string KEY_FILE_NAME_PATTERNS = "FileNamePatterns";
const string KEY_IN_FOLDER_PATH = "InFolderPath";
var result = new FuncSftpByIniResultDto();
var iniSftpDic = IniReferenceResolveUxer.ReadSectionWithReference(iniFilePath, sessionName);
var host = DictionaryUxer.GetValueOrDefault(iniSftpDic, KEY_HOST, string.Empty);
if (string.IsNullOrEmpty(host))
{
LoggerUxer.Info("ホストが未指定のためSFTPアップロード機能をスキップしました。");
result.Succeeded = true;
return result;
}
// オブジェクト生成
if (!SftpUxer.TryCreateByDic(iniSftpDic, out SftpUxer sftp, out var messageForMe))
{
result.ResultMessage = $"SFTPアップロード機能でオブジェクト生成に失敗しました。[エラー内容:{messageForMe}]";
result.ExitCode = (int)RetCodeUxer.RetCode.SftpObjectCreateError;
result.Succeeded = false;
return result;
}
// テスト接続
if (!sftp.TestConnect(out messageForMe))
{
result.ResultMessage = $"SFTPアップロード機能でテスト接続に失敗しました。[エラー内容:{messageForMe}]";
result.ExitCode = (int)RetCodeUxer.RetCode.SftpTestConnectError;
result.Succeeded = false;
return result;
}
LoggerUxer.Info($"SFTPテスト接続成功");
// リモートパス
var remotePath = DictionaryUxer.GetValueOrDefault(iniSftpDic, KEY_REMOTE_PATH, string.Empty);
// 入力フォルダパス
var inFolderPath = DictionaryUxer.GetValueOrDefault(iniSftpDic, KEY_IN_FOLDER_PATH, ".");
if (!Directory.Exists(inFolderPath))
{
result.ResultMessage = $"SFTPアップロード機能で入力フォルダパスが存在しません。[{KEY_IN_FOLDER_PATH}:{inFolderPath}]";
result.ExitCode = (int)RetCodeUxer.RetCode.SftpInFolderPathNotExist;
result.Succeeded = false;
return result;
}
// ファイル名パターン
var fileNamePatterns = DictionaryUxer.GetValueOrDefault(iniSftpDic, KEY_FILE_NAME_PATTERNS, "*");
var fileNamePatternList = fileNamePatterns.Split(',').ToList();
// ファイルパスリストの作成
var filePathList = new List<string>();
foreach(var pattern in fileNamePatternList)
{
foreach(var filePath in Directory.GetFiles(inFolderPath, pattern))
{
filePathList.Add(filePath);
}
}
LoggerUxer.Info($"[対象ファイル数:{filePathList.Count},対象ファイルパス:{string.Join(",", filePathList)}]");
if (filePathList.Count == 0)
{
var msg = $"SFTPアップロード機能で対象ファイルが無かったためSTFPアップロードは実行されませんでした。";
LoggerUxer.Info(msg);
result.ResultMessage = msg;
result.Succeeded = true;
return result;
}
// 対象のアップロード
if (!sftp.Upload(filePathList, remotePath, out messageForMe))
{
result.ResultMessage = $"SFTPアップロード機能のアップロードに失敗しました。[エラー内容:{messageForMe}]";
result.ExitCode = (int)RetCodeUxer.RetCode.SftpUploadError;
result.Succeeded = false;
return result;
}
LoggerUxer.Info($"SFTPアップロード機能完了[対象ファイル数:{filePathList.Count},対象ファイル:{string.Join(",", filePathList)},リモートパス:{remotePath},ファイル名パターン:{string.Join(",", fileNamePatternList)}]");
result.Succeeded = true;
return result;
}
public FuncSftpByIniResultDto TryFuncSftpDownloadByIni(
string iniFilePath,
string sectionName)
{
const string KEY_HOST = "Host";
const string KEY_REMOTE_PATH = "RemotePath";
const string KEY_OUT_FOLDER_PATH = "OutFolderPath";
const string KEY_FILE_NAME_PATTERNS = "FileNamePatterns";
var result = new FuncSftpByIniResultDto();
var iniDic = IniReferenceResolveUxer.ReadSectionWithReference(iniFilePath, sectionName);
var host = DictionaryUxer.GetValueOrDefault(iniDic, KEY_HOST, string.Empty);
if (!string.IsNullOrEmpty(host))
{
LoggerUxer.Info($"SFTPダウンロード処理を開始します。");
try
{
// SFTPから対象ファイルをダウンロード
// オブジェクト生成
if (!SftpUxer.TryCreateByDic(iniDic, out SftpUxer sftp, out string messageForMe))
{
LoggerUxer.Error(messageForMe);
result.Succeeded = false;
result.ExitCode = (int)RetCodeUxer.RetCode.SftpObjectCreateError;
result.ResultMessage = messageForMe;
return result;
}
// テスト接続
if (!sftp.TestConnect(out messageForMe))
{
LoggerUxer.Error(messageForMe);
result.Succeeded = false;
result.ExitCode = (int)RetCodeUxer.RetCode.SftpTestConnectError;
result.ResultMessage = messageForMe;
return result;
}
LoggerUxer.Info($"SFTPテスト接続成功");
// パターンにマッチするファイルのダウンロード
var fileNamePatterns = DictionaryUxer.GetValueOrDefault(iniDic, KEY_FILE_NAME_PATTERNS, string.Empty);
if (string.IsNullOrEmpty(fileNamePatterns))
{
var msg = $"SFTPのファイル名パターンが設定されていません。[キー:{KEY_FILE_NAME_PATTERNS}]";
LoggerUxer.Error(msg);
result.Succeeded = false;
result.ExitCode = (int)RetCodeUxer.RetCode.SftpCsvFileNamePatternsNotSetting;
result.ResultMessage = msg;
return result;
}
var remotePath = DictionaryUxer.GetValueOrDefault(iniDic, KEY_REMOTE_PATH, string.Empty);
var outFolderPath = DictionaryUxer.GetValueOrDefault(iniDic, KEY_OUT_FOLDER_PATH, string.Empty);
LoggerUxer.Info($"[リモートパス:{remotePath},CSV一時フォルダ:{outFolderPath}]");
foreach (var pattern in fileNamePatterns.Split(','))
{
LoggerUxer.Info($"[ファイルパターン:{pattern}]");
if (!sftp.DownloadMatchingFiles(remotePath, outFolderPath, out var downloadedFileCount, out messageForMe, pattern))
{
var msg = $"STFPでのダウンロードに失敗しました。[エラー内容:{messageForMe}]";
LoggerUxer.Error(msg);
result.Succeeded = false;
result.ExitCode = (int)RetCodeUxer.RetCode.SftpMatchFilesDownloadError;
result.ResultMessage = msg;
return result;
}
LoggerUxer.Info($"[ダウンロードしたファイル数:{downloadedFileCount}]");
}
result.Succeeded = true;
return result;
}
catch (Exception ex)
{
var msg = $"SFTPダウンロードでエラーが発生しました。[エラー内容:{ex.Message}]";
LoggerUxer.Error(msg);
result.Succeeded = false;
result.ExitCode = (int)RetCodeUxer.RetCode.SftpDownloadException;
result.ResultMessage = msg;
return result;
}
}
else
{
LoggerUxer.Info($"SFTP処理の設定がないため、スキップします。");
result.Succeeded = true;
return result;
}
}
public FuncSftpByIniResultDto TryFuncSftpDeleteByIni(
string iniFilePath,
string sectionName)
{
const string KEY_REMOTE_PATH = "RemotePath";
const string KEY_FILE_NAME_PATTERNS = "FileNamePatterns";
FuncSftpByIniResultDto result = new FuncSftpByIniResultDto();
var iniDic = IniReferenceResolveUxer.ReadSectionWithReference(iniFilePath, sectionName);
// オブジェクト生成
if (!SftpUxer.TryCreateByDic(iniDic, out SftpUxer sftp, out string messageForMe))
{
LoggerUxer.Error(messageForMe);
result.Succeeded = false;
result.ExitCode = (int)RetCodeUxer.RetCode.SftpObjectCreateError;
result.ResultMessage = messageForMe;
return result;
}
foreach (var pattern in iniDic[KEY_FILE_NAME_PATTERNS].Split(','))
{
if (!sftp.DeleteMatchingFiles(iniDic[KEY_REMOTE_PATH], out var deletedFileCount, out messageForMe, pattern))
{
LoggerUxer.Error(messageForMe);
result.Succeeded = false;
result.ExitCode = (int)RetCodeUxer.RetCode.SftpDeleteMatchingFilesError;
result.ResultMessage = messageForMe;
return result;
}
LoggerUxer.Info($"SFTPのファイルを削除しました。[ファイル名パターン:{pattern},削除ファイル数:{deletedFileCount}]");
}
result.Succeeded = true;
return result;
}
public class FuncSftpByIniResultDto: IFuncResultDto
{
public bool Succeeded { get; set; } = false;
public int ExitCode { get; set; } = 0;
public string ResultMessage { get; set; } = string.Empty;
}
}
}
+54
View File
@@ -0,0 +1,54 @@
using KssSmaPlaLib.Batch.DTO;
using KssSmaPlaLib.Commons;
using KssSmaPlaLib.AutoTest.Script;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using KssSmaPlaLib.Batch.Commons;
namespace KssSmaPlaLib.Batch.StepInfo
{
public static class StepInfoUxer
{
public static bool TryCreateStepInfoFromIniDic(
Dictionary<string, string> iniDic,
out StepInfoDto stepInfo,
out string resultMessage)
{
stepInfo = new StepInfoDto
{
StepList = new List<StepDto>()
};
resultMessage = string.Empty;
foreach (var key in iniDic.Keys.OrderBy(m => m))
{
// コメント行は除く
if (key.Trim().StartsWith(";")) continue;
// タイトル
if (key.Trim() == CommonUxer.DIC_KEY_TITLE)
{
stepInfo.Title = iniDic[key];
continue;
}
stepInfo.StepList.Add(new StepDto()
{
StepNo = key.Trim(),
StepSignature = iniDic[key].Trim(),
});
}
if (stepInfo.StepList.Count == 0)
{
resultMessage = $"設定ファイルに処理区分のステップ情報が登録されていません。[タイトル:{stepInfo.Title}]";
return false;
}
return true;
}
}
}