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;
        }
    }
}