using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using ChatworkBulkSender.Daos;
using ChatworkBulkSender.Dtos;
using ChatworkBulkSender.Utils;
namespace ChatworkBulkSender.UserControls
{
public partial class MessageSendResultControl : AbstractUserControl
{
// 区切り線
private readonly string SEPARATOR_LINE = "\r\n";
// 親フォーム通知用
public event EventHandler CloseForm;
public event Action<int> SwitchToMessageInputOnlyFailed;
// 送信履歴(ヘッダ)
SendHistoryDto _header;
// 送信履歴(明細)
List<SendHistoryDetailDto> _details;
public MessageSendResultControl()
{
InitializeComponent();
if (IsInDesignMode()) return;
DataGridViewDestinationExtensions.InitializeDestinationGrid(dgvDestinationAndFilePath, txtActualContents);
}
public void ChangeMode(Constants.SEND_TYPE sendType)
{
// 不定期送信の場合、レイアウトを調整
// ※初期状態のレイアウトは定期送信用。
if (sendType == Constants.SEND_TYPE.ADHOC_SENDING)
{
// タイトル変更
lblAttemtedPath.Text = "添付ファイル";
}
}
private void LoadResults(int? sendHistoryId)
{
// dto初期化
_header = null;
_details = null;
// 表示対象のデータ未指定
if (sendHistoryId == null) return;
// 送信履歴テーブル取得
SendHistoryDao headerDao = new SendHistoryDao();
_header = headerDao.GetSendHistory((int)sendHistoryId);
// 送信履歴_詳細テーブル取得
SendHistoryDetailDao detailDao = new SendHistoryDetailDao();
_details = detailDao.GetSendHistoryDetails((int)sendHistoryId);
}
/// <summary>
/// 表示対象の履歴データの読み込み+表示
/// </summary>
/// <param name="SendHistoryId"></param>
public void RenderResults(int? SendHistoryId)
{
// 表示する履歴データをロード
LoadResults(SendHistoryId);
// 表示対象のデータがない場合、メッセージを表示して終了。
if (_header == null)
{
MessageBoxUtil.ShowErr("表示する履歴データを特定できませんでした。");
return;
}
// 表示処理
// 成功件数
lblNumberOfSuccesses.Text = _header.SuccessCount.ToString();
// 失敗件数
int failedSendCount = _header.FailureCount;
lblNumberOfFailures.Text = failedSendCount.ToString();
lblNumberOfFailures.ForeColor = failedSendCount == 0 ? Color.Black : Color.Red;
// 送信日時
lblSendDt.Text = _header.SendDt?.ToString("yyyy/MM/dd HH:mm:ss") ?? "";
// パターン名
lblPatternName.Text = _header.PatternName ?? "<パターン未指定>";
// 送信者PC名
lblSenderPcName.Text = _header.CreateBy;
// 送信者Windowsユーザー名
lblSenderUserName.Text = _header.WindowsUserName;
// 送信対象およびファイルパス
List<CustomerMasterDto> destinationList = new List<CustomerMasterDto>();
{
foreach (var detail in _details)
{
CustomerMasterDto dest = new CustomerMasterDto
{
ManagementNumber = detail.ManagementNumber,
CustomerName = detail.CustomerName,
FilePath = detail.FilePath,
SendActualContents = detail.SendActualContents
};
destinationList.Add(dest);
}
}
DataGridViewDestinationExtensions.SetupDestinationAndFilePathGrid(dgvDestinationAndFilePath);
DataGridViewDestinationExtensions.BindDestinationData(dgvDestinationAndFilePath, destinationList);
// 添付ファイル保存場所
txtAttachmentFolder.Text = _header.RegularFolderPath;
// 再送ボタン(送信失敗データがあれば表示)
btnReSend.Visible = _header.FailureCount > 0;
// 送信結果(全件)
var sbAll = new StringBuilder();
// 送信結果(エラーのみ)
var sbErr = new StringBuilder();
// 送信結果を表示用の文字列に加工
foreach (var detail in _details)
{
// 送信結果を表示用文字列に加工
var status = detail.Success == Constants.SEND_RESULT.SUCCESS ? "成功" : "失敗";
var managementNumber = detail.ManagementNumber.ToString();
var customerName = detail.CustomerName ?? "";
// 結果を表示用の文字列に加工
if (detail.SendAttempted == Constants.SEND_ATTEMPTED.NO_ATTEMPTED)
{
// 送信処理未実施の場合
// 全件用
sbAll
.Append("[結果]").Append(status).AppendLine()
.Append("[管理番号]").Append(managementNumber).AppendLine()
.Append("[顧客名]").Append(customerName).AppendLine()
.Append("[エラー内容】緊急停止により送信処理が行われなかったため。").AppendLine()
.Append(SEPARATOR_LINE);
// エラーのみ用
sbErr
.Append("[結果]").Append(status).AppendLine()
.Append("[管理番号]").Append(managementNumber).AppendLine()
.Append("[顧客名]").Append(customerName).AppendLine()
.Append("[エラー内容]緊急停止により送信処理が行われなかったため。").AppendLine()
.Append(SEPARATOR_LINE);
}
else if (detail.Success == Constants.SEND_RESULT.FAILURE)
{
// 送信失敗の場合
// 全件用
sbAll
.Append("[結果]").Append(status).AppendLine()
.Append("[管理番号]").Append(managementNumber).AppendLine()
.Append("[顧客名]").Append(customerName).AppendLine()
.Append($"[エラー内容】{detail.DisplayErrorMessage}").AppendLine()
.Append($"[送信に使用したパラメータ】APIトークン:{_header.SenderApiToken}, 送信先ルームID:{detail.RoomId}").AppendLine()
.Append(SEPARATOR_LINE);
// エラーのみ用
sbErr
.Append("[結果]").Append(status).AppendLine()
.Append("[管理番号]").Append(managementNumber).AppendLine()
.Append("[顧客名]").Append(customerName).AppendLine()
.Append($"[エラー内容】{detail.DisplayErrorMessage}").AppendLine()
.Append($"[送信に使用したパラメータ】APIトークン:{_header.SenderApiToken}, 送信先ルームID:{detail.RoomId}").AppendLine()
.Append(SEPARATOR_LINE);
}
else
{
// 送信成功の場合
// 全件用
sbAll
.Append("[結果]").Append(status).AppendLine()
.Append("[管理番号]").Append(managementNumber).AppendLine()
.Append("[顧客名]").Append(customerName).AppendLine()
.Append(SEPARATOR_LINE);
}
}
if (sbErr.Length == 0)
{
sbErr.Append("エラーなし。");
}
txtResults.Text = sbAll.ToString().TrimEnd();
txtErrorResults.Text = sbErr.ToString().TrimEnd();
}
private void btnBack_Click(object sender, EventArgs e)
{
CloseForm?.Invoke(this, EventArgs.Empty);
}
/// <summary>
/// 送信失敗したデータのみ再送する。
/// ※送信入力画面を開き、再送対象データを自動セットした状態にする。
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
private void btnReSend_Click(object sender, EventArgs e)
{
// 最終確認
var result = MessageBoxUtil.ShowYesNo(MessageBoxUtil.ASK_004);
if (result != DialogResult.Yes) return;
// 送信入力画面を開く(失敗データ再送モード)
SwitchToMessageInputOnlyFailed?.Invoke(_header.SendHistoryId);
}
}
}