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(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 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 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 Get() { List oPMSObjectiveProgress = null; TransactionContext tc = null; try { tc = TransactionContext.Begin(); DataReader oreader = new DataReader(PMSObjectiveProgressDA.Get(tc)); oPMSObjectiveProgress = this.CreateObjects(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(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 GetByObjectiveID(int objectiveId) { List oPMSObjectiveProgress = null; TransactionContext tc = null; try { tc = TransactionContext.Begin(); DataReader oreader = new DataReader(PMSObjectiveProgressDA.GetByObjectiveID(tc, objectiveId)); oPMSObjectiveProgress = this.CreateObjects(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 GetUnreadMessage(string objectiveIDs) { List oPMSObjectiveProgress = null; TransactionContext tc = null; try { tc = TransactionContext.Begin(); DataReader oreader = new DataReader(PMSObjectiveProgressDA.GetUnreadMessage(tc, objectiveIDs)); oPMSObjectiveProgress = this.CreateObjects(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 = $@"

{pmsObjectiveProgress.Description}

"; 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; } } }