我的目的是让用户点击一个MenuItem,然后弹出如下窗口。
当用户输入客户账户和报关单号,点击确认后,系统会自动插入一条报关单记录。我们需要写这样一个类:
/***************************************************
(C) Copyright VYA All rights reserved.
Created Date:2019-09-16
Created By:Grady
Description of Behavior:批处理Demo
***************************************************/
public class VYA_Grady_BuildCustomsOrder extends RunBaseBatch
{
dialog dialog;
DialogField dialogCustAccount;
DialogField dialogCustomsOrderNum;
CustAccount custAccount;
VYA_CustomsOrderNum customsOrderNum;
VYA_CustomsOrderTable customsOrderTable;
CustTable custTable;
#define.CurrentVersion(1)
#LOCALMACRO.CurrentList
custAccount,
customsOrderNum
#ENDMACRO
Object dialog()
{
;
dialog = super();
dialogCustAccount = dialog.addFieldValue(extendedTypeStr(CustAccount),custAccount,"客户账户");
dialogCustomsOrderNum = dialog.addFieldValue(extendedTypeStr(VYA_CustomsOrderNum),customsOrderNum,"报关单号");
return dialog;
}
boolean getFromDialog()
{
boolean ret;
;
ret = super();
custAccount = dialogCustAccount.value();
customsOrderNum = dialogCustomsOrderNum.value();
return ret;
}
public void initParmDefault()//初始化参数
{
super();
}
public container pack()
{
return [#CurrentVersion, #CurrentList];
}
public boolean unpack(container packedClass)
{
Integer version = RunBase::getVersion(packedClass);
switch (version)
{
case #CurrentVersion:
[version, #CurrentList] = packedClass;
break;
default :
return false;
}
return true;
}
void run()
{
#OCCRetryCount
int retryCount;
;
if (!this.validate())
{
throw error("校验失败。");
}
try
{
ttsbegin;
select firstFast custTable
where custTable.AccountNum == custAccount;
customsOrderTable.clear();
customsOrderTable.ItemId = custTable.VYA_DefaultItemId;
customsOrderTable.AccountNum = custAccount;
customsOrderTable.CustomsOrderNum = customsOrderNum;
customsOrderTable.insert();
info("插入成功");
ttscommit;
}
catch (Exception::Deadlock)
{
ttsAbort;
if (retryCount < #RetryNum)
{
retryCount++;
retry;
}
}
catch (Exception::UpdateConflict)
{
ttsAbort;
if (appl.ttsLevel() == 0)
{
if (xSession::currentRetryCount() >= #RetryNum)
{
throw Exception::UpdateConflictNotRecovered;
}
else
{
retry;
}
}
else
{
throw Exception::UpdateConflict;
}
}
}
boolean validate(Object _calledFrom = null)//校验
{
boolean ret =true;
;
return ret;
}
static VYA_Grady_BuildCustomsOrder construct()
{
return new VYA_Grady_BuildCustomsOrder();
}
static public ClassDescription description()//批处理名称
{
return "插入一条报关单记录。";
}
static void main(Args _args)//程序主入口
{
VYA_Grady_BuildCustomsOrder runBaseBatchDemo;
#OCCRetryCount
setprefix(VYA_Grady_BuildCustomsOrder::description());
try
{
runBaseBatchDemo = new VYA_Grady_BuildCustomsOrder();
if (runBaseBatchDemo.prompt())
{
runBaseBatchDemo.run();
}
}
catch (Exception::Deadlock)
{
ttsAbort;
retry;
}
catch (Exception::UpdateConflict)
{
ttsAbort;
if (appl.ttsLevel() == 0)
{
if (xSession::currentRetryCount() >= #RetryNum)
{
throw Exception::UpdateConflictNotRecovered;
}
else
{
retry;
}
}
else
{
throw Exception::UpdateConflict;
}
}
}
}
然后把这个类做成菜单就行啦。
这里用到了一个小技巧,那就是通过序列化保存用户上次输入的值。
原理是将
#LOCALMACRO.CurrentList
custAccount,
customsOrderNum
#ENDMACRO
这里的值将被序列化,然后存入数据库。
pack是序列化的过程,unpack是反序列化的过程。
要将序列化后的值存入数据库,有两种办法:
1、类继承runbase类,然后用saveLast()和getLast()
2、未继承此类的直接调用xSysLastValue的两个静态方法saveLast()和getLast()