153 lines
3.7 KiB
C#
153 lines
3.7 KiB
C#
using System;
|
|
using System.Collections.Generic;
|
|
using System.IO;
|
|
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<string> elections = _excelImportService.LoadElectionYears();
|
|
_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)
|
|
{
|
|
// 초기 디렉터리는 _txtFile의 값(폴더이거나 파일이면 그 폴더) 또는 _appBasePath
|
|
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;
|
|
}
|
|
|
|
if (_cmbElection.SelectedItem == null)
|
|
{
|
|
MessageBox.Show(this, "선거년월을 선택하세요.", "확인", 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;
|
|
string electionName = _cmbElection.SelectedItem.ToString();
|
|
|
|
ToggleState(false);
|
|
_progress.Value = 0;
|
|
_lblMain.Text = "엑셀자료 입력 시작 - 최종 투표구 추출용";
|
|
|
|
Progress<ImportProgress> progress = new Progress<ImportProgress>(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;
|
|
}
|
|
}
|
|
}
|