EntityHelper.cs 22 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Text;
  5. using CoreFS.CA06;
  6. using CoreFS.SA06;
  7. using System.Reflection;
  8. using System.Collections;
  9. using Infragistics.Win.UltraWinGrid;
  10. using System.ComponentModel;
  11. using Core.Mes.Client.Comm.Attribute;
  12. namespace Core.Mes.Client.Comm.Tool
  13. {
  14. /// <summary>
  15. /// 实体类帮助类
  16. /// </summary>
  17. public class EntityHelper
  18. {
  19. /// <summary>
  20. /// 获取实体类集合
  21. /// </summary>
  22. /// <typeparam name="T">实体类</typeparam>
  23. /// <param name="methodId">服务端方法ID</param>
  24. /// <param name="param">参数</param>
  25. /// <param name="ob">平台操作对象</param>
  26. /// <returns></returns>
  27. public static DataSourceList<T> GetData<T>(string methodId, object[] param, OpeBase ob)
  28. {
  29. try
  30. {
  31. DataSourceList<T> listResult = new DataSourceList<T>();
  32. CoreClientParam ccp = new CoreClientParam();
  33. ccp.ServerName = methodId.Substring(0, methodId.LastIndexOf("."));
  34. ccp.MethodName = methodId.Substring(methodId.LastIndexOf(".") + 1);
  35. ccp.ServerParams = param;
  36. ccp = ob.ExecuteQuery(ccp, CoreInvokeType.Internal);
  37. string[] strColumns = (ccp.ReturnObject as CoreReturnSortObject).getColIdx();
  38. string[] strColumnsEntity = ConvertColumnName(strColumns);
  39. ArrayList list = (ccp.ReturnObject as CoreReturnSortObject).getResult();
  40. Type type = typeof(T);
  41. Dictionary<string, PropertyInfo> propertyInfoDic = new Dictionary<string, PropertyInfo>();
  42. for (int j = 0; j < strColumns.Length; j++)
  43. {
  44. string column = strColumns[j];
  45. string columnEntity = strColumnsEntity[j];
  46. PropertyInfo propertyInfo = type.GetProperty(columnEntity);
  47. if (propertyInfo == null) continue;
  48. if (propertyInfoDic.ContainsKey(column))
  49. {
  50. propertyInfoDic[column] = propertyInfo;
  51. }
  52. else
  53. {
  54. propertyInfoDic.Add(column, propertyInfo);
  55. }
  56. }
  57. for (int i = 0; i < list.Count; i++)
  58. {
  59. Hashtable hashtable = (Hashtable)list[i];
  60. ArrayList list2 = new ArrayList();
  61. T obj = (T)type.Assembly.CreateInstance(type.FullName);
  62. foreach (KeyValuePair<string, PropertyInfo> propertyInfoKV in propertyInfoDic)
  63. {
  64. object obj2 = hashtable[propertyInfoKV.Key];
  65. PropertyInfo propertyInfo = propertyInfoKV.Value;
  66. if ((obj2 != null) && typeof(Hashtable).Equals(obj2.GetType()))
  67. {
  68. Hashtable hashtable2 = (Hashtable)obj2;
  69. propertyInfo.SetValue(obj, ConvertDataType(propertyInfo.PropertyType, hashtable2["value"]), null);
  70. }
  71. else
  72. {
  73. propertyInfo.SetValue(obj, ConvertDataType(propertyInfo.PropertyType, obj2), null);
  74. }
  75. }
  76. listResult.Add(obj);
  77. }
  78. return listResult;
  79. }
  80. catch (Exception ex)
  81. {
  82. throw ex;
  83. }
  84. finally
  85. {
  86. }
  87. }
  88. private static object ConvertDataType(Type type, object objValue)
  89. {
  90. if (type == typeof(bool))
  91. {
  92. bool result;
  93. if (bool.TryParse(objValue.ToString(), out result))
  94. {
  95. return result;
  96. }
  97. else
  98. {
  99. return false;
  100. }
  101. }
  102. else if(type == typeof(bool?))
  103. {
  104. bool result;
  105. if (bool.TryParse(objValue.ToString(), out result))
  106. {
  107. return result;
  108. }
  109. else
  110. {
  111. return null;
  112. }
  113. }
  114. else if (type == typeof(int?))
  115. {
  116. if (objValue == null)
  117. {
  118. return null;
  119. }
  120. else
  121. {
  122. return int.Parse(objValue.ToString());
  123. }
  124. }
  125. else if (type == typeof(long?))
  126. {
  127. if (objValue == null)
  128. {
  129. return null;
  130. }
  131. else
  132. {
  133. return long.Parse(objValue.ToString());
  134. }
  135. }
  136. else if (type == typeof(short?))
  137. {
  138. if (objValue == null)
  139. {
  140. return null;
  141. }
  142. else
  143. {
  144. return short.Parse(objValue.ToString());
  145. }
  146. }
  147. else if (type == typeof(float?))
  148. {
  149. if (objValue == null)
  150. {
  151. return null;
  152. }
  153. else
  154. {
  155. return float.Parse(objValue.ToString());
  156. }
  157. }
  158. else if (type == typeof(double?))
  159. {
  160. if (objValue == null)
  161. {
  162. return null;
  163. }
  164. else
  165. {
  166. return double.Parse(objValue.ToString());
  167. }
  168. }
  169. else if (type == typeof(decimal?))
  170. {
  171. if (objValue == null)
  172. {
  173. return null;
  174. }
  175. else
  176. {
  177. return decimal.Parse(objValue.ToString());
  178. }
  179. }
  180. else if (type == typeof(DateTime?))
  181. {
  182. if (objValue == null)
  183. {
  184. return null;
  185. }
  186. else
  187. {
  188. return DateTime.Parse(objValue.ToString());
  189. }
  190. }
  191. else
  192. {
  193. return objValue == null ? "" : objValue.ToString();
  194. }
  195. }
  196. private static string[] ConvertColumnName(string[] strColumns)
  197. {
  198. string[] strs = new string[strColumns.Length];
  199. for (int i = 0; i < strColumns.Length; i++ )
  200. {
  201. strs[i] = GetLanguageFormat(strColumns[i], false);
  202. }
  203. return strs;
  204. }
  205. private static string GetLanguageFormat(string strName, bool isLowerBegin)
  206. {
  207. string[] strs = strName.Split('_');
  208. strName = "";
  209. for (int i = 0; i < strs.Length; i++)
  210. {
  211. if (i == 0 && isLowerBegin)
  212. {
  213. strName += strs[i].ToLower();
  214. }
  215. else
  216. {
  217. strName += strs[i].Substring(0, 1).ToUpper() + strs[i].Substring(1).ToLower();
  218. }
  219. }
  220. return strName;
  221. }
  222. /// <summary>
  223. /// 复制实体类数据(源数据可以为任何类型,该方法只赋值属性名称相同的属性。)
  224. /// </summary>
  225. /// <typeparam name="T">实体类</typeparam>
  226. /// <param name="srcObj">源对象</param>
  227. public static T CopyEntity<T>(object srcObj)
  228. {
  229. try
  230. {
  231. if (srcObj == null) return default(T);
  232. T Desobj = (T)typeof(T).Assembly.CreateInstance(typeof(T).FullName);
  233. PropertyInfo[] srcPropertyInfos = srcObj.GetType().GetProperties();
  234. foreach (PropertyInfo srcPropertyInfo in srcPropertyInfos)
  235. {
  236. PropertyInfo DespropertyInfo = Desobj.GetType().GetProperty(srcPropertyInfo.Name);
  237. if (DespropertyInfo == null) continue;
  238. if (DespropertyInfo.CanWrite)
  239. {
  240. DespropertyInfo.SetValue(Desobj, srcPropertyInfo.GetValue(srcObj, null), null);
  241. }
  242. }
  243. return Desobj;
  244. }
  245. catch (Exception ex)
  246. {
  247. throw;
  248. }
  249. }
  250. /// <summary>
  251. /// 复制实体类数据(源数据可以为任何类型,该方法只赋值属性名称相同的属性。)
  252. /// </summary>
  253. /// <typeparam name="T">实体类</typeparam>
  254. /// <param name="srcObj">源对象</param>
  255. public static void CopyEntity<T>(object srcObj, T descObj)
  256. {
  257. try
  258. {
  259. if (srcObj == null)
  260. {
  261. descObj = default(T);
  262. return;
  263. }
  264. PropertyInfo[] srcPropertyInfos = srcObj.GetType().GetProperties();
  265. foreach (PropertyInfo srcPropertyInfo in srcPropertyInfos)
  266. {
  267. PropertyInfo DespropertyInfo = descObj.GetType().GetProperty(srcPropertyInfo.Name);
  268. if (DespropertyInfo == null) continue;
  269. if (DespropertyInfo.CanWrite)
  270. {
  271. DespropertyInfo.SetValue(descObj, srcPropertyInfo.GetValue(srcObj, null), null);
  272. }
  273. }
  274. }
  275. catch (Exception ex)
  276. {
  277. throw;
  278. }
  279. }
  280. /// <summary>
  281. /// 复制实体类集合
  282. /// </summary>
  283. /// <typeparam name="T">实体类类型</typeparam>
  284. /// <param name="srcObjs">数据源</param>
  285. /// <returns>复制结果</returns>
  286. public static DataSourceList<T> CopyEntitys<T>(List<T> srcObjs)
  287. {
  288. T[] objs = new T[srcObjs.Count];
  289. srcObjs.CopyTo(objs);
  290. DataSourceList<T> desObjs = new DataSourceList<T>();
  291. desObjs.AddRange(objs);
  292. return desObjs;
  293. }
  294. /// <summary>
  295. /// 比较实体类的内容是否完全一致
  296. /// </summary>
  297. /// <typeparam name="T">实体类类型</typeparam>
  298. /// <param name="obj">比较对象</param>
  299. /// <param name="obj2">比较对象2</param>
  300. /// <returns>结果</returns>
  301. public static bool Compare<T>(T obj, T obj2)
  302. {
  303. PropertyInfo[] propertyInfos = obj.GetType().GetProperties();
  304. foreach (PropertyInfo propertyInfo in propertyInfos)
  305. {
  306. if (propertyInfo.GetValue(obj, null).ToString2() != propertyInfo.GetValue(obj2, null).ToString2())
  307. {
  308. return false;
  309. }
  310. }
  311. return true;
  312. }
  313. /// <summary>
  314. /// 根据实体类中的DescriptionAttribute特性描述,转换为Grid列标题。
  315. /// </summary>
  316. /// <typeparam name="T">实体类</typeparam>
  317. /// <param name="gridBand">需要转换的UltraGridBand</param>
  318. public static void ShowGridCaption<T>(UltraGridBand gridBand)
  319. {
  320. try
  321. {
  322. PropertyInfo[] propertyInfos = typeof(T).GetProperties();
  323. foreach (PropertyInfo propertyInfo in propertyInfos)
  324. {
  325. if (gridBand.Columns.Exists(propertyInfo.Name))
  326. {
  327. //设置单元格为空时的值。
  328. if (propertyInfo.PropertyType == typeof(string))
  329. {
  330. gridBand.Columns[propertyInfo.Name].Nullable = Infragistics.Win.UltraWinGrid.Nullable.EmptyString;
  331. }
  332. object[] desAttributes = propertyInfo.GetCustomAttributes(typeof(DescriptionAttribute), false);
  333. if (desAttributes.Length > 0)
  334. {
  335. gridBand.Columns[propertyInfo.Name].Header.Caption = ((DescriptionAttribute)desAttributes[0]).Description;
  336. }
  337. object[] dataHiddenAttributes = propertyInfo.GetCustomAttributes(typeof(DataHiddenAttribute), false);
  338. if (dataHiddenAttributes.Length > 0)
  339. {
  340. DataHiddenAttribute nullableAttribute = (DataHiddenAttribute)dataHiddenAttributes[0];
  341. gridBand.Columns[propertyInfo.Name].Hidden = nullableAttribute.DataHidden;
  342. }
  343. }
  344. }
  345. }
  346. catch (Exception ex)
  347. {
  348. throw;
  349. }
  350. }
  351. /// <summary>
  352. /// UltraGrid列是否隐藏
  353. /// </summary>isShow 为true隐藏,false显示
  354. /// <param name="ugr">UltraGrid</param>
  355. /// <param name="keys">隐藏列</param>
  356. public static void setColumnShowOrHidden(UltraGrid ugr, string[] keys, bool isShow)
  357. {
  358. if (keys == null || keys.Length == 0)
  359. {
  360. foreach (UltraGridColumn ugc in ugr.DisplayLayout.Bands[0].Columns)
  361. {
  362. ugc.Hidden = false;
  363. }
  364. }
  365. else
  366. {
  367. keys.ToArray();
  368. foreach (UltraGridColumn ugc in ugr.DisplayLayout.Bands[0].Columns)
  369. {
  370. if (keys.Contains(ugc.Key))
  371. {
  372. ugc.Hidden = isShow;
  373. }
  374. }
  375. }
  376. }
  377. /// <summary>
  378. /// 建立实体类的关系数据
  379. /// </summary>
  380. /// <typeparam name="T">主实体类</typeparam>
  381. /// <typeparam name="T2">从实体类</typeparam>
  382. /// <param name="parentObjs">主实体类对象集合</param>
  383. /// <param name="childObjs">从实体类对象集合</param>
  384. /// <param name="parentName">关联关系属性</param>
  385. /// <param name="childName">关联关系属性</param>
  386. public static void AddEntityRelation<T, T2>(List<T> parentObjs, List<T2> childObjs, string parentName, string childName)
  387. {
  388. try
  389. {
  390. PropertyInfo parentNameProperty = typeof(T).GetProperty(parentName);
  391. PropertyInfo childNameProperty = typeof(T2).GetProperty(childName);
  392. if (parentObjs.Count == 0 || childObjs.Count == 0) return;
  393. PropertyInfo childCollectionProperty = null;
  394. foreach (PropertyInfo tempParentProperty in typeof(T).GetProperties())
  395. {
  396. if (tempParentProperty.PropertyType.Equals(childObjs.GetType())
  397. || tempParentProperty.PropertyType.Equals(childObjs.GetType().BaseType))
  398. {
  399. childCollectionProperty = tempParentProperty;
  400. }
  401. }
  402. if (childCollectionProperty == null) return;
  403. foreach (T obj in parentObjs)
  404. {
  405. List<T2> childCollection = new List<T2>();
  406. for (int i = 0; i < childObjs.Count; i++)
  407. {
  408. T2 obj2 = childObjs[i];
  409. if (parentNameProperty.GetValue(obj, null).ToString() == childNameProperty.GetValue(obj2, null).ToString())
  410. {
  411. childCollection.Add(obj2);
  412. childObjs.Remove(obj2);
  413. i--;
  414. }
  415. }
  416. childCollectionProperty.SetValue(obj, childCollection, null);
  417. }
  418. }
  419. catch (Exception ex)
  420. {
  421. throw;
  422. }
  423. }
  424. /// <summary>
  425. /// 建立实体类的关系数据
  426. /// </summary>
  427. /// <typeparam name="T">主实体类</typeparam>
  428. /// <typeparam name="T2">从实体类</typeparam>
  429. /// <param name="parentObjs">主实体类对象集合</param>
  430. /// <param name="childObjs">从实体类对象集合</param>
  431. /// <param name="parentName">关联关系属性</param>
  432. /// <param name="childName">关联关系属性</param>
  433. public static void AddEntityRelation<T, T2>(List<T> parentObjs, List<T2> childObjs, string[] parentNames, string[] childNames)
  434. {
  435. try
  436. {
  437. if (parentObjs.Count == 0 || childObjs.Count == 0) return;
  438. if (parentNames.Length == 0 || childNames.Length == 0)
  439. {
  440. throw new Exception("关联属性不能为空!");
  441. }
  442. if (parentNames.Length != childNames.Length)
  443. {
  444. throw new Exception("关联属性个数不一致!");
  445. }
  446. PropertyInfo childCollectionProperty = null;
  447. foreach (PropertyInfo tempParentProperty in typeof(T).GetProperties())
  448. {
  449. if (tempParentProperty.PropertyType.Equals(childObjs.GetType())
  450. || tempParentProperty.PropertyType.Equals(childObjs.GetType().BaseType))
  451. {
  452. childCollectionProperty = tempParentProperty;
  453. }
  454. }
  455. if (childCollectionProperty == null) return;
  456. foreach (T obj in parentObjs)
  457. {
  458. List<T2> childCollection = new List<T2>();
  459. for (int i = 0; i < childObjs.Count; i++)
  460. {
  461. T2 obj2 = childObjs[i];
  462. bool isSame = true;
  463. for (int j = 0; j < parentNames.Length; j++)
  464. {
  465. PropertyInfo parentNameProperty = typeof(T).GetProperty(parentNames[j]);
  466. PropertyInfo childNameProperty = typeof(T2).GetProperty(childNames[j]);
  467. if (parentNameProperty.GetValue(obj, null).ToString() != childNameProperty.GetValue(obj2, null).ToString())
  468. {
  469. isSame = false;
  470. break;
  471. }
  472. }
  473. if (isSame)
  474. {
  475. childCollection.Add(obj2);
  476. childObjs.Remove(obj2);
  477. i--;
  478. }
  479. }
  480. childCollectionProperty.SetValue(obj, childCollection, null);
  481. }
  482. }
  483. catch (Exception ex)
  484. {
  485. throw;
  486. }
  487. }
  488. /// <summary>
  489. /// 验证实体类的内容
  490. /// </summary>
  491. /// <param name="obj">实体类</param>
  492. /// <param name="msg">输入提示消息</param>
  493. /// <param name="fieldName">错误字段</param>
  494. /// <returns>验证结果</returns>
  495. public static bool CheckEntity(object obj, out string msg, out string fieldName)
  496. {
  497. msg = "";
  498. fieldName = "";
  499. PropertyInfo[] propertyInfos = obj.GetType().GetProperties();
  500. foreach (PropertyInfo propertyInfo in propertyInfos)
  501. {
  502. object[] nullableAttributes = propertyInfo.GetCustomAttributes(typeof(NullableAttribute), false);
  503. object[] dataLengthAttributes = propertyInfo.GetCustomAttributes(typeof(DataLengthAttribute), false);
  504. object[] descAtrributes = propertyInfo.GetCustomAttributes(typeof(DescriptionAttribute), false);
  505. object proValue = propertyInfo.GetValue(obj, null);
  506. DescriptionAttribute descriptionAttribute = (DescriptionAttribute)descAtrributes[0];
  507. if (nullableAttributes.Length > 0 && descAtrributes.Length > 0)
  508. {
  509. NullableAttribute nullableAttribute = (NullableAttribute)nullableAttributes[0];
  510. if (nullableAttribute.Nullable == false && (proValue == null || proValue.ToString() == ""))
  511. {
  512. msg = descriptionAttribute.Description + "不能为空!";
  513. fieldName = propertyInfo.Name;
  514. return false;
  515. }
  516. }
  517. //只验证字符串的长度。
  518. if (propertyInfo.PropertyType == typeof(string))
  519. {
  520. if (dataLengthAttributes.Length > 0 && descAtrributes.Length > 0)
  521. {
  522. DataLengthAttribute dataLengthAttribute = (DataLengthAttribute)dataLengthAttributes[0];
  523. int strSize = GetCharacterSize(proValue == null ? "" : proValue.ToString());
  524. if (strSize > dataLengthAttribute.DataLength)
  525. {
  526. msg = descriptionAttribute.Description + "超出系统定义的字符长度!";
  527. fieldName = propertyInfo.Name;
  528. return false;
  529. }
  530. //if()
  531. }
  532. }
  533. }
  534. return true;
  535. }
  536. private static int GetCharacterSize(string str)
  537. {
  538. return Encoding.Unicode.GetByteCount(str);
  539. }
  540. /// <summary>
  541. /// 根据属性名称获取对象中的值。
  542. /// </summary>
  543. /// <param name="obj">对象</param>
  544. /// <param name="propertyName">属性名称</param>
  545. /// <returns>返回值</returns>
  546. public static object GetEntityPropertyValue(object obj, string propertyName)
  547. {
  548. PropertyInfo propertyInfo = obj.GetType().GetProperty(propertyName);
  549. if (propertyInfo == null) return null;
  550. else return propertyInfo.GetValue(obj, null);
  551. }
  552. }
  553. }