Exemplo n.º 1
0
        /// <summary>
        /// 取多个函数的值
        /// </summary>
        /// <param name="xml"></param>
        /// <returns></returns>
        public static string GetFuncsValue(string xml)
        {
            //生成本次报表运算的GUID
            IList <FuncInfo> funcs = XmlHelper.GetFuncs(xml);

            IList <FuncCalcResult> results = new List <FuncCalcResult>();


            funcs.ToList().ForEach(p => {
                var result = FuncFactory.GetFuncValue(p);
                if (result.Status != EnumFuncActionStatus.Failure)
                {
                    //设置返回值的数据类型
                    result.ResultDataType = FuncCache.GetFuncInfoMetaData(p.Name).ResultType;
                }
                results.Add(result);
            });
            //返回结果集
            return(XmlHelper.GetFuncResult(results));
        }
Exemplo n.º 2
0
        //报表格式有可能会产生变化
        internal static FuncCalcResult GetFuncValue(FuncInfo func)
        {
            if (func == null)
            {
                throw new FuncException("要进行计算的函数为空");
            }

            FuncCalcResult calcResult = null;

            //本次计算KEY
            string taskId = FuncCalcTask.GetTaskId();

            if (string.IsNullOrEmpty(taskId))
            {
                throw new FuncException("函数计算任务的taskId不能为空");
            }

            var funcInfoMateData = FuncCache.GetFuncInfoMetaData(func.Name);

            if (funcInfoMateData == null)
            {
                //计算结果
                calcResult = new FuncCalcResult {
                    Status = EnumFuncActionStatus.Failure
                };
                calcResult.Fault = FaultBuilder.Fault("错误", func.Name + "没有注册函数信息");
                return(calcResult);
            }


            //没有缓存则计算
            var refInfo = FuncConfigure.GetFuncRefInfo(func);

            if (refInfo == null)
            {
                //计算结果
                calcResult = new FuncCalcResult {
                    Status = EnumFuncActionStatus.Failure
                };
                calcResult.Fault = FaultBuilder.Fault("错误", func.Name + "没有注册函数实现类信息");
                return(calcResult);
            }

            try
            {
                //反射调用
                var assembly = Assembly.Load(refInfo.AssemblyName);
                var clazz    = assembly.GetType(refInfo.ClassName);

                //使用新封装的接口,所有的参数都是通过request取
                var calcObject = Activator.CreateInstance(clazz) as IFunction;
                if (calcObject == null)
                {
                    //计算结果
                    calcResult = new FuncCalcResult {
                        Status = EnumFuncActionStatus.Failure
                    };

                    calcResult.Fault = FaultBuilder.Fault("错误", func.Name + "没有实现接口IFuncCalc");
                    return(calcResult);
                }

                if (calcObject != null)
                {
                    calcObject.Func = func;
                    var funcInfo = calcObject.PreHandle(); //返回预处理结果,主要是参数的处理

                    var cache = FuncCache.GetFunc(taskId, funcInfo);
                    if (cache != null)
                    {
                        //已经有值,表示已经缓存,则直接取数
                        return(cache.CalcResult);
                    }
                    else
                    {
                        var funcCalc = calcObject as IFuncCalc;
                        var result   = funcCalc.GetValue();

                        //同时加入到缓存中,对象是否要clone
                        funcInfo.CalcResult = result;
                        FuncCache.AddFunc(taskId, funcInfo);
                        return(result);
                    }
                }
            }
            catch (Exception ex)
            {
                //计算结果
                calcResult = new FuncCalcResult {
                    Status = EnumFuncActionStatus.Failure
                };
                calcResult.Fault = FaultBuilder.Fault("错误", func.Name + "反射调用异常");
            }

            return(calcResult);
        }