本文档主要介绍了在金蝶BOS平台下,进行插件开发时常用的操作方法和代码示例。包括插件继承体系及基类命名参考、单据相关信息的获取(如行号、单据体集合、选中行数据等)、界面数据保存、字符串检验、弹出提示框、通过主键获取对象、表单字段值的获取与设置、数据库执行语句、单据体行的增删、界面刷新、单元格锁定、服务端操作、焦点行设置与获取、单据体每行数据包的获取、异常抛出、仓位赋值、字符串与集合去重、打开窗口状态、列表插件获取单据体以及获取当前时间并判断星期几等。这些操作覆盖了插件开发中常见的数据处理、界面交互、业务逻辑处理等多个方面。
1:插件继承体系
领域类型 | 基类 | 命名参考 |
动态表单 – 维护 | AbstractDynamicFormPlugIn | ExpressionEdit |
业务单据 – 维护 | AbstractBillPlugIn | PurchaseOrderEdit |
业务单据 – 列表 | AbstractListPlugIn | PurchaseOrderList |
基础资料 – 维护 | AbstractBasePlugIn | MaterialEdit |
基础资料 – 列表 | AbstractListPlugIn | MaterialList |
2:获取单据相关信息
(1)获取当前行号
int rowIndex = this.Model.GetEntryCurrentRowIndex("FEntity"); FEntity为单据体标识
(2)获取单据体集合
Entity entity = this.View.BillBusinessInfo.GetEntity("FSaleOrderEntry"); [size=12.6667px]FSaleOrderEntry为单据体标识
DynamicObjectCollection entrys = this.Model.GetEntityDataObject(entity);
(3)操作插件获取选中行
[size=12.6667px] // 扩展方法,需要 using Kingdee.BOS.Core.DynamicForm
var selectedRows = this.Option.GetBillOperationSelectedRows();
(4)获取选中行
int[] selectedIndexsR = this.View.GetControl<EntryGrid>("FSaleOrderEntry").GetSelectedRows();
(5)获取选中行数据
//当前选中行行号
int[] selectedIndexsR = this.View.GetControl<EntryGrid>("FSaleOrderEntry").GetSelectedRows();
//单据体数据
DynamicObjectCollection selectedRowsDy = this.Model.DataObject["SaleOrderEntry"] as DynamicObjectCollection;
//选中行数据
DynamicObject selectedRow = selectedRowsDy[selectedIndexsR[0]];
(6)获取单据内码
long billNo = Convert.ToInt64(this.View.Model.GetPKValue());//获取当前单据编号内码
(7)获取单据分录内码
int row = this.Model.GetEntryCurrentRowIndex("FSubEntity");
Entity entiry = this.View.Model.BillBusinessInfo.GetEntity("FSubEntity");
object pkValue = this.View.Model.GetEntryPKValue(entiry.Key, row);
(8)在单据列表界面,使用如下语句获取当前选择行的单据内码
this.ListView.CurrentSelectedRowInfo.PrimaryKeyValue
3:当前界面的数据保存,直接调用保存操作
Kingdee.BOS.ServiceHelper.BusinessDataServiceHelper.Save(this.Context, this.View.BusinessInfo, this.View.Model.DataObject);
4:检验字符串函数
ObjectUtils.IsNullOrEmptyOrWhiteSpace
IsNullOrWhiteSpace
5:利用代码弹出提示框
this.View.ShowMessage("详细信息", MessageBoxOptions.YesNo, ret =>
{
if (ret == MessageBoxResult.No)
{
e.Cancel = true;
return;
}
}, string.Format("物料编码为:{0}的物料存在不同的报关项号,是否继续?", string.Join(",", list)), MessageBoxType.Notice);
6:通过主键获取对象
FormMetadata formMetadata = MetaDataServiceHelper.Load(this.Context, "BD_MATERIAL") as FormMetadata;
DynamicObject dynamicObject = BusinessDataServiceHelper.LoadSingle(
this.Context,
id,
formMetadata .BusinessInfo.GetDynamicObjectType());
7:获取表单字段的值
DynamicObject FMaterial = (DynamicObject)this.View.Model.GetValue("FMaterialId");[size=12.6667px]FMaterialId为字段标识,本字段是一个基础资料字段
DynamicObject FMaterial = (DynamicObject)this.View.Model.GetValue("FMaterialId",e.Row);e.ROW为单据体行号
8:设置表单字段的值
1:给分录赋值
this.View.Model.SetValue("FEntryNote", FNOTE,rowIndex);
2:给单据头赋值
this.View.Model.SetValue("FEntryNote",FNOTE);
9:数据库执行语句
DBUtils.Execute(this.Context, updateSql);
var mas = DBUtils.ExecuteDynamicObject(this.Context, sql);
10:新增、删除单据体行
this.View.Model.DeleteEntryRow("FSONEntity", i);
this.View.Model.CreateNewEntryRow("FSONEntity");
11:刷新操作
this.View.UpdateView("FieldKey")刷新某个字段
this.View.InvokeFormOperation(Kingdee.BOS.Core.DynamicForm.FormOperationEnum.Refresh)//刷新整单
12:代码锁定单元格
this.View.GetFieldEditor("FBaseUnitId", 0).SetEnabled("", false);
this.View.GetFieldEditor("FBarCodeAmount", 0).Enabled = false;
13:常用服务端操作
https://vip.kingdee.com/article/285118203137744695
14:设置焦点行、获取焦点行
//设置父窗体的焦点行
this.View.ParentFormView.SetEntityFocusRow("FEntity", selectedIndexsR[0]);
//获取当前分录焦点行FocusRow
EntryGrid entryGrid = this.View.GetControl<EntryGrid>("FEntity");
int rowIndex = entryGrid.GetFocusRowIndex();
15:获取单据体每一行的数据包
//获取父窗
体选中行
int[] selectedIndexsR = this.View.ParentFormView.GetControl<EntryGrid>("FEntity").GetSelectedRows();
Entity entity = this.View.ParentFormView.Model.BillBusinessInfo.GetEntity("FEntity");
foreach (int i in selectedIndexsR)
{
DynamicObject rowObj = this.View.ParentFormView.Model.GetEntityDataObject(entity, i) as DynamicObject;
}
16:抛出异常
throw new KDException("", "已经产生条码不能反审核");
17:仓位赋值
(1)表单插件
//获取组织
string OrGID = Convert.ToString(this.Context.CurrentOrganizationInfo.ID);
RelatedFlexGroupField stockLocFld = this.View.BillBusinessInfo.GetField("FStockLocID") as RelatedFlexGroupField;
if ("100498".Equals(OrGID))
{
FSTOCKID = "1317258";
stockLocld = 104431;
}
//仓库仓位赋值
this.View.Model.SetValue("FStockID", FSTOCKID, i);
DynamicObject[] stockLoc = BusinessDataServiceHelper.LoadFromCache(this.Context, new object[] { stockLocld }, stockLocFld.RefFormDynamicObjectType);
stockLocFld.DynamicProperty.SetValue(obj, stockLoc[0]);
stockLocFld.RefIDDynamicProperty.SetValue(obj, stockLocld);
this.View.UpdateView("FStockLocID", i);
(2)操作插件、列表插件
直接后台更新仓位ID
18:字符串、集合去重
BillNo = String.Join(";", BillNo.Split(';').Distinct());
PrimaryKeyValues.Distinct()
19:代码打开窗口状态
BillShowParameter showParam = new BillShowParameter();
showParam.FormId = "SAL_SaleOrder";
showParam.PageId = Guid.NewGuid().ToString();
showParam.OpenStyle.ShowType = ShowType.MainNewTabPage;
20:列表插件获取单据体
ListSelectedRowCollection rows = this.ListView.SelectedRowsInfo;
List<object> pkIds = new List<object>();
foreach (var row in rows)
{
pkIds.Add(row.PrimaryKeyValue);
}
FormMetadata metadata = MetaDataServiceHelper.Load(this.Context, "SAL_DELIVERYNOTICE") as FormMetadata;
DynamicObject[] dynamicObjects = BusinessDataServiceHelper.Load(this.Context, pkIds.ToArray(), metadata.BusinessInfo.GetDynamicObjectType());
21:获取当前时间,确定当前时间是周几
// 获取当前时间
DateTime dateTime=Kingdee.BOS.ServiceHelper.TimeServiceHelper.GetSystemDateTime(this.Context);
//获取单据日期时间
string times = (string)Convert.ToDateTime(this.View.Model.GetValue("FDate")).ToString("yyyy-MM-dd", DateTimeFormatInfo.InvariantInfo);
//获取当前日期是星期几
string Text = System.Globalization.CultureInfo.CurrentCulture.DateTimeFormat.GetDayName(DateTime.Now.DayOfWeek);
DateTime time=Convert.ToDateTime((times+" 17:30 "));
if (dateTime>= time)//超过五点半
{
if ("星期六".Equals(Text))
{
this.View.Model.SetValue("FDate", Convert.ToDateTime(this.View.Model.GetValue("FDate")).AddDays(2));
}
else
{
this.View.ShowMessage("您做单的时间超过下午五点半了,保存后系统将把做单日期往后推一天");
this.View.Model.SetValue("FDate", Convert.ToDateTime(this.View.Model.GetValue("FDate")).AddDays(1));
}
}
22:获取当前用户信息
?this.Context.UserId
16394
?this.Context.UserName
"Administrator"
?this.Context.UserToken
"a9cd8152-8923-487d-bf91-77297bfd8fde"
?this.Context.LoginName
"Administrator"
?this.Context.CustomName
"金蝶软件"
?this.Context.ComputerName
"SmartSoftCs"
?this.Context.ClientInfo.IpAddress
"192.168.0.105"
?this.Context.ClientInfo.MacAddress
"00:24:2B:59:EE:C5"
?this.Context.ClientInfo.Version
?this.Context.ClientInfo.ClientType
23:辅助属性相关表
select * from T_BD_FLEXSITEMDETAILV where fid in ('102122','100149')
select * from T_BD_FLEXAUXPROPERTY
select * from T_BD_FLEXAUXPROPERTY_L
SELECT * FROM T_BD_MaterialAuxPty
--辅助资料
select * FROM T_BAS_ASSISTANTDATA
select * from T_BAS_ASSISTANTDATAENTRY
24:操作插件新增分录
(1)获取分录集合
DynamicObjectCollection entrysHZ = (DynamicObjectCollection)Entity["FHZEntity"];
(2)分录集合增加一个行数据包
1)如果分录一行都没有
DynamicObject obj = new DynamicObject(entrysHZ.DynamicCollectionItemPropertyType);
2)如果分录存在单据行
using Kingdee.BOS.Orm;
DynamicObject cloneEntity=obj.Clone(false, true) as DynamicObject;
(3)调用单据保存方法
25:分录保存主键重复问题解决
DynamicObject obj = new DynamicObject(entrysHZ.DynamicCollectionItemPropertyType);
var MaxFEntryId = Kingdee.BOS.ServiceHelper.DBServiceHelper.GetSequenceInt64(this.Context, "Z_PRD_PICKMTRLDATAHZ", 1);
obj["id"] = Convert.ToString(MaxFEntryId.ElementAt(0)) ;
26:数据库日期转换及C#代码日期转换
C#时间转换:string times = (string)Convert.ToDateTime(dyFilter["FDateYM"]).ToString("yyyy-MM-dd", DateTimeFormatInfo.InvariantInfo);
数据库日期:
select CONVERT(datetime2,'2019-04-01', 23)
select CONVERT(varchar(100), GETDATE(), 23)
select CONVERT(date, GETDATE(), 23)
27:数据库去掉尾零及C#代码去掉尾零
数据库:cast(BARCODEMAIN.FENTITY_LENGTH as real)
c#:Convert.ToDecimal(obj["FBARVOLUMENUMBER"]).ToString("0.###")
28:修改表单只读属性
SELECT FPACKAGEID, * FROM T_META_OBJECTTYPE WHERE FID='...' ...表示该表单的标识
当FPACKAGEID=null时表示该表单不是只读
推荐阅读