webapi.二开案例.自定义下推接口的返回信息
【场景】webapi 自定义下推接口的返回信息
【参考】
api自定义返回信息案例:[WebApi返回定制结果](https://wenku.my7c.com/article/183606?productLineId=1)
业务流程.常见问题.link实体 源单信息字段:[业务流程.常见问题.link实体 源单信息字段](https://wenku.my7c.com/article/541338415162331392?productLineId=1)
【案例】
在上游单据挂表单插件,干预webapi的返回结果
![image.webp](/download/01000bb5940d4b6a4fcf85b84ed8117b2540.webp)
```language
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Kingdee.BOS.Core.Bill.PlugIn;
using Kingdee.BOS.Core.Bill.PlugIn.Args;
using Kingdee.BOS.Util;
using Kingdee.BOS.Core.DynamicForm.PlugIn.Args;
using Kingdee.BOS.JSON;
using Kingdee.BOS.Core.Metadata.FormElement;
using Kingdee.BOS.Core.Metadata;
using Kingdee.BOS.Core.Metadata.EntityElement;
using Kingdee.BOS.Orm.DataEntity;
namespace DynamicFormPlugIn.WebApi
{
[Kingdee.BOS.Util.HotUpdate]
[System.ComponentModel.Description("单据 api操作干预案例")]
public class BillApiFormPlugIn : AbstractBillPlugIn
{
/*
* 业务对象 api操作 表单插件干预
*/
/// <summary>
/// api操作前事件
/// </summary>
/// <param name="e"></param>
public override void OnBeforeWebApiOperation(AfterWebApiOperationArgs e)
{
base.OnBeforeWebApiOperation(e);
}
/// <summary>
/// api操作后事件,支持对结果做干预
/// </summary>
/// <param name="e"></param>
public override void OnAfterWebApiOperation(AfterWebApiOperationArgs e)
{
//判断是否下推操作
if (!string.Equals("Push", e.WebContext.FormOperation.Operation, StringComparison.OrdinalIgnoreCase))
return;
var resultDto = e.WebContext.ResponseDTO as Dictionary<string, object>;
if (resultDto == null || !resultDto.ContainsKey("Result"))
return;
var result = resultDto["Result"] as Dictionary<string, object>;
if (result == null || !result.ContainsKey("ResponseStatus"))
return;
var responseStatus = result["ResponseStatus"] as Dictionary<string, object>;
if (responseStatus == null || !responseStatus.ContainsKey("IsSuccess") || !responseStatus.ContainsKey("SuccessEntitys"))
return;
//判断是否成功
bool isSuccess = Convert.ToBoolean(responseStatus["IsSuccess"]);
if (!isSuccess)
return;
//获取成功的下游单据主键
var successEntitys = responseStatus["SuccessEntitys"] as List<object>;
if (successEntitys == null)
return;
List<object> billIds = new List<object>();
foreach (var successItem in successEntitys)
{
var successEntity = successItem as Dictionary<string, object>;
if (successEntity != null && successEntity.ContainsKey("Id"))
{
billIds.Add(successEntity["Id"]);
}
}
//获取下推参数中的目标单业务对象标识
var inputArgs = JSONObject.Parse(Convert.ToString(e.WebContext.RequestDTO));
if (inputArgs == null)
return;
string targetFormId = inputArgs["TargetFormId"].ToString();
List<object> customInfo = new List<object>();
var businessInfo = (Kingdee.BOS.ServiceHelper.MetaDataServiceHelper.Load(this.Context, targetFormId) as FormMetadata).BusinessInfo;
foreach (var billId in billIds)
{
//通过sql或者单据数据包获取link信息追加到api返回结果,通过sql加载最优,自行参考原理实现
var billObj = Kingdee.BOS.ServiceHelper.BusinessDataServiceHelper.LoadSingle(this.Context, billId, businessInfo.GetDynamicObjectType());
var message = DynamicFormPlugIn.Test.LinkHelper.GetSrcLinkInfoByBillObj(businessInfo, billObj);
customInfo.Add(message);
}
resultDto.Add("CustomInfo", customInfo);
}
}
public static class LinkHelper
{
/// <summary>
/// 获取link实体
/// </summary>
/// <param name="businessInfo"></param>
/// <returns></returns>
public static LinkEntity GetLinkEntity(BusinessInfo businessInfo)
{
var billForm = businessInfo.GetForm();
if (billForm.LinkSet == null || billForm.LinkSet.LinkEntitys.Count == 0)
return null;
return billForm.LinkSet.LinkEntitys[0];
}
/// <summary>
/// 获取link实体的关联实体
/// </summary>
/// <param name="businessInfo"></param>
/// <returns></returns>
public static Entity GetLinkParentEntiy(BusinessInfo businessInfo)
{
var linkEntity = GetLinkEntity(businessInfo);
if (linkEntity == null)
return null;
var linkParentEntity = businessInfo.GetEntity(linkEntity.ParentEntityKey);
return linkParentEntity;
}
/// <summary>
/// 输出数据包的关联源单关系
/// </summary>
/// <param name="businessInfo">元数据</param>
/// <param name="billDataObj">数据包</param>
/// <returns></returns>
public static string GetSrcLinkInfoByBillObj(BusinessInfo businessInfo, DynamicObject billDataObj)
{
var linkParentEntity = GetLinkParentEntiy(businessInfo);
var linkEntity = GetLinkEntity(businessInfo);
StringBuilder msgBuilder = new StringBuilder();
if (linkParentEntity is HeadEntity)
{
//关联实体在单据头
if (!billDataObj.DynamicObjectType.Properties.ContainsKey(linkEntity.Key))
return string.Empty;
msgBuilder.AppendLine("关联实体在单据头");
DynamicObjectCollection linkObjColl = billDataObj[linkEntity.Key] as DynamicObjectCollection;
ScanLinkDataObj(linkObjColl, (x) => { GetLinkMessage(x, msgBuilder); });
}
else if (linkParentEntity is EntryEntity)
{
if (!billDataObj.DynamicObjectType.Properties.Contains(linkParentEntity.DynamicProperty))
return string.Empty;
msgBuilder.AppendLine("关联实体在单据体");
DynamicObjectCollection entityObjColl = billDataObj[linkParentEntity.DynamicProperty] as DynamicObjectCollection;
for (int i = 0; i < entityObjColl.Count; ++i)
{
var entityObj = entityObjColl[i];
if (entityObj == null)
continue;
msgBuilder.AppendLine(string.Format("分录序号:{0}", i + 1));
if (!entityObj.DynamicObjectType.Properties.ContainsKey(linkEntity.Key))
return string.Empty;
DynamicObjectCollection linkObjColl = entityObj[linkEntity.Key] as DynamicObjectCollection;
ScanLinkDataObj(linkObjColl, (x) => { GetLinkMessage(x, msgBuilder); });
}
}
return msgBuilder.ToString();
}
/// <summary>
/// link实体遍历
/// </summary>
/// <param name="linkObjColl"></param>
/// <param name="linkObjAct"></param>
private static void ScanLinkDataObj(DynamicObjectCollection linkObjColl, Action<DynamicObject> linkObjAct)
{
if (linkObjColl == null)
return;
foreach (var linkRowObj in linkObjColl)
{
linkObjAct(linkRowObj);
}
}
/// <summary>
/// 输出单个link数据包的源单信息
/// </summary>
/// <param name="linkObj"></param>
/// <param name="msgBuilder"></param>
private static void GetLinkMessage(DynamicObject linkObj, StringBuilder msgBuilder)
{
if (linkObj == null)
return;
const string fstablename = "STABLENAME";
const string fsid = "SID";
const string fsbillid = "SBILLID";
string sTableName = linkObj[fstablename].ToString();
long sid = Convert.ToInt64(linkObj[fsid]);
long sbillId = Convert.ToInt64(linkObj[fsbillid]);
msgBuilder.AppendLine(string.Format("源单实体表名:{0},源单实体分录内码:{1},源单单据内码:{2}", sTableName, sid, sbillId));
}
/// <summary>
/// 输出单据内码的关联源单关系
/// </summary>
/// <param name="businessInfo"></param>
/// <param name="billId"></param>
/// <returns></returns>
public static string GetSrcLinkInfoByBillId(BusinessInfo businessInfo, object billId)
{
return string.Empty;
}
}
}
```
【效果】
![image.webp](/download/010081093ba03112447b857d2c7333a9549d.webp)
webapi.二开案例.自定义下推接口的返回信息
【场景】webapi 自定义下推接口的返回信息【参考】api自定义返回信息案例:[WebApi返回定制结果](https://wenku.my7c.com/article/183606?...
点击下载文档
本文2024-09-16 18:11:35发表“云星空知识”栏目。
本文链接:https://wenku.my7c.com/article/kingdee-k3cloud-20542.html
您需要登录后才可以发表评论, 登录登录 或者 注册
最新文档
热门文章