分享一个利用递归方式进行BOM表正查的代码,仅供参考,如有不足之处,请多多包涵!
private void GetBomAll(DynamicObject parentBomObj)
{
var bomTreeEntity = (DynamicObjectCollection)parentBomObj["TreeEntity"];
foreach (DynamicObject bomRowObj in bomTreeEntity)
{
//获取BOM行子件物料数据
var materialId = (DynamicObject)bomRowObj["MATERIALIDCHILD"];
//判断是否有子BOM版本
if (bomRowObj["BOMID"] != null)
{
//这里需要再获取一下BOM对象完整数据包
var bomObj = KEDUtils.GetQueryData(this.Context, "ENG_BOM",string.Format(@"FID = {0}", bomRowObj["BOMID_Id"]));
this.GetBomAll(bomObj);
}
else
{
//判断是否可为主产品
if (Convert.ToBoolean((materialId["MaterialProduce"] as DynamicObjectCollection)?[0]["IsMainPrd"]))
throw new KDException("", "部件:" + materialId["Name"] + " 未指定子BOM版本!");
}
//TODO 业务处理
}
}
/// <summary>
/// 取数方案,通过业务对象来获取单个数据,返回单个DynamicObject对象
/// </summary>
/// <param name="formId"></param>
/// <param name="queryWhere">查询条件</param>
/// <returns>返回单个DynamicObject对象</returns>
public static DynamicObject GetQueryData(Context ctx, string formId, string queryWhere)
{
FormMetadata meta = MetaDataServiceHelper.Load(ctx, formId) as FormMetadata;
QueryBuilderParemeter queryParam = new QueryBuilderParemeter();
queryParam.FormId = formId;
if (meta != null) queryParam.BusinessInfo = meta.BusinessInfo;
queryParam.FilterClauseWihtKey = queryWhere;
var dyDatas = BusinessDataServiceHelper.Load(ctx, meta.BusinessInfo.GetDynamicObjectType(), queryParam);
if (dyDatas.Length == 0) return null;
return dyDatas[0];
}
补充:为保证每个BOM表的子BOM版本都有录入,可以添加录入子项物料自动携带子BOM插件。
//物料清单单据插件-值更新事件
public override void DataChanged(DataChangedEventArgs e)
{
base.DataChanged(e);
if (e.Field.Key.Equals("FMATERIALIDCHILD"))
{
var subMatObj = (DynamicObject)this.View.Model.GetValue("FMATERIALIDCHILD", e.Row);
if(subMatObj == null) return;
var erpClsId = Convert.ToInt32(((DynamicObjectCollection)subMatObj["MaterialBase"])[0]["ErpClsID"]);
if(erpClsId != 1)
{
var subBomObj = KEDUtils.GetQueryData(this.Context, "ENG_BOM", string.Format(@"FMaterialId = {0} and FDocumentStatus='C' and FForbidStatus='A'", e.NewValue));
if(subBomObj == null) return;
this.Model.SetItemValueByID("FBOMID",subBomObj["Id"],e.Row);
}
}
}