DbHelper.cs 6.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173
  1. using com.hnshituo.core.webapp.vo;
  2. using CoreFS.CA06;
  3. using System;
  4. using System.Collections.Generic;
  5. using System.Reflection;
  6. using System.Windows.Forms;
  7. namespace Common
  8. {
  9. public class DbHelper
  10. {
  11. private Log lg = Log.GetInstance();
  12. /// <summary>
  13. ///
  14. /// </summary>
  15. /// <typeparam name="T"></typeparam>
  16. /// <param name="strServiceName"></param>
  17. /// <param name="strMethodName"></param>
  18. /// <param name="objs"></param>
  19. /// <param name="iType">0get查询 1post增删改及传sql查询</param>
  20. /// <returns></returns>
  21. public RESTfulResult<T> doOption<T>(string strServiceName, string strMethodName, object[] objs, int iType)
  22. {
  23. int iQueryType = 0;
  24. string sId = "";
  25. RESTfulResult<T> rm = new RESTfulResult<T>();
  26. //Type t = typeof(T);
  27. //t.Name可得List PageList等名称
  28. lg.WriteLog(16, string.Format("开始调用:服务[{0}],接口[{1}]", strServiceName, strMethodName));
  29. try
  30. {
  31. if (iType == 0)
  32. {
  33. Dictionary<string, object> d = ReflexCls.method(objs, out iQueryType, out sId);
  34. if (iQueryType == 0) //Id查询
  35. {
  36. rm = doOptionId<T>(strServiceName, sId);
  37. if (rm.Succeed && rm.Data != null)
  38. {
  39. PropertyInfo[] PropertyInfos1 = rm.Data.GetType().GetProperties();
  40. foreach (var item in d)
  41. {
  42. foreach (PropertyInfo p1 in PropertyInfos1)
  43. {
  44. if (item.Key.ToUpper() == p1.Name.ToUpper())
  45. {
  46. object value = p1.GetValue(rm.Data, null);
  47. if (value != null && value.ToString() != item.Value.ToString())
  48. {
  49. rm.Data = default(T);
  50. }
  51. }
  52. }
  53. }
  54. }
  55. }
  56. else //非ID查询
  57. {
  58. rm = doOptionOther<T>(strServiceName, strMethodName, d);
  59. }
  60. }
  61. else
  62. {
  63. rm = doOptionPost<T>(strServiceName, strMethodName, objs);
  64. }
  65. if (!rm.Succeed)
  66. {
  67. if (rm.ResultMessage == null && !string.IsNullOrEmpty(rm.Message))
  68. {
  69. MessageBox.Show(rm.Message);
  70. //System.Environment.Exit(0);
  71. }
  72. if (rm.ResultMessage != null && rm.ResultMessage.Contains("授权"))
  73. {
  74. MessageBox.Show(rm.ResultMessage);
  75. //System.Environment.Exit(0);
  76. }
  77. lg.WriteLog(16, string.Format("服务[{0}],接口[{1}],错误信息:{2}", strServiceName, strMethodName,
  78. rm.ResultMessage ==null?"服务器访问异常": rm.ResultMessage));
  79. }
  80. }
  81. catch (Exception ex)
  82. {
  83. rm = new RESTfulResult<T>();
  84. rm.Succeed = false;
  85. rm.ResultMessage = string.Format("服务[{0}],接口[{1}],错误信息:{2}", strServiceName, strMethodName, ex.Message.Trim());
  86. lg.WriteLog(16, string.Format("服务[{0}],接口[{1}],错误信息:{2}", strServiceName, strMethodName, ex.Message.Trim()));
  87. }
  88. lg.WriteLog(16, string.Format("完成调用:服务[{0}],接口[{1}]", strServiceName, strMethodName));
  89. return rm;
  90. }
  91. private CoreRESTfulService restful = new CoreRESTfulService(AppConfigCache.serviceUrl, "", true);
  92. /// <summary>
  93. /// 操作数据并返回结果其中尖括号里面的可以是class List<class> DataTable string等等
  94. /// </summary>
  95. /// <param name="strServiceName">服务端的服务名称</param>
  96. /// <param name="strMethodName">服务端方法名称</param>
  97. /// <param name="objs">请求参数 new object[]{ }</param>
  98. /// <returns></returns>
  99. public RESTfulResult<T> doOptionPost<T>(string strServiceName, string strMethodName, object[] objs)
  100. {
  101. RESTfulResult<T> rm = new RESTfulResult<T>();
  102. try
  103. {
  104. ReqParam[] req = new ReqParam[objs.Length];
  105. for (int i = 0; i < objs.Length; i++)
  106. {
  107. req[i] = new ReqParam(objs[i]);
  108. }
  109. //rm = restful.Post<RESTfulResult<T>>(strServiceName, strMethodName, req);
  110. rm = restful.Post<T>(strServiceName, strMethodName, req);
  111. }
  112. catch (Exception ex)
  113. {
  114. rm.Succeed = false;
  115. rm.ResultMessage = ex.Message.Trim();
  116. }
  117. return rm;
  118. }
  119. //*/
  120. /// <summary>
  121. /// 非ID的搞法
  122. /// </summary>
  123. /// <typeparam name="T"></typeparam>
  124. /// <param name="strServiceName"></param>
  125. /// <param name="strMethodName"></param>
  126. /// <param name="objs"></param>
  127. /// <returns></returns>
  128. public RESTfulResult<T> doOptionOther<T>(string strServiceName, string strMethodName, Dictionary<string, object> dc)
  129. {
  130. RESTfulResult<T> rm = new RESTfulResult<T>();
  131. try
  132. {
  133. return restful.Get<T>(strServiceName, strMethodName, dc);
  134. }
  135. catch (Exception ex)
  136. {
  137. rm.Succeed = false;
  138. rm.Message = ex.Message.Trim();
  139. }
  140. return rm;
  141. }
  142. /// <summary>
  143. /// ID的搞法
  144. /// </summary>
  145. /// <typeparam name="T"></typeparam>
  146. /// <param name="strServiceName"></param>
  147. /// <param name="strIdValue">id值</param>
  148. /// <returns></returns>
  149. public RESTfulResult<T> doOptionId<T>(string strServiceName, string strIdValue)
  150. {
  151. RESTfulResult<T> rm = new RESTfulResult<T>();
  152. try
  153. {
  154. return restful.Get<T>(strServiceName, strIdValue, new Dictionary<string, object>());
  155. }
  156. catch (Exception ex)
  157. {
  158. rm.Succeed = false;
  159. rm.Message = ex.Message.Trim();
  160. }
  161. return rm;
  162. }
  163. }
  164. }