UIModel.cs 7.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Reflection;
  5. using System.Text;
  6. using QWPlatform.Models;
  7. namespace PMS.BusinessModels
  8. {
  9. /// <summary>
  10. /// 创 建 人:王海洋
  11. /// 创建日期:2018-12-10
  12. /// 功能描述:业务对象与数据实体对象进行转换
  13. /// </summary>
  14. public class UIModel
  15. {
  16. /// <summary>
  17. /// 将当前对象转为数据实体对象
  18. /// </summary>
  19. /// <typeparam name="T">指定的数据实体对象</typeparam>
  20. /// <returns>返回数据实体</returns>
  21. public T ToDBModel<T>() where T : DataModelBase, new()
  22. {
  23. var type = typeof(T);
  24. //获取到当前类的字段映射数据列及值
  25. var dict = this.GetUIModelValues();
  26. var model = new T();
  27. //获取所有属性
  28. var props = type.GetProperties();
  29. foreach (var p in props)
  30. {
  31. //找出定义了字段的属性
  32. var t = typeof(FieldBaseAttribute);
  33. var objs = p.GetCustomAttributes(t, false);
  34. if (objs.Length > 0)
  35. {
  36. //获取一个对象
  37. var obj = objs.First() as FieldBaseAttribute;
  38. if (obj != null)
  39. {//返回该字段对象
  40. var key = obj.ColumnName.Trim();
  41. if (dict.ContainsKey(key))
  42. {
  43. var val = dict[key];
  44. if (val != null)
  45. {
  46. var csVal = QWPlatform.SystemLibrary.DBTypeConvert.DbTypeToCSType(val);
  47. //获取原型预定义的type
  48. if (p.PropertyType.IsGenericType)
  49. {
  50. //获取泛型基础类型
  51. var baseTypes = p.PropertyType.GetGenericArguments();
  52. //获取原型定义类型
  53. var ptype = p.PropertyType.GetGenericTypeDefinition();
  54. if (ptype == typeof(Nullable<>))
  55. {
  56. if (baseTypes.Length > 0)
  57. {//获取默认第一个类型
  58. var baseType = baseTypes[0];
  59. var cv = Convert.ChangeType(csVal, baseType);
  60. p.SetValue(model, cv, null);
  61. }
  62. }
  63. }
  64. else
  65. {
  66. var vtype = csVal.GetType();
  67. //将类型进行转换
  68. var rval = Convert.ChangeType(csVal, vtype);
  69. p.SetValue(model, rval, null);
  70. }
  71. }
  72. else
  73. {
  74. p.SetValue(model, DefaultForType(p.PropertyType), null);
  75. }
  76. }
  77. }
  78. }
  79. }
  80. return model;
  81. }
  82. /// <summary>
  83. /// 将数据库的Model转换到UI的model中
  84. /// </summary>
  85. /// <typeparam name="T">数据库Model的类型</typeparam>
  86. /// <param name="t">数据库Model的实例</param>
  87. public void FromDBModel<T>(T t) where T : DataModelBase, new()
  88. {
  89. //获取数据字典
  90. var dict = this.GetDBModelValues<T>(t);
  91. //获取UI的属性列表
  92. var uiprops = this.GetType().GetProperties();
  93. foreach (var p in uiprops)
  94. {//循环所有UI的属性。进行设置值
  95. var attrs = p.GetCustomAttributes(typeof(DbColumn), true);
  96. if (attrs.Length > 0)
  97. {
  98. var dbcolAttr = attrs.First() as DbColumn;
  99. var key = dbcolAttr.ColName;
  100. if (dict.ContainsKey(key))
  101. {//字典中包括该值
  102. var val = dict[key];
  103. //转换后的值
  104. var csVal = QWPlatform.SystemLibrary.DBTypeConvert.DbTypeToCSType(val);
  105. //设置到当前对象中
  106. p.SetValue(this, csVal, null);
  107. }
  108. }
  109. }
  110. }
  111. //获取当前模型的值
  112. private Dictionary<string, object> GetUIModelValues()
  113. {
  114. var type = this.GetType();
  115. //获取所有属性
  116. var props = type.GetProperties();
  117. var dict = new Dictionary<string, object>();
  118. foreach (var p in props)
  119. {
  120. //找出定义了字段的属性
  121. var t = typeof(DbColumn);
  122. var objs = p.GetCustomAttributes(t, false);
  123. if (objs.Length > 0)
  124. {
  125. //获取一个对象
  126. var obj = objs.First() as DbColumn;
  127. if (obj != null)
  128. {//返回该字段对象
  129. if (!dict.ContainsKey(obj.ColName))
  130. {
  131. var val = this.GetModelValue(this, p.Name);
  132. dict.Add(obj.ColName, val);
  133. }
  134. }
  135. }
  136. }
  137. return dict;
  138. }
  139. //获取数据库返回的model对象值
  140. private Dictionary<string, object> GetDBModelValues<T>(T t) where T : DataModelBase
  141. {
  142. var type = t.GetType();
  143. //获取所有属性
  144. var props = type.GetProperties();
  145. var dict = new Dictionary<string, object>();
  146. foreach (var p in props)
  147. {
  148. //找出定义了字段的属性
  149. var fieldAttr = typeof(FieldBaseAttribute);
  150. var objs = p.GetCustomAttributes(fieldAttr, false);
  151. if (objs.Length > 0)
  152. {
  153. //获取一个对象
  154. var obj = objs.First() as FieldBaseAttribute;
  155. if (obj != null)
  156. {//返回该字段对象
  157. var key = obj.ColumnName;
  158. if (!dict.ContainsKey(key))
  159. {//字段名:值
  160. var val = this.GetModelValue(t, p.Name);
  161. dict.Add(key, val);
  162. }
  163. }
  164. }
  165. }
  166. return dict;
  167. }
  168. /*根据模型取出对象值*/
  169. private object GetModelValue(object obj, string propName)
  170. {
  171. var type = obj.GetType();
  172. PropertyInfo propertyInfo = type.GetProperty(propName, BindingFlags.Public | BindingFlags.Instance | BindingFlags.IgnoreCase);
  173. if (propertyInfo != null)
  174. {
  175. return propertyInfo.GetValue(obj, null);
  176. }
  177. return null;
  178. }
  179. /*获取当前类型的默认值*/
  180. private static object DefaultForType(Type targetType)
  181. {
  182. return targetType.IsValueType ? Activator.CreateInstance(targetType) : null;
  183. }
  184. /// <summary>
  185. /// 将当前对象转换为json数据
  186. /// </summary>
  187. /// <returns></returns>
  188. public string ToJson()
  189. {
  190. return QWPlatform.SystemLibrary.Utils.Strings.ObjectToJson(this);
  191. }
  192. }
  193. }