Browse Source

커밋 수정

main
최승민 1 week ago
parent
commit
bbb117e58a
  1. 104
      leak_test_project/Services/LotEngravingService.cs

104
leak_test_project/Services/LotEngravingService.cs

@ -1,104 +0,0 @@
using System;
using System.Collections.Generic;
using System.IO;
using System.Text.Json;
namespace leak_test_project.Services
{
/// <summary>
/// 로트번호(Lot No)별 각인 순번을 저장하고 관리하는 서비스.
/// DB COUNT(*) 의존성을 제거하고, Lot No별로 검사할 때마다 무조건 +1씩 증가하며,
/// Lot No 변경 시 1부터 시작하고, 기존 Lot No로 복귀 시 이전 진행 번호부터 이어받아 증가합니다.
/// </summary>
public class LotEngravingService
{
private static readonly object _fileLock = new object();
private static LotEngravingService _instance;
public static LotEngravingService Instance => _instance ??= new LotEngravingService();
private readonly string _filePath;
private readonly Dictionary<string, int> _sequences = new Dictionary<string, int>(StringComparer.OrdinalIgnoreCase);
public LotEngravingService()
{
string appDir = AppDomain.CurrentDomain.BaseDirectory;
_filePath = Path.Combine(appDir, "lot_sequences.json");
LoadSequences();
}
private void LoadSequences()
{
lock (_fileLock)
{
try
{
if (File.Exists(_filePath))
{
string json = File.ReadAllText(_filePath);
var data = JsonSerializer.Deserialize<Dictionary<string, int>>(json);
if (data != null)
{
_sequences.Clear();
foreach (var kvp in data)
{
_sequences[kvp.Key] = kvp.Value;
}
}
}
}
catch (Exception ex)
{
System.Diagnostics.Debug.WriteLine($"[LotEngravingService] LoadSequences Error: {ex.Message}");
}
}
}
private void SaveSequences()
{
lock (_fileLock)
{
try
{
string json = JsonSerializer.Serialize(_sequences, new JsonSerializerOptions { WriteIndented = true });
File.WriteAllText(_filePath, json);
}
catch (Exception ex)
{
System.Diagnostics.Debug.WriteLine($"[LotEngravingService] SaveSequences Error: {ex.Message}");
}
}
}
/// <summary>
/// 지정한 Lot No의 현재 마지막 각인 순번을 조회합니다. (없을 경우 0)
/// </summary>
public int GetLastSequence(string lotNo)
{
if (string.IsNullOrWhiteSpace(lotNo)) return 0;
string key = lotNo.Trim();
lock (_fileLock)
{
return _sequences.TryGetValue(key, out int seq) ? seq : 0;
}
}
/// <summary>
/// 지정한 Lot No의 다음 각인 순번을 가져오고, 카운터를 +1 증가시켜 영구 저장합니다.
/// </summary>
public int GetNextSequence(string lotNo)
{
if (string.IsNullOrWhiteSpace(lotNo)) lotNo = "DEFAULT";
string key = lotNo.Trim();
lock (_fileLock)
{
int current = _sequences.TryGetValue(key, out int seq) ? seq : 0;
int next = current + 1;
_sequences[key] = next;
SaveSequences();
return next;
}
}
}
}
Loading…
Cancel
Save