215 lines
9.8 KiB
C#
215 lines
9.8 KiB
C#
using System;
|
|
using System.Data;
|
|
using System.Data.OleDb;
|
|
using System.IO;
|
|
using System.Text;
|
|
|
|
namespace ControlServer
|
|
{
|
|
public class DbFileManager
|
|
{
|
|
// ⚠️ 실제 개발 환경에 맞게 데이터 소스, 아이디, 비밀번호를 변경해 주세요.
|
|
private readonly string _connectionString = "Provider=Tibero.OLEDB;Data Source=YourDB;User Id=YourID;Password=YourPassword;";
|
|
private readonly string _fileDirPath = Path.Combine(AppDomain.CurrentDomain.BaseDirectory, "SendData");
|
|
|
|
public DbFileManager()
|
|
{
|
|
if (!Directory.Exists(_fileDirPath))
|
|
{
|
|
Directory.CreateDirectory(_fileDirPath);
|
|
}
|
|
}
|
|
|
|
/// <summary>
|
|
/// [Form1 연동 규격] DB에서 에이전트 IP 혹은 단말기명에 맞는 데이터를 가져와
|
|
/// XML 파일로 저장하고, 가상 모니터 폼 갱신을 위해 DataTable을 반환
|
|
/// </summary>
|
|
/// <param name="terminalName">단말기 식별자 (IP 주소 또는 단말기명)</param>
|
|
/// <returns>가상 모니터링 그리드에 바인딩할 DataTable</returns>
|
|
public DataTable GetTerminalData(string terminalName)
|
|
{
|
|
// 1. 에이전트 화면 규격(AuctionTable)과 컬럼 구조 정의
|
|
DataTable dt = new DataTable("AuctionTable");
|
|
dt.Columns.Add("aucDate", typeof(string));
|
|
dt.Columns.Add("itemName", typeof(string));
|
|
dt.Columns.Add("origin", typeof(string));
|
|
dt.Columns.Add("totalQty", typeof(int));
|
|
dt.Columns.Add("fixPriceQty", typeof(int));
|
|
dt.Columns.Add("totalAmt", typeof(int));
|
|
dt.Columns.Add("fixPriceAmt", typeof(int));
|
|
|
|
// 2. Tibero DB 조회 쿼리
|
|
string query = @"SELECT AUC_DATE, ITEM_NAME, ORIGIN, TOTAL_QTY, FIX_QTY, TOTAL_AMT, FIX_AMT
|
|
FROM FIXED_PRICE_TRADE
|
|
WHERE TERMINAL_NAME = ?";
|
|
|
|
using (OleDbConnection conn = new OleDbConnection(_connectionString))
|
|
{
|
|
using (OleDbCommand cmd = new OleDbCommand(query, conn))
|
|
{
|
|
cmd.Parameters.AddWithValue("@TerminalName", terminalName);
|
|
|
|
try
|
|
{
|
|
conn.Open();
|
|
using (OleDbDataReader reader = cmd.ExecuteReader())
|
|
{
|
|
while (reader.Read())
|
|
{
|
|
dt.Rows.Add(
|
|
reader["AUC_DATE"]?.ToString() ?? "",
|
|
reader["ITEM_NAME"]?.ToString() ?? "",
|
|
reader["ORIGIN"]?.ToString() ?? "",
|
|
reader["TOTAL_QTY"] != DBNull.Value ? Convert.ToInt32(reader["TOTAL_QTY"]) : 0,
|
|
reader["FIX_QTY"] != DBNull.Value ? Convert.ToInt32(reader["FIX_QTY"]) : 0,
|
|
reader["TOTAL_AMT"] != DBNull.Value ? Convert.ToInt32(reader["TOTAL_AMT"]) : 0,
|
|
reader["FIX_AMT"] != DBNull.Value ? Convert.ToInt32(reader["FIX_AMT"]) : 0
|
|
);
|
|
}
|
|
}
|
|
}
|
|
catch (Exception ex)
|
|
{
|
|
Console.WriteLine($"[{terminalName}] DB 연동 실패: " + ex.Message);
|
|
// 에러 발생 시 프로그램이 뻗지 않도록 null 반환 처리
|
|
return null;
|
|
}
|
|
}
|
|
}
|
|
|
|
// 3. 조회 데이터가 전혀 없을 경우 가상 모니터 가독성을 위한 기본 행 처리
|
|
if (dt.Rows.Count == 0)
|
|
{
|
|
string today = DateTime.Now.ToString("MM.dd");
|
|
dt.Rows.Add(today, "조회데이터 없음", "-", 0, 0, 0, 0);
|
|
}
|
|
|
|
// 4. 에이전트 전송 호환용 내부 로컬 XML 파일 백업 저장 (.xml)
|
|
string fileName = $"{terminalName}.xml";
|
|
string fullPath = Path.Combine(_fileDirPath, fileName);
|
|
|
|
try
|
|
{
|
|
dt.WriteXml(fullPath, XmlWriteMode.WriteSchema);
|
|
Console.WriteLine($"[XML 로컬 백업 완료] {fullPath}");
|
|
}
|
|
catch (Exception ex)
|
|
{
|
|
Console.WriteLine($"[{terminalName}] XML 파일 저장 실패: " + ex.Message);
|
|
}
|
|
|
|
// Form1의 agentCurrentData 변수로 데이터 테이블을 그대로 토스
|
|
return dt;
|
|
}
|
|
|
|
/// <summary>
|
|
/// 기존 소켓 전송 규격 유지용 메서드 (필요시 사용)
|
|
/// </summary>
|
|
public string CreateAndSaveTerminalFile(string terminalName)
|
|
{
|
|
//DataTable dt = GetTerminalData(terminalName);
|
|
//if (dt == null) return "DB_ERROR|조회 실패";
|
|
|
|
//using (StringWriter sw = new StringWriter())
|
|
//{
|
|
// dt.WriteXml(sw, XmlWriteMode.WriteSchema);
|
|
// return sw.ToString();
|
|
//}
|
|
|
|
// 파일 경로 설정
|
|
string filePath = GetTerminalFilePath(terminalName);
|
|
|
|
// -----------------------------------------------------------------
|
|
// [임시 테스트용 데이터 하드코딩] DB 연동 전까지 사용할 더미 데이터
|
|
// -----------------------------------------------------------------
|
|
string xmlData =
|
|
"<AuctionRows>\n" +
|
|
" <AuctionTable>\n" +
|
|
$" <aucDate>{DateTime.Now:MM.dd}</aucDate>\n" +
|
|
" <itemName>테스트품목1</itemName>\n" +
|
|
" <origin>테스트산지1</origin>\n" +
|
|
" <totalQty>1000</totalQty>\n" +
|
|
" <fixPriceQty>800</fixPriceQty>\n" +
|
|
" <totalAmt>5000</totalAmt>\n" +
|
|
" <fixPriceAmt>4000</fixPriceAmt>\n" +
|
|
" </AuctionTable>\n" +
|
|
" <AuctionTable>\n" +
|
|
$" <aucDate>{DateTime.Now:MM.dd}</aucDate>\n" +
|
|
" <itemName>테스트품목2</itemName>\n" +
|
|
" <origin>테스트산지2</origin>\n" +
|
|
" <totalQty>1030</totalQty>\n" +
|
|
" <fixPriceQty>500</fixPriceQty>\n" +
|
|
" <totalAmt>4400</totalAmt>\n" +
|
|
" <fixPriceAmt>2000</fixPriceAmt>\n" +
|
|
" </AuctionTable>\n" +
|
|
" <AuctionTable>\n" +
|
|
$" <aucDate>{DateTime.Now:MM.dd}</aucDate>\n" +
|
|
" <itemName>테스트품목3</itemName>\n" +
|
|
" <origin>테스트산지3</origin>\n" +
|
|
" <totalQty>1000</totalQty>\n" +
|
|
" <fixPriceQty>800</fixPriceQty>\n" +
|
|
" <totalAmt>5000</totalAmt>\n" +
|
|
" <fixPriceAmt>4000</fixPriceAmt>\n" +
|
|
" </AuctionTable>\n" +
|
|
" <AuctionTable>\n" +
|
|
$" <aucDate>{DateTime.Now:MM.dd}</aucDate>\n" +
|
|
" <itemName>테스트품목4</itemName>\n" +
|
|
" <origin>테스트산지4</origin>\n" +
|
|
" <totalQty>1000</totalQty>\n" +
|
|
" <fixPriceQty>800</fixPriceQty>\n" +
|
|
" <totalAmt>5000</totalAmt>\n" +
|
|
" <fixPriceAmt>4000</fixPriceAmt>\n" +
|
|
" </AuctionTable>\n" +
|
|
" <AuctionTable>\n" +
|
|
$" <aucDate>{DateTime.Now:MM.dd}</aucDate>\n" +
|
|
" <itemName>테스트품목5</itemName>\n" +
|
|
" <origin>테스트산지5</origin>\n" +
|
|
" <totalQty>1000</totalQty>\n" +
|
|
" <fixPriceQty>800</fixPriceQty>\n" +
|
|
" <totalAmt>5000</totalAmt>\n" +
|
|
" <fixPriceAmt>4000</fixPriceAmt>\n" +
|
|
" </AuctionTable>\n" +
|
|
" <AuctionTable>\n" +
|
|
$" <aucDate>{DateTime.Now:MM.dd}</aucDate>\n" +
|
|
" <itemName>테스트품목6</itemName>\n" +
|
|
" <origin>테스트산지6</origin>\n" +
|
|
" <totalQty>1000</totalQty>\n" +
|
|
" <fixPriceQty>800</fixPriceQty>\n" +
|
|
" <totalAmt>5000</totalAmt>\n" +
|
|
" <fixPriceAmt>4000</fixPriceAmt>\n" +
|
|
" </AuctionTable>\n" +
|
|
" <AuctionTable>\n" +
|
|
$" <aucDate>{DateTime.Now:MM.dd}</aucDate>\n" +
|
|
" <itemName>테스트품목7</itemName>\n" +
|
|
" <origin>테스트산지7</origin>\n" +
|
|
" <totalQty>1000</totalQty>\n" +
|
|
" <fixPriceQty>800</fixPriceQty>\n" +
|
|
" <totalAmt>5000</totalAmt>\n" +
|
|
" <fixPriceAmt>4000</fixPriceAmt>\n" +
|
|
" </AuctionTable>\n" +
|
|
" <AuctionTable>\n" +
|
|
$" <aucDate>{DateTime.Now:MM.dd}</aucDate>\n" +
|
|
" <itemName>테스트품목8</itemName>\n" +
|
|
" <origin>테스트산지8</origin>\n" +
|
|
" <totalQty>1000</totalQty>\n" +
|
|
" <fixPriceQty>800</fixPriceQty>\n" +
|
|
" <totalAmt>5000</totalAmt>\n" +
|
|
" <fixPriceAmt>4000</fixPriceAmt>\n" +
|
|
" </AuctionTable>\n" +
|
|
"</AuctionRows>";
|
|
|
|
// 2. 파일 저장
|
|
File.WriteAllText(filePath, xmlData);
|
|
|
|
Console.WriteLine($"[DB Manager] XML 파일 생성 완료: {filePath}");
|
|
|
|
return xmlData; // 데이터 반환
|
|
}
|
|
|
|
public string GetTerminalFilePath(string terminalName)
|
|
{
|
|
// 확장자를 .xml로 통일
|
|
return Path.Combine(_fileDirPath, $"{terminalName}.xml");
|
|
}
|
|
}
|
|
} |