EchoTex_Payroll/HRM.DA/Service/Users/UserRoleService.cs

674 lines
21 KiB
C#
Raw Normal View History

2024-10-14 10:01:49 +06:00
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;
namespace HRM.DA
{
public class UserRoleService : ServiceTemplate, IUserRoleService
{
public UserRoleService()
{
}
private void MapObject(UserRole oUserRole, DataReader oReader)
{
base.SetObjectID(oUserRole, oReader.GetInt32("UserRoleID", 0));
oUserRole.RoleID = oReader.GetInt32("RoleID").Value;
oUserRole.UserID = oReader.GetInt32("UserID", 0);
oUserRole.EventDate = oReader.GetDateTime("EventDate").Value;
oUserRole.CreatedBy = oReader.GetInt32("CreatedBy", 0);
oUserRole.AuthorizedBy = oReader.GetInt32("AuthorizedBy", 0);
oUserRole.AuthorizedDate = oReader.GetDateTime("AuthorizedDate");
oUserRole.Status = (EnumStatus)oReader.GetInt32("Status").Value;
oUserRole.ComputerName = oReader.GetString("ComputerName");
oUserRole.ApprovedComputerName = oReader.GetString("ApprovedComputerName");
oUserRole.UserRoleStatus = (EnumAuthStatus)oReader.GetInt32("Status").Value;
oUserRole.employeeID = oReader.GetInt32("employeeID",true, 0) ==0? null: oReader.GetInt32("employeeID");
oUserRole.loginIDView = oReader.GetString("LoginID", true, string.Empty);
oUserRole.userNameView = oReader.GetString("userName", true, string.Empty);
oUserRole.roleNameView = oReader.GetString("roleName", true, string.Empty);
this.SetObjectState(oUserRole, Ease.Core.ObjectState.Saved);
}
protected override T CreateObject<T>(DataReader oReader)
{
UserRole oUserRole = new UserRole();
MapObject(oUserRole, oReader);
return oUserRole as T;
}
#region Service implementation
public UserRole Get(int id)
{
UserRole oUserRole = null;
TransactionContext tc = null;
try
{
tc = TransactionContext.Begin();
DataReader oreader = new DataReader(UserRoleDA.Get(tc, id));
if (oreader.Read())
{
oUserRole = this.CreateObject<UserRole>(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 oUserRole;
}
public List<UserRole> Get(EnumRoleType roleType, int? userid, int? RoleID)
{
List<UserRole> userRoles = new List<UserRole>();
TransactionContext tc = null;
try
{
tc = TransactionContext.Begin();
DataReader dr = new DataReader(UserRoleDA.Get(tc, roleType, userid, RoleID));
userRoles = this.CreateObjects<UserRole>(dr);
dr.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 userRoles;
}
public List<UserRole> Get(List<SearchEmployee> searchEmps)
{
List<UserRole> userRoles = new List<UserRole>();
string insQL = "";
searchEmps.ForEach(x => { insQL = insQL + x.EmployeeID + ", "; });
if (insQL.Length > 2)
{
insQL = insQL.Substring(0, insQL.Length - 2);
}
TransactionContext tc = null;
try
{
tc = TransactionContext.Begin();
DataReader dr = new DataReader(UserRoleDA.getAllByUserEMpNo(tc, insQL));
userRoles = this.CreateObjects<UserRole>(dr);
dr.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 userRoles;
}
public List<UserRole> GetByUserID(int UserID)
{
List<UserRole> userRoles = new List<UserRole>();
TransactionContext tc = null;
try
{
tc = TransactionContext.Begin();
DataReader dr = new DataReader(UserRoleDA.GetByUserID(tc, UserID));
userRoles = this.CreateObjects<UserRole>(dr);
dr.Close();
tc.End();
}
catch (Exception e)
{
#region Handle Exception
if (tc != null)
tc.HandleError();
ExceptionLog.Write(e);
throw new ServiceException(e.Message, e);
#endregion
}
#endregion
return userRoles;
}
public int Save(UserRole oUserRole)
{
TransactionContext tc = null;
try
{
tc = TransactionContext.Begin(true);
if (oUserRole.IsNew)
{
int id = tc.GenerateID("UserRole", "UserRoleID");
base.SetObjectID(oUserRole, id);
UserRoleDA.Insert(tc, oUserRole);
}
else
{
UserRoleDA.Update(tc, oUserRole);
}
tc.End();
return oUserRole.ID;
}
catch (Exception e)
{
#region Handle Exception
if (tc != null)
tc.HandleError();
ExceptionLog.Write(e);
throw new ServiceException(e.Message, e);
#endregion
}
}
//public void SaveSingleUserRole(UserRole oUserRole)
//{
// TransactionContext tc = null;
// try
// {
// tc = TransactionContext.Begin(true);
// int id = tc.GenerateID("UserRole", "UserRoleID");
// oUserRole.SetObjectID(id);
// UserRoleDA.Insert(tc, oUserRole);
// 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 DeleteByUserID2(int id)
//{
// TransactionContext tc = null;
// try
// {
// tc = TransactionContext.Begin(true);
// this.DeleteByUserID(tc, id);
// tc.End();
// }
// catch (Exception e)
// {
// #region Handle Exception
// if (tc != null)
// tc.HandleError();
// ExceptionLog.Write(e);
// throw new ServiceException(e.Message, e);
// #endregion
// }
//}
private string MakePassWord(DateTime birthdate)
{
string password = String.Empty;
int year = birthdate.Year;
int month = birthdate.Month;
int day = birthdate.Day;
string emonth;
if (month < 10) emonth = "0" + month.ToString();
else emonth = month.ToString();
string eday;
if (day < 10) eday = "0" + day.ToString();
else eday = day.ToString();
password = year.ToString() + emonth + eday;
return password;
}
public void Save(List<UserRole> oUserRoles, int parentUserID)
{
TransactionContext tc = null;
try
{
List<Employee> oemps = new List<Employee>();
List<User> oUsers = new List<User>();
List<UserRole> PreviousRoles = new List<UserRole>();
if (oUserRoles[0].UserID == 0)
{
string empIn = "";
oUserRoles.ForEach(x => { empIn = empIn + x.employeeID + ", "; });
empIn = empIn.Substring(0, empIn.Length - 2);
EmployeeService empService = new EmployeeService();
tc = TransactionContext.Begin();
DataReader dr = new DataReader(UserRoleDA.getAllByUserEMpNo(tc, empIn));
PreviousRoles = this.CreateObjects<UserRole>(dr);
dr.Close();
tc.End();
foreach(UserRole prvUser in PreviousRoles)
{
oUserRoles.ForEach(u => {
if(u.employeeID == prvUser.employeeID)
u.UserID = prvUser.UserID;
});
//oUserRoles.ForEach(u => {
// if (u.UserID == prvUser.UserID && u.RoleID == prvUser.RoleID)
// u.ID = prvUser.ID;
//});
}
empIn = "";
oUserRoles.ForEach(x =>
{
if (x.UserID == 0)
empIn = empIn + x.employeeID + ", ";
});
if (empIn.Length > 0)
{
empIn = empIn.Substring(0, empIn.Length - 2);
oemps = empService.GetByEmpIDs(empIn, (int)oUserRoles[0].payrollTypeID);
foreach (Employee emp in oemps)
{
User userObject = new User();
userObject.LoginID = emp.EmployeeNo;
userObject.UserName = emp.Name;
userObject.Password = MakePassWord(emp.BirthDate);
userObject.ParentID = parentUserID;
userObject.PasswordHints = MakePassWord(emp.BirthDate);
userObject.EmployeeID = emp.ID;
userObject.UserStatus = EnumAuthStatus.Approved;
userObject.ChangePasswordAtNextLogon = true;
userObject.SISU = false;
userObject.CreatedDate = DateTime.Now;
userObject.UserType = EnumUserType.Employee;
oUsers.Add(userObject);
}
}
}
tc = TransactionContext.Begin(true);
foreach (User ouser in oUsers)
{
UserService oUService = new UserService();
int ID = oUService.Save(tc, ouser);
oUserRoles.ForEach(x => {
if(x.loginIDView == ouser.LoginID)
x.UserID = ID;
});
}
var usersids = oUserRoles.GroupBy(x => x.UserID).ToList();
usersids.ForEach(x =>{
this.DeleteByUserID(tc, x.Key);
});
int id = tc.GenerateID("UserRole", "UserRoleID");
foreach (UserRole userRole in oUserRoles)
{
this.Save(tc, userRole, id);
id = id + 1;
}
tc.End();
// net
EmailSettings st = new EmailSettings();
MailSender sender = null;
var builder = new ConfigurationBuilder()
.SetBasePath(Directory.GetCurrentDirectory())
.AddJsonFile("appsettings.json");
IConfiguration Configuration = builder.Build();
Configuration.GetSection("EmailSettings").Bind(st);
sender = new MailSender();
foreach (Employee emp in oemps)
{
if (emp.EmailAddress != "")
{
EmailSettings emailSettings = st;
string password = MakePassWord(emp.BirthDate);
MailSender mailSender = new MailSender();
mailSender.AddTo(emp.EmailAddress);
mailSender.Subject = "Your HR software Login information";
mailSender.Body = @"Your HR software Login information are following.
<p> <b>Log-in ID : </b> " + emp.EmployeeNo + "</p>";
mailSender.Body += "<p> <b>Password : </b> " + password.Trim() + "</p>";
mailSender.ThreadSendMail(emailSettings);
}
}
}
catch (Exception e)
{
#region Handle Exception
if (tc != null)
tc.HandleError();
ExceptionLog.Write(e);
throw new ServiceException(e.Message, e);
#endregion
}
}
//public void Update(List<UserRole> oUserRoles)
//{
// TransactionContext tc = null;
// try
// {
// tc = TransactionContext.Begin(true);
// //this.DeleteByUserID(tc,oUserRoles[0].UserID);
// foreach (UserRole userRole in oUserRoles)
// {
// this.Save3(tc, userRole);
// }
// tc.End();
// }
// catch (Exception e)
// {
// #region Handle Exception
// if (tc != null)
// tc.HandleError();
// ExceptionLog.Write(e);
// throw new ServiceException(e.Message, e);
// #endregion
// }
//}
internal int Save(TransactionContext tc, UserRole oUserRole, int userRoleID)
{
try
{
if (oUserRole.IsNew)
{
oUserRole.ID = userRoleID;
UserRoleDA.Insert(tc, oUserRole);
}
else
{
UserRoleDA.Update(tc, oUserRole);
}
return oUserRole.ID;
}
catch (Exception e)
{
#region Handle Exception
ExceptionLog.Write(e);
throw new ServiceException(e.Message, e);
#endregion
}
}
//internal int Save2(TransactionContext tc, UserRole oUserRole)
//{
// try
// {
// //if (oUserRole.IsNew)
// //{
// int id = tc.GenerateID("UserRole", "UserRoleID");
// base.SetObjectID(oUserRole, id);
// UserRoleDA.Insert(tc, oUserRole);
// //}
// //else
// //{
// // UserRoleDA.Update(tc, oUserRole);
// //}
// return oUserRole.ID;
// }
// catch (Exception e)
// {
// #region Handle Exception
// ExceptionLog.Write(e);
// throw new ServiceException(e.Message, e);
// #endregion
// }
//}
//internal int Save3(TransactionContext tc, UserRole oUserRole)
//{
// try
// {
// //if (oUserRole.IsNew)
// //{
// //int id = tc.GenerateID("UserRole", "UserRoleID");
// //base.SetObjectID(oUserRole, ID.FromInteger(id));
// //UserRoleDA.Insert(tc, oUserRole);
// //}
// //else
// //{
// UserRoleDA.Update(tc, oUserRole);
// //}
// return oUserRole.ID;
// }
// catch (Exception e)
// {
// #region Handle Exception
// ExceptionLog.Write(e);
// throw new ServiceException(e.Message, e);
// #endregion
// }
//}
public void Delete(int id)
{
TransactionContext tc = null;
try
{
tc = TransactionContext.Begin(true);
UserRoleDA.Delete(tc, id);
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(TransactionContext tc, int id)
{
try
{
UserRoleDA.Delete(tc, id);
}
catch (Exception e)
{
#region Handle Exception
if (tc != null)
tc.HandleError();
ExceptionLog.Write(e);
throw new ServiceException(e.Message, e);
#endregion
}
}
public void DeleteByUserID(int id)
{
TransactionContext tc = null;
try
{
tc = TransactionContext.Begin(true);
UserRoleDA.DeleteByUserID(tc, id);
tc.End();
}
catch (Exception e)
{
#region Handle Exception
if (tc != null)
tc.HandleError();
ExceptionLog.Write(e);
throw new ServiceException(e.Message, e);
#endregion
}
}
public DataSet GetRoleAudit(int type, DateTime fromDate, DateTime ToDate)
{
DataSet role = null;
TransactionContext tc = null;
try
{
tc = TransactionContext.Begin();
role = UserRoleDA.GetRoleAudit(tc, type, fromDate, ToDate);
tc.End();
}
catch (Exception e)
{
#region Handle Exception
if (tc != null)
tc.HandleError();
ExceptionLog.Write(e);
throw new ServiceException(e.Message, e);
#endregion
}
return role;
}
public DataTable GetUserMenuByRole(string roleId)
{
DataTable dtMenu = new DataTable();
TransactionContext tc = null;
try
{
tc = TransactionContext.Begin();
DataSet ds = UserRoleDA.GetUserMenuByRole(tc, roleId);
if (ds != null && ds.Tables != null && ds.Tables.Count > 0)
dtMenu = ds.Tables[0];
tc.End();
}
catch (Exception e)
{
#region Handle Exception
if (tc != null)
tc.HandleError();
ExceptionLog.Write(e);
throw new ServiceException(e.Message, e);
#endregion
}
return dtMenu;
}
public void DeleteByUserID(TransactionContext tc, int id)
{
try
{
UserRoleDA.DeleteByUserID(tc, id);
}
catch (Exception e)
{
#region Handle Exception
if (tc != null)
tc.HandleError();
ExceptionLog.Write(e);
throw new ServiceException(e.Message, e);
#endregion
}
}
public void Update(int id, EnumStatus status)
{
TransactionContext tc = null;
try
{
tc = TransactionContext.Begin(true);
UserRoleDA.Update(tc, id, status);
tc.End();
}
catch (Exception e)
{
#region Handle Exception
if (tc != null)
tc.HandleError();
ExceptionLog.Write(e);
throw new ServiceException(e.Message, e);
#endregion
}
}
}
}