Dal.cs 10 KB

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