first commit
This commit is contained in:
150
Keps.WinForms/Services/LookupService.cs
Normal file
150
Keps.WinForms/Services/LookupService.cs
Normal file
@@ -0,0 +1,150 @@
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Data.SqlClient;
|
||||
using Keps.WinForms.Infrastructure;
|
||||
using Keps.WinForms.Models;
|
||||
|
||||
namespace Keps.WinForms.Services
|
||||
{
|
||||
/// <summary>
|
||||
/// 조회(룩업)용 데이터 로딩을 담당하는 서비스입니다.
|
||||
/// </summary>
|
||||
public sealed class LookupService
|
||||
{
|
||||
private readonly SqlConnectionFactory _connectionFactory;
|
||||
|
||||
/// <summary>
|
||||
/// 조회 관련 서비스 인스턴스를 생성합니다.
|
||||
/// </summary>
|
||||
/// <param name="connectionFactory">DB 연결을 제공하는 팩토리.</param>
|
||||
public LookupService(SqlConnectionFactory connectionFactory)
|
||||
{
|
||||
_connectionFactory = connectionFactory;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 실 데이터(poll_data)에 존재하는 선거명 목록을 조회합니다.
|
||||
/// </summary>
|
||||
/// <returns>선거명 문자열 목록.</returns>
|
||||
public List<string> LoadRealElections()
|
||||
{
|
||||
return LoadSingleColumnList(LegacySqlCatalog.DistinctRealElections, null);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 특정 선거명에 대해 시도명(지역) 목록을 조회합니다.
|
||||
/// </summary>
|
||||
/// <param name="electionName">선거명(예: "201804" 등).</param>
|
||||
/// <returns>지역명 문자열 목록.</returns>
|
||||
public List<string> LoadRealAreas(string electionName)
|
||||
{
|
||||
SqlParameter[] parameters = { new SqlParameter("@electionName", electionName) };
|
||||
return LoadSingleColumnList(LegacySqlCatalog.DistinctRealAreas, parameters);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 특정 선거명과 지역에 대해 설정된 군집 최대값 목록을 조회합니다.
|
||||
/// </summary>
|
||||
/// <param name="electionName">선거명.</param>
|
||||
/// <param name="areaName">지역명.</param>
|
||||
/// <returns>군집 최대값 정수 목록.</returns>
|
||||
public List<int> LoadRealClusterMaxList(string electionName, string areaName)
|
||||
{
|
||||
List<int> result = new List<int>();
|
||||
|
||||
using (SqlConnection connection = _connectionFactory.Open())
|
||||
using (SqlCommand command = new SqlCommand(LegacySqlCatalog.RealClusterMax, connection))
|
||||
{
|
||||
command.Parameters.AddWithValue("@electionName", electionName);
|
||||
command.Parameters.AddWithValue("@areaName", areaName);
|
||||
|
||||
using (SqlDataReader reader = command.ExecuteReader())
|
||||
{
|
||||
while (reader.Read())
|
||||
{
|
||||
result.Add(Convert.ToInt32(reader[0]));
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
return result;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 특정 선거명에 대해 후보자 옵션 목록을 조회합니다.
|
||||
/// </summary>
|
||||
/// <param name="electionName">선거명 또는 선거 번호.</param>
|
||||
/// <returns>후보자 표시 이름과 키를 담은 옵션 목록.</returns>
|
||||
public List<CandidateOption> LoadCandidateOptions(string electionName)
|
||||
{
|
||||
List<CandidateOption> result = new List<CandidateOption>();
|
||||
|
||||
using (SqlConnection connection = _connectionFactory.Open())
|
||||
using (SqlCommand command = new SqlCommand(LegacySqlCatalog.RealCandidateNames, connection))
|
||||
{
|
||||
int electionNo;
|
||||
if (int.TryParse(electionName, out electionNo))
|
||||
{
|
||||
command.Parameters.AddWithValue("@electionName", electionNo);
|
||||
}
|
||||
else
|
||||
{
|
||||
command.Parameters.AddWithValue("@electionName", electionName);
|
||||
}
|
||||
|
||||
try
|
||||
{
|
||||
using (SqlDataReader reader = command.ExecuteReader())
|
||||
{
|
||||
while (reader.Read())
|
||||
{
|
||||
string candNo = Convert.ToString(reader["cand_no"]);
|
||||
string candName = Convert.ToString(reader["cand_name"]);
|
||||
result.Add(new CandidateOption
|
||||
{
|
||||
Key = "cand_ratio_" + candNo,
|
||||
DisplayName = candName + "(" + candNo + ")"
|
||||
});
|
||||
}
|
||||
}
|
||||
}
|
||||
catch
|
||||
{
|
||||
}
|
||||
}
|
||||
|
||||
result.Add(new CandidateOption { Key = "투표인수", DisplayName = "투표인수" });
|
||||
return result;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 단일 컬럼을 반환하는 쿼리를 실행하여 문자열 목록으로 변환합니다.
|
||||
/// </summary>
|
||||
/// <param name="sql">실행할 SQL.</param>
|
||||
/// <param name="parameters">바인딩할 매개변수 배열. null일 수 있습니다.</param>
|
||||
/// <returns>첫 번째 컬럼 값을 문자열로 담은 목록.</returns>
|
||||
private List<string> LoadSingleColumnList(string sql, SqlParameter[] parameters)
|
||||
{
|
||||
List<string> result = new List<string>();
|
||||
|
||||
using (SqlConnection connection = _connectionFactory.Open())
|
||||
using (SqlCommand command = new SqlCommand(sql, connection))
|
||||
{
|
||||
if (parameters != null)
|
||||
{
|
||||
command.Parameters.AddRange(parameters);
|
||||
}
|
||||
|
||||
using (SqlDataReader reader = command.ExecuteReader())
|
||||
{
|
||||
while (reader.Read())
|
||||
{
|
||||
result.Add(Convert.ToString(reader[0]));
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
return result;
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user