You cannot select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

154 lines
4.4 KiB
C#

using Newtonsoft.Json;
using Newtonsoft.Json.Linq;
using System;
using System.Collections.Generic;
using System.ComponentModel.DataAnnotations.Schema;
using System.Linq;
using System.Reflection;
using System.Text;
using System.Threading.Tasks;
namespace ZKLT.Hadoop.Model
{
/// <summary>
/// 表
/// </summary>
[AttributeUsage(AttributeTargets.Class, Inherited = true)]
[HDP_Table(Key = "HDP_Table", Description = "表")]
public class HDP_Table : Attribute
{
private string? _Id;
private string? _SourceId;
private string? _Key;
private string? _Description;
private HDP_Column[]? _Columns;
/// <summary>
/// 编号
/// </summary>
[HDP_Column(Key = "Id", Description = "编号", Length = 100, DataType = HDP_ColumnDataType.VARCHAR, IsPrimary = true)]
public string? Id { get => _Id; set => _Id = value; }
/// <summary>
/// 源编号
/// </summary>
[HDP_Column(Key = "SourceId", Description = "源编号", Length = 100, DataType = HDP_ColumnDataType.VARCHAR)]
public string? SourceId { get => _SourceId; set => _SourceId = value; }
/// <summary>
/// 键
/// </summary>
[HDP_Column(Key = "Key", Description = "键", Length = 100, DataType = HDP_ColumnDataType.VARCHAR)]
public string? Key { get => _Key; set => _Key = value; }
/// <summary>
/// 描述
/// </summary>
[HDP_Column(Key = "Description", Description = "描述", Length = 200, DataType = HDP_ColumnDataType.VARCHAR)]
public string? Description { get => _Description; set => _Description = value; }
/// <summary>
/// 列
/// </summary>
public HDP_Column[]? Columns { get => _Columns; set => _Columns = value; }
/// <summary>
/// 类转换Table
/// </summary>
/// <typeparam name="T">类型</typeparam>
/// <returns></returns>
public static HDP_Table Class2Table<T>()
{
Type _type = typeof(T);
var _tableAttribute = _type.GetCustomAttribute<HDP_Table>();
if (_tableAttribute == null)
{
throw new ArgumentNullException("Table标记不存在");
}
if (string.IsNullOrEmpty(_tableAttribute.Key))
{
_tableAttribute.Key = _type.Name;
}
if (string.IsNullOrEmpty(_tableAttribute.Id))
{
_tableAttribute.Id = _tableAttribute.Key;
}
var _properties = _type.GetProperties();
var _columns = new List<HDP_Column>();
foreach (var _property in _properties)
{
var _column = _property.GetCustomAttribute<HDP_Column>();
if (_column == null)
{
continue;
}
if (string.IsNullOrEmpty(_column.TableId))
{
_column.TableId = _tableAttribute.Id;
}
if (string.IsNullOrEmpty(_column.Key))
{
_column.Key = _property.Name;
}
if (string.IsNullOrEmpty(_column.Id))
{
_column.Id = _column.Key;
}
_columns.Add(_column);
}
_tableAttribute.Columns = _columns.ToArray();
return _tableAttribute;
}
/// <summary>
/// 类
/// </summary>
/// <param name="data">数据</param>
/// <returns>字典</returns>
public static Dictionary<string, object> Class2Dictionary(object data)
{
var _result = new Dictionary<string, object>();
Type _type = data.GetType();
var _properties = _type.GetProperties();
foreach (var _property in _properties)
{
if (_property.GetValue(data) != null) {
_result.Add(_property.Name, _property.GetValue(data)!);
}
}
return _result;
}
/// <summary>
/// 类转JObject
/// </summary>
/// <param name="data">数据</param>
/// <returns></returns>
public static JObject Class2JObject(object data) {
return JsonConvert.DeserializeObject<JObject>(JsonConvert.SerializeObject(data)!)!;
}
}
}