分页账表封装自定义WebApi接口供第三方系统调用示例原创
6人赞赏了该文章
2,676次浏览
编辑于2022年07月14日 14:13:46
摘要由AI智能服务提供
文本概述:本文介绍了在业务需求下,第三方系统如何便捷地使用实时统计的报表数据,而无需直接访问内部取数逻辑。通过定义自定义接口,第三方系统可以发送请求并获取所需报表数据,具体实现包括接收参数、验证参数、调用服务获取数据并返回JSON格式的结果。此方案简化了第三方系统的使用难度,提高了数据交互的便捷性。
有用
反馈
一、业务场景
因业务需求,第三方系统需要使用报表的数据进行二次加工或其他需求的使用,而报表数据是实时统计计算出来,并没有一个单独的物理表去存储这些数据且没有一个标准的Api接口,,如果要完全二开此报表则不仅需要其取数逻辑,还会涉及权限等各方面的内容,大大的增加了第三方使用的难度,这里提供一种更简便的方案,不需要熟悉报表内部取数逻辑,只需要简单的几个操作即可。
二、方案示例
1、定义自定义接口
如何创建自定义接口说明参考:https://vip.kingdee.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": "具体数值" } ] }
调用示例:
赞 6
6人点赞
还没有人点赞,快来当第一个点赞的人吧!
打赏
0人打赏
还没有人打赏,快来当第一个打赏的人吧!