using System;
using System.Data;
using HRM.BO;
using Ease.Core.DataAccess;
using Ease.Core.DataAccess.SQL;
using Microsoft.Data.SqlClient;
namespace HRM.DA
{
///
/// Schedule Email (CalendarItem.ItemType = 4): its attachment (dbo.CalendarItemAttachment), its runs
/// (dbo.CalendarEmailRun, one per run, linked to the MasterEmailLog batch the run created) and the employee data
/// its body tags are filled from.
///
internal static class CalendarEmailDA
{
#region Attachment
internal static void InsertAttachment(TransactionContext tc, int attachmentID, int calendarItemID, string fileName,
byte[] fileData, int createdBy, DateTime createdDate)
{
// The bytes go as a parameter: MakeSQL has no placeholder for binary data.
SqlParameter[] p = new SqlParameter[]
{
SqlHelperExtension.CreateInParam("@FileData", SqlDbType.VarBinary, fileData)
};
tc.ExecuteNonQuery(CommandType.Text, SQLParser.MakeSQL(@"INSERT INTO CalendarItemAttachment
(CalendarItemAttachmentID, CalendarItemID, FileName, FileSize, FileData, CreatedBy, CreatedDate)
VALUES (%n, %n, %u, %n, @FileData, %n, %D)",
attachmentID, calendarItemID, fileName, fileData.Length, createdBy, createdDate), p);
}
/// Copies the attachment of one item onto another (a part split off a repeating schedule email).
internal static void CopyAttachment(TransactionContext tc, int attachmentID, int fromItemID, int toItemID,
int createdBy, DateTime createdDate)
{
tc.ExecuteNonQuery(@"INSERT INTO CalendarItemAttachment
(CalendarItemAttachmentID, CalendarItemID, FileName, FileSize, FileData, CreatedBy, CreatedDate)
SELECT TOP 1 %n, %n, FileName, FileSize, FileData, %n, %D
FROM CalendarItemAttachment WHERE CalendarItemID = %n ORDER BY CalendarItemAttachmentID DESC",
attachmentID, toItemID, createdBy, createdDate, fromItemID);
}
internal static void DeleteAttachments(TransactionContext tc, int calendarItemID)
{
tc.ExecuteNonQuery("DELETE FROM CalendarItemAttachment WHERE CalendarItemID = %n", calendarItemID);
}
/// Name and size of the item's attachment (no bytes), for showing it in the form and the detail.
internal static IDataReader GetAttachmentInfo(TransactionContext tc, int calendarItemID)
{
return tc.ExecuteReader(@"SELECT TOP 1 FileName, FileSize FROM CalendarItemAttachment
WHERE CalendarItemID = %n ORDER BY CalendarItemAttachmentID DESC", calendarItemID);
}
/// The item's attachment with its bytes, for a run.
internal static IDataReader GetAttachment(TransactionContext tc, int calendarItemID)
{
return tc.ExecuteReader(@"SELECT TOP 1 FileName, FileData FROM CalendarItemAttachment
WHERE CalendarItemID = %n ORDER BY CalendarItemAttachmentID DESC", calendarItemID);
}
#endregion
#region Runs
/// The run row of one run, or no row when that run has not been taken yet.
internal static IDataReader GetRun(TransactionContext tc, int calendarItemID, DateTime runAt)
{
return tc.ExecuteReader("SELECT CalendarEmailRunID, MasterEmailLogID FROM CalendarEmailRun WHERE CalendarItemID = %n AND RunAt = %D",
calendarItemID, runAt);
}
/// Claims one run. The (item, run) pair is unique, so a second claim of the same run fails.
internal static void InsertRun(TransactionContext tc, int runID, int calendarItemID, DateTime runAt, int recipientCount,
DateTime createdDate)
{
tc.ExecuteNonQuery(@"INSERT INTO CalendarEmailRun (CalendarEmailRunID, CalendarItemID, RunAt, MasterEmailLogID, RecipientCount, CreatedDate)
VALUES (%n, %n, %D, NULL, %n, %D)",
runID, calendarItemID, runAt, recipientCount, createdDate);
}
internal static void SetRunBatch(TransactionContext tc, int runID, int masterEmailLogID, int recipientCount)
{
tc.ExecuteNonQuery("UPDATE CalendarEmailRun SET MasterEmailLogID = %n, RecipientCount = %n WHERE CalendarEmailRunID = %n",
masterEmailLogID, recipientCount, runID);
}
///
/// The latest runs of a schedule email, newest first, with the counts of their recipient rows. Counted from the rows
/// (not the batch header, whose counters are written only when a send ends) so a run in progress shows real numbers.
///
internal static IDataReader GetRuns(TransactionContext tc, int calendarItemID, int maxRuns)
{
return tc.ExecuteReader(@"SELECT TOP (%n) r.RunAt, r.MasterEmailLogID,
ISNULL(c.TotalCount, 0) AS TotalCount, ISNULL(c.SentCount, 0) AS SentCount,
ISNULL(c.FailedCount, 0) AS FailedCount, ISNULL(c.SkippedCount, 0) AS SkippedCount,
ISNULL(c.PendingCount, 0) AS PendingCount
FROM CalendarEmailRun r
OUTER APPLY (SELECT COUNT(*) AS TotalCount,
SUM(CASE WHEN d.SendStatus = %n THEN 1 ELSE 0 END) AS SentCount,
SUM(CASE WHEN d.SendStatus = %n THEN 1 ELSE 0 END) AS FailedCount,
SUM(CASE WHEN d.SendStatus = %n THEN 1 ELSE 0 END) AS SkippedCount,
SUM(CASE WHEN d.SendStatus = %n THEN 1 ELSE 0 END) AS PendingCount
FROM MasterEmailLogDetails d WHERE d.MasterEmailLogID = r.MasterEmailLogID) c
WHERE r.CalendarItemID = %n
ORDER BY r.RunAt DESC",
maxRuns, (int)EnumEmailSendStatus.Success, (int)EnumEmailSendStatus.Failed, (int)EnumEmailSendStatus.Skipped,
(int)EnumEmailSendStatus.Pending, calendarItemID);
}
#endregion
#region Tag data
///
/// What the body tags of a schedule email are filled from, for the given employees. employeeIDs is a
/// comma-separated list of ints built by code.
///
internal static IDataReader GetEmployeeTagData(TransactionContext tc, string employeeIDs)
{
return tc.ExecuteReader(@"SELECT e.EMPLOYEEID, e.EMPLOYEENO, e.NAME, e.GENDER, e.MARITALSTATUSID, e.JOININGDATE,
e.BASICSALARY, e.GROSSSALARY, e.LINEMANAGERID,
COALESCE(NULLIF(LTRIM(RTRIM(dsg.NAME)), ''), e.DESIGDESCRIPTION) AS DesignationName,
dpt.DESCRIPTION AS DepartmentName, loc.DESCRIPTION AS LocationName,
cat.DESCRIPTION AS CategoryName, rel.NAME AS ReligionName, lm.NAME AS LineManagerName
FROM EMPLOYEE e
LEFT JOIN DESIGNATION dsg ON dsg.DESIGNATIONID = e.DESIGNATIONID
LEFT JOIN DEPARTMENT dpt ON dpt.DEPARTMENTID = e.DEPARTMENTID
LEFT JOIN LOCATION loc ON loc.LOCATIONID = e.LOCATIONID
LEFT JOIN CATEGORY cat ON cat.CATEGORYID = e.CATEGORYID
LEFT JOIN RELIGION rel ON rel.RELIGIONID = e.RELIGIONID
LEFT JOIN EMPLOYEE lm ON lm.EMPLOYEEID = e.LINEMANAGERID
WHERE e.EMPLOYEEID IN (%q)", employeeIDs);
}
#endregion
}
}