需求背景:
单据列表插件中,只能够直接获取到单据内码、编号字段值,其他字段需要自行到数据库读取。
本帖介绍如何到数据库单据,并进行循环获取字段值。
案例说明:
本案例所使用的单据,有两个单据体。
本案例分别介绍了如下三种取数需求:
1. 取单据内码、单据编号
2. 取单据头字段+单据体字段
3. 取单据头字段+单据体1字段 + 单据体2字段
示例代码:
//*********************************************************
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.ComponentModel;
// 引用Kingdee.BOS.dll
using Kingdee.BOS;
using Kingdee.BOS.Util;
// 引用Kingdee.BOS.Core.dll
using Kingdee.BOS.Core;
using Kingdee.BOS.Core.DynamicForm;
using Kingdee.BOS.Core.DynamicForm.PlugIn;
using Kingdee.BOS.Core.DynamicForm.PlugIn.Args;
using Kingdee.BOS.Core.List;
using Kingdee.BOS.Core.List.PlugIn;
using Kingdee.BOS.Core.List.PlugIn.Args;
using Kingdee.BOS.Core.Metadata;
using Kingdee.BOS.Core.Metadata.EntityElement;
using Kingdee.BOS.Core.Metadata.FieldElement;
using Kingdee.BOS.Core.SqlBuilder;
// 引用Kingdee.BOS.ServiceHelper.dll
using Kingdee.BOS.ServiceHelper;
// 引用Kingdee.BOS.DataEntity.dll
using Kingdee.BOS.Orm.DataEntity;
namespace JDSample.FormPlugIn.List
{
[Description("演示在列表上如何加载字段值")]
public class S160121LoadFldValueList : AbstractListPlugIn
{
public override void AfterBarItemClick(AfterBarItemClickEventArgs e)
{
if (e.BarItemKey.EqualsIgnoreCase("tbLoadFldValue"))
{// 点击"加载字段值"菜单
#region 需求场景一:读取列表上,当前所选行的内码、编号、单据体行内码
if (this.ListView.CurrentSelectedRowInfo != null)
{
// 单据内码
string pkValue = this.ListView.CurrentSelectedRowInfo.PrimaryKeyValue;
// 单据编号
string billNo = this.ListView.CurrentSelectedRowInfo.BillNo;
// 列表显示的单据体标识(Key):如果列表未显示任何单据体,此属性为空
string entityKey = this.ListView.CurrentSelectedRowInfo.EntryEntityKey;
// 单据体内码(如果列表未显示单据体,则此属性为空)
string entryPKValue = this.ListView.CurrentSelectedRowInfo.EntryPrimaryKeyValue;
}
#endregion 需求场景一:示例结束
#region 需求场景二:读取单据头其他字段,以及明细单据体上的字段值
// 需求特征:
// 1. 不需要同时读取不同单据体上的字段值
// 2. 可能需要根据单据体内码进行过滤
// 构建取数参数
QueryBuilderParemeter queryParam = new QueryBuilderParemeter();
// 指定单据FormId及其元数据
queryParam.FormId = this.View.BillBusinessInfo.GetForm().Id;
queryParam.BusinessInfo =this.View.BillBusinessInfo;
// 指定需要加载的字段:使用单据设计时的字段标识(Key)
// 单据主键
queryParam.SelectItems.Add(new SelectorItemInfo( this.View.BillBusinessInfo.GetForm().PkFieldName));
// 单据头字段:普通字段类型,如文本、日期、数值、复选框等
queryParam.SelectItems.Add(new SelectorItemInfo("F_JD_Date"));
// 单据头字段:基础资料字段内码
queryParam.SelectItems.Add(new SelectorItemInfo("F_JD_Supplier"));
// 单据头字段:基础资料字段编码
// SelectorRefItemInfo类型用于加载基础资料引用属性,key的格式为:"字段Key.引用属性Key"
// PropertyName 用于指定此引用属性的别名:后续需要使用此别名到返回结果中获取字段值
queryParam.SelectItems.Add(
new SelectorRefItemInfo("F_JD_Supplier.FNumber") { PropertyName = "F_JD_Supplier_FNumber" });
// 单据头字段:基础资料字段名称
queryParam.SelectItems.Add(new SelectorRefItemInfo("F_JD_Supplier.FName") { PropertyName = "F_JD_Supplier_FName" });
// 单据体主键
Entity entity = this.View.BillBusinessInfo.GetEntity("FEntity");
queryParam.SelectItems.Add(new SelectorItemInfo(
string.Format("{0}_{1}", entity.Key, entity.EntryPkFieldName)));
// 单据体字段
queryParam.SelectItems.Add(new SelectorItemInfo("F_JD_FMaterialId"));
queryParam.SelectItems.Add(new SelectorRefItemInfo("F_JD_FMaterialId.FNumber") { PropertyName = "F_JD_FMaterialId_FNumber" });
queryParam.SelectItems.Add(new SelectorRefItemInfo("F_JD_FMaterialId.FNumber") { PropertyName = "F_JD_FMaterialId.FName" });
queryParam.SelectItems.Add(new SelectorItemInfo("F_JD_UnitID"));
queryParam.SelectItems.Add(new SelectorRefItemInfo("F_JD_UnitID.FNumber") { PropertyName = "F_JD_UnitID_FNumber" });
queryParam.SelectItems.Add(new SelectorRefItemInfo("F_JD_UnitID.FNumber") { PropertyName = "F_JD_UnitID.FName" });
queryParam.SelectItems.Add(new SelectorItemInfo("F_JD_Qty"));
// 过滤条件:需要根据单据体内码过滤
if ("FEntity".EqualsIgnoreCase(this.ListView.CurrentSelectedRowInfo.EntryEntityKey))
{// 列表显示了单据体,则以单据体内码过滤
//queryParam.FilterClauseWihtKey = string.Format(" {0}_{1} = {2} ",
// entity.Key, entity.EntryPkFieldName,
// this.ListView.CurrentSelectedRowInfo.EntryPrimaryKeyValue);
List<long> entityIds = (from p in this.ListView.SelectedRowsInfo
select Convert.ToInt64(p.EntryPrimaryKeyValue)).ToList();
queryParam.FilterClauseWihtKey = string.Format(" {0}_{1} IN ( {2} )",
entity.Key, entity.EntryPkFieldName,
string.Join(",", entityIds));
}
else
{// 列表未显示单据体,则以单据内码过滤
//queryParam.FilterClauseWihtKey = string.Format(" {0} = {1} ",
// this.ListView.BillBusinessInfo.GetForm().PkFieldName,
// this.ListView.CurrentSelectedRowInfo.PrimaryKeyValue);
List<long> billIds = (from p in this.ListView.SelectedRowsInfo
select Convert.ToInt64(p.PrimaryKeyValue)).ToList();
queryParam.FilterClauseWihtKey = string.Format(" {0} IN ( {1} ) ",
this.ListView.BillBusinessInfo.GetForm().PkFieldName,
string.Join(",", billIds));
}
// 读取数据
var rows = QueryServiceHelper.GetDynamicObjectCollection(this.Context, queryParam);
// 获取取得的字段值
foreach(var row in rows)
{
// 单据内码
long billId = Convert.ToInt64(row[this.ListView.BillBusinessInfo.GetForm().PkFieldName]);
// 日期
DateTime date = Convert.ToDateTime(row["F_JD_Date"]);
// 供应商
long supplierId = Convert.ToInt64(row["F_JD_Supplier"]);
// 供应商代码
string supplierNumber = Convert.ToString(row["F_JD_Supplier_FNumber"]);
// 供应商名称
string supplierName = Convert.ToString(row["F_JD_Supplier_FName"]);
}
#endregion 需求场景二:示例结束
#region 需求场景三:读取单据头其他字段值,以及多个单据体上的字段值
// 需求特征:
// 1. 需同时读取在不同单据体上的字段值
// 2. 不需要根据单据体内码进行过滤
// 指定需要加载的字段
List<SelectorItemInfo> selectorList = new List<SelectorItemInfo>();
// 单据头字段
selectorList.Add(new SelectorItemInfo(this.ListView.BillBusinessInfo.GetForm().PkFieldName));
selectorList.Add(new SelectorItemInfo("F_JD_Date"));
selectorList.Add(new SelectorItemInfo("F_JD_Supplier"));
// 单据体1的字段
Entity entity1 = this.ListView.BillBusinessInfo.GetEntity("FEntity");
selectorList.Add(new SelectorItemInfo(string.Format("{0}_{1}", entity1.Key, entity1.EntryPkFieldName)));
selectorList.Add(new SelectorItemInfo("F_JD_FMaterialId"));
selectorList.Add(new SelectorItemInfo("F_JD_UnitID"));
selectorList.Add(new SelectorItemInfo("F_JD_Qty"));
// 单据体2的字段
Entity entity2 = this.ListView.BillBusinessInfo.GetEntity("FEntity2");
selectorList.Add(new SelectorItemInfo(string.Format("{0}_{1}", entity2.Key, entity2.EntryPkFieldName)));
selectorList.Add(new SelectorItemInfo("F_JD_Text"));
// 过滤条件
string filter = string.Format(" {0} = {1} ",
this.ListView.BillBusinessInfo.GetForm().PkFieldName,
this.ListView.CurrentSelectedRowInfo.PrimaryKeyValue);
// 加载单据数据包
var billObjs = Kingdee.BOS.ServiceHelper.BusinessDataServiceHelper.Load(
this.Context,
this.View.BillBusinessInfo,
selectorList,
OQLFilter.CreateHeadEntityFilter(filter));
// 获取字段值
Field fldDate = this.ListView.BillBusinessInfo.GetField("F_JD_Date");
BaseDataField fldSupplier = this.ListView.BillBusinessInfo.GetField("F_JD_Supplier") as BaseDataField;
BaseDataField fldMaterial = this.ListView.BillBusinessInfo.GetField("F_JD_FMaterialId") as BaseDataField;
BaseDataField fldUnit = this.ListView.BillBusinessInfo.GetField("F_JD_UnitID") as BaseDataField;
Field fldQty = this.ListView.BillBusinessInfo.GetField("F_JD_Qty");
Field fldText = this.ListView.BillBusinessInfo.GetField("F_JD_Text");
foreach (var billObj in billObjs)
{
// 单据头字段
// 内码
long billId = Convert.ToInt64(billObj[0]);
// 日期
DateTime fldDateValue = Convert.ToDateTime(fldDate.DynamicProperty.GetValue(billObj));
// 供应商:基础资料字段
DynamicObject fldSupplierValue = fldSupplier.DynamicProperty.GetValue(billObj) as DynamicObject;
if (fldSupplierValue != null)
{
long supplierId = Convert.ToInt64(fldSupplierValue[0]);
string supplierNumber = fldSupplier.GetRefPropertyValue(fldSupplierValue, "FNumber").ToString();
string supplierName = fldSupplier.GetRefPropertyValue(fldSupplierValue, "FName").ToString();
}
// 单据体1的字段
DynamicObjectCollection entity1Rows = entity1.DynamicProperty.GetValue(billObj) as DynamicObjectCollection;
foreach (var entity1Row in entity1Rows)
{
// 内码
long entity1Id = Convert.ToInt64(entity1Row[0]);
// 物料:基础资料字段
DynamicObject fldMaterialValue = fldMaterial.DynamicProperty.GetValue(entity1Row) as DynamicObject;
if (fldMaterialValue != null)
{
long materialId = Convert.ToInt64(fldMaterialValue[0]);
string materialNumber = fldMaterial.GetRefPropertyValue(fldMaterialValue, "FNumber").ToString();
string materialName = fldMaterial.GetRefPropertyValue(fldMaterialValue, "FName").ToString();
}
// 单位:基础资料字段
DynamicObject fldUnitValue = fldUnit.DynamicProperty.GetValue(entity1Row) as DynamicObject;
if (fldUnitValue != null)
{
long unitId = Convert.ToInt64(fldUnitValue[0]);
string unitNumber = fldUnit.GetRefPropertyValue(fldUnitValue, "FNumber").ToString();
string unitName = fldUnit.GetRefPropertyValue(fldUnitValue, "FName").ToString();
}
// 数量
decimal fldQtyValue = Convert.ToDecimal(fldQty.DynamicProperty.GetValue(entity1Row));
}
// 单据体2的字段
DynamicObjectCollection entity2Rows = entity2.DynamicProperty.GetValue(billObj) as DynamicObjectCollection;
foreach (var entity2Row in entity2Rows)
{
// 内码
long entity1Id = Convert.ToInt64(entity2Row[0]);
// 文本
string fldTextValue = Convert.ToString(fldText.DynamicProperty.GetValue(entity2Row));
}
}
#endregion 需求场景三:示例结束
}
}
}
}
——转载自于金蝶社区大神--JohnnyDing
推荐阅读