HRM-10/HRM.DA/DA/Mail/MasterEmailLogDetailsDA.cs
Mohaimen Hasan 838b8d4480 first commit
2026-11-03 13:06:00 +06:00

264 lines
13 KiB
C#

using HRM.BO;
using Ease.Core.DataAccess;
using Microsoft.Data.SqlClient;
using System;
using System.Collections.Generic;
using System.Data;
namespace HRM.DA
{
#region MasterEmailLogDetailsDA
internal class MasterEmailLogDetailsDA
{
#region Constructor
private MasterEmailLogDetailsDA()
{
}
#endregion
#region Insert function
/// <summary>
/// Writes every recipient row of a batch in one round trip. A thousand
/// individual INSERTs is the one thing that would make this feature feel
/// slow before a single mail has even been sent, so the header is saved
/// with MakeSQL and the rows go in through SqlBulkCopy.
///
/// The rows are written without AttachmentData - the bytes do not exist
/// yet at this point. Each row is updated with its result, attachment
/// included, by <see cref="UpdateResult"/> right after it is sent.
/// </summary>
internal static void BulkInsert(TransactionContext tc, List<MasterEmailLogDetails> items)
{
if (items == null || items.Count == 0)
{
return;
}
DataTable detailTable = new DataTable();
detailTable.Columns.Add(new DataColumn("MasterEmailLogDetailsID", typeof(int)));
detailTable.Columns.Add(new DataColumn("MasterEmailLogID", typeof(int)));
detailTable.Columns.Add(new DataColumn("EmployeeID", typeof(int)));
detailTable.Columns.Add(new DataColumn("EmployeeNo", typeof(string)));
detailTable.Columns.Add(new DataColumn("EmployeeName", typeof(string)));
detailTable.Columns.Add(new DataColumn("MailFrom", typeof(string)));
detailTable.Columns.Add(new DataColumn("MailTo", typeof(string)));
detailTable.Columns.Add(new DataColumn("CC", typeof(string)));
detailTable.Columns.Add(new DataColumn("Subject", typeof(string)));
detailTable.Columns.Add(new DataColumn("Body", typeof(string)));
detailTable.Columns.Add(new DataColumn("AttachmentName", typeof(string)));
detailTable.Columns.Add(new DataColumn("SalaryMonth", typeof(DateTime)));
detailTable.Columns.Add(new DataColumn("ReferenceID", typeof(int)));
detailTable.Columns.Add(new DataColumn("SendStatus", typeof(int)));
detailTable.Columns.Add(new DataColumn("ErrorMessage", typeof(string)));
detailTable.Columns.Add(new DataColumn("SentTime", typeof(DateTime)));
detailTable.Columns.Add(new DataColumn("AttemptCount", typeof(int)));
detailTable.Columns.Add(new DataColumn("LastRetryDate", typeof(DateTime)));
detailTable.Columns.Add(new DataColumn("CreatedBy", typeof(int)));
detailTable.Columns.Add(new DataColumn("CreationDate", typeof(DateTime)));
foreach (MasterEmailLogDetails item in items)
{
detailTable.Rows.Add(
item.ID,
item.MasterEmailLogID,
(object)item.EmployeeID ?? DBNull.Value,
(object)item.EmployeeNo ?? DBNull.Value,
(object)item.EmployeeName ?? DBNull.Value,
(object)item.MailFrom ?? DBNull.Value,
(object)item.MailTo ?? DBNull.Value,
(object)item.CC ?? DBNull.Value,
(object)item.Subject ?? DBNull.Value,
(object)item.Body ?? DBNull.Value,
(object)item.AttachmentName ?? DBNull.Value,
(object)item.SalaryMonth ?? DBNull.Value,
(object)item.ReferenceID ?? DBNull.Value,
(int)item.SendStatus,
(object)item.ErrorMessage ?? DBNull.Value,
(object)item.SentTime ?? DBNull.Value,
item.AttemptCount,
(object)item.LastRetryDate ?? DBNull.Value,
item.CreatedBy,
item.CreatedDate);
}
using (SqlBulkCopy bulkCopy = new SqlBulkCopy((SqlConnection)tc.Connection, SqlBulkCopyOptions.Default,
(SqlTransaction)tc.Transaction))
{
bulkCopy.BulkCopyTimeout = 600; // in seconds
bulkCopy.BatchSize = 5000;
// Mapped by name on purpose: the table has more columns than we
// write here, and an ordinal mapping would silently shift if a
// column were ever added to either side.
bulkCopy.ColumnMappings.Add("MasterEmailLogDetailsID", "MasterEmailLogDetailsID");
bulkCopy.ColumnMappings.Add("MasterEmailLogID", "MasterEmailLogID");
bulkCopy.ColumnMappings.Add("EmployeeID", "EmployeeID");
bulkCopy.ColumnMappings.Add("EmployeeNo", "EmployeeNo");
bulkCopy.ColumnMappings.Add("EmployeeName", "EmployeeName");
bulkCopy.ColumnMappings.Add("MailFrom", "MailFrom");
bulkCopy.ColumnMappings.Add("MailTo", "MailTo");
bulkCopy.ColumnMappings.Add("CC", "CC");
bulkCopy.ColumnMappings.Add("Subject", "Subject");
bulkCopy.ColumnMappings.Add("Body", "Body");
bulkCopy.ColumnMappings.Add("AttachmentName", "AttachmentName");
bulkCopy.ColumnMappings.Add("SalaryMonth", "SalaryMonth");
bulkCopy.ColumnMappings.Add("ReferenceID", "ReferenceID");
bulkCopy.ColumnMappings.Add("SendStatus", "SendStatus");
bulkCopy.ColumnMappings.Add("ErrorMessage", "ErrorMessage");
bulkCopy.ColumnMappings.Add("SentTime", "SentTime");
bulkCopy.ColumnMappings.Add("AttemptCount", "AttemptCount");
bulkCopy.ColumnMappings.Add("LastRetryDate", "LastRetryDate");
bulkCopy.ColumnMappings.Add("CreatedBy", "CreatedBy");
bulkCopy.ColumnMappings.Add("CreationDate", "CreationDate");
bulkCopy.DestinationTableName = "MasterEmailLogDetails";
bulkCopy.WriteToServer(detailTable);
}
}
#endregion
#region Update function
/// <summary>
/// Stores the outcome of one recipient. MakeSQL cannot carry VARBINARY,
/// so this goes through a parameterised SqlCommand the same way
/// FileAttachmentDA writes its file bytes.
/// </summary>
internal static void UpdateResult(TransactionContext tc, MasterEmailLogDetails item)
{
string sql =
@"UPDATE MasterEmailLogDetails SET
MailFrom = @MailFrom,
MailTo = @MailTo,
Subject = @Subject,
Body = @Body,
AttachmentName = @AttachmentName,
AttachmentData = @AttachmentData,
SendStatus = @SendStatus,
ErrorMessage = @ErrorMessage,
SentTime = @SentTime,
AttemptCount = @AttemptCount,
LastRetryDate = @LastRetryDate,
ModifiedBy = @ModifiedBy,
ModifiedDate = @ModifiedDate
WHERE MasterEmailLogDetailsID = @MasterEmailLogDetailsID";
using (SqlCommand cmd = new SqlCommand(sql, (SqlConnection)tc.Connection))
{
cmd.Transaction = (SqlTransaction)tc.Transaction;
cmd.CommandTimeout = 300;
cmd.Parameters.Add("@MailFrom", SqlDbType.NVarChar, 250).Value = ToDb(item.MailFrom);
cmd.Parameters.Add("@MailTo", SqlDbType.NVarChar, 500).Value = ToDb(item.MailTo);
cmd.Parameters.Add("@Subject", SqlDbType.NVarChar, 500).Value = ToDb(item.Subject);
cmd.Parameters.Add("@Body", SqlDbType.NVarChar, -1).Value = ToDb(item.Body);
cmd.Parameters.Add("@AttachmentName", SqlDbType.NVarChar, 500).Value = ToDb(item.AttachmentName);
cmd.Parameters.Add("@AttachmentData", SqlDbType.VarBinary, -1).Value =
(object)item.AttachmentData ?? DBNull.Value;
cmd.Parameters.Add("@SendStatus", SqlDbType.Int).Value = (int)item.SendStatus;
cmd.Parameters.Add("@ErrorMessage", SqlDbType.NVarChar, -1).Value = ToDb(item.ErrorMessage);
cmd.Parameters.Add("@SentTime", SqlDbType.DateTime).Value = (object)item.SentTime ?? DBNull.Value;
cmd.Parameters.Add("@AttemptCount", SqlDbType.Int).Value = item.AttemptCount;
cmd.Parameters.Add("@LastRetryDate", SqlDbType.DateTime).Value =
(object)item.LastRetryDate ?? DBNull.Value;
cmd.Parameters.Add("@ModifiedBy", SqlDbType.Int).Value = (object)item.ModifiedBy ?? DBNull.Value;
cmd.Parameters.Add("@ModifiedDate", SqlDbType.DateTime).Value =
(object)item.ModifiedDate ?? DBNull.Value;
cmd.Parameters.Add("@MasterEmailLogDetailsID", SqlDbType.Int).Value = item.ID;
cmd.ExecuteNonQuery();
}
}
/// <summary>Empty string and null both land in the database as NULL.</summary>
private static object ToDb(string value)
{
return string.IsNullOrEmpty(value) ? (object)DBNull.Value : value;
}
#endregion
#region Get function
/// <summary>
/// Every read deliberately lists its columns instead of using * so that
/// AttachmentData is never dragged into a grid query. Use
/// <see cref="GetAttachment"/> when the bytes are actually needed.
/// </summary>
private const string SelectColumns =
"MasterEmailLogDetailsID, MasterEmailLogID, EmployeeID, EmployeeNo, EmployeeName, MailFrom, MailTo," +
" CC, Subject, Body, AttachmentName, SalaryMonth, ReferenceID, SendStatus, ErrorMessage, SentTime," +
" AttemptCount, LastRetryDate, CreatedBy, CreationDate, ModifiedBy, ModifiedDate," +
" CASE WHEN AttachmentData IS NULL THEN 0 ELSE 1 END AS HasAttachment";
internal static IDataReader Get(TransactionContext tc, int id)
{
return tc.ExecuteReader("SELECT %q FROM MasterEmailLogDetails WHERE MasterEmailLogDetailsID=%n",
SelectColumns, id);
}
internal static IDataReader GetByMasterID(TransactionContext tc, int masterEmailLogID)
{
return tc.ExecuteReader(
"SELECT %q FROM MasterEmailLogDetails WHERE MasterEmailLogID=%n ORDER BY MasterEmailLogDetailsID",
SelectColumns, masterEmailLogID);
}
internal static IDataReader GetByMasterID(TransactionContext tc, int masterEmailLogID,
EnumEmailSendStatus sendStatus)
{
return tc.ExecuteReader(
"SELECT %q FROM MasterEmailLogDetails WHERE MasterEmailLogID=%n AND SendStatus=%n" +
" ORDER BY MasterEmailLogDetailsID",
SelectColumns, masterEmailLogID, (int)sendStatus);
}
internal static IDataReader GetByEmployeeID(TransactionContext tc, int employeeID)
{
return tc.ExecuteReader(
"SELECT %q FROM MasterEmailLogDetails WHERE EmployeeID=%n ORDER BY MasterEmailLogDetailsID DESC",
SelectColumns, employeeID);
}
/// <summary>The stored bytes of one row, or null when nothing was kept.</summary>
internal static byte[] GetAttachment(TransactionContext tc, int masterEmailLogDetailsID)
{
object data = tc.ExecuteScalar(
"SELECT AttachmentData FROM MasterEmailLogDetails WHERE MasterEmailLogDetailsID=%n",
masterEmailLogDetailsID);
return (data == null || data == DBNull.Value) ? null : (byte[])data;
}
/// <summary>
/// One row per SendStatus with its count. The batch header is rebuilt
/// from this rather than from anything held in memory, so the numbers
/// stay correct no matter how many retries have run.
/// </summary>
internal static IDataReader GetStatusCounts(TransactionContext tc, int masterEmailLogID)
{
return tc.ExecuteReader(
"SELECT SendStatus, COUNT(*) AS StatusCount FROM MasterEmailLogDetails" +
" WHERE MasterEmailLogID=%n GROUP BY SendStatus", masterEmailLogID);
}
#endregion
#region Delete function
internal static void DeleteByMasterID(TransactionContext tc, int masterEmailLogID)
{
tc.ExecuteNonQuery("DELETE FROM MasterEmailLogDetails WHERE MasterEmailLogID=%n", masterEmailLogID);
}
#endregion
}
#endregion
}