EchoTex_Payroll/HRM.UI/Controllers/BasicBuilderController.cs
2024-10-14 10:01:49 +06:00

511 lines
19 KiB
C#

using HRM.BO;
using Microsoft.AspNetCore.Authorization;
using Microsoft.AspNetCore.Http;
using Microsoft.AspNetCore.Mvc;
using Microsoft.Extensions.Logging;
using System;
using System.Collections;
using System.Collections.Generic;
using System.Data;
using System.Linq;
using System.Reflection;
using System.Security.AccessControl;
namespace HRM.UI.Controllers
{
[ApiController]
[Route("api/BasicBuilder")]
[Authorize]
public class BasicBuilderController : ControllerBase
{
private readonly ILogger<BasicBuilderController> _logger;
private string ServiceAssemblyName = "HRM.DA";
private string BOAssemblyName = "HRM.BO";
[HttpPost]
[Route("SaveItem")]
public ActionResult<BasicEntryForm> SaveItem(BasicEntryForm cty)
{
Dictionary<int, string> oreponse = new Dictionary<int, string>();
CurrentUser ouser = CurrentUser.GetCurrentUser(HttpContext.User);
try
{
var obj = Activator.CreateInstance(this.GetTypeofObject(cty.SourceObject, false)); //return Ok();
foreach (var ptr in obj.GetType()
.GetProperties(
BindingFlags.Public
| BindingFlags.Instance))
{
if (ptr.Name.ToLower() == cty.IDColumnName.ToLower())
{
if (cty.IDValue > 0 && cty.IsNew ==false)
ptr.SetValue(obj, cty.IDValue, null);
}
if (cty.fromViewType == enumfromViewType.TreeView)
{
if (ptr.Name.ToLower() == cty.parentColumnName.ToLower())
{
if (cty.parentID != null && cty.parentID > 0)
ptr.SetValue(obj, (int) cty.parentID, null);
}
}
if (ptr.Name.ToLower() == "payrolltypeid")
{
ptr.SetValue(obj, (int) ouser.PayrollTypeID, null);
}
//if (ptr.Name.ToLower() == "parentid")
//{
// if(cty.parentID!=-1)
// ptr.SetValue(obj, (int) cty.parentID, null);
//}
if (ptr.Name.ToLower() == "tier")
{
if (cty.IsNew == true) cty.treeTier = cty.treeTier;
ptr.SetValue(obj, cty.treeTier, null);
}
if (ptr.Name.ToLower() == "sequence")
{
if (cty.IsNew == false)
{
ptr.SetValue(obj, cty.Sequence, null);
}
}
if (cty.IsNew)
{
if (ptr.Name.ToLower() == "createdby")
ptr.SetValue(obj, ouser.UserID, null);
if (ptr.Name.ToLower() == "createddate")
ptr.SetValue(obj, DateTime.Today, null);
}
else
{
if (ptr.Name.ToLower() == "modifiedby")
ptr.SetValue(obj, ouser.UserID, null);
if (ptr.Name.ToLower() == "modifieddate")
ptr.SetValue(obj, DateTime.Today, null);
}
foreach (var item in cty.Controls)
{
if (ptr.Name.ToLower() == item.PropertyName.ToLower() && ptr.CanWrite)
{
if (item.controlType == EnumDynamicControlType.TextBox && item.value != "")
ptr.SetValue(obj, item.value, null);
else if (item.controlType == EnumDynamicControlType.DateTime && item.value != "")
ptr.SetValue(obj, Convert.ToDateTime(item.value), null);
else if ((item.controlType == EnumDynamicControlType.DropDownEnumData
|| item.controlType == EnumDynamicControlType.DropDownServerData) &&
item.value != "")
ptr.SetValue(obj, Convert.ToInt32(item.value), null);
else if (item.controlType == EnumDynamicControlType.CheckBox && item.value != "")
{
if (item.value == "1") ptr.SetValue(obj, true, null);
else ptr.SetValue(obj, false, null);
}
else if (item.controlType == EnumDynamicControlType.NumText && item.value != "")
{
ptr.SetValue(obj, Convert.ToDouble(item.value), null);
}
else if (item.controlType == EnumDynamicControlType.IntNumText && item.value != "")
{
ptr.SetValue(obj, Convert.ToInt32(item.value), null);
}
}
}
}
#region Old Coce
// if (cty.IsNew == false)
//{
// PropertyInfo propIDvalue = obj.GetType().GetProperty(cty.IDColumnName, BindingFlags.Public | BindingFlags.Instance);
// propIDvalue.SetValue(obj, cty.IDValue, null);
// PropertyInfo propIsNew = obj.GetType().GetProperty("IsNew", BindingFlags.Public | BindingFlags.Instance);
// if(propIsNew!=null)
// propIsNew.SetValue(obj, false, null);
//}
//if(cty.fromViewType == enumfromViewType.TreeView && cty.IDValue!=null)
//{
// PropertyInfo propIDvalue = obj.GetType().GetProperty(cty.parentColumnName, BindingFlags.Public | BindingFlags.Instance);
// propIDvalue.SetValue(obj, cty.IDValue, null);
// PropertyInfo idpropIDvalue = obj.GetType().GetProperty(cty.IDColumnName, BindingFlags.Public | BindingFlags.Instance);
// idpropIDvalue.SetValue(obj, cty.IDValue +10, null);
//}
//foreach (var pty in cty.Controls)
//{
// PropertyInfo prop = obj.GetType().GetProperty(pty.PropertyName, BindingFlags.Public | BindingFlags.Instance);
// if (null != prop && prop.CanWrite)
// {
// if (pty.controlType == EnumDynamicControlType.TextBox)
// prop.SetValue(obj, pty.value, null);
// else if (pty.controlType == EnumDynamicControlType.DateTime)
// prop.SetValue(obj, Convert.ToDateTime(pty.value), null);
// else if (pty.controlType == EnumDynamicControlType.DropDownEnumData || pty.controlType == EnumDynamicControlType.DropDownServerData)
// prop.SetValue(obj, Convert.ToInt32(pty.value), null);
// else if (pty.controlType == EnumDynamicControlType.CheckBox)
// {
// if (pty.value == "1") prop.SetValue(obj, true, null);
// else prop.SetValue(obj, false, null);
// }
// }
//}
#endregion
Type tservice = this.GetTypeofObject(cty.SourceObject, true);
var serviceObj = Activator.CreateInstance(tservice);
object[] param = new object[1];
param[0] = obj;
Type[] paramtypes = new Type[1];
paramtypes[0] = obj.GetType();
MethodInfo voidMethodInfo = tservice.GetMethod("Save", paramtypes);
var pkid= voidMethodInfo.Invoke(serviceObj, param);
if (pkid != null && cty.IsNew == true)
{
foreach (var ptr in obj.GetType()
.GetProperties(
BindingFlags.Public
| BindingFlags.Instance))
{
if (ptr.Name.ToLower() == cty.IDColumnName.ToLower())
{
ptr.SetValue(obj, Convert.ToInt32( pkid), null);
break;
}
}
}
return Ok(obj);
}
catch (Exception ex)
{
return StatusCode(500, ex.InnerException != null ? ex.InnerException.Message : ex.Message);
// oreponse.Add(500, ex.InnerException != null ? ex.InnerException.Message : ex.Message);
}
// return Ok(oreponse);
}
[HttpGet]
[Route("updateStatus/{id}/{sourceObj}/{status}")]
public ActionResult UpdateStatus(int id, string sourceObj, string status)
{
CurrentUser ouser = CurrentUser.GetCurrentUser(HttpContext.User);
int userId = ouser.UserID;
try
{
Type t = this.GetTypeofObject(sourceObj, true);
var obj = Activator.CreateInstance(this.GetTypeofObject(sourceObj, false));
var getObj = Activator.CreateInstance(t);
string fName = "Get";
object[] paramGet;
Type[] paramtypesGet;
paramGet = new object[1];
paramGet[0] = id;
paramtypesGet = new Type[1];
paramtypesGet[0] = typeof(int);
MethodInfo voidMethodInfoGet = t.GetMethod(fName, paramtypesGet);
object ost = voidMethodInfoGet.Invoke(getObj, paramGet);
if (status == "Active")
{
PropertyInfo propertyInfo = obj.GetType().GetProperty("Status");
propertyInfo.SetValue(ost, EnumStatus.Active, null);
}
else
{
PropertyInfo propertyInfo = obj.GetType().GetProperty("Status");
propertyInfo.SetValue(ost, EnumStatus.Inactive, null);
}
PropertyInfo propertyInfoModifiedBy = obj.GetType().GetProperty("ModifiedBy");
propertyInfoModifiedBy.SetValue(ost, userId, null);
Type tservice = this.GetTypeofObject(sourceObj, true);
var serviceObj = Activator.CreateInstance(tservice);
object[] param = new object[1];
param[0] = ost;
Type[] paramtypes = new Type[1];
paramtypes[0] = ost.GetType();
MethodInfo voidMethodInfo = tservice.GetMethod("Save", paramtypes);
var pkid = voidMethodInfo.Invoke(serviceObj, param);
return Ok(obj);
}
catch (Exception ex)
{
return StatusCode(500, ex.InnerException != null ? ex.InnerException.Message : ex.Message);
// oreponse.Add(500, ex.InnerException != null ? ex.InnerException.Message : ex.Message);
}
// return Ok(oreponse);
}
[HttpGet("GetMasterViewData/{ObjectName}/{FunctionName}/{status}/{payrolltypeSearch}")]
public ActionResult<ICollection> GetMasterViewData(string ObjectName,
string FunctionName, EnumStatus status, bool payrolltypeSearch)
{
CurrentUser ouser = CurrentUser.GetCurrentUser(HttpContext.User);
Type t = this.GetTypeofObject(ObjectName, true);
var obj = Activator.CreateInstance(t); //return Ok();
string fName = "Get";
if (FunctionName != null && FunctionName != "undefined")
{
fName = FunctionName;
}
object[] param;
Type[] paramtypes;
if (payrolltypeSearch == true)
{
param = new object[2];
param[0] = status;
param[1] = (int) ouser.PayrollTypeID;
paramtypes = new Type[2];
paramtypes[0] = typeof(EnumStatus);
paramtypes[1] = typeof(int);
}
else
{
param = new object[1];
param[0] = status;
paramtypes = new Type[1];
paramtypes[0] = typeof(EnumStatus);
}
MethodInfo voidMethodInfo = t.GetMethod(fName, paramtypes);
object ost = voidMethodInfo.Invoke(obj, param);
if (ost is DataTable)
{
IEnumerable<DataRow> sequence = ((DataTable) ost).AsEnumerable().ToList();
dynamic collection = sequence as IEnumerable;
return collection;
//List<DataRow> list = ((DataTable)ost).AsEnumerable().ToList();
//return list;
}
else
{
dynamic collection = ost as IEnumerable;
return collection;
}
}
private Type GetTypeofObject(string objectName, bool IsService)
{
string AssName = IsService == false
? this.BOAssemblyName + "." + objectName
: this.ServiceAssemblyName + "." + objectName + "Service";
Assembly assem = Assembly.Load(IsService == false ? this.BOAssemblyName : this.ServiceAssemblyName);
Type t = assem.GetType(AssName);
return t;
}
[HttpGet("Get/{ObjectName}/{FunctionName}/{IDValue}")]
public ActionResult<object> Get(string ObjectName, string FunctionName, int IDValue)
{
Type t = this.GetTypeofObject(ObjectName, true);
var obj = Activator.CreateInstance(t); //return Ok();
string fName = "Get";
if (FunctionName == null || FunctionName == "undefined")
{
fName = FunctionName;
}
object[] param = new object[1];
param[0] = IDValue;
Type[] paramtypes = new Type[1];
paramtypes[0] = ((int) param[0]).GetType();
MethodInfo voidMethodInfo = t.GetMethod(fName, paramtypes);
object ost = voidMethodInfo.Invoke(obj, param);
return ost;
}
[HttpGet("DoStatusChange/{tableName}/{IDValue}/{currentStatus}")]
public ActionResult<object> DoStatusChange(string tableName, int IDValue, bool currentStatus)
{
// updated user and DAteTime are needed
//Type t = this.GetTypeofObject(ObjectName, true);
//var obj = Activator.CreateInstance(t); //return Ok();
//string fName = "Get";
//if (FunctionName == null || FunctionName == "undefined")
//{
// fName = FunctionName;
//}
//object[] param = new object[1];
//param[0] = IDValue;
//Type[] paramtypes = new Type[1];
//paramtypes[0] = ((int)param[0]).GetType();
//MethodInfo voidMethodInfo = t.GetMethod(fName, paramtypes);
//object ost = voidMethodInfo.Invoke(obj, param);
return "";
}
[HttpGet("GetNextCode/{ObjectName}/{tier}/{parentID}")]
public ActionResult<object> GetNextCode(string ObjectName, int tier, int parentID)
{
Type t = this.GetTypeofObject(ObjectName, true);
var targetInstance = Activator.CreateInstance(t); //return Ok();
object[] param;
Type[] paramtypes;
if (tier != -1)
{
param = new object[2];
param[0] = tier;
param[1] = parentID;
paramtypes = new Type[2];
paramtypes[0] = ((int) param[0]).GetType();
paramtypes[1] = ((int) param[1]).GetType();
}
else
{
paramtypes = new Type[0];
param = null;
}
MethodInfo voidMethodInfo = t.GetMethod("GetNextCode", paramtypes);
object ost = voidMethodInfo.Invoke(targetInstance, param);
return Ok(ost);
}
[HttpGet("GetCurrentEmployeeInfo/{ObjectName}")]
public ActionResult<object> GetCurrentEmployeeInfo(string ObjectName)
{
Type t = this.GetTypeofObject(ObjectName, true);
var targetInstance = Activator.CreateInstance(t); //return Ok();
object[] param;
CurrentUser currentUser = CurrentUser.GetCurrentUser(HttpContext.User);
Type[] paramtypes;
param = new object[1];
param[0] = 21;
paramtypes = new Type[1];
paramtypes[0] = ((int)param[0]).GetType();
MethodInfo voidMethodInfo = t.GetMethod("Get", paramtypes);
object ost = voidMethodInfo.Invoke(targetInstance, param);
return Ok(ost);
}
[HttpPost("Delete")]
public ActionResult Delete(BasicEntryForm cty)
{
try
{
Type t = this.GetTypeofObject(cty.SourceObject, true);
var obj = Activator.CreateInstance(t); //return Ok();
string fName = "Delete";
object[] param = new object[1];
param[0] = cty.IDValue;
Type[] paramtypes = new Type[1];
paramtypes[0] = ((int)param[0]).GetType();
MethodInfo voidMethodInfo = t.GetMethod(fName, paramtypes);
object ost = voidMethodInfo.Invoke(obj, param);
return Ok(true);
}
catch (Exception e)
{
return StatusCode(StatusCodes.Status500InternalServerError, e.InnerException.Message);
}
}
}
public enum EnumDynamicControlType
{
Lebel,
TextBox,
DropDownEnumData,
DropDownServerData,
CheckBox,
DateTime,
NumText,
IntNumText
}
public enum enumfromViewType
{
ListView,
TreeView,
}
public class BasicEntryForm
{
public enumfromViewType fromViewType { get; set; }
public string Caption { get; set; }
public string SourceObject { get; set; }
public BasicEntryFormControl[] Controls { get; set; }
public int IDValue { get; set; }
public bool IsNew { get; set; }
public string IDColumnName { get; set; }
public int? parentID { get; set; }
public string parentColumnName { get; set; }
public string MasterViewGet { get; set; }
public string SingleObjectGet { get; set; }
public int treeTier { get; set; }
public int Sequence { get; set; }
//public string dataGetFunctionName { get; set; }
//public EnumStatus searchStatus { get; set; }
//public bool searchPayrolltype { get; set; }
public BasicEntryForm()
{
this.fromViewType = enumfromViewType.ListView;
}
}
public class BasicEntryFormControl
{
public string value { get; set; }
public string PropertyName { get; set; }
public string label { get; set; }
public string controlID { get; set; }
public EnumDynamicControlType controlType { get; set; }
}
}