Files
manual_wpf/csharp_mapping_usage.cs
2025-08-12 14:33:18 +09:00

456 lines
16 KiB
C#

using System;
using System.Collections.Generic;
using System.IO;
using System.Text.Json;
using System.Text.Json.Serialization;
// 매핑 테이블 데이터 구조 정의
public class MappingTableData
{
[JsonPropertyName("mapping_table")]
public MappingTable MappingTable { get; set; } = default!;
}
public class MappingTable
{
[JsonPropertyName("ailabel_to_systems")]
public Dictionary<string, SystemFields> AilabelToSystems { get; set; } = default!;
[JsonPropertyName("system_mappings")]
public SystemMappings SystemMappings { get; set; } = default!;
}
public class SystemFields
{
[JsonPropertyName("molit")]
public string Molit { get; set; } = default!;
[JsonPropertyName("expressway")]
public string Expressway { get; set; } = default!;
[JsonPropertyName("railway")]
public string Railway { get; set; } = default!;
[JsonPropertyName("docaikey")]
public string DocAiKey { get; set; } = default!;
}
public class SystemMappings
{
[JsonPropertyName("expressway_to_transportation")]
public Dictionary<string, string> ExpresswayToTransportation { get; set; } = default!;
}
// 필드 매퍼 클래스
public class FieldMapper
{
private readonly MappingTableData _mappingData;
public FieldMapper(MappingTableData mappingData)
{
_mappingData = mappingData;
}
/// <summary>
/// JSON 파일에서 매핑 테이블을 로드합니다.
/// </summary>
public static FieldMapper LoadFromFile(string jsonFilePath)
{
try
{
string jsonContent = File.ReadAllText(jsonFilePath, System.Text.Encoding.UTF8);
Console.WriteLine($"[DEBUG] 매핑 테이블 JSON 파일 크기: {jsonContent.Length} bytes");
// JSON 내용 정리 (주석 제거 등)
jsonContent = CleanJsonContent(jsonContent);
var options = new JsonSerializerOptions
{
PropertyNameCaseInsensitive = true,
Encoder = System.Text.Encodings.Web.JavaScriptEncoder.UnsafeRelaxedJsonEscaping,
AllowTrailingCommas = true
};
var mappingData = JsonSerializer.Deserialize<MappingTableData>(jsonContent, options);
Console.WriteLine($"[DEBUG] 매핑 테이블 로드 성공: {mappingData?.MappingTable?.AilabelToSystems?.Count ?? 0}개 항목");
return new FieldMapper(mappingData!);
}
catch (JsonException jsonEx)
{
Console.WriteLine($"❌ 매핑 테이블 JSON 파싱 오류: {jsonEx.Message}");
Console.WriteLine($"❌ 파일: {jsonFilePath}");
if (File.Exists(jsonFilePath))
{
string content = File.ReadAllText(jsonFilePath);
Console.WriteLine($"❌ JSON 내용 미리보기 (첫 500자):");
Console.WriteLine(content.Length > 500 ? content.Substring(0, 500) + "..." : content);
}
throw new Exception($"매핑 테이블 JSON 파일 파싱 실패: {jsonEx.Message}\n파일: {jsonFilePath}");
}
catch (Exception ex)
{
Console.WriteLine($"❌ 매핑 테이블 로드 중 오류: {ex.Message}");
throw;
}
}
/// <summary>
/// JSON 내용을 정리하여 파싱 가능한 상태로 만듭니다.
/// 주석 제거 및 기타 무효한 문자 처리
/// </summary>
/// <param name="jsonContent">원본 JSON 내용</param>
/// <returns>정리된 JSON 내용</returns>
private static string CleanJsonContent(string jsonContent)
{
if (string.IsNullOrEmpty(jsonContent))
return jsonContent;
try
{
// 줄별로 처리하여 주석 제거
var lines = jsonContent.Split('\n');
var cleanedLines = new List<string>();
bool inMultiLineComment = false;
foreach (string line in lines)
{
string processedLine = line;
// 멀티라인 주석 처리 (/* */)
if (inMultiLineComment)
{
int endIndex = processedLine.IndexOf("*/");
if (endIndex >= 0)
{
processedLine = processedLine.Substring(endIndex + 2);
inMultiLineComment = false;
}
else
{
continue; // 전체 라인이 주석
}
}
// 멀티라인 주석 시작 확인
int multiLineStart = processedLine.IndexOf("/*");
if (multiLineStart >= 0)
{
int multiLineEnd = processedLine.IndexOf("*/", multiLineStart + 2);
if (multiLineEnd >= 0)
{
// 같은 라인에서 시작하고 끝나는 주석
processedLine = processedLine.Substring(0, multiLineStart) +
processedLine.Substring(multiLineEnd + 2);
}
else
{
// 멀티라인 주석 시작
processedLine = processedLine.Substring(0, multiLineStart);
inMultiLineComment = true;
}
}
// 싱글라인 주석 제거 (//) - 문자열 내부의 //는 제외
bool inString = false;
bool escaped = false;
int commentIndex = -1;
for (int i = 0; i < processedLine.Length - 1; i++)
{
char current = processedLine[i];
char next = processedLine[i + 1];
if (escaped)
{
escaped = false;
continue;
}
if (current == '\\')
{
escaped = true;
continue;
}
if (current == '"')
{
inString = !inString;
continue;
}
if (!inString && current == '/' && next == '/')
{
commentIndex = i;
break;
}
}
if (commentIndex >= 0)
{
processedLine = processedLine.Substring(0, commentIndex);
}
// 빈 라인이 아니면 추가
if (!string.IsNullOrWhiteSpace(processedLine))
{
cleanedLines.Add(processedLine);
}
}
string result = string.Join("\n", cleanedLines);
Console.WriteLine($"[DEBUG] 매핑 테이블 JSON 정리 완료: {jsonContent.Length} -> {result.Length} bytes");
return result;
}
catch (Exception ex)
{
Console.WriteLine($"❌ 매핑 테이블 JSON 정리 중 오류: {ex.Message}");
// 정리 실패시 원본 반환
return jsonContent;
}
}
/// <summary>
/// AI 라벨을 고속도로공사 필드명으로 변환
/// </summary>
public string? AilabelToExpressway(string ailabel)
{
if (_mappingData.MappingTable.AilabelToSystems.TryGetValue(ailabel, out var systemFields))
{
return systemFields.Expressway;
}
return null;
}
/// <summary>
/// AI 라벨을 DocAiKey 값으로 변환
/// </summary>
public string? AilabelToDocAiKey(string ailabel)
{
if (_mappingData.MappingTable.AilabelToSystems.TryGetValue(ailabel, out var systemFields))
{
return systemFields.DocAiKey;
}
return null;
}
/// <summary>
/// 고속도로공사 필드명을 교통부 필드명으로 변환
/// </summary>
public string? ExpresswayToTransportation(string expresswayField)
{
if (_mappingData.MappingTable.SystemMappings.ExpresswayToTransportation.TryGetValue(expresswayField, out var transportationField))
{
return transportationField;
}
return null;
}
/// <summary>
/// DocAiKey 값으로부터 해당하는 AI 라벨을 반환
/// </summary>
public string? DocAiKeyToAilabel(string docAiKey)
{
if (string.IsNullOrEmpty(docAiKey))
{
return null;
}
foreach (var kvp in _mappingData.MappingTable.AilabelToSystems)
{
if (kvp.Value.DocAiKey == docAiKey)
{
return kvp.Key;
}
}
return null;
}
/// <summary>
/// Expressway 필드값으로부터 해당하는 AI 라벨을 반환
/// </summary>
public string? ExpresswayToAilabel(string expresswayField)
{
if (string.IsNullOrEmpty(expresswayField))
{
return null;
}
foreach (var kvp in _mappingData.MappingTable.AilabelToSystems)
{
if (kvp.Value.Expressway == expresswayField)
{
return kvp.Key;
}
}
return null;
}
/// <summary>
/// AI 라벨 → 고속도로공사 → 교통부 순서로 변환
/// </summary>
public string? AilabelToTransportationViaExpressway(string ailabel)
{
var expresswayField = AilabelToExpressway(ailabel);
if (!string.IsNullOrEmpty(expresswayField))
{
return ExpresswayToTransportation(expresswayField);
}
return null;
}
/// <summary>
/// AI 라벨에 해당하는 모든 시스템의 필드명을 반환
/// </summary>
public SystemFields? GetAllSystemFields(string ailabel)
{
if (_mappingData.MappingTable.AilabelToSystems.TryGetValue(ailabel, out var systemFields))
{
return systemFields;
}
return null;
}
/// <summary>
/// 여러 AI 라벨을 한번에 고속도로공사 필드명으로 변환
/// </summary>
public Dictionary<string, string?> BatchConvertAilabelToExpressway(IEnumerable<string> ailabels)
{
var results = new Dictionary<string, string?>();
foreach (var label in ailabels)
{
results[label] = AilabelToExpressway(label);
}
return results;
}
/// <summary>
/// 여러 고속도로공사 필드를 한번에 교통부 필드명으로 변환
/// </summary>
public Dictionary<string, string?> BatchConvertExpresswayToTransportation(IEnumerable<string> expresswayFields)
{
var results = new Dictionary<string, string?>();
foreach (var field in expresswayFields)
{
results[field] = ExpresswayToTransportation(field);
}
return results;
}
/// <summary>
/// 매핑 테이블에서 모든 DocAiKey 값의 목록을 반환합니다.
/// </summary>
public List<string> GetAllDocAiKeys()
{
var docAiKeys = new List<string>();
foreach (var kvp in _mappingData.MappingTable.AilabelToSystems)
{
var docAiKey = kvp.Value.DocAiKey;
if (!string.IsNullOrEmpty(docAiKey))
{
docAiKeys.Add(docAiKey);
}
}
return docAiKeys;
}
}
// 사용 예제 프로그램
//class Program
//{
// static void Main(string[] args)
// {
// try
// {
// // 매핑 테이블 로드
// var mapper = FieldMapper.LoadFromFile("mapping_table.json");
// Console.WriteLine("=== AI 라벨 → 고속도로공사 필드명 변환 ===");
// var testLabels = new[] { "도면명", "편철번호", "도면번호", "Main Title", "계정번호" };
// foreach (var label in testLabels)
// {
// var expresswayField = mapper.AilabelToExpressway(label);
// Console.WriteLine($"{label} → {expresswayField ?? "N/A"}");
// }
// Console.WriteLine("\n=== 고속도로공사 → 교통부 필드명 변환 ===");
// var expresswayFields = new[] { "TD_DNAME_MAIN", "TD_DWGNO", "TD_DWGCODE", "TR_RNUM1" };
// foreach (var field in expresswayFields)
// {
// var transportationField = mapper.ExpresswayToTransportation(field);
// Console.WriteLine($"{field} → {transportationField ?? "N/A"}");
// }
// Console.WriteLine("\n=== AI 라벨 → 고속도로공사 → 교통부 (연속 변환) ===");
// foreach (var label in testLabels)
// {
// var expresswayField = mapper.AilabelToExpressway(label);
// var transportationField = mapper.AilabelToTransportationViaExpressway(label);
// Console.WriteLine($"{label} → {expresswayField ?? "N/A"} → {transportationField ?? "N/A"}");
// }
// Console.WriteLine("\n=== 특정 AI 라벨의 모든 시스템 필드명 ===");
// var allFields = mapper.GetAllSystemFields("도면명");
// if (allFields != null)
// {
// Console.WriteLine("도면명에 해당하는 모든 시스템 필드:");
// Console.WriteLine($" 국토교통부: {allFields.Molit}");
// Console.WriteLine($" 고속도로공사: {allFields.Expressway}");
// Console.WriteLine($" 국가철도공단: {allFields.Railway}");
// Console.WriteLine($" 문서AI키: {allFields.DocAiKey}");
// }
// Console.WriteLine("\n=== 배치 처리 예제 ===");
// var batchResults = mapper.BatchConvertAilabelToExpressway(testLabels);
// foreach (var result in batchResults)
// {
// Console.WriteLine($"배치 변환: {result.Key} → {result.Value ?? "N/A"}");
// }
// }
// catch (Exception ex)
// {
// Console.WriteLine($"오류 발생: {ex.Message}");
// }
// }
// 확장 메서드 (선택사항)
//public static class FieldMapperExtensions
//{
// /// <summary>
// /// 특정 시스템의 필드명을 다른 시스템으로 변환
// /// </summary>
// public static string ConvertBetweenSystems(this FieldMapper mapper, string sourceField, string sourceSystem, string targetSystem)
// {
// // 역방향 조회를 위한 확장 메서드
// foreach (var kvp in mapper._mappingData.MappingTable.AilabelToSystems)
// {
// var systemFields = kvp.Value;
// string sourceFieldValue = sourceSystem switch
// {
// "molit" => systemFields.Molit,
// "expressway" => systemFields.Expressway,
// "railway" => systemFields.Railway,
// "docaikey" => systemFields.DocAiKey,
// _ => null
// };
// if (sourceFieldValue == sourceField)LL
// {
// return targetSystem switch
// {
// "molit" => systemFields.Molit,
// "expressway" => systemFields.Expressway,
// "railway" => systemFields.Railway,
// "docaikey" => systemFields.DocAiKey,
// _ => null
// };
// }
// }
// return null;
// }
//}