using System; using System.Collections.Generic; using System.IO; using System.Linq; using System.Threading.Tasks; using System.Windows.Forms; using Keps.WinForms.Models; using Keps.WinForms.Services; namespace Keps.WinForms.Forms { public sealed partial class DataInputForm : Form { private readonly string _appBasePath = Application.StartupPath; private ExcelImportService _excelImportService; public DataInputForm() { InitializeComponent(); } public DataInputForm(ExcelImportService excelImportService) : this() { SetServices(excelImportService); } public void SetServices(ExcelImportService excelImportService) { _excelImportService = excelImportService; } private void DataInputForm_Load(object sender, EventArgs e) { if (_excelImportService == null) { _lblMain.Text = "서비스 미연결"; _lblDetail.Text = "MainMdiForm.InitializeRuntime 호출 후 사용하세요."; return; } List elections = _excelImportService.LoadElectionYears(); _cmbElection.DropDownStyle = ComboBoxStyle.DropDown; _cmbElection.Items.Clear(); foreach (string election in elections) { _cmbElection.Items.Add(election); } if (_cmbElection.Items.Count > 0) { _cmbElection.SelectedIndex = 0; } } private void BtnFolder_Click(object sender, EventArgs e) { // 초기 디렉터리는 선택된 파일의 폴더 또는 앱 실행 폴더를 사용합니다. string initialDir = _appBasePath; if (!string.IsNullOrWhiteSpace(_txtFile.Text)) { try { if (Directory.Exists(_txtFile.Text)) { initialDir = _txtFile.Text; } else { var dir = Path.GetDirectoryName(_txtFile.Text); if (!string.IsNullOrWhiteSpace(dir) && Directory.Exists(dir)) initialDir = dir; } } catch { initialDir = _appBasePath; } } _fileDialog.InitialDirectory = initialDir; _fileDialog.FileName = string.Empty; if (_fileDialog.ShowDialog(this) == DialogResult.OK) { _txtFile.Text = _fileDialog.FileName; } } private async void BtnImportReal_Click(object sender, EventArgs e) { if (_excelImportService == null) { MessageBox.Show(this, "ExcelImportService가 초기화되지 않았습니다.", "확인", MessageBoxButtons.OK, MessageBoxIcon.Warning); return; } string electionName = _cmbElection.Text.Trim(); if (!IsValidElectionYear(electionName)) { MessageBox.Show(this, "선거년월을 yyyymm 형식으로 입력 또는 선택해주세요.", "확인", MessageBoxButtons.OK, MessageBoxIcon.Warning); return; } if (string.IsNullOrWhiteSpace(_txtFile.Text) || !File.Exists(_txtFile.Text)) { MessageBox.Show(this, "엑셀 파일을 선택하세요.", "확인", MessageBoxButtons.OK, MessageBoxIcon.Warning); return; } string filePath = _txtFile.Text; ToggleState(false); _progress.Value = 0; _lblMain.Text = "엑셀자료 입력 시작 - 최종 투표소 추출용"; Progress progress = new Progress(p => { _progress.Value = Math.Max(0, Math.Min(100, p.Percent)); _lblDetail.Text = p.Message; }); try { await Task.Run(() => { _excelImportService.Import(filePath, electionName, ImportMode.Real, progress); }); _lblMain.Text = "완료"; MessageBox.Show(this, "엑셀 기초자료 DB 적재와 군집 재구성이 완료되었습니다.", "완료", MessageBoxButtons.OK, MessageBoxIcon.Information); } catch (Exception ex) { _lblMain.Text = "실패"; MessageBox.Show(this, ex.Message, "오류", MessageBoxButtons.OK, MessageBoxIcon.Error); } finally { ToggleState(true); } } private void ToggleState(bool enabled) { _cmbElection.Enabled = enabled; _btnFolder.Enabled = enabled; _btnImportReal.Enabled = enabled; } private static bool IsValidElectionYear(string value) { return !string.IsNullOrWhiteSpace(value) && value.Length == 6 && value.All(char.IsDigit); } } }