Files
manual_wpf/csharp_mapping_usage.cs
2025-07-17 16:26:58 +09:00

242 lines
8.1 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; }
}
public class MappingTable
{
[JsonPropertyName("ailabel_to_systems")]
public Dictionary<string, SystemFields> AilabelToSystems { get; set; }
[JsonPropertyName("system_mappings")]
public SystemMappings SystemMappings { get; set; }
}
public class SystemFields
{
[JsonPropertyName("molit")]
public string Molit { get; set; }
[JsonPropertyName("expressway")]
public string Expressway { get; set; }
[JsonPropertyName("railway")]
public string Railway { get; set; }
[JsonPropertyName("docaikey")]
public string DocAiKey { get; set; }
}
public class SystemMappings
{
[JsonPropertyName("expressway_to_transportation")]
public Dictionary<string, string> ExpresswayToTransportation { get; set; }
}
// 필드 매퍼 클래스
public class FieldMapper
{
private readonly MappingTableData _mappingData;
public FieldMapper(MappingTableData mappingData)
{
_mappingData = mappingData;
}
/// <summary>
/// JSON 파일에서 매핑 테이블을 로드합니다.
/// </summary>
public static FieldMapper LoadFromFile(string jsonFilePath)
{
string jsonContent = File.ReadAllText(jsonFilePath);
var options = new JsonSerializerOptions
{
PropertyNameCaseInsensitive = true,
Encoder = System.Text.Encodings.Web.JavaScriptEncoder.UnsafeRelaxedJsonEscaping
};
var mappingData = JsonSerializer.Deserialize<MappingTableData>(jsonContent, options);
return new FieldMapper(mappingData);
}
/// <summary>
/// AI 라벨을 고속도로공사 필드명으로 변환
/// </summary>
public string AilabelToExpressway(string ailabel)
{
if (_mappingData.MappingTable.AilabelToSystems.TryGetValue(ailabel, out var systemFields))
{
return systemFields.Expressway;
}
return null;
}
/// <summary>
/// 고속도로공사 필드명을 교통부 필드명으로 변환
/// </summary>
public string ExpresswayToTransportation(string expresswayField)
{
if (_mappingData.MappingTable.SystemMappings.ExpresswayToTransportation.TryGetValue(expresswayField, out var transportationField))
{
return transportationField;
}
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;
}
}
// 사용 예제 프로그램
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)
{
return targetSystem switch
{
"molit" => systemFields.Molit,
"expressway" => systemFields.Expressway,
"railway" => systemFields.Railway,
"docaikey" => systemFields.DocAiKey,
_ => null
};
}
}
return null;
}
}