分页账表封装自定义WebApi接口供第三方系统调用示例

栏目:云星空知识作者:金蝶来源:金蝶云社区发布:2024-09-16浏览:1

分页账表封装自定义WebApi接口供第三方系统调用示例

一、业务场景

       因业务需求,第三方系统需要使用报表的数据进行二次加工或其他需求的使用,而报表数据是实时统计计算出来,并没有一个单独的物理表去存储这些数据且没有一个标准的Api接口,,如果要完全二开此报表则不仅需要其取数逻辑,还会涉及权限等各方面的内容,大大的增加了第三方使用的难度,这里提供一种更简便的方案,不需要熟悉报表内部取数逻辑,只需要简单的几个操作即可。


二、方案示例

      1、定义自定义接口

           如何创建自定义接口说明参考:https://wenku.my7c.com/article/53574187566093824

 

update 2022-07-14

using Kingdee.BOS.Contracts;
using Kingdee.BOS.Core.Report;
using Kingdee.BOS.Model.ReportFilter;
using Kingdee.BOS.ServiceFacade.KDServiceFx;
using Kingdee.BOS.ServiceHelper;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Data;
using Newtonsoft.Json;
using Newtonsoft.Json.Converters;
using Kingdee.BOS.Orm.DataEntity;
using Kingdee.BOS.Util;


namespace TestWebApiDemo
{
    public class TestReport : AbstractWebApiBusinessService
    {
        public TestReport(Kingdee.BOS.ServiceFacade.KDServiceFx.KDServiceContext context)
            : base(context)
        {

        }
		
		public string GetReportData(string paramJson)
        {
            string jsonStr = "";
            var ctx = this.KDContext.Session.AppContext;
            if (ctx == null)
            {
                throw new Exception("请先登录系统!");
            }
            if (string.IsNullOrEmpty(paramJson))
            {
                return JsonConvert.SerializeObject(new ResponseModel<DataTable>(false, "参数错误,请检查!"));
            }
            ReportParamModel paramModel = JsonConvert.DeserializeObject<ReportParamModel>(paramJson);
            if (paramModel == null)
            {
                return JsonConvert.SerializeObject(new ResponseModel<DataTable>(false, "参数错误,请检查!"));
            }
            if (string.IsNullOrEmpty(paramModel.FORMID)
                || string.IsNullOrEmpty(paramModel.FSCHEMEID))
            {
                return JsonConvert.SerializeObject(new ResponseModel<DataTable>(false, "参数FORMID、FSCHEMEID必填!"));
            }
            var result = GetDataFromRpt(ctx, paramModel);
            IsoDateTimeConverter timeConverter = new IsoDateTimeConverter();
            timeConverter.DateTimeFormat = "yyyy-MM-dd HH:mm:ss";
            jsonStr = JsonConvert.SerializeObject(result, timeConverter, new DataTableConverter());
            return jsonStr;
        }
		
		/// <summary>
        /// 从报表获取数据
        /// </summary>
        /// <param name="ctx"></param>
        /// <param name="paramModel"></param>
        /// <returns></returns>
        private ResponseModel<DataTable> GetDataFromRpt(Kingdee.BOS.Context ctx, ReportParamModel paramModel)
        {
            ISysReportService sysReporSservice = ServiceFactory.GetSysReportService(ctx);
            var filterMetadata = FormMetaDataCache.GetCachedFilterMetaData(ctx);//加载字段比较条件元数据。
            var reportMetadata = FormMetaDataCache.GetCachedFormMetaData(ctx, paramModel.FORMID);//加载物料收发明细表
            var reportFilterFormId = reportMetadata.BusinessInfo.GetForm().FilterObject;
            var reportFilterMetadata = FormMetaDataCache.GetCachedFormMetaData(ctx, reportFilterFormId);//加载物料收发明细表表过滤条件元数据。
            var reportFilterServiceProvider = reportFilterMetadata.BusinessInfo.GetForm().GetFormServiceProvider();
            var model = new SysReportFilterModel();
            model.SetContext(ctx, reportFilterMetadata.BusinessInfo, reportFilterServiceProvider);
            model.FormId = reportFilterMetadata.BusinessInfo.GetForm().Id;
            model.FilterObject.FilterMetaData = filterMetadata;
            model.InitFieldList(reportMetadata, reportFilterMetadata);
            model.GetSchemeList();
            var entity = model.Load(paramModel.FSCHEMEID);

            if (entity == null || (entity != null && entity.FormID.ToUpperInvariant() != paramModel.FORMID.ToUpperInvariant()))
            {
                return (new ResponseModel<DataTable>(false, "过滤方案FSCHEMEID不属于当前报表!"));
            }

            var filter = model.GetFilterParameter();
			
			#region 快捷过滤条件
            if (paramModel.QuicklyCondition != null && paramModel.QuicklyCondition.Count > 0)
            {
                try
                {
                    //先找基础资料字段
                    List<Kingdee.BOS.Core.Metadata.FieldElement.BaseDataField> baseDataFields = new List<Kingdee.BOS.Core.Metadata.FieldElement.BaseDataField>();
                    foreach (var element in reportFilterMetadata.BusinessInfo.Elements)
                    {
                        if (element is Kingdee.BOS.Core.Metadata.FieldElement.BaseDataField)
                        {
                            if (paramModel.QuicklyCondition.Exists(m => m.FieldName.ToUpperInvariant() == ((Kingdee.BOS.Core.Metadata.FieldElement.BaseDataField)element).PropertyName.ToUpperInvariant()))
                            {
                                baseDataFields.Add(((Kingdee.BOS.Core.Metadata.FieldElement.BaseDataField)element));
                            }
                        }
                    }
					
					foreach (var item in paramModel.QuicklyCondition)
                    {
                        if (filter.CustomFilter.Contains(item.FieldName))
                        {
                            if (baseDataFields.Exists(m => m.PropertyName.ToUpperInvariant() == item.FieldName.ToUpperInvariant()))
                            {
                                DynamicObject dyobj = new DynamicObject(baseDataFields.Find(m => m.PropertyName.ToUpperInvariant() == item.FieldName.ToUpperInvariant()).RefFormDynamicObjectType);
                                if (string.IsNullOrEmpty(item.FieldValue))
                                {
                                    filter.CustomFilter[item.FieldName] = null;
                                }
                                else
                                {
                                    dyobj["Number"] = item.FieldValue;
                                    filter.CustomFilter[item.FieldName] = dyobj;
                                }
                            }
                            else if (!string.IsNullOrEmpty(item.FieldValue))
                            {
                                filter.CustomFilter[item.FieldName] = item.FieldValue;
                            }
                        }
                    }
                }
                catch (Exception ex)
                {
                    throw new Exception(string.Format("快捷过滤条件赋值异常,请参考说明进行设置过滤方案或传参,详情:\n{0}", ex.Message));
                }
            }

            #endregion
			
			IRptParams p = new RptParams();
            p.FormId = reportFilterMetadata.BusinessInfo.GetForm().Id;
            p.StartRow = 1;
            p.EndRow = int.MaxValue;
            p.FilterParameter = filter;
            p.FilterFieldInfo = model.FilterFieldInfo;
            p.CustomParams.Add("OpenParameter", "");

            MoveReportServiceParameter param = new MoveReportServiceParameter(ctx, reportMetadata.BusinessInfo, Guid.NewGuid().ToString(), p);
            int reportType = reportMetadata.ModelTypeSubId;
            if (reportType == 902)
            {
                //分页账表
                DataTable dt = new DataTable();
                var rpt = sysReporSservice.GetListAndReportData(param);
                dt = rpt.DataSource;
                int listcount = rpt.ListCount;
                if (listcount > 0)
                {
                    for (int i = 1; i < listcount; i++)
                    {
                        param.RptFilterParams.CurrentPosition = i;
                        var rptTmp = sysReporSservice.GetListAndReportData(param);
                        if (rptTmp.DataSource != null && rptTmp.DataSource.Rows.Count > 0)
                        {
                            foreach (DataRow row in rptTmp.DataSource.Rows)
                            {
                                dt.ImportRow(row);
                            }
                        }
                    }
                }
                var result = new ResponseModel<DataTable>(true, "查询成功");
                result.data = dt;
                return result;
            }
            else
            {
                return (new ResponseModel<DataTable>(false, "仅支持分页账表,请检查!"));
            }
        }
    }
	
	/// <summary>
    /// 接口请求返回信息
    /// </summary>
    /// <typeparam name="T"></typeparam>
    public class ResponseModel<T>
    {
        /// <summary>
        /// 
        /// </summary>
        /// <param name="success">是否成功 ,true、false</param>
        /// <param name="message">提示消息</param>
        public ResponseModel(bool success, string message)
        {
            this.success = success;
            this.message = message;
        }

        /// <summary>
        /// 查询成功与否
        /// </summary>
        public bool success { get; set; }

        /// <summary>
        /// 消息
        /// </summary>
        public string message { get; set; }

        /// <summary>
        /// 返回数据
        /// </summary>
        public T data { get; set; }

    }
	
	class ReportParamModel
    {
        /// <summary>
        /// 报表FORMID
        /// </summary>
        public string FORMID { get; set; }

        /// <summary>
        /// 过滤方案ID
        /// </summary>
        public string FSCHEMEID { get; set; }

        /// <summary>
        /// 快捷过滤条件
        /// </summary>
        public List<ReportQuicklyConditionModel> QuicklyCondition { get; set; }
    }

    class ReportQuicklyConditionModel
    {
        public string FieldName { get; set; }

        public string FieldValue { get; set; }
    }
	
	


      2、调用自定义接口获取报表数据

           调用路径:http://ServerIp/K3Cloud/接口命名空间.InvAgeAnalyzeTestApi.GetReportData,组件名.common.kdsvc



     参数格式:

{
    "FORMID": "报表FORMID",
    "FSCHEMEID": "过滤方案ID",
    "QuicklyCondition": [
        {
            "FieldName": "快捷过滤字段绑定属性1",
            "FieldValue": "具体数值"
        },
        {
            "FieldName": "快捷过滤字段绑定属性2",
            "FieldValue": "具体数值"
        }
    ]
}


    调用示例:

111111111111.webp



过滤方案ID取数:通过SQL语句查询 SELECT FSCHEMEID,FSCHEMENAME FROM T_BAS_FILTERSCHEME WHERE FFORMID='报表FORMID',取对应的FSCHEMEID值


如何配置过滤方案的显示隐藏字段呢?

以物料收发汇总为例,默认是不显示“批号”的,如何设置显示“批号”字段。

原因是:如果不显示,汇总表数据不以批号为汇总条件,数据表里有FLOTNO字段但值为空。只能显示该字段,才会按批号汇总。


过滤方案ID取数:通过SQL语句查询 SELECT FSCHEMEID,FSCHEMENAME FROM T_BAS_FILTERSCHEME WHERE FFORMID='报表FORMID',取对应的FSCHEMEID值


你好 var ctx = this.KDContext.Session.AppContext;

api调用为null怎么解决呢


分页账表封装自定义WebApi接口供第三方系统调用示例

一、业务场景 因业务需求,第三方系统需要使用报表的数据进行二次加工或其他需求的使用,而报表数据是实时统计计算出来,并没有一...
点击下载文档
确认删除?
回到顶部
客服QQ
  • 客服QQ点击这里给我发消息