using Common;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using com.hnshituo.core.webapp.vo;

namespace MeterSceneLibrary
{
    public class ExecuteMethod
    {
        /// <summary>
        /// 调用并执行指定类里面的函数
        /// </summary>
        /// <param name="nameSpace">命名空间</param>
        /// <param name="className">需要调用的类名(包含其命名空间)</param>
        /// <param name="methodName">需要调用的方法名</param>
        /// <param name="parameters">传递的参数值</param>
        /// <return>返回值</return>
        public RESTfulResult<T> GetAndExecuteMethod<T>(string nameSpace, string className, string methodName, object[] parameters = null)
        {
            RESTfulResult<T> rm = new RESTfulResult<T>();
            string strCls = nameSpace + "." + className;
            try
            {
                var type = Type.GetType(strCls);
                if (type == null)
                {
                    rm.Succeed = false;
                    rm.ResultMessage = "类[" + strCls + "]不存在";
                }

                var obj = type.Assembly.CreateInstance(strCls);
                //调用其方法
                var method = type.GetMethod(methodName);
                if (method == null)
                {
                    rm.Succeed = false;
                    rm.ResultMessage = "类[" + strCls + "]不存在方法[" + methodName + "]";
                }
                //*/

                //执行方法
                rm.Data = (T)method.Invoke(obj, parameters);
                rm.Succeed = true;
            }
            catch (Exception ex)
            {
                rm.Succeed = false;
                rm.ResultMessage = "类[" + strCls + "]的方法[" + methodName + "]执行失败：" + ex.Message;
            }
            return rm;
        }
    }

    public class rtInfo 
    { 
        /// <summary>
        /// 为true则执行成功了，为false则未成功
        /// </summary>
        public bool result { get; set; }

        /// <summary>
        /// 执行后返回的信息
        /// </summary>
        public string resultInfo { get; set; }

        /// <summary>
        /// 是否报错导致的false，如果这里未true则认为是报错了
        /// </summary>
        public bool isError { get; set; }

        /// <summary>
        /// 为1时提示取样
        /// </summary>
        public string message { get; set; }

        /// <summary>
        /// 警告标识
        /// </summary>
        public bool isWarn { get; set; }

        /// <summary>
        /// 警告标识
        /// </summary>
        public string WarnContent { get; set; }

        /// <summary>
        /// 求助标识
        /// </summary>
        public bool isHelp { get; set; }
    }
}

