using System;
using System.Collections.Generic;
using System.Linq;
using System.Reflection;
using System.Text;
using QWPlatform.Models;
namespace PMS.BusinessModels
{
///
/// 创 建 人:王海洋
/// 创建日期:2018-12-10
/// 功能描述:业务对象与数据实体对象进行转换
///
public class UIModel
{
///
/// 将当前对象转为数据实体对象
///
/// 指定的数据实体对象
/// 返回数据实体
public T ToDBModel() where T : DataModelBase, new()
{
var type = typeof(T);
//获取到当前类的字段映射数据列及值
var dict = this.GetUIModelValues();
var model = new T();
//获取所有属性
var props = type.GetProperties();
foreach (var p in props)
{
//找出定义了字段的属性
var t = typeof(FieldBaseAttribute);
var objs = p.GetCustomAttributes(t, false);
if (objs.Length > 0)
{
//获取一个对象
var obj = objs.First() as FieldBaseAttribute;
if (obj != null)
{//返回该字段对象
var key = obj.ColumnName.Trim();
if (dict.ContainsKey(key))
{
var val = dict[key];
if (val != null)
{
var csVal = QWPlatform.SystemLibrary.DBTypeConvert.DbTypeToCSType(val);
//获取原型预定义的type
if (p.PropertyType.IsGenericType)
{
//获取泛型基础类型
var baseTypes = p.PropertyType.GetGenericArguments();
//获取原型定义类型
var ptype = p.PropertyType.GetGenericTypeDefinition();
if (ptype == typeof(Nullable<>))
{
if (baseTypes.Length > 0)
{//获取默认第一个类型
var baseType = baseTypes[0];
var cv = Convert.ChangeType(csVal, baseType);
p.SetValue(model, cv, null);
}
}
}
else
{
var vtype = csVal.GetType();
//将类型进行转换
var rval = Convert.ChangeType(csVal, vtype);
p.SetValue(model, rval, null);
}
}
else
{
p.SetValue(model, DefaultForType(p.PropertyType), null);
}
}
}
}
}
return model;
}
///
/// 将数据库的Model转换到UI的model中
///
/// 数据库Model的类型
/// 数据库Model的实例
public void FromDBModel(T t) where T : DataModelBase, new()
{
//获取数据字典
var dict = this.GetDBModelValues(t);
//获取UI的属性列表
var uiprops = this.GetType().GetProperties();
foreach (var p in uiprops)
{//循环所有UI的属性。进行设置值
var attrs = p.GetCustomAttributes(typeof(DbColumn), true);
if (attrs.Length > 0)
{
var dbcolAttr = attrs.First() as DbColumn;
var key = dbcolAttr.ColName;
if (dict.ContainsKey(key))
{//字典中包括该值
var val = dict[key];
//转换后的值
var csVal = QWPlatform.SystemLibrary.DBTypeConvert.DbTypeToCSType(val);
//设置到当前对象中
p.SetValue(this, csVal, null);
}
}
}
}
//获取当前模型的值
private Dictionary GetUIModelValues()
{
var type = this.GetType();
//获取所有属性
var props = type.GetProperties();
var dict = new Dictionary();
foreach (var p in props)
{
//找出定义了字段的属性
var t = typeof(DbColumn);
var objs = p.GetCustomAttributes(t, false);
if (objs.Length > 0)
{
//获取一个对象
var obj = objs.First() as DbColumn;
if (obj != null)
{//返回该字段对象
if (!dict.ContainsKey(obj.ColName))
{
var val = this.GetModelValue(this, p.Name);
dict.Add(obj.ColName, val);
}
}
}
}
return dict;
}
//获取数据库返回的model对象值
private Dictionary GetDBModelValues(T t) where T : DataModelBase
{
var type = t.GetType();
//获取所有属性
var props = type.GetProperties();
var dict = new Dictionary();
foreach (var p in props)
{
//找出定义了字段的属性
var fieldAttr = typeof(FieldBaseAttribute);
var objs = p.GetCustomAttributes(fieldAttr, false);
if (objs.Length > 0)
{
//获取一个对象
var obj = objs.First() as FieldBaseAttribute;
if (obj != null)
{//返回该字段对象
var key = obj.ColumnName;
if (!dict.ContainsKey(key))
{//字段名:值
var val = this.GetModelValue(t, p.Name);
dict.Add(key, val);
}
}
}
}
return dict;
}
/*根据模型取出对象值*/
private object GetModelValue(object obj, string propName)
{
var type = obj.GetType();
PropertyInfo propertyInfo = type.GetProperty(propName, BindingFlags.Public | BindingFlags.Instance | BindingFlags.IgnoreCase);
if (propertyInfo != null)
{
return propertyInfo.GetValue(obj, null);
}
return null;
}
/*获取当前类型的默认值*/
private static object DefaultForType(Type targetType)
{
return targetType.IsValueType ? Activator.CreateInstance(targetType) : null;
}
///
/// 将当前对象转换为json数据
///
///
public string ToJson()
{
return QWPlatform.SystemLibrary.Utils.Strings.ObjectToJson(this);
}
}
}