自定义WebAPI接口调用BOM展开
创建接口
1、新建类库,此处类库名为WebApiService,添加引用如图所示
2、代码具体实现如下:
增加调用bom展开传入参数model类
public class BomExpandParam { /// /// 需求日期 /// public DateTime ValidDate { get; set; } /// /// bomID /// public List<long> BomIds { get; set; } /// /// 需求数量 /// public decimal Qty { get; set; } /// /// BOM展开层级数,默认只展开一层 0表示全部展开 /// public int ExpandLevel { get; set; } ///是否考虑替代方案:默认不考虑,是否考虑替代:默认不考虑 public bool IsShowSubMtrl { get; set; } /// /// 是否展开替代料:默认不考虑 /// public bool IsExpandSubMtrl { get; set; } /// /// 是否隐藏外购BOM:默认考虑 /// public bool IsHideOutSourceBOM { get; set; } }增加一个类GetBomExpandService,继承AbstractWebApiBusinessService
using Kingdee.BOS; using Kingdee.BOS.Core.Metadata; using Kingdee.BOS.Core.SqlBuilder; using Kingdee.BOS.Orm.DataEntity; using Kingdee.BOS.ServiceFacade.KDServiceFx; using Kingdee.BOS.ServiceHelper; using Kingdee.BOS.Util; using Kingdee.BOS.WebApi.ServicesStub; using Kingdee.K3.BD.ServiceHelper; using Kingdee.K3.Core.BD; using Kingdee.K3.Core.BD.ServiceArgs; using Kingdee.K3.Core.MFG.ENG.BomExpand; using Kingdee.K3.Core.MFG.ENG.ParamOption; using Kingdee.K3.Core.MFG.EntityHelper; using Kingdee.K3.MFG.ServiceHelper.ENG; using Newtonsoft.Json; using Newtonsoft.Json.Linq; using System; using System.Collections.Generic; using System.Linq; using System.Text; namespace WebApiService { public class GetBomExpandService : AbstractWebApiBusinessService { protected MemBomExpandOption_ForPSV memBomExpandOption = null; public GetBomExpandService(KDServiceContext context) : base(context) { } /// /// 传入的数据 /// /// <param name="info">包含 需求日期 bomID</param> /// <returns></returns> public JObject ExecuteService(string info) { JObject result = new JObject(); var param = JsonConvert.DeserializeObject<BomExpandParam>(info); var memBomExpandOption = UpdateBomQueryOption(param); List<DynamicObject> lstExpandSource = this.BuildBomExpandSourceData(param); if (lstExpandSource.IsEmpty()) { result.Add("Status", "False"); result.Add("Message", "当前物料对应的BOM不存在,请确认!"); return result; } var queryDatas = GetBomChildData(lstExpandSource, memBomExpandOption); result.Add("Status", "Success"); result.Add("Data", queryDatas.IsEmpty() ? "" : JsonConvert.SerializeObject(queryDatas)); result.Add("Message", "查询成功"); return result; } /// /// 构建bom查询参数 /// /// <param name="bomExpandParam"></param> /// <returns></returns> private MemBomExpandOption_ForPSV UpdateBomQueryOption(BomExpandParam bomExpandParam) { MemBomExpandOption_ForPSV memBomExpandOption = new MemBomExpandOption_ForPSV(); //设置BOM展开到几层 memBomExpandOption.ExpandLevelTo = bomExpandParam.ExpandLevel; DateTime? dtValidDate = bomExpandParam.ValidDate; if (dtValidDate != null && dtValidDate >= new DateTime(1900, 01, 01)) { memBomExpandOption.ValidDate = (DateTime)dtValidDate; } //获取工程数据参数 memBomExpandOption.BomExpandCalType = Kingdee.K3.Core.MFG.EnumConst.Enums.Enu_BomExpandCalType.kdBySystemProfile; memBomExpandOption.CsdSubstitution = bomExpandParam.IsShowSubMtrl; memBomExpandOption.IsExpandSubMtrl = bomExpandParam.IsExpandSubMtrl; memBomExpandOption.IsHideOutSourceBOM = bomExpandParam.IsHideOutSourceBOM; return memBomExpandOption; } /// /// 构建bom展开的源数据 /// /// <param name="bomIds"></param> /// <returns></returns> private List<DynamicObject> BuildBomExpandSourceData(BomExpandParam bomExpandParam) { List<DynamicObject> lstExpandSource = new List<DynamicObject>(); QueryBuilderParemeter batchParam = new QueryBuilderParemeter { FormId = "ENG_BOM", SelectItems = SelectorItemInfo.CreateItems("FID", "FMaterialId.FMASTERID as MaterialMasterId", "FMATERIALID", "FUseOrgId", "FUNITID", "FBaseUnitId", "FParentAuxPropId"), }; batchParam.ExtJoinTables.Add(new ExtJoinTableDescription { FieldName = "FID", TableName = "table(fn_StrSplit(@ids,',',1))", TableNameAs = "ts", ScourceKey = "FID", }); List<SqlParam> sqlParam = new List<SqlParam> { new SqlParam("@ids",KDDbType.udt_inttable,bomExpandParam.BomIds.Distinct().ToArray()), }; DynamicObjectCollection bomHeadObjs = QueryServiceHelper.GetDynamicObjectCollection(this.KDContext.Session.AppContext, batchParam, sqlParam); Dictionary<Int64, DynamicObject> dctBomHeadObjs = bomHeadObjs.ToDictionary(x => Convert.ToInt64(x["FID"])); List<GetUnitConvertRateArgs> converRateArgs = new List<GetUnitConvertRateArgs>(); foreach (DynamicObject bomHeadObj in bomHeadObjs) { long materialId = Convert.ToInt64(bomHeadObj["FMATERIALID"]); long masterId = Convert.ToInt64(bomHeadObj["MaterialMasterId"]); long unitId = Convert.ToInt64(bomHeadObj["FUNITID"]); long baseUnitId = Convert.ToInt64(bomHeadObj["FBaseUnitId"]); long supplyOrgId = Convert.ToInt64(bomHeadObj["FUseOrgId"]); //此处要对数量进行换算:不能直接传常用单位数量 converRateArgs.Add(new GetUnitConvertRateArgs() { PrimaryKey = bomHeadObjs.IndexOf(bomHeadObj), MaterialId = materialId, MasterId = masterId, SourceUnitId = unitId, DestUnitId = baseUnitId }); } Dictionary<long, UnitConvert> dctUnitConvert = UnitConvertServiceHelper.GetUnitConvertRateList(this.KDContext.Session.AppContext, converRateArgs); foreach (DynamicObject bomHeadObj in bomHeadObjs) { long bomId = Convert.ToInt64(bomHeadObj["FID"]); long materialId = Convert.ToInt64(bomHeadObj["FMATERIALID"]); long unitId = Convert.ToInt64(bomHeadObj["FUNITID"]); long baseUnitId = Convert.ToInt64(bomHeadObj["FBaseUnitId"]); long supplyOrgId = Convert.ToInt64(bomHeadObj["FUseOrgId"]); long parentAuxProId = Convert.ToInt64(bomHeadObj["FParentAuxPropId"]); BomForwardSourceDynamicRow bomExpandSourceRowView = BomForwardSourceDynamicRow.CreateInstance(); UnitConvert rate = null; decimal dQty = bomExpandParam.Qty; if (dctUnitConvert.TryGetValue(bomHeadObjs.IndexOf(bomHeadObj), out rate)) { dQty = rate.ConvertQty(dQty); } bomExpandSourceRowView.MaterialId_Id = materialId; bomExpandSourceRowView.BomId_Id = bomId; bomExpandSourceRowView.NeedQty = dQty; bomExpandSourceRowView.NeedDate = bomExpandParam.ValidDate; bomExpandSourceRowView.UnitId_Id = unitId; bomExpandSourceRowView.BaseUnitId_Id = baseUnitId; bomExpandSourceRowView.SupplyOrgId_Id = supplyOrgId; bomExpandSourceRowView.AuxPropId = parentAuxProId; //默认BOM展开过程使用的时间单位为天。 bomExpandSourceRowView.TimeUnit = ((int)Kingdee.K3.Core.MFG.EnumConst.Enums.Enu_TimeUnit.KdDay).ToString(); lstExpandSource.Add(bomExpandSourceRowView.DataEntity); } return lstExpandSource; } /// /// 获取bom子项数据 /// /// <param name="lstExpandSource"></param> /// <param name="memBomExpandOption"></param> /// <returns></returns> private List<DynamicObject> GetBomChildData(List<DynamicObject> lstExpandSource, MemBomExpandOption_ForPSV memBomExpandOption) { memBomExpandOption.IsConvertUnitQty = true; List<DynamicObject> result = BomQueryServiceHelper.GetBomQueryForwardResult(this.KDContext.Session.AppContext, lstExpandSource, memBomExpandOption); //根据BOM有无联副产品,更新物料清单正查明细的“存在联副产品” SetIsExistCoby(result); return result; } /// /// 根据BOM有无联副产品,更新物料清单正查明细的“存在联副产品” /// /// <param name="queryChildData"></param> private void SetIsExistCoby(List<DynamicObject> queryChildData) { if (queryChildData.IsEmpty()) return; foreach (var query in queryChildData) { var bom = query.GetDynamicValue<DynamicObject>("BomId"); if (!bom.IsNullOrEmpty()) { var coby = bom.GetDynamicValue<DynamicObjectCollection>("EntryBOMCOBY"); if (!coby.IsEmpty()) { query.SetDynamicObjectItemValue("IsExistCoby", true); } } } } } }至此,api接口已经封装完成。
将此类库生成dll文件,放到应用站点目录下:一般是:./WebSite/bin。
客户端调用此api接口
创建控制台程序,调用此api接口
此工程添加引用如下:Kingdee.BOS.WebApi.Client,首先调用ValidateLogin()验证登录信息,登录成功之后再调用bom展开接口。
调用接口代码实现
using Kingdee.BOS.WebApi.Client; using Newtonsoft.Json; using Newtonsoft.Json.Linq; using System; using System.Collections.Generic; using System.Linq; using System.Net.Http; using System.Text; namespace HttpRequest { class Program { static void Main(string[] args) { try { K3CloudApiClient client = new K3CloudApiClient("http://localhost:1700/"); var loginResult = client.ValidateLogin("5ef84228d60ade", "Administrator", "********", 2052); var resultType = JObject.Parse(loginResult)["LoginResultType"].Value<int>(); if (resultType == 1) { BomExpandParam bomExpandParam = new BomExpandParam() { BomIds = new List<long>() { 193904 }, ValidDate = DateTime.Now, Qty = 1 }; string serviceName = "WebApiService.GetBomExpandService.ExecuteService,WebApiService"; var info = client.Execute<JObject>(serviceName, new object[] { JsonConvert.SerializeObject(bomExpandParam) }); Console.ReadKey(); } } catch (Exception ex) { Console.WriteLine(ex.Message); } } } public class ValidModelEntity { public string Acctid { get; set; } public string userName { get; set; } public string password { get; set; } public int lcid { get; set; } } }结果验证
接口返回结果如下图:其中Data数据就是bom展开结果



HTTP调用

作者:Tracy_Huang
来源:金蝶云社区
原文链接:https://vip.kingdee.com/knowledge/388095148611278336?productLineId=1&isKnowledge=2&lang=zh-CN
著作权归作者所有。未经允许禁止转载,如需转载请联系作者获得授权。