HRM-10/HRM.DA/Service/Mail/MasterEmailLogService.cs

507 lines
17 KiB
C#
Raw Permalink Normal View History

2026-11-03 13:06:00 +06:00
using HRM.BO;
using Ease.Core.DataAccess;
using Ease.Core.Model;
using Ease.Core.Utility;
using System;
using System.Collections.Generic;
namespace HRM.DA
{
public class MasterEmailLogService : ServiceTemplate, IMasterEmailLogService
{
public MasterEmailLogService()
{
}
#region Object mapping
private void MapObject(MasterEmailLog oItem, DataReader oReader)
{
base.SetObjectID(oItem, oReader.GetInt32("MasterEmailLogID").Value);
oItem.EmailType = (EnumEmailType)oReader.GetInt32("EmailType").Value;
oItem.Description = oReader.GetString("Description");
oItem.PayrollTypeID = oReader.GetInt32("PayrollTypeID");
oItem.ParameterJson = oReader.GetString("ParameterJson");
oItem.TotalCount = oReader.GetInt32("TotalCount", 0);
oItem.SuccessCount = oReader.GetInt32("SuccessCount", 0);
oItem.FailedCount = oReader.GetInt32("FailedCount", 0);
oItem.SkippedCount = oReader.GetInt32("SkippedCount", 0);
oItem.BatchStatus = (EnumEmailBatchStatus)oReader.GetInt32("BatchStatus").Value;
oItem.StartTime = oReader.GetDateTime("StartTime");
oItem.EndTime = oReader.GetDateTime("EndTime");
oItem.RetryCount = oReader.GetInt32("RetryCount", 0);
oItem.LastRetryDate = oReader.GetDateTime("LastRetryDate");
oItem.PreserveAttachment = oReader.GetBoolean("PreserveAttachment", true);
oItem.CreatedBy = oReader.GetInt32("CreatedBy", 0);
oItem.CreatedDate = oReader.GetDateTime("CreationDate").Value;
oItem.ModifiedBy = oReader.GetInt32("ModifiedBy");
oItem.ModifiedDate = oReader.GetDateTime("ModifiedDate");
// Only the history query joins Users, so this column is optional.
oItem.CreatedByName = oReader.GetString("CreatedByName", true, string.Empty);
oItem.EmailTypeName = oItem.EmailType.ToString().Replace("_", " ");
oItem.BatchStatusName = oItem.BatchStatus.ToString().Replace("_", " ");
this.SetObjectState(oItem, Ease.Core.ObjectState.Saved);
}
private void MapDetailObject(MasterEmailLogDetails oItem, DataReader oReader)
{
base.SetObjectID(oItem, oReader.GetInt32("MasterEmailLogDetailsID").Value);
oItem.MasterEmailLogID = oReader.GetInt32("MasterEmailLogID").Value;
oItem.EmployeeID = oReader.GetInt32("EmployeeID");
oItem.EmployeeNo = oReader.GetString("EmployeeNo");
oItem.EmployeeName = oReader.GetString("EmployeeName");
oItem.MailFrom = oReader.GetString("MailFrom");
oItem.MailTo = oReader.GetString("MailTo");
oItem.CC = oReader.GetString("CC");
oItem.Subject = oReader.GetString("Subject");
oItem.Body = oReader.GetString("Body");
oItem.AttachmentName = oReader.GetString("AttachmentName");
oItem.SalaryMonth = oReader.GetDateTime("SalaryMonth");
oItem.ReferenceID = oReader.GetInt32("ReferenceID");
oItem.SendStatus = (EnumEmailSendStatus)oReader.GetInt32("SendStatus").Value;
oItem.ErrorMessage = oReader.GetString("ErrorMessage");
oItem.SentTime = oReader.GetDateTime("SentTime");
oItem.AttemptCount = oReader.GetInt32("AttemptCount", 0);
oItem.LastRetryDate = oReader.GetDateTime("LastRetryDate");
oItem.CreatedBy = oReader.GetInt32("CreatedBy", 0);
oItem.CreatedDate = oReader.GetDateTime("CreationDate").Value;
oItem.ModifiedBy = oReader.GetInt32("ModifiedBy");
oItem.ModifiedDate = oReader.GetDateTime("ModifiedDate");
oItem.HasAttachment = oReader.GetInt32("HasAttachment", 0) == 1;
oItem.SendStatusName = oItem.SendStatus.ToString().Replace("_", " ");
this.SetObjectState(oItem, Ease.Core.ObjectState.Saved);
}
protected override T CreateObject<T>(DataReader oReader)
{
if (typeof(T) == typeof(MasterEmailLogDetails))
{
MasterEmailLogDetails oDetail = new MasterEmailLogDetails();
MapDetailObject(oDetail, oReader);
return oDetail as T;
}
MasterEmailLog oItem = new MasterEmailLog();
MapObject(oItem, oReader);
return oItem as T;
}
#endregion
#region Service implementation : Read
public MasterEmailLog Get(int id)
{
MasterEmailLog oItem = null;
TransactionContext tc = null;
try
{
tc = TransactionContext.Begin();
DataReader oreader = new DataReader(MasterEmailLogDA.Get(tc, id));
if (oreader.Read())
{
oItem = this.CreateObject<MasterEmailLog>(oreader);
}
oreader.Close();
tc.End();
}
catch (Exception e)
{
#region Handle Exception
if (tc != null)
tc.HandleError();
ExceptionLog.Write(e);
throw new ServiceException(e.Message, e);
#endregion
}
return oItem;
}
public List<MasterEmailLog> Get(EnumEmailType emailType, int? payrollTypeID, DateTime fromDate,
DateTime toDate)
{
List<MasterEmailLog> items = new List<MasterEmailLog>();
TransactionContext tc = null;
try
{
tc = TransactionContext.Begin();
DataReader oreader =
new DataReader(MasterEmailLogDA.Get(tc, emailType, payrollTypeID, fromDate, toDate));
items = this.CreateObjects<MasterEmailLog>(oreader);
oreader.Close();
tc.End();
}
catch (Exception e)
{
#region Handle Exception
if (tc != null)
tc.HandleError();
ExceptionLog.Write(e);
throw new ServiceException(e.Message, e);
#endregion
}
return items;
}
public MasterEmailLog GetWithDetails(int id)
{
MasterEmailLog oItem = null;
TransactionContext tc = null;
try
{
tc = TransactionContext.Begin();
DataReader oreader = new DataReader(MasterEmailLogDA.Get(tc, id));
if (oreader.Read())
{
oItem = this.CreateObject<MasterEmailLog>(oreader);
}
oreader.Close();
if (oItem != null)
{
DataReader dreader = new DataReader(MasterEmailLogDetailsDA.GetByMasterID(tc, id));
oItem.Details = this.CreateObjects<MasterEmailLogDetails>(dreader);
dreader.Close();
}
tc.End();
}
catch (Exception e)
{
#region Handle Exception
if (tc != null)
tc.HandleError();
ExceptionLog.Write(e);
throw new ServiceException(e.Message, e);
#endregion
}
return oItem;
}
public List<MasterEmailLogDetails> GetDetails(int masterEmailLogID)
{
List<MasterEmailLogDetails> items = new List<MasterEmailLogDetails>();
TransactionContext tc = null;
try
{
tc = TransactionContext.Begin();
DataReader oreader = new DataReader(MasterEmailLogDetailsDA.GetByMasterID(tc, masterEmailLogID));
items = this.CreateObjects<MasterEmailLogDetails>(oreader);
oreader.Close();
tc.End();
}
catch (Exception e)
{
#region Handle Exception
if (tc != null)
tc.HandleError();
ExceptionLog.Write(e);
throw new ServiceException(e.Message, e);
#endregion
}
return items;
}
public List<MasterEmailLogDetails> GetDetails(int masterEmailLogID, EnumEmailSendStatus sendStatus)
{
List<MasterEmailLogDetails> items = new List<MasterEmailLogDetails>();
TransactionContext tc = null;
try
{
tc = TransactionContext.Begin();
DataReader oreader =
new DataReader(MasterEmailLogDetailsDA.GetByMasterID(tc, masterEmailLogID, sendStatus));
items = this.CreateObjects<MasterEmailLogDetails>(oreader);
oreader.Close();
tc.End();
}
catch (Exception e)
{
#region Handle Exception
if (tc != null)
tc.HandleError();
ExceptionLog.Write(e);
throw new ServiceException(e.Message, e);
#endregion
}
return items;
}
public List<MasterEmailLogDetails> GetDetailsByEmployee(int employeeID)
{
List<MasterEmailLogDetails> items = new List<MasterEmailLogDetails>();
TransactionContext tc = null;
try
{
tc = TransactionContext.Begin();
DataReader oreader = new DataReader(MasterEmailLogDetailsDA.GetByEmployeeID(tc, employeeID));
items = this.CreateObjects<MasterEmailLogDetails>(oreader);
oreader.Close();
tc.End();
}
catch (Exception e)
{
#region Handle Exception
if (tc != null)
tc.HandleError();
ExceptionLog.Write(e);
throw new ServiceException(e.Message, e);
#endregion
}
return items;
}
public byte[] GetAttachment(int masterEmailLogDetailsID)
{
byte[] data = null;
TransactionContext tc = null;
try
{
tc = TransactionContext.Begin();
data = MasterEmailLogDetailsDA.GetAttachment(tc, masterEmailLogDetailsID);
tc.End();
}
catch (Exception e)
{
#region Handle Exception
if (tc != null)
tc.HandleError();
ExceptionLog.Write(e);
throw new ServiceException(e.Message, e);
#endregion
}
return data;
}
#endregion
#region Service implementation : Write
public int SaveBatch(MasterEmailLog log, List<MasterEmailLogDetails> details)
{
TransactionContext tc = null;
try
{
tc = TransactionContext.Begin(true);
base.SetObjectID(log, tc.GenerateID("MasterEmailLog", "MasterEmailLogID"));
log.TotalCount = details == null ? 0 : details.Count;
MasterEmailLogDA.Insert(tc, log);
if (details != null && details.Count > 0)
{
// One GenerateID call, then a running number. GenerateID is a
// SELECT MAX+1, so asking it once per row would be both slow
// and wrong - every call inside this transaction would hand
// back the same value until the rows are actually written.
int nextID = tc.GenerateID("MasterEmailLogDetails", "MasterEmailLogDetailsID");
foreach (MasterEmailLogDetails detail in details)
{
detail.MasterEmailLogID = log.ID;
base.SetObjectID(detail, nextID++);
detail.CreatedBy = log.CreatedBy;
detail.CreatedDate = log.CreatedDate;
}
MasterEmailLogDetailsDA.BulkInsert(tc, details);
}
tc.End();
return log.ID;
}
catch (Exception e)
{
#region Handle Exception
if (tc != null)
tc.HandleError();
ExceptionLog.Write(e);
throw new ServiceException(e.Message, e);
#endregion
}
}
public void UpdateDetailResult(MasterEmailLogDetails item)
{
TransactionContext tc = null;
try
{
tc = TransactionContext.Begin(true);
MasterEmailLogDetailsDA.UpdateResult(tc, item);
tc.End();
}
catch (Exception e)
{
#region Handle Exception
if (tc != null)
tc.HandleError();
ExceptionLog.Write(e);
throw new ServiceException(e.Message, e);
#endregion
}
}
public MasterEmailLog RefreshBatchCounters(int masterEmailLogID, bool markEnded, int? modifiedBy)
{
MasterEmailLog oItem = null;
TransactionContext tc = null;
try
{
tc = TransactionContext.Begin(true);
int total = 0, success = 0, failed = 0, skipped = 0, pending = 0;
DataReader oreader = new DataReader(MasterEmailLogDetailsDA.GetStatusCounts(tc, masterEmailLogID));
while (oreader.Read())
{
EnumEmailSendStatus status = (EnumEmailSendStatus)oreader.GetInt32("SendStatus").Value;
int count = oreader.GetInt32("StatusCount", 0);
total += count;
switch (status)
{
case EnumEmailSendStatus.Success:
success = count;
break;
case EnumEmailSendStatus.Failed:
failed = count;
break;
case EnumEmailSendStatus.Skipped:
skipped = count;
break;
default:
pending = count;
break;
}
}
oreader.Close();
EnumEmailBatchStatus batchStatus;
if (!markEnded || pending > 0)
{
batchStatus = EnumEmailBatchStatus.Processing;
}
else if (failed > 0 || skipped > 0)
{
// Nothing got through at all - that is a batch-level failure
// (bad SMTP settings, for instance), not a per-employee one.
batchStatus = success > 0
? EnumEmailBatchStatus.CompletedWithError
: EnumEmailBatchStatus.Failed;
}
else
{
batchStatus = EnumEmailBatchStatus.Completed;
}
DateTime now = DateTime.Now;
MasterEmailLogDA.UpdateCounters(tc, masterEmailLogID, total, success, failed, skipped, batchStatus,
markEnded && pending == 0 ? (DateTime?)now : null, modifiedBy, now);
DataReader mreader = new DataReader(MasterEmailLogDA.Get(tc, masterEmailLogID));
if (mreader.Read())
{
oItem = this.CreateObject<MasterEmailLog>(mreader);
}
mreader.Close();
tc.End();
}
catch (Exception e)
{
#region Handle Exception
if (tc != null)
tc.HandleError();
ExceptionLog.Write(e);
throw new ServiceException(e.Message, e);
#endregion
}
return oItem;
}
public void MarkRetryStarted(int masterEmailLogID, int modifiedBy)
{
TransactionContext tc = null;
try
{
tc = TransactionContext.Begin(true);
MasterEmailLogDA.UpdateRetryStarted(tc, masterEmailLogID, modifiedBy, DateTime.Now);
tc.End();
}
catch (Exception e)
{
#region Handle Exception
if (tc != null)
tc.HandleError();
ExceptionLog.Write(e);
throw new ServiceException(e.Message, e);
#endregion
}
}
public void Delete(int masterEmailLogID)
{
TransactionContext tc = null;
try
{
tc = TransactionContext.Begin(true);
MasterEmailLogDetailsDA.DeleteByMasterID(tc, masterEmailLogID);
MasterEmailLogDA.Delete(tc, masterEmailLogID);
tc.End();
}
catch (Exception e)
{
#region Handle Exception
if (tc != null)
tc.HandleError();
ExceptionLog.Write(e);
throw new ServiceException(e.Message, e);
#endregion
}
}
#endregion
}
}