FrmDtConverter.cs 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275
  1. using Core.Mes.Client.Comm.Control;
  2. using Core.Mes.Client.Comm.Tool;
  3. using CoreFS.SA06;
  4. using Infragistics.Win.UltraWinGrid;
  5. using System;
  6. using System.Reflection;
  7. using System.Windows.Forms;
  8. //Create By Cx 2015年1月17日 14:12:40
  9. namespace Core.StlMes.Client.Qcm
  10. {
  11. /// <summary>
  12. /// 通用实体类转换器(前提条件:实体类设置了转换器所需要的特性)
  13. /// </summary>
  14. /// <typeparam name="Tobj"></typeparam>
  15. class FrmDtConverter<Tobj>
  16. {
  17. /// <summary>
  18. /// UltraGridRow转换为实体类,并验证是否为空。所能识别的特性:FieldAttribute, FieldAttributeType, NullableAttribute, CoreUserInfo。
  19. /// </summary>
  20. /// <param name="grid"></param>
  21. /// <param name="TObj"></param>
  22. /// <returns></returns>
  23. public Tobj Convert(UltraGridRow row, CoreUserInfo userInfo, FieldAttributeType fieldAttributeType)
  24. {
  25. Type type = typeof(Tobj);
  26. Tobj obj = (Tobj)type.Assembly.CreateInstance(type.FullName);
  27. PropertyInfo[] propertyInfos = type.GetProperties();
  28. foreach (PropertyInfo propertyInfo in propertyInfos)
  29. {
  30. SetUserInfo(obj, propertyInfo, userInfo);//设置用户信息。
  31. object[] objs = propertyInfo.GetCustomAttributes(typeof(FieldAttribute), true);
  32. if (objs.Length > 0)
  33. {
  34. FieldAttribute fieldAttribute = (FieldAttribute)objs[0];
  35. if ((fieldAttribute.FieldAttributeType & fieldAttributeType) != fieldAttributeType) continue; //如果该属性没有指定的字段类型则跳过。
  36. string columnName = fieldAttribute.FieldName == "" ? GetColumnName(row, propertyInfo.Name) : fieldAttribute.FieldName.ToUpper();
  37. if (row.Cells.Exists(columnName) == false)
  38. {
  39. throw new Exception("实体类定义了字段名特性,但在Grid中没有找到该列名!");
  40. }
  41. if (fieldAttribute.IsGetValue == true)
  42. {
  43. propertyInfo.SetValue(obj, row.Cells[columnName].Value.ToString(), null);
  44. }
  45. else
  46. {
  47. propertyInfo.SetValue(obj, row.Cells[columnName].Text.ToString(), null);
  48. }
  49. if (CheckIsNull(obj, propertyInfo) == true)
  50. {
  51. MessageUtil.ShowWarning(row.Cells[columnName].Column.Header.Caption + "不能为空!");
  52. row.Activate();
  53. row.Cells[columnName].Activate();
  54. return default(Tobj); //引用类型返回null
  55. }
  56. }
  57. }
  58. return obj;
  59. }
  60. private string GetColumnName(UltraGridRow row, string propertyName)
  61. {
  62. foreach (UltraGridCell cell in row.Cells)
  63. {
  64. string columName = "";
  65. string[] strAry = cell.Column.Key.Split('_');
  66. for (int i = 0; i < strAry.Length; i++)
  67. {
  68. columName += strAry[i].Substring(0, 1).ToUpper() + strAry[i].Substring(1).ToLower();
  69. }
  70. if (columName == propertyName)
  71. {
  72. return cell.Column.Key;
  73. }
  74. }
  75. return "";
  76. }
  77. private bool CheckIsNull(Tobj obj, PropertyInfo propertyInfo)
  78. {
  79. object[] objs = propertyInfo.GetCustomAttributes(typeof(NullableAttribute), true);
  80. if (objs.Length > 0)
  81. {
  82. NullableAttribute nullableAttribute = (NullableAttribute)objs[0];
  83. if (nullableAttribute.Nullable == false
  84. && (propertyInfo.GetValue(obj, null) == null || propertyInfo.GetValue(obj, null).ToString() == ""))
  85. {
  86. return true;
  87. }
  88. }
  89. return false;
  90. }
  91. private void SetUserInfo(Tobj obj, PropertyInfo propertyInfo, CoreUserInfo userInfo)
  92. {
  93. object[] objs = propertyInfo.GetCustomAttributes(typeof(UserInfoAttribute), true);
  94. if (objs.Length > 0)
  95. {
  96. UserInfoAttribute userInfoAttribute = (UserInfoAttribute)objs[0];
  97. string strUserInfo = "";
  98. switch (userInfoAttribute.UserInfoAttributeType)
  99. {
  100. case UserInfoAttributeType.Department:
  101. strUserInfo = userInfo.GetDepartment();
  102. break;
  103. case UserInfoAttributeType.Deptid:
  104. strUserInfo = userInfo.GetDeptid();
  105. break;
  106. case UserInfoAttributeType.GuidCode:
  107. strUserInfo = userInfo.GetGuidCode();
  108. break;
  109. case UserInfoAttributeType.LoginID:
  110. strUserInfo = userInfo.GetLoginID();
  111. break;
  112. case UserInfoAttributeType.LoginTime:
  113. strUserInfo = userInfo.GetLoginTime();
  114. break;
  115. case UserInfoAttributeType.MachineIP:
  116. strUserInfo = userInfo.GetMachineIP();
  117. break;
  118. case UserInfoAttributeType.MachineName:
  119. strUserInfo = userInfo.GetMachineName();
  120. break;
  121. case UserInfoAttributeType.PurviewInfo:
  122. strUserInfo = userInfo.GetPurviewInfo();
  123. break;
  124. case UserInfoAttributeType.Role:
  125. strUserInfo = userInfo.GetRole();
  126. break;
  127. case UserInfoAttributeType.UserGroup:
  128. strUserInfo = userInfo.GetUserGroup();
  129. break;
  130. case UserInfoAttributeType.UserGroupText:
  131. strUserInfo = userInfo.GetUserGroupText();
  132. break;
  133. case UserInfoAttributeType.UserID:
  134. strUserInfo = userInfo.GetUserID();
  135. break;
  136. case UserInfoAttributeType.UserName:
  137. strUserInfo = userInfo.GetUserName();
  138. break;
  139. case UserInfoAttributeType.UserOrder:
  140. strUserInfo = userInfo.GetUserOrder();
  141. break;
  142. case UserInfoAttributeType.UserOrderText:
  143. strUserInfo = userInfo.GetUserOrderText();
  144. break;
  145. }
  146. propertyInfo.SetValue(obj, strUserInfo, null);
  147. }
  148. }
  149. /// <summary>
  150. /// Control中的控件集合转换为实体类,并验证是否为空。所能识别的特性:ControlAttribute, ControlAttributeType, NullableAttribute, CoreUserInfo。
  151. /// </summary>
  152. /// <param name="grid"></param>
  153. /// <param name="TObj"></param>
  154. /// <returns></returns>
  155. public Tobj Convert(System.Windows.Forms.Control parentControl, CoreUserInfo userInfo, ControlAttributeType controlAttributeType)
  156. {
  157. Type type = typeof(Tobj);
  158. Tobj obj = (Tobj)type.Assembly.CreateInstance(type.FullName);
  159. PropertyInfo[] propertyInfos = type.GetProperties();
  160. foreach (PropertyInfo propertyInfo in propertyInfos)
  161. {
  162. SetUserInfo(obj, propertyInfo, userInfo);//设置用户信息。
  163. object[] objs = propertyInfo.GetCustomAttributes(typeof(ControlAttribute), true);
  164. if (objs.Length > 0)
  165. {
  166. ControlAttribute controlAttribute = (ControlAttribute)objs[0];
  167. if ((controlAttribute.ControlAttributeType & controlAttributeType) != controlAttributeType) continue; //如果该属性没有指定的字段类型则跳过。
  168. string controlName = controlAttribute.ControlName;
  169. System.Windows.Forms.Control[] childControls = parentControl.Controls.Find(controlName, true);
  170. System.Windows.Forms.Control childControl = null;
  171. if (childControls.Length == 0)
  172. {
  173. throw new Exception("实体类定义了字段名特性,但在parentControl中没有找到该控件名称!");
  174. }
  175. else
  176. {
  177. childControl = childControls[0];
  178. }
  179. string strValue = GetControlValue(childControl, controlAttribute.IsGetValue);
  180. propertyInfo.SetValue(obj, strValue, null);
  181. if (CheckIsNull(obj, propertyInfo) == true)
  182. {
  183. if (childControl is LabelControlInterface)
  184. {
  185. LabelControlInterface labelControl = (LabelControlInterface)childControl;
  186. if (labelControl is LabelComboBox)
  187. {
  188. MessageUtil.ShowWarning("请选择" + labelControl.Caption + "!");
  189. }
  190. else
  191. {
  192. MessageUtil.ShowWarning(labelControl.Caption + "不能为空!");
  193. }
  194. }
  195. else
  196. {
  197. throw new Exception(childControl.Name + ":不能为空!");
  198. }
  199. childControl.Focus();//combobox下拉
  200. return default(Tobj); //引用类型返回null
  201. }
  202. }
  203. }
  204. return obj;
  205. }
  206. private string GetControlValue(System.Windows.Forms.Control control, bool isGetValue)
  207. {
  208. switch (control.GetType().FullName)
  209. {
  210. case "System.Windows.Forms.TextBox":
  211. return ((TextBox)control).Text.Trim();
  212. case "System.Windows.Forms.ComboBox":
  213. if (isGetValue)
  214. {
  215. if (((ComboBox)control).SelectedValue != null)
  216. {
  217. return ((ComboBox)control).SelectedValue.ToString();
  218. }
  219. break;
  220. }
  221. else
  222. {
  223. return ((ComboBox)control).Text.Trim();
  224. }
  225. case "Core.Mes.Client.Comm.Control.DataFilterControl":
  226. if (isGetValue)
  227. {
  228. if (((DataFilterControl)control).SelectedValue != null)
  229. {
  230. return ((DataFilterControl)control).SelectedValue.ToString();
  231. }
  232. break;
  233. }
  234. else
  235. {
  236. return ((DataFilterControl)control).Text.Trim();
  237. }
  238. case "Core.Mes.Client.Comm.Control.LabelComboBox":
  239. if (isGetValue)
  240. {
  241. if (((LabelComboBox)control).SelecteValue != null)
  242. {
  243. return ((LabelComboBox)control).SelecteValue.ToString();
  244. }
  245. break;
  246. }
  247. else
  248. {
  249. return ((LabelComboBox)control).Text.Trim();
  250. }
  251. case "Core.Mes.Client.Comm.Control.LabelTextBox":
  252. return ((LabelTextBox)control).Text.Trim();
  253. case "Core.Mes.Client.Comm.Control.LabelCheckBox":
  254. return ((LabelCheckBox)control).Checked.ToString();
  255. case "Core.Mes.Client.Comm.Control.LabelNumericText":
  256. return ((LabelNumericText)control).Value.ToString().Trim();
  257. case "Core.Mes.Client.Comm.Control.LabelDateTimePicker":
  258. return ((LabelDateTimePicker)control).Value.ToString("yyyy-MM-dd HH:mm:ss");
  259. }
  260. return "";
  261. }
  262. }
  263. }