HRM-10/HRM.DA/Service/PMP/PMSObjectiveProgressService.cs

387 lines
12 KiB
C#
Raw Permalink Normal View History

2026-11-03 13:06:00 +06:00
using Ease.Core;
using Ease.Core.DataAccess;
using Ease.Core.Model;
using Ease.Core.Utility;
using HRM.BO;
using HRM.BO.Configuration;
using Microsoft.Extensions.Configuration;
using System;
using System.Collections.Generic;
using System.Data;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace HRM.DA
{
public class PMSObjectiveProgressService : ServiceTemplate, IPMSObjectiveProgressService
{
#region ObjectMapping
private void MapObject(PMSObjectiveProgress oPMSObjectiveProgress, DataReader oReader)
{
SetObjectID(oPMSObjectiveProgress, oReader.GetInt32("PMSObjectiveProgressID", 0));
oPMSObjectiveProgress.ObjectiveID = oReader.GetInt32("ObjectiveID", 0);
oPMSObjectiveProgress.EmployeeID = oReader.GetInt32("EmployeeID", 0);
oPMSObjectiveProgress.Description = oReader.GetString("Description", null);
oPMSObjectiveProgress.UpdateTime = oReader.GetDateTime("UpdateTime").Value;
oPMSObjectiveProgress.AchivePercent = oReader.GetDouble("AchivePercent", 0);
oPMSObjectiveProgress.IsRead = oReader.GetBoolean("IsRead").Value;
oPMSObjectiveProgress.ReadTime = oReader.GetDateTime("ReadTime").HasValue ? oReader.GetDateTime("ReadTime").Value : null;
oPMSObjectiveProgress.IsEmployee = oReader.GetBoolean("IsEmployee").Value;
oPMSObjectiveProgress.fromEmpID = oReader.GetInt32("fromEmpID").Value;
oPMSObjectiveProgress.IsLmAchknowldged = oReader.GetBoolean("IsLmAchknowldged", false);
oPMSObjectiveProgress.RatingStart = oReader.GetInt32("RatingStart").HasValue ? oReader.GetInt32("RatingStart").Value : null;
this.SetObjectState(oPMSObjectiveProgress, ObjectState.Saved);
}
protected override T CreateObject<T>(DataReader oReader)
{
PMSObjectiveProgress oPMSObjectiveProgress = new PMSObjectiveProgress();
MapObject(oPMSObjectiveProgress, oReader);
return oPMSObjectiveProgress as T;
}
#endregion
#region Save
public int Save(PMSObjectiveProgress item)
{
TransactionContext tc = null;
try
{
tc = TransactionContext.Begin(true);
if (item.IsNew)
{
int id = tc.GenerateID("PMSObjectiveProgress", "PMSObjectiveProgressID");
base.SetObjectID(item, (id));
PMSObjectiveProgressDA.Insert(tc, item);
}
else
{
PMSObjectiveProgressDA.Update(tc, item);
}
tc.End();
return item.ID;
}
catch (Exception e)
{
#region Handle Exception
if (tc != null)
tc.HandleError();
ExceptionLog.Write(e);
throw new ServiceException(e.Message, e);
#endregion
}
}
public void Save(List<PMSObjectiveProgress> items)
{
TransactionContext tc = null;
try
{
tc = TransactionContext.Begin(true);
int id = tc.GenerateID("PMSObjectiveProgress", "PMSObjectiveProgressID");
foreach (PMSObjectiveProgress item in items)
{
if (item.IsNew)
{
base.SetObjectID(item, (id));
PMSObjectiveProgressDA.Insert(tc, item);
id++;
if(item.AchivePercent > 0 && !item.IsEmployee)
{
PMSObjectiveProgressDA.UpdateProgressStatus(tc, item.ObjectiveID, (double)item.AchivePercent);
}
}
else
{
PMSObjectiveProgressDA.Update(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 void UpdateReadTime(List<PMSObjectiveProgress> items)
{
TransactionContext tc = null;
try
{
tc = TransactionContext.Begin(true);
foreach (PMSObjectiveProgress item in items)
{
if (item.IsNew)
{
}
else
{
PMSObjectiveProgressDA.UpdateReadTime(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
}
}
#endregion
#region GetAll
public List<PMSObjectiveProgress> Get()
{
List<PMSObjectiveProgress> oPMSObjectiveProgress = null;
TransactionContext tc = null;
try
{
tc = TransactionContext.Begin();
DataReader oreader = new DataReader(PMSObjectiveProgressDA.Get(tc));
oPMSObjectiveProgress = this.CreateObjects<PMSObjectiveProgress>(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 oPMSObjectiveProgress;
}
#endregion
#region Get By ID
public PMSObjectiveProgress Get(int id)
{
PMSObjectiveProgress oPMSObjectiveProgress = null;
TransactionContext tc = null;
try
{
tc = TransactionContext.Begin();
DataReader oreader = new DataReader(PMSObjectiveProgressDA.Get(tc, id));
if (oreader.Read())
{
oPMSObjectiveProgress = this.CreateObject<PMSObjectiveProgress>(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 oPMSObjectiveProgress;
}
#endregion
#region Get By ObjectiveSet
public List<PMSObjectiveProgress> GetByObjectiveID(int objectiveId)
{
List<PMSObjectiveProgress> oPMSObjectiveProgress = null;
TransactionContext tc = null;
try
{
tc = TransactionContext.Begin();
DataReader oreader = new DataReader(PMSObjectiveProgressDA.GetByObjectiveID(tc, objectiveId));
oPMSObjectiveProgress = this.CreateObjects<PMSObjectiveProgress>(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 oPMSObjectiveProgress;
}
#endregion
#region Get Unread Message
public List<PMSObjectiveProgress> GetUnreadMessage(string objectiveIDs)
{
List<PMSObjectiveProgress> oPMSObjectiveProgress = null;
TransactionContext tc = null;
try
{
tc = TransactionContext.Begin();
DataReader oreader = new DataReader(PMSObjectiveProgressDA.GetUnreadMessage(tc, objectiveIDs));
oPMSObjectiveProgress = this.CreateObjects<PMSObjectiveProgress>(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 oPMSObjectiveProgress;
}
public int GetUnreadEmployeeMessageCountByObjectiveSetID(int objectiveSetID)
{
int unreadMessageCount = 0;
TransactionContext tc = null;
try
{
tc = TransactionContext.Begin();
unreadMessageCount = PMSObjectiveProgressDA.GetUnreadEmployeeMessageCountByObjectiveSetID(tc, objectiveSetID);
tc.End();
}
catch (Exception e)
{
#region Handle Exception
if (tc != null)
tc.HandleError();
ExceptionLog.Write(e);
throw new ServiceException(e.Message, e);
#endregion
}
return unreadMessageCount;
}
#endregion
public void SendMail(PMSObjectiveProgress pmsObjectiveProgress)
{
try
{
Employee reciever = null;
Employee sender = null;
string Name = "";
string Email = "";
if (pmsObjectiveProgress.IsEmployee)
{
sender = new EmployeeService().Get(pmsObjectiveProgress.EmployeeID);
if (sender.LineManagerID != null)
reciever = new EmployeeService().Get((int)sender.LineManagerID);
else
throw new Exception("Line Manager Not Assined. Cannot send Email");
Name = reciever.Name;
Email = reciever.EmailAddress;
}
else
{
reciever = new EmployeeService().Get(pmsObjectiveProgress.EmployeeID);
if (reciever.LineManagerID != null)
sender = new EmployeeService().Get((int)reciever.LineManagerID);
Name = reciever.Name;
Email = reciever.EmailAddress;
}
var builder = new ConfigurationBuilder().SetBasePath(Directory.GetCurrentDirectory()).AddJsonFile("appsettings.json");
EmailSettings emailSettings = new EmailSettings();
IConfiguration Configuration = builder.Build();
Configuration.GetSection("EmailSettings").Bind(emailSettings);
MailSender mailSender = new MailSender();
mailSender.AddTo(Email)
;
mailSender.Subject = "Message from [" + sender.EmployeeNo + "] " + sender.Name + " at " + pmsObjectiveProgress.UpdateTime.ToString("g") + " regarding PMS";
mailSender.Body = $@"<p>{pmsObjectiveProgress.Description}</p>";
mailSender.ThreadSendMail(emailSettings);
}
catch (Exception ex)
{
throw new ServiceException(ex.Message);
//throw new CustomException(EnumExceptionType.Informational, "Successfully submitted but system could not send E-mail. You do not need to submit it again.");
}
}
public DataTable GetEmpProgress(int pmpYearID)
{
TransactionContext tc = null;
DataTable otab;
try
{
tc = TransactionContext.Begin();
otab = PMSObjectiveProgressDA.GetEmpProgress(tc, pmpYearID);
tc.End();
}
catch (Exception e)
{
#region Handle Exception
if (tc != null)
tc.HandleError();
ExceptionLog.Write(e);
throw new ServiceException(e.Message, e);
#endregion
}
return otab;
}
}
}