first commit
This commit is contained in:
264
Keps.WinForms/Services/ClusterMaintenanceService.cs
Normal file
264
Keps.WinForms/Services/ClusterMaintenanceService.cs
Normal file
@@ -0,0 +1,264 @@
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Data.SqlClient;
|
||||
using Keps.WinForms.Infrastructure;
|
||||
|
||||
namespace Keps.WinForms.Services
|
||||
{
|
||||
/// <summary>
|
||||
/// 군집(층화) 관련 중간 테이블 생성 및 poll_data 반영 작업을 수행하는 유지보수용 서비스입니다.
|
||||
/// </summary>
|
||||
public sealed class ClusterMaintenanceService
|
||||
{
|
||||
private readonly SqlConnectionFactory _connectionFactory;
|
||||
|
||||
/// <summary>
|
||||
/// 군집 관련 유지보수 작업을 수행하는 서비스를 생성합니다.
|
||||
/// </summary>
|
||||
/// <param name="connectionFactory">DB 연결을 제공하는 팩토리.</param>
|
||||
public ClusterMaintenanceService(SqlConnectionFactory connectionFactory)
|
||||
{
|
||||
_connectionFactory = connectionFactory;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 주어진 선거 연도에 대해 군집 관련 중간 테이블을 재구축하고 poll_data에 반영합니다.
|
||||
/// </summary>
|
||||
/// <param name="electionYear">선거 연도(예: "201804").</param>
|
||||
public void RebuildRealClusters(string electionYear)
|
||||
{
|
||||
using (SqlConnection connection = _connectionFactory.Open())
|
||||
{
|
||||
DeleteWorkingTables(connection, electionYear);
|
||||
InsertDistrictClusterMap(connection, electionYear);
|
||||
InsertClusterRatios(connection, electionYear);
|
||||
ResetPollDataClusters(connection, electionYear);
|
||||
ApplyClusterValuesToPollData(connection, electionYear);
|
||||
}
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 대상 선거 연도의 작업용 테이블 데이터를 초기화합니다.
|
||||
/// </summary>
|
||||
/// <param name="connection">열린 DB 연결.</param>
|
||||
/// <param name="electionYear">선거 연도.</param>
|
||||
private void DeleteWorkingTables(SqlConnection connection, string electionYear)
|
||||
{
|
||||
Execute(connection, "DELETE FROM [시군구별유권자수와군집] WHERE ele_ym = @eleYm", electionYear);
|
||||
Execute(connection, "DELETE FROM [군집별비율] WHERE ele_ym = @eleYm", electionYear);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 시군구별 유권자수와 군집 정보를 계산하여 매핑 테이블에 적재합니다.
|
||||
/// </summary>
|
||||
/// <param name="connection">열린 DB 연결.</param>
|
||||
/// <param name="electionYear">선거 연도.</param>
|
||||
private void InsertDistrictClusterMap(SqlConnection connection, string electionYear)
|
||||
{
|
||||
string sql = @"
|
||||
INSERT INTO [시군구별유권자수와군집]
|
||||
(
|
||||
ele_ym, 시도명, 시군구,
|
||||
군집1개, 군집2개, 군집3개, 군집4개, 군집5개, 군집6개, 군집7개,
|
||||
투표자수_etc1, 투표자수_etc2, 투표자수_etc3, 투표자수_etc4, 투표자수_etc5
|
||||
)
|
||||
SELECT
|
||||
@eleYm AS ele_ym,
|
||||
B.시도명,
|
||||
B.선거구명 AS 시군구,
|
||||
1 AS 군집1개,
|
||||
CASE WHEN A.층 = 2 THEN RANK() OVER(PARTITION BY B.시도명 ORDER BY B.선거구명) ELSE 0 END AS 군집2개,
|
||||
CASE WHEN A.층 = 3 THEN RANK() OVER(PARTITION BY B.시도명 ORDER BY B.선거구명) ELSE 0 END AS 군집3개,
|
||||
CASE WHEN A.층 = 4 THEN RANK() OVER(PARTITION BY B.시도명 ORDER BY B.선거구명) ELSE 0 END AS 군집4개,
|
||||
CASE WHEN A.층 = 5 THEN RANK() OVER(PARTITION BY B.시도명 ORDER BY B.선거구명) ELSE 0 END AS 군집5개,
|
||||
CASE WHEN A.층 = 6 THEN RANK() OVER(PARTITION BY B.시도명 ORDER BY B.선거구명) ELSE 0 END AS 군집6개,
|
||||
CASE WHEN A.층 = 7 THEN RANK() OVER(PARTITION BY B.시도명 ORDER BY B.선거구명) ELSE 0 END AS 군집7개,
|
||||
ISNULL((SELECT SUM(후보별득표수계)
|
||||
FROM poll_data
|
||||
WHERE ele_ym = @eleYm AND 시도명 = B.시도명 AND 선거구명 = B.선거구명), 0) AS 투표자수_etc1,
|
||||
0, 0, 0, 0
|
||||
FROM
|
||||
(
|
||||
SELECT 시도명, COUNT(*) AS 층
|
||||
FROM
|
||||
(
|
||||
SELECT 시도명, 선거구명
|
||||
FROM poll_data
|
||||
WHERE ele_ym = @eleYm
|
||||
GROUP BY 시도명, 선거구명
|
||||
) T
|
||||
GROUP BY 시도명
|
||||
) A
|
||||
LEFT JOIN
|
||||
(
|
||||
SELECT 시도명, 선거구명
|
||||
FROM poll_data
|
||||
WHERE ele_ym = @eleYm
|
||||
GROUP BY 시도명, 선거구명
|
||||
) B
|
||||
ON A.시도명 = B.시도명";
|
||||
Execute(connection, sql, electionYear);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 선거명/시도명별로 군집 비율(득표수 기반)을 계산하여 적재합니다.
|
||||
/// </summary>
|
||||
/// <param name="connection">열린 DB 연결.</param>
|
||||
/// <param name="electionYear">선거 연도.</param>
|
||||
private void InsertClusterRatios(SqlConnection connection, string electionYear)
|
||||
{
|
||||
string sql = @"
|
||||
INSERT INTO [군집별비율]
|
||||
(ele_ym, 선거명, 시도명, 군집최대값, 군집, 비율)
|
||||
SELECT
|
||||
@eleYm AS ele_ym,
|
||||
A.선거명,
|
||||
B.시도명,
|
||||
A.층 AS 군집최대값,
|
||||
RANK() OVER(PARTITION BY B.선거명, B.시도명 ORDER BY B.선거구명) AS 군집,
|
||||
CAST(ROUND(
|
||||
CAST(ISNULL((SELECT SUM(후보별득표수계)
|
||||
FROM poll_data
|
||||
WHERE ele_ym = @eleYm AND 선거명 = B.선거명 AND 시도명 = B.시도명 AND 선거구명 = B.선거구명), 0) AS NUMERIC(10,3))
|
||||
/
|
||||
NULLIF(CAST(ISNULL((SELECT SUM(후보별득표수계)
|
||||
FROM poll_data
|
||||
WHERE ele_ym = @eleYm AND 선거명 = B.선거명 AND 시도명 = B.시도명), 0) AS NUMERIC(10,3)), 0),
|
||||
5
|
||||
) AS NUMERIC(10,5)) AS 비율
|
||||
FROM
|
||||
(
|
||||
SELECT 선거명, 시도명, COUNT(*) AS 층
|
||||
FROM
|
||||
(
|
||||
SELECT 선거명, 시도명, 선거구명
|
||||
FROM poll_data
|
||||
WHERE ele_ym = @eleYm
|
||||
GROUP BY 선거명, 시도명, 선거구명
|
||||
) T
|
||||
GROUP BY 선거명, 시도명
|
||||
) A
|
||||
LEFT JOIN
|
||||
(
|
||||
SELECT 선거명, 선거구명, 시도명
|
||||
FROM poll_data
|
||||
WHERE ele_ym = @eleYm
|
||||
GROUP BY 선거명, 시도명, 선거구명
|
||||
) B
|
||||
ON A.선거명 = B.선거명 AND A.시도명 = B.시도명
|
||||
UNION ALL
|
||||
SELECT
|
||||
@eleYm,
|
||||
선거명,
|
||||
시도명,
|
||||
1,
|
||||
1,
|
||||
CAST(1.00000 AS NUMERIC(10,5))
|
||||
FROM poll_data
|
||||
WHERE ele_ym = @eleYm
|
||||
GROUP BY 선거명, 시도명, 선거구명";
|
||||
Execute(connection, sql, electionYear);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// poll_data 테이블의 군집 관련 컬럼 값을 초기화합니다.
|
||||
/// </summary>
|
||||
/// <param name="connection">열린 DB 연결.</param>
|
||||
/// <param name="electionYear">선거 연도.</param>
|
||||
private void ResetPollDataClusters(SqlConnection connection, string electionYear)
|
||||
{
|
||||
string sql = @"
|
||||
UPDATE poll_data
|
||||
SET
|
||||
군집1개 = 0,
|
||||
군집2개 = 0,
|
||||
군집3개 = 0,
|
||||
군집4개 = 0,
|
||||
군집5개 = 0,
|
||||
군집6개 = 0,
|
||||
군집7개 = 0,
|
||||
비고 = 1
|
||||
WHERE ele_ym = @eleYm";
|
||||
Execute(connection, sql, electionYear);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 시군구별 군집 값을 poll_data 테이블의 해당 행들에 반영합니다.
|
||||
/// </summary>
|
||||
/// <param name="connection">열린 DB 연결.</param>
|
||||
/// <param name="electionYear">선거 연도.</param>
|
||||
private void ApplyClusterValuesToPollData(SqlConnection connection, string electionYear)
|
||||
{
|
||||
string query = "SELECT 시도명, 시군구, 군집1개, 군집2개, 군집3개, 군집4개, 군집5개, 군집6개, 군집7개 FROM [시군구별유권자수와군집] WHERE ele_ym = @eleYm";
|
||||
using (SqlCommand command = new SqlCommand(query, connection))
|
||||
{
|
||||
command.Parameters.AddWithValue("@eleYm", electionYear);
|
||||
using (SqlDataReader reader = command.ExecuteReader())
|
||||
{
|
||||
List<Dictionary<string, object>> rows = new List<Dictionary<string, object>>();
|
||||
while (reader.Read())
|
||||
{
|
||||
Dictionary<string, object> row = new Dictionary<string, object>();
|
||||
row["시도명"] = reader["시도명"];
|
||||
row["시군구"] = reader["시군구"];
|
||||
row["군집1개"] = reader["군집1개"];
|
||||
row["군집2개"] = reader["군집2개"];
|
||||
row["군집3개"] = reader["군집3개"];
|
||||
row["군집4개"] = reader["군집4개"];
|
||||
row["군집5개"] = reader["군집5개"];
|
||||
row["군집6개"] = reader["군집6개"];
|
||||
row["군집7개"] = reader["군집7개"];
|
||||
rows.Add(row);
|
||||
}
|
||||
|
||||
reader.Close();
|
||||
|
||||
foreach (Dictionary<string, object> row in rows)
|
||||
{
|
||||
string update = @"
|
||||
UPDATE poll_data
|
||||
SET
|
||||
군집1개 = @c1,
|
||||
군집2개 = @c2,
|
||||
군집3개 = @c3,
|
||||
군집4개 = @c4,
|
||||
군집5개 = @c5,
|
||||
군집6개 = @c6,
|
||||
군집7개 = @c7
|
||||
WHERE ele_ym = @eleYm AND 시도명 = @areaName AND 선거구명 = @districtName";
|
||||
|
||||
using (SqlCommand updateCommand = new SqlCommand(update, connection))
|
||||
{
|
||||
updateCommand.Parameters.AddWithValue("@eleYm", electionYear);
|
||||
updateCommand.Parameters.AddWithValue("@areaName", row["시도명"]);
|
||||
updateCommand.Parameters.AddWithValue("@districtName", row["시군구"]);
|
||||
updateCommand.Parameters.AddWithValue("@c1", row["군집1개"]);
|
||||
updateCommand.Parameters.AddWithValue("@c2", row["군집2개"]);
|
||||
updateCommand.Parameters.AddWithValue("@c3", row["군집3개"]);
|
||||
updateCommand.Parameters.AddWithValue("@c4", row["군집4개"]);
|
||||
updateCommand.Parameters.AddWithValue("@c5", row["군집5개"]);
|
||||
updateCommand.Parameters.AddWithValue("@c6", row["군집6개"]);
|
||||
updateCommand.Parameters.AddWithValue("@c7", row["군집7개"]);
|
||||
updateCommand.ExecuteNonQuery();
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 공통 파라미터(<c>@eleYm</c>)를 바인딩하여 지정된 SQL을 실행합니다.
|
||||
/// </summary>
|
||||
/// <param name="connection">열린 DB 연결.</param>
|
||||
/// <param name="sql">실행할 SQL 문.</param>
|
||||
/// <param name="electionYear">바인딩할 선거 연도 값.</param>
|
||||
private void Execute(SqlConnection connection, string sql, string electionYear)
|
||||
{
|
||||
using (SqlCommand command = new SqlCommand(sql, connection))
|
||||
{
|
||||
command.Parameters.AddWithValue("@eleYm", electionYear);
|
||||
command.ExecuteNonQuery();
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user