using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using ChatworkBulkSender.Utils;
namespace ChatworkBulkSender.Dtos
{
/// <summary>
/// 送信処理に関するすべての情報を管理するクラス。
/// ※送信するための情報、送信結果いずれも管理。
/// </summary>
public class BulkSendJobDto
{
// ↓送信処理前に決定するデータ
/// <summary>
/// 送信タイプ
/// </summary>
public Constants.SEND_TYPE SendType { get; set; }
/// <summary>
/// 基準としたパターンマスタのデータ
/// </summary>
public PatternMasterDto PatternMaster { get; set; }
/// <summary>
/// 送信内容(画面上でユーザーが入力したもの)
/// </summary>
public string SendContents
{
get => _sendContents;
set
{
_sendContents = value;
RemakeSendActualContents();
}
}
private string _sendContents;
/// <summary>
/// 送信対象(自動選択or手動選択)
/// </summary>
public Constants.DESTINAION_SELECT_TYPE RegularDestinationSelectType { get; set; }
/// <summary>
/// 送信対象選択
/// ※送信対象としてチェックがついた顧客のみを保持
/// </summary>
public List<CustomerMasterDto> DestinationList
{
get => _destinationList;
set
{
_destinationList = value;
RemakeFilePaths();
RemakeSendActualContents();
}
}
private List<CustomerMasterDto> _destinationList;
/// <summary>
/// 定期送信_添付ファイル管理フォルダパス
/// </summary>
public string RegularAttachmentFolderPath
{
get => _regularAttachmentFolderPath;
set
{
_regularAttachmentFolderPath = value;
RemakeFilePaths();
}
}
private string _regularAttachmentFolderPath;
/// <summary>
/// 不定期送信_添付ファイルパス
/// </summary>
public string AdhocAttachmentFilePath
{
get => _AdhocAttachmentFilePath;
set
{
_AdhocAttachmentFilePath = value;
AdhocHasAttachment = true;
RemakeFilePaths();
}
}
private string _AdhocAttachmentFilePath;
/// <summary>
/// 不定期送信_添付ファイル有無
/// </summary>
public bool AdhocHasAttachment { get; set; }
// ↓送信処理後に決定するデータ
/// <summary>
/// [送信履歴].[送信履歴ID]の値。
/// エラーなどで確定しなかった場合はnull。
/// </summary>
public int? SendHistoryId { get; set; } = null;
/// <summary>
/// 送信成功件数
/// </summary>
public int SuccessfulSendCount
{
get
{
if (DestinationList == null || DestinationList.Count == 0) return 0;
int cnt = 0;
foreach (var customer in DestinationList)
{
if (customer.Result.Success == Constants.SEND_RESULT.SUCCESS) cnt++;
}
return cnt;
}
}
/// <summary>
/// 送信失敗件数
/// </summary>
public int FailedSendCount
{
get
{
if (DestinationList == null || DestinationList.Count == 0) return 0;
int cnt = 0;
foreach (var customer in DestinationList)
{
if (customer.Result.Success == Constants.SEND_RESULT.FAILURE)
{
cnt++;
}
}
return cnt;
}
}
/// <summary>
/// 全ての送信処理が完了した日時
/// </summary>
public DateTime? SendCompletedDt;
/// <summary>
/// 送信に使用したChatworkAPIトークン
/// </summary>
public string ApiToken { get; set; }
/// <summary>
/// 実際の送信メッセージを作成
/// </summary>
private void RemakeSendActualContents()
{
if (SendContents == null || DestinationList == null)
return;
foreach (var customer in DestinationList)
{
customer.SendActualContents = "";
// 宛先アカウントIDが設定されていれば、1行目に宛先を記載する。
if (!string.IsNullOrEmpty(customer.DestinationAccountId))
{
customer.SendActualContents = $"[To:{customer.DestinationAccountId}][pname:{customer.DestinationAccountId}]さん\r\n";
}
// 入力画面で設定された内容をセット
customer.SendActualContents += SendContents;
}
}
/// <summary>
/// フォルダ配下から「*{ManagementNumber}.pdf」を探して DestinationList.FilePath にセットする
/// </summary>
private void RemakeFilePaths()
{
if(SendType == Constants.SEND_TYPE.REGULAR_SENDING)
{
// 定期送信の場合(基準となるフォルダの中を探索してファイルを特定)
if (string.IsNullOrEmpty(RegularAttachmentFolderPath) || DestinationList == null)
{
return;
}
if (!Directory.Exists(RegularAttachmentFolderPath))
{
return;
}
foreach (var customer in DestinationList)
{
// パターン "*{ManagementNumber}.pdf" にマッチするファイルを最大2件取得
var pattern = $"*{customer.ManagementNumber}.pdf";
var matches = Directory
.EnumerateFiles(_regularAttachmentFolderPath, pattern, SearchOption.TopDirectoryOnly)
.Take(2)
.ToArray();
// 実際のファイルパス(あれば1件目をセット)
customer.FilePath = matches.FirstOrDefault();
// 2件以上あればフラグを立てる
customer.ExistsMultiplePDF = matches.Length > 1;
}
}
else
{
// 不定期送信の場合(基準となるファイルをそのまま使用)
if (string.IsNullOrEmpty(AdhocAttachmentFilePath) || DestinationList == null)
{
return;
}
if (!File.Exists(AdhocAttachmentFilePath))
{
return;
}
foreach (var customer in DestinationList)
{
customer.FilePath = this.AdhocAttachmentFilePath;
}
}
}
}
}