Files
CPXV2/CPXV2 TRA JSON/SystemX.Product.CP.TRA.Extract/DBService.cs
2026-02-11 10:44:06 +09:00

196 lines
8.4 KiB
C#

using Newtonsoft.Json;
using System;
using System.Collections.Generic;
using System.Data.SqlClient;
using System.Diagnostics;
using System.IO;
using System.IO.Compression;
using System.Linq;
using System.Text;
using System.Threading;
using System.Threading.Tasks;
namespace SystemX.Product.CP.TRA.Extract
{
public class DBService
{
public string CPXV2ConnectionString { get; set; }
public string ConnectionString { get; set; }
public List<TestList> GetTestList(Config config)
{
List<TestList> result = new List<TestList>();
// 1. 연결 생성
using (SqlConnection conn = new SqlConnection(CPXV2ConnectionString))
{
try
{
// 2. 실행할 쿼리 작성 (JOIN 포함)
string sql = $@"SELECT [No]
,[TestListFileNo]
,[StepID]
,[StepVersion]
,[StepDesc]
,[SpecMin]
,[SpecMax]
,[Dim]
FROM [CPXV2].[dbo].[VRFY_TestListFileRelease] WITH(NOLOCK) where StepDesc = '{config.MO}'";
SqlCommand cmd = new SqlCommand(sql, conn);
// 타임아웃 설정 (600만 건은 시간이 걸릴 수 있으므로 0(무제한) 또는 넉넉하게 설정)
cmd.CommandTimeout = 300;
conn.Open();
// 3. DataReader로 데이터 읽기
using (SqlDataReader reader = cmd.ExecuteReader())
{
while (reader.Read())
{
var tl = new TestList
{
TestListFileNo = Convert.ToInt32(reader["TestListFileNo"]),
StepID = Convert.ToInt32(reader["StepID"]),
StepVersion = Convert.ToInt32(reader["StepVersion"]),
StepDesc = reader["StepDesc"].ToString(),
SpecMin = reader["SpecMin"].ToString(),
SpecMax = reader["SpecMax"].ToString(),
Dim = reader["Dim"].ToString(),
};
result.Add(tl);
}
}
}
catch (SqlException ex)
{
Console.WriteLine("DB 오류 발생: " + ex.Message);
}
}
Console.WriteLine($"{config.MO} 포함 테스트리스트 개수: {result.Count}");
return result;
}
public void GetLargeData(List<TestList> testList, Config config)
{
int rowCount = 0;
int fileNo = 0;
string fileName = $"";
foreach (var tl in testList)
{
Console.WriteLine($"진행중 테스트리스트(MO:{config.MO}): TestListFileNo:{tl.TestListFileNo}, StepVersion:{tl.StepVersion}");
// 1. 연결 생성
using (SqlConnection conn = new SqlConnection(ConnectionString))
{
try
{
// 2. 실행할 쿼리 작성 (JOIN 포함)
string sql = $@"SELECT * FROM {config.SummaryTable} as Summary WITH(NOLOCK)
JOIN {config.ResultTable} as Result WITH(NOLOCK) ON Summary.No = Result.No
WHERE TestListFileNo = {tl.TestListFileNo} and StepVersion={tl.StepVersion}";
//test code
if(string.IsNullOrEmpty(config.TestCode) == false)
sql += $" and TestCode = '{config.TestCode}'";
//productNo
if(string.IsNullOrEmpty(config.ProductNo) == false)
sql += $" and ProdNo_C = '{config.ProductNo}'";
//TestDT
DateTime endDate = Convert.ToDateTime(config.EndDate).AddDays(1);
sql += $" and '{config.StartDate}' <= Summary.TestDT and Summary.TestDT < '{endDate.ToString("yyyy-MM-dd")}'";
SqlCommand cmd = new SqlCommand(sql, conn);
// 타임아웃 설정 (600만 건은 시간이 걸릴 수 있으므로 0(무제한) 또는 넉넉하게 설정)
cmd.CommandTimeout = 0;
conn.Open();
// 3. DataReader로 데이터 읽기
using (SqlDataReader reader = cmd.ExecuteReader())
{
bool queryLog = false;
while (reader.Read())
{
if (queryLog == false)
{
Console.ForegroundColor = ConsoleColor.DarkYellow;
Thread.Sleep(10);
Console.WriteLine($"Query: {sql}");
Console.ForegroundColor = ConsoleColor.White;
queryLog = true;
}
// 예: 데이터 처리 로직
var val = Decompression(reader["LogData"].ToString());
var date = Convert.ToDateTime(reader["TestDT"]);
var productNo = reader["ProdNo_C"].ToString();
var testCode = reader["TestCode"].ToString();
var host = reader["HostID"].ToString();
var productID = reader["ProductID"].ToString();
var r = JsonConvert.DeserializeObject<List<TestResult>>(val);
var findStep = r.Find(x => x.StepID == tl.StepID);
if (rowCount % config.RowCount == 0)
{
rowCount = 0;
fileNo += 1;
fileName = $"{config.MO}";
//testcode
if (string.IsNullOrEmpty(config.TestCode) == false)
fileName += $"_{testCode}";
if(string.IsNullOrEmpty(config.ProductNo) == false)
fileName += $"_{productNo}";
fileName += $"_{config.StartDate}_{config.EndDate}";
File.AppendAllText($"{fileName}_{fileNo}.csv", $"ProductNo,TestCode,TestDT,MO,MeasVal,MeasValStr,Host,ProductID\n");
Console.WriteLine($"Create New File: {fileName}_{fileNo}.csv");
}
if (findStep != null)
{
File.AppendAllText($"./{fileName}_{fileNo}.csv", $"{productNo},{testCode},{date.ToString("yyyy-MM-dd HH:mm:ss")},{tl.StepDesc},{findStep.MeasVal},{findStep.MeasValStr},{host},{productID}\n");
rowCount += 1;
}
}
}
}
catch (SqlException ex)
{
Console.WriteLine("DB 오류 발생: " + ex.Message);
}
}
}
}
public static string Decompression(string compressedDataStr)
{
string result = null;
byte[] buffer = Convert.FromBase64String(compressedDataStr);
using (MemoryStream stream = new MemoryStream(buffer))
{
using (GZipStream stream2 = new GZipStream(stream, CompressionMode.Decompress))
{
using (StreamReader streamReader = new StreamReader(stream2))
{
result = streamReader.ReadToEnd();
}
}
}
return result;
}
}
}