| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173 |
- using com.hnshituo.core.webapp.vo;
- using CoreFS.CA06;
- using System;
- using System.Collections.Generic;
- using System.Reflection;
- using System.Windows.Forms;
- namespace Common
- {
- public class DbHelper
- {
- private Log lg = Log.GetInstance();
- /// <summary>
- ///
- /// </summary>
- /// <typeparam name="T"></typeparam>
- /// <param name="strServiceName"></param>
- /// <param name="strMethodName"></param>
- /// <param name="objs"></param>
- /// <param name="iType">0get查询 1post增删改及传sql查询</param>
- /// <returns></returns>
- public RESTfulResult<T> doOption<T>(string strServiceName, string strMethodName, object[] objs, int iType)
- {
- int iQueryType = 0;
- string sId = "";
- RESTfulResult<T> rm = new RESTfulResult<T>();
- //Type t = typeof(T);
- //t.Name可得List PageList等名称
- lg.WriteLog(16, string.Format("开始调用:服务[{0}],接口[{1}]", strServiceName, strMethodName));
- try
- {
- if (iType == 0)
- {
- Dictionary<string, object> d = ReflexCls.method(objs, out iQueryType, out sId);
- if (iQueryType == 0) //Id查询
- {
- rm = doOptionId<T>(strServiceName, sId);
- if (rm.Succeed && rm.Data != null)
- {
- PropertyInfo[] PropertyInfos1 = rm.Data.GetType().GetProperties();
- foreach (var item in d)
- {
- foreach (PropertyInfo p1 in PropertyInfos1)
- {
- if (item.Key.ToUpper() == p1.Name.ToUpper())
- {
- object value = p1.GetValue(rm.Data, null);
- if (value != null && value.ToString() != item.Value.ToString())
- {
- rm.Data = default(T);
- }
- }
- }
- }
- }
- }
- else //非ID查询
- {
- rm = doOptionOther<T>(strServiceName, strMethodName, d);
- }
- }
- else
- {
- rm = doOptionPost<T>(strServiceName, strMethodName, objs);
- }
- if (!rm.Succeed)
- {
- if (rm.ResultMessage == null && !string.IsNullOrEmpty(rm.Message))
- {
- MessageBox.Show(rm.Message);
- //System.Environment.Exit(0);
- }
- if (rm.ResultMessage != null && rm.ResultMessage.Contains("授权"))
- {
- MessageBox.Show(rm.ResultMessage);
- //System.Environment.Exit(0);
- }
- lg.WriteLog(16, string.Format("服务[{0}],接口[{1}],错误信息:{2}", strServiceName, strMethodName,
- rm.ResultMessage ==null?"服务器访问异常": rm.ResultMessage));
- }
- }
- catch (Exception ex)
- {
- rm = new RESTfulResult<T>();
- rm.Succeed = false;
- rm.ResultMessage = string.Format("服务[{0}],接口[{1}],错误信息:{2}", strServiceName, strMethodName, ex.Message.Trim());
- lg.WriteLog(16, string.Format("服务[{0}],接口[{1}],错误信息:{2}", strServiceName, strMethodName, ex.Message.Trim()));
- }
- lg.WriteLog(16, string.Format("完成调用:服务[{0}],接口[{1}]", strServiceName, strMethodName));
- return rm;
- }
- private CoreRESTfulService restful = new CoreRESTfulService(AppConfigCache.serviceUrl, "", true);
- /// <summary>
- /// 操作数据并返回结果其中尖括号里面的可以是class List<class> DataTable string等等
- /// </summary>
- /// <param name="strServiceName">服务端的服务名称</param>
- /// <param name="strMethodName">服务端方法名称</param>
- /// <param name="objs">请求参数 new object[]{ }</param>
- /// <returns></returns>
- public RESTfulResult<T> doOptionPost<T>(string strServiceName, string strMethodName, object[] objs)
- {
- RESTfulResult<T> rm = new RESTfulResult<T>();
- try
- {
- ReqParam[] req = new ReqParam[objs.Length];
- for (int i = 0; i < objs.Length; i++)
- {
- req[i] = new ReqParam(objs[i]);
- }
- //rm = restful.Post<RESTfulResult<T>>(strServiceName, strMethodName, req);
- rm = restful.Post<T>(strServiceName, strMethodName, req);
- }
- catch (Exception ex)
- {
- rm.Succeed = false;
- rm.ResultMessage = ex.Message.Trim();
- }
- return rm;
- }
- //*/
- /// <summary>
- /// 非ID的搞法
- /// </summary>
- /// <typeparam name="T"></typeparam>
- /// <param name="strServiceName"></param>
- /// <param name="strMethodName"></param>
- /// <param name="objs"></param>
- /// <returns></returns>
- public RESTfulResult<T> doOptionOther<T>(string strServiceName, string strMethodName, Dictionary<string, object> dc)
- {
- RESTfulResult<T> rm = new RESTfulResult<T>();
- try
- {
- return restful.Get<T>(strServiceName, strMethodName, dc);
- }
- catch (Exception ex)
- {
- rm.Succeed = false;
- rm.Message = ex.Message.Trim();
- }
- return rm;
- }
- /// <summary>
- /// ID的搞法
- /// </summary>
- /// <typeparam name="T"></typeparam>
- /// <param name="strServiceName"></param>
- /// <param name="strIdValue">id值</param>
- /// <returns></returns>
- public RESTfulResult<T> doOptionId<T>(string strServiceName, string strIdValue)
- {
- RESTfulResult<T> rm = new RESTfulResult<T>();
- try
- {
- return restful.Get<T>(strServiceName, strIdValue, new Dictionary<string, object>());
- }
- catch (Exception ex)
- {
- rm.Succeed = false;
- rm.Message = ex.Message.Trim();
- }
- return rm;
- }
- }
- }
|