216 lines
9.6 KiB
C#
216 lines
9.6 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 [{config.CPXV2Database}].[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 = $"";
|
|
|
|
// 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}";
|
|
|
|
string sql = $@"SELECT
|
|
Summary.*,
|
|
Result.LogData,
|
|
TL.StepVersion AS Matched_StepVersion
|
|
FROM [{config.DataBase}].[dbo].[{config.SummaryTable}] AS Summary WITH(NOLOCK)
|
|
JOIN [{config.DataBase}].[dbo].[{config.ResultTable}] AS Result WITH(NOLOCK)
|
|
ON Summary.No = Result.No
|
|
OUTER APPLY (
|
|
SELECT TOP 1 *
|
|
FROM [{config.CPXV2Database}].[dbo].[VRFY_TestListFileRelease]
|
|
WHERE StepVersion <= Summary.StepVersion and StepDesc = '{config.MO}'
|
|
ORDER BY StepVersion DESC
|
|
) AS TL";
|
|
|
|
//TestDT
|
|
DateTime endDate = Convert.ToDateTime(config.EndDate).AddDays(1);
|
|
sql += $" where '{config.StartDate}' <= Summary.TestDT and Summary.TestDT < '{endDate.ToString("yyyy-MM-dd")}'";
|
|
|
|
//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}'";
|
|
|
|
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 testListFileNo = Convert.ToInt32(reader["TestListFileNo"]);
|
|
var stepVersion = Convert.ToInt32(reader["Matched_StepVersion"]);
|
|
|
|
var no = Convert.ToInt32(reader["No"]);
|
|
|
|
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 tl = testList.Find(x => x.TestListFileNo == testListFileNo && x.StepVersion == stepVersion);
|
|
|
|
if(tl != null)
|
|
{
|
|
//Console.WriteLine($"Current No:{no}");
|
|
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;
|
|
}
|
|
}
|
|
}
|