Dal.cs 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348
  1. using Core.Mes.Client.Comm.Tool;
  2. using CoreFS.CA06;
  3. using CoreFS.SA06;
  4. using System;
  5. using System.Collections;
  6. using System.Collections.Generic;
  7. using System.Data;
  8. using System.Diagnostics;
  9. using System.Reflection;
  10. namespace Core.StlMes.Client.Judge.Commons
  11. {
  12. public class Dal
  13. {
  14. private OpeBase _ob = null;
  15. public OpeBase Ob
  16. {
  17. get { return _ob; }
  18. set { _ob = value; }
  19. }
  20. public Dal(OpeBase ob)
  21. {
  22. _ob = ob;
  23. }
  24. public DataTable GetTable(string remoteStr, params object[] param)
  25. {
  26. CoreClientParam ccp = new CoreClientParam
  27. {
  28. ServerName = remoteStr.Substring(0, remoteStr.LastIndexOf(".")),
  29. MethodName = remoteStr.Substring(remoteStr.LastIndexOf(".") + 1),
  30. ServerParams = param
  31. };
  32. ccp = _ob.ExecuteSortResultByQueryToDataTable(ccp, CoreInvokeType.Internal);
  33. DataTable src = ccp.SourceDataTable;
  34. foreach (DataColumn col in src.Columns)
  35. {
  36. col.ColumnName = GetLanguageFormat(col.ColumnName, true);
  37. }
  38. return src;
  39. }
  40. /// <summary>
  41. /// 直接通过服务端xmlId获取数据
  42. /// </summary>
  43. /// <param name="xmlId"></param>
  44. /// <param name="parms"></param>
  45. /// <returns></returns>
  46. public DataTable GetTableByXmlId(string xmlId, params object[] parms)
  47. {
  48. CoreClientParam ccp = new CoreClientParam();
  49. ccp.ServerName = "com.steering.pss.judge.comm.ClientQueryHelper";
  50. ccp.MethodName = "query";
  51. ccp.ServerParams = new object[] { xmlId, parms };
  52. ccp = _ob.ExecuteSortResultByQueryToDataTable(ccp, CoreInvokeType.Internal);
  53. DataTable src = ccp.SourceDataTable;
  54. if (src != null)
  55. {
  56. foreach (DataColumn col in src.Columns)
  57. {
  58. col.ColumnName = GetLanguageFormat(col.ColumnName, true);
  59. }
  60. }
  61. return src;
  62. }
  63. public CoreClientParam Set(string remoteStr, params object[] param)
  64. {
  65. CoreClientParam ccp = new CoreClientParam
  66. {
  67. IfShowErrMsg = false,
  68. ServerName = remoteStr.Substring(0, remoteStr.LastIndexOf(".")),
  69. MethodName = remoteStr.Substring(remoteStr.LastIndexOf(".") + 1),
  70. ServerParams = param
  71. };
  72. ccp = _ob.ExecuteNonQuery(ccp, CoreInvokeType.Internal);
  73. return ccp;
  74. }
  75. /// <summary>
  76. /// 直接通过服务端xmlId获取数据
  77. /// </summary>
  78. /// <param name="methodName"></param>
  79. /// <param name="param"></param>
  80. /// <returns></returns>
  81. public DataRow GetRow(string remoteStr, params object[] param)
  82. {
  83. DataTable dt = GetTable(remoteStr, param);
  84. if (dt.Rows.Count == 0)
  85. {
  86. return null;
  87. }
  88. else
  89. {
  90. return dt.Rows[0];
  91. }
  92. }
  93. public DataRow GetRowByXmlId(string xmlId, params object[] parms)
  94. {
  95. DataTable dt = GetTableByXmlId(xmlId, parms);
  96. if (dt.Rows.Count == 0)
  97. {
  98. return null;
  99. }
  100. else
  101. {
  102. return dt.Rows[0];
  103. }
  104. }
  105. private string GetLanguageFormat(string strName, bool isLowerBegin)
  106. {
  107. string[] strs = strName.Split('_');
  108. strName = "";
  109. for (int i = 0; i < strs.Length; i++)
  110. {
  111. if (i == 0 && isLowerBegin)
  112. {
  113. strName += strs[i].ToLower();
  114. }
  115. else
  116. {
  117. strName += strs[i].Substring(0, 1).ToUpper() + strs[i].Substring(1).ToLower();
  118. }
  119. }
  120. return strName;
  121. }
  122. public DataSourceList<T> GetEntitysByXmlId<T>(string xmlId, params object[] parms)
  123. {
  124. return GetEntitys<T>("com.steering.pss.judge.comm.ClientQueryHelper.query", new object[] { xmlId, parms });
  125. }
  126. public DataSourceList<T> GetEntitys<T>(string remoteStr, params object[] parms)
  127. {
  128. string key;
  129. try
  130. {
  131. DataSourceList<T> listResult = new DataSourceList<T>();
  132. CoreClientParam ccp = new CoreClientParam
  133. {
  134. ServerName = remoteStr.Substring(0, remoteStr.LastIndexOf(".")),
  135. MethodName = remoteStr.Substring(remoteStr.LastIndexOf(".") + 1),
  136. ServerParams = parms
  137. };
  138. ccp = _ob.ExecuteQuery(ccp, CoreInvokeType.Internal);
  139. string[] strColumns = (ccp.ReturnObject as CoreReturnSortObject).getColIdx();
  140. string[] strColumnsEntity = ConvertColumnName(strColumns);
  141. ArrayList list = (ccp.ReturnObject as CoreReturnSortObject).getResult();
  142. Type type = typeof(T);
  143. Dictionary<string, PropertyInfo> propertyInfoDic = new Dictionary<string, PropertyInfo>();
  144. for (int j = 0; j < strColumns.Length; j++)
  145. {
  146. string column = strColumns[j];
  147. string columnEntity = strColumnsEntity[j];
  148. PropertyInfo propertyInfo = type.GetProperty(columnEntity);
  149. if (propertyInfo == null) continue;
  150. if (propertyInfoDic.ContainsKey(column))
  151. {
  152. propertyInfoDic[column] = propertyInfo;
  153. }
  154. else
  155. {
  156. propertyInfoDic.Add(column, propertyInfo);
  157. }
  158. }
  159. for (int i = 0; i < list.Count; i++)
  160. {
  161. Hashtable hashtable = (Hashtable)list[i];
  162. ArrayList list2 = new ArrayList();
  163. T obj = (T)type.Assembly.CreateInstance(type.FullName);
  164. foreach (KeyValuePair<string, PropertyInfo> propertyInfoKV in propertyInfoDic)
  165. {
  166. key = propertyInfoKV.Key;
  167. object obj2 = hashtable[propertyInfoKV.Key];
  168. PropertyInfo propertyInfo = propertyInfoKV.Value;
  169. if ((obj2 != null) && typeof(Hashtable).Equals(obj2.GetType()))
  170. {
  171. Hashtable hashtable2 = (Hashtable)obj2;
  172. propertyInfo.SetValue(obj, ConvertDataType(propertyInfo.PropertyType, hashtable2["value"]), null);
  173. }
  174. else
  175. {
  176. propertyInfo.SetValue(obj, ConvertDataType(propertyInfo.PropertyType, obj2), null);
  177. }
  178. }
  179. listResult.Add(obj);
  180. }
  181. return listResult;
  182. }
  183. catch (Exception ex)
  184. {
  185. Debug.WriteLine(ex.Message);
  186. throw;
  187. }
  188. finally
  189. {
  190. }
  191. }
  192. private static object ConvertDataType(Type type, object objValue)
  193. {
  194. if (type == typeof(bool))
  195. {
  196. bool result;
  197. if (bool.TryParse(objValue.ToString(), out result))
  198. {
  199. return result;
  200. }
  201. else
  202. {
  203. return false;
  204. }
  205. }
  206. else if (type == typeof(bool?))
  207. {
  208. bool result;
  209. if (bool.TryParse(objValue.ToString(), out result))
  210. {
  211. return result;
  212. }
  213. else
  214. {
  215. return null;
  216. }
  217. }
  218. else if (type == typeof(int?))
  219. {
  220. if (objValue == null)
  221. {
  222. return null;
  223. }
  224. else
  225. {
  226. return int.Parse(objValue.ToString());
  227. }
  228. }
  229. else if (type == typeof(long?))
  230. {
  231. if (objValue == null)
  232. {
  233. return null;
  234. }
  235. else
  236. {
  237. return long.Parse(objValue.ToString());
  238. }
  239. }
  240. else if (type == typeof(short?))
  241. {
  242. if (objValue == null)
  243. {
  244. return null;
  245. }
  246. else
  247. {
  248. return short.Parse(objValue.ToString());
  249. }
  250. }
  251. else if (type == typeof(float?))
  252. {
  253. if (objValue == null)
  254. {
  255. return null;
  256. }
  257. else
  258. {
  259. return float.Parse(objValue.ToString());
  260. }
  261. }
  262. else if (type == typeof(double?))
  263. {
  264. if (objValue == null)
  265. {
  266. return null;
  267. }
  268. else
  269. {
  270. return double.Parse(objValue.ToString());
  271. }
  272. }
  273. else if (type == typeof(decimal?))
  274. {
  275. if (objValue == null)
  276. {
  277. return null;
  278. }
  279. else
  280. {
  281. return decimal.Parse(objValue.ToString());
  282. }
  283. }
  284. else if (type == typeof(DateTime?))
  285. {
  286. if (objValue == null)
  287. {
  288. return null;
  289. }
  290. else
  291. {
  292. return DateTime.Parse(objValue.ToString());
  293. }
  294. }
  295. else
  296. {
  297. return objValue == null ? "" : objValue.ToString();
  298. }
  299. }
  300. private string[] ConvertColumnName(string[] strColumns)
  301. {
  302. string[] strs = new string[strColumns.Length];
  303. for (int i = 0; i < strColumns.Length; i++)
  304. {
  305. strs[i] = GetLanguageFormat(strColumns[i], false);
  306. }
  307. return strs;
  308. }
  309. public string GetJsonData(string remoteStr, params object[] param)
  310. {
  311. CoreClientParam ccp = new CoreClientParam
  312. {
  313. ServerName = remoteStr.Substring(0, remoteStr.LastIndexOf(".")),
  314. MethodName = remoteStr.Substring(remoteStr.LastIndexOf(".") + 1),
  315. ServerParams = param
  316. };
  317. ccp = _ob.ExecuteQuery(ccp, CoreInvokeType.Internal);
  318. return ccp.ReturnObject.ToString2();
  319. }
  320. }
  321. }