ปรับปรุงการทำ log

This commit is contained in:
LAPTOP-KB8JC2K2\acer
2021-05-30 12:41:44 +07:00
parent 0666c64024
commit 0dbfb54829
5 changed files with 127 additions and 10 deletions

View File

@@ -9,17 +9,97 @@ using Microsoft.Extensions.Configuration;
using System.Net;
using TodoAPI2.Models;
using Microsoft.AspNetCore.Http;
using System.ComponentModel.DataAnnotations;
using TTSW.Utils;
/// <summary>
/// Summary description for MyHelper
/// </summary>
public class MyHelper
public static class MyHelper
{
public MyHelper()
public static DataRow GetDataRow(object id, object model, IMyDatabase _db)
{
//
// TODO: Add constructor logic here
//
DataRow result = null;
if (id.GetType().Name.Contains("int"))
{
result = _db.ExecuteDataTableNpgsql($"select * from {model.GetType().Name.Replace("Entity", "")} where id={id.ToString()} ", null).Rows[0];
}
else
{
result = _db.ExecuteDataTableNpgsql($"select * from {model.GetType().Name.Replace("Entity", "")} where id='{id.ToString()}' ", null).Rows[0];
}
return result;
}
public static object GetObject(this DataRow dataRow, string data_type)
{
if (data_type == "eva_adjust_postponement") return ToObject<eva_adjust_postponementEntity>(dataRow);
if (data_type == "eva_adjust_postponement_detail") return ToObject<eva_adjust_postponement_detailEntity>(dataRow);
if (data_type == "eva_create_evaluation") return ToObject<eva_create_evaluationEntity>(dataRow);
if (data_type == "eva_create_evaluation_detail") return ToObject<eva_create_evaluation_detailEntity>(dataRow);
if (data_type == "eva_create_evaluation_detail_history") return ToObject<eva_create_evaluation_detail_historyEntity>(dataRow);
if (data_type == "eva_evaluation_achievement") return ToObject<eva_evaluation_achievementEntity>(dataRow);
if (data_type == "eva_evaluation_achievement_attach") return ToObject<eva_evaluation_achievement_attachEntity>(dataRow);
if (data_type == "eva_evaluation_behavior") return ToObject<eva_evaluation_behaviorEntity>(dataRow);
if (data_type == "eva_evaluation_group") return ToObject<eva_evaluation_groupEntity>(dataRow);
if (data_type == "eva_evaluation_group_detail") return ToObject<eva_evaluation_group_detailEntity>(dataRow);
if (data_type == "eva_evaluation_operating_agreement") return ToObject<eva_evaluation_operating_agreementEntity>(dataRow);
if (data_type == "eva_idp_plan") return ToObject<eva_idp_planEntity>(dataRow);
if (data_type == "eva_level_score") return ToObject<eva_level_scoreEntity>(dataRow);
if (data_type == "eva_limit_frame_employee") return ToObject<eva_limit_frame_employeeEntity>(dataRow);
if (data_type == "eva_limit_frame_group") return ToObject<eva_limit_frame_groupEntity>(dataRow);
if (data_type == "eva_limit_frame_plan") return ToObject<eva_limit_frame_planEntity>(dataRow);
if (data_type == "eva_performance_plan") return ToObject<eva_performance_planEntity>(dataRow);
if (data_type == "eva_performance_plan_detail") return ToObject<eva_performance_plan_detailEntity>(dataRow);
if (data_type == "eva_promoted_percentage") return ToObject<eva_promoted_percentageEntity>(dataRow);
if (data_type == "eva_salary_cylinder") return ToObject<eva_salary_cylinderEntity>(dataRow);
return null;
}
public static T ToObject<T>(this DataRow dataRow) where T : new()
{
T item = new T();
foreach (DataColumn column in dataRow.Table.Columns)
{
PropertyInfo property = GetProperty(typeof(T), column.ColumnName);
if (property != null && dataRow[column] != DBNull.Value && dataRow[column].ToString() != "NULL")
{
property.SetValue(item, ChangeType(dataRow[column], property.PropertyType), null);
}
}
return item;
}
private static PropertyInfo GetProperty(Type type, string attributeName)
{
PropertyInfo property = type.GetProperty(attributeName);
if (property != null)
{
return property;
}
return type.GetProperties()
.Where(p => p.IsDefined(typeof(DisplayAttribute), false) && p.GetCustomAttributes(typeof(DisplayAttribute), false).Cast<DisplayAttribute>().Single().Name == attributeName)
.FirstOrDefault();
}
public static object ChangeType(object value, Type type)
{
if (type.IsGenericType && type.GetGenericTypeDefinition().Equals(typeof(Nullable<>)))
{
if (value == null)
{
return null;
}
return Convert.ChangeType(value, Nullable.GetUnderlyingType(type));
}
return Convert.ChangeType(value, type);
}
public static string GetDummyText()