知識共享 - 單據數據包DynamicObject的結構及操作

1. 背景介紹:
需要在插件代碼中,對單據的數據包直接進行操作,如果對單據數據包不瞭解,則非常難以入手。

本帖將嘗試去介紹下單據數據包DynamicObject的結構特點,同時介紹如何進行操作,如何給字段取值、賦值;

本帖依賴的單據,包含兩個單據體,各有若干個字段,單據設計界面如下:

20160128 00單據數據包介紹04.png

2. 結構說明:
單據數據包DynamicObject,相當於一個有層次結構的數據字典
第一層包含全部的單據頭字段以及單據體行集合;
單據體數據行集合、基礎資料字段,則需要通過第二層的DynamicObject來展示。

基本特徵:
1. 包含了全部單據頭字段值
2. 包含了單據體行集合對象
3. 字段通過Key + Value,形成一個鍵值對,佔據DynamicObject的一個節點
4. 字段在數據包中的Key,使用的是字段的屬性名
5. 基礎資料字段的值,也是一個DynamicObject對象,其中嵌套包含了各個引用屬性的值

3. 結構截圖
可以在插件中,需要操作單據數據包的,進入調試狀態,查看單據數據包:

圖一:單據頭數據結構

20160128 00單據數據包介紹01.png

圖二:單據體行集合,DynamicObjectCollection類型

20160128 00單據數據包介紹02.png

圖三:單據體行,又是一個DynamicObject

20160128 00單據數據包介紹03.png

圖四:使用字段的屬性名作爲數據包中的字段Key

20160128 00單據數據包介紹05.png

4. 操作示例:

//******************************************************
方法一:直接操作DynamicObject

// 假設billObj是單據的數據包
DynamicObject billObj = this.Model.DataObject;


// 讀取單據內碼
long billId = Convert.ToInt64(billObj[0]);
// 普通文本字段(讀取 + 設置)
string fldBillNoValue = Convert.ToString(billObj["BillNo"]);
billObj["BillNo"] =  fldBillNoValue ;
// 日期字段(讀取 + 設置)
DateTime fldDateValue = Convert.ToDateTime(billObj["F_JD_Date"]);
billObj["F_JD_Date"] = fldDateValue;
// 基礎資料字段(讀取 + 設置)
DynamicObject fldSupplierValue = billObj["F_JD_Supplier"] as DynamicObject;
billObj["F_JD_Supplier"]  = fldSupplierValue ;
if (fldSupplierValue != null)
{
    billObj["F_JD_Supplier_Id"]  = Convert.ToInt64(fldSupplierValue[0]);
  long supplierId = Convert.ToInt64(fldSupplierValue[0]);
  string supplierNumber = fldSupplierValue["Number"].ToString();
  string supplierName = fldSupplierValue["Name"].ToString();
}



// 單據體(單據體行集合屬性本身只讀,可以通過單據體集合提供的方法,添加行、刪除行)
DynamicObjectCollection entityRows = billObj["FEntity"] as DynamicObjectCollection;
foreach (var entityRow in entity1Rows)
{
    // 內碼
    long entityId = Convert.ToInt64(entityRow[0]);

    // 數量 (讀取 + 設置)
    decimal fldQtyValue = Convert.ToDecimal(entityRow["F_JD_Qty"]);

    entityRow["F_JD_Qty"] = fldQtyValue;
}


//*******************************************************
方法二:通過字段的元數據,操作DynamicObject(推薦)


// 假設billObj是單據的數據包
DynamicObject billObj = this.Model.DataObject;


// 首先獲取各種元素的元數據
Field fldBillNo = this.View.BillBusinessInfo.GetField("FBillNo");
Field fldDate = this.View.BillBusinessInfo.GetField("F_JD_Date");
BaseDataField fldSupplier = this.View.BillBusinessInfo.GetField("F_JD_Supplier") as BaseDataField;
BaseDataField fldMaterial = this.View.BillBusinessInfo.GetField("F_JD_FMaterialId") as BaseDataField;
Field fldQty = this.View.BillBusinessInfo.GetField("F_JD_Qty");

Entity entity = this.ListView.BillBusinessInfo.GetEntity("FEntity");

// 讀取單據內碼
long billId = Convert.ToInt64(billObj[0]);
//單據編號
string billNo = Convert.ToDateTime(fldBillNo.DynamicProperty.GetValue(billObj));
fldBillNo.DynamicProperty.SetValue(billObj, billNo));
// 日期
DateTime fldDateValue = Convert.ToDateTime(fldDate.DynamicProperty.GetValue(billObj));
fldDate.DynamicProperty.SetValue(billObj, fldDateValue)
// 供應商:基礎資料字段
DynamicObject fldSupplierValue = fldSupplier.DynamicProperty.GetValue(billObj) as DynamicObject;

// 設置供應商基礎字段值
DynamicObject[] supplierObjs = Kingdee.BOS.ServiceHelper.BusinessDataServiceHelper.LoadFromCache(
                                         this.Context,
                                         new object[] { fldSupplierValue[0] },
                                         fldSupplier.RefFormDynamicObjectType);

fldSupplier.RefIDDynamicProperty.SetValue(billObj, supplierObjs[0][0]);
fldSupplier.DynamicProperty.SetValue(billObj, supplierObjs[0]);

// 基礎資料屬性值
if (fldSupplierValue != null)
{
    long supplierId = Convert.ToInt64(fldSupplierValue[0]);
    string supplierNumber = fldSupplier.GetRefPropertyValue(fldSupplierValue, "FNumber").ToString();
    string supplierName = fldSupplier.GetRefPropertyValue(fldSupplierValue, "FName").ToString();
}

// 單據體的字段
DynamicObjectCollection entityRows = entity.DynamicProperty.GetValue(billObj) as DynamicObjectCollection;
foreach (var entityRow in entityRows)
{
    // 內碼
    long entityId = Convert.ToInt64(entity1Row[0]);
    // 物料:基礎資料字段
    DynamicObject fldMaterialValue = fldMaterial.DynamicProperty.GetValue(entityRow) 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();
    }
    // 數量
    decimal fldQtyValue = Convert.ToDecimal(fldQty.DynamicProperty.GetValue(entityRow));
    fldQty.DynamicProperty.SetValue(entityRow, fldQtyValue);
}

// 給單據體添加新行
DynamicObject newRow = new DynamicObject(entity.DynamicObjectType);
entityRows.Add(newRow);




發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章