DbHelper.cs 9.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239
  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.Security.Cryptography;
  7. using System.Text;
  8. using System.Windows.Forms;
  9. namespace Common
  10. {
  11. public class DbHelper
  12. {
  13. private Log lg = Log.GetInstance();
  14. /// <summary>
  15. ///
  16. /// </summary>
  17. /// <typeparam name="T"></typeparam>
  18. /// <param name="strServiceName"></param>
  19. /// <param name="strMethodName"></param>
  20. /// <param name="objs"></param>
  21. /// <param name="iType">0get查询 1post增删改及传sql查询</param>
  22. /// <returns></returns>
  23. public RESTfulResult<T> doOption<T>(string strServiceName, string strMethodName, object[] objs, int iType)
  24. {
  25. int iQueryType = 0;
  26. string sId = "";
  27. RESTfulResult<T> rm = new RESTfulResult<T>();
  28. //Type t = typeof(T);
  29. //t.Name可得List PageList等名称
  30. lg.WriteLog(16, string.Format("开始调用:服务[{0}],接口[{1}]", strServiceName, strMethodName));
  31. try
  32. {
  33. if (iType == 0)
  34. {
  35. Dictionary<string, object> d = ReflexCls.method(objs, out iQueryType, out sId);
  36. if (iQueryType == 0) //Id查询
  37. {
  38. rm = doOptionId<T>(strServiceName, sId);
  39. if (rm.Succeed && rm.Data != null)
  40. {
  41. PropertyInfo[] PropertyInfos1 = rm.Data.GetType().GetProperties();
  42. foreach (var item in d)
  43. {
  44. foreach (PropertyInfo p1 in PropertyInfos1)
  45. {
  46. if (item.Key.ToUpper() == p1.Name.ToUpper())
  47. {
  48. object value = p1.GetValue(rm.Data, null);
  49. if (value != null && value.ToString() != item.Value.ToString())
  50. {
  51. rm.Data = default(T);
  52. }
  53. }
  54. }
  55. }
  56. }
  57. }
  58. else //非ID查询
  59. {
  60. rm = doOptionOther<T>(strServiceName, strMethodName, d);
  61. }
  62. }
  63. else
  64. {
  65. rm = doOptionPost<T>(strServiceName, strMethodName, objs);
  66. }
  67. if (!rm.Succeed)
  68. {
  69. if (rm.ResultMessage == null && !string.IsNullOrEmpty(rm.Message))
  70. {
  71. //MessageBox.Show(rm.Message);
  72. //System.Environment.Exit(0);
  73. }
  74. if (rm.ResultMessage != null && rm.ResultMessage.Contains("授权"))
  75. {
  76. //MessageBox.Show(rm.ResultMessage);
  77. //System.Environment.Exit(0);
  78. }
  79. lg.WriteLog(16, string.Format("服务[{0}],接口[{1}],错误信息:{2}", strServiceName, strMethodName,
  80. rm.ResultMessage ==null?"服务器访问异常": rm.ResultMessage));
  81. }
  82. }
  83. catch (Exception ex)
  84. {
  85. rm = new RESTfulResult<T>();
  86. rm.Succeed = false;
  87. rm.ResultMessage = string.Format("服务[{0}],接口[{1}],错误信息:{2}", strServiceName, strMethodName, ex.Message.Trim());
  88. lg.WriteLog(16, string.Format("服务[{0}],接口[{1}],错误信息:{2}", strServiceName, strMethodName, ex.Message.Trim()));
  89. }
  90. lg.WriteLog(16, string.Format("完成调用:服务[{0}],接口[{1}]", strServiceName, strMethodName));
  91. return rm;
  92. }
  93. private CoreRESTfulService restful = new CoreRESTfulService(AppConfigCache.serviceUrl, "", true);
  94. /// <summary>
  95. /// 操作数据并返回结果其中尖括号里面的可以是class List<class> DataTable string等等
  96. /// </summary>
  97. /// <param name="strServiceName">服务端的服务名称</param>
  98. /// <param name="strMethodName">服务端方法名称</param>
  99. /// <param name="objs">请求参数 new object[]{ }</param>
  100. /// <returns></returns>
  101. public RESTfulResult<T> doOptionPost<T>(string strServiceName, string strMethodName, object[] objs)
  102. {
  103. RESTfulResult<T> rm = new RESTfulResult<T>();
  104. try
  105. {
  106. Dictionary<string, object> dc = new Dictionary<string, object>();
  107. dc.Add("verificationCode", VerificationCode.getCode());
  108. ReqParam[] req = new ReqParam[objs.Length + 1];
  109. for (int i = 0; i < objs.Length; i++)
  110. {
  111. req[i] = new ReqParam(objs[i]);
  112. }
  113. req[objs.Length] = new ReqParam("verificationCode", VerificationCode.getCode());
  114. //rm = restful.Post<RESTfulResult<T>>(strServiceName, strMethodName, req);
  115. rm = restful.Post<T>(strServiceName, strMethodName, req);
  116. }
  117. catch (Exception ex)
  118. {
  119. rm.Succeed = false;
  120. rm.ResultMessage = ex.Message.Trim();
  121. }
  122. return rm;
  123. }
  124. //*/
  125. /// <summary>
  126. /// 非ID的搞法
  127. /// </summary>
  128. /// <typeparam name="T"></typeparam>
  129. /// <param name="strServiceName"></param>
  130. /// <param name="strMethodName"></param>
  131. /// <param name="objs"></param>
  132. /// <returns></returns>
  133. public RESTfulResult<T> doOptionOther<T>(string strServiceName, string strMethodName, Dictionary<string, object> dc)
  134. {
  135. RESTfulResult<T> rm = new RESTfulResult<T>();
  136. try
  137. {
  138. dc.Add("verificationCode", VerificationCode.getCode());
  139. return restful.Get<T>(strServiceName, strMethodName, dc);
  140. }
  141. catch (Exception ex)
  142. {
  143. rm.Succeed = false;
  144. rm.Message = ex.Message.Trim();
  145. }
  146. return rm;
  147. }
  148. /// <summary>
  149. /// ID的搞法
  150. /// </summary>
  151. /// <typeparam name="T"></typeparam>
  152. /// <param name="strServiceName"></param>
  153. /// <param name="strIdValue">id值</param>
  154. /// <returns></returns>
  155. public RESTfulResult<T> doOptionId<T>(string strServiceName, string strIdValue)
  156. {
  157. RESTfulResult<T> rm = new RESTfulResult<T>();
  158. try
  159. {
  160. Dictionary<string, object> dc = new Dictionary<string, object>();
  161. dc.Add("verificationCode", VerificationCode.getCode());
  162. return restful.Get<T>(strServiceName, strIdValue, dc);
  163. }
  164. catch (Exception ex)
  165. {
  166. rm.Succeed = false;
  167. rm.Message = ex.Message.Trim();
  168. }
  169. return rm;
  170. }
  171. public RESTfulResult<T> doOptionPut<T>(string strServiceName, string strMethodName, object[] objs)
  172. {
  173. RESTfulResult<T> rm = new RESTfulResult<T>();
  174. try
  175. {
  176. Dictionary<string, object> dc = new Dictionary<string, object>();
  177. dc.Add("verificationCode", VerificationCode.getCode());
  178. ReqParam[] req = new ReqParam[objs.Length + 1];
  179. for (int i = 0; i < objs.Length; i++)
  180. {
  181. req[i] = new ReqParam(objs[i]);
  182. }
  183. req[objs.Length] = new ReqParam("verificationCode", VerificationCode.getCode());
  184. //Dictionary<string, object> dc = new Dictionary<string, object>();
  185. //dc.Add("verificationCode", VerificationCode.getCode());
  186. rm = restful.Put<T>(strServiceName, strMethodName, req);
  187. }
  188. catch (Exception ex)
  189. {
  190. rm.Succeed = false;
  191. rm.ResultMessage = ex.Message.Trim();
  192. }
  193. return rm;
  194. }
  195. }
  196. public static class VerificationCode
  197. {
  198. public static string getCode()
  199. {
  200. DateTime dt = DateTime.Now;
  201. string sdt = dt.ToString("MM-yyyy-dd");
  202. string newdt = sdt.Substring(0, 2) + "jisco" + sdt.Substring(2);
  203. return md5(newdt);
  204. }
  205. public static string md5(string input)
  206. {
  207. // Use input string to calculate MD5 hash
  208. MD5 md5 = System.Security.Cryptography.MD5.Create();
  209. byte[] inputBytes = System.Text.Encoding.ASCII.GetBytes(input);
  210. byte[] hashBytes = md5.ComputeHash(inputBytes);
  211. // Convert the byte array to hexadecimal string
  212. StringBuilder sb = new StringBuilder();
  213. for (int i = 0; i < hashBytes.Length; i++)
  214. {
  215. sb.Append(hashBytes[i].ToString("X2"));
  216. // To force the hex string to lower-case letters instead of
  217. // upper-case, use he following line instead:
  218. // sb.Append(hashBytes[i].ToString("x2"));
  219. }
  220. return sb.ToString();
  221. }
  222. }
  223. }