当前位置: 代码迷 >> 综合 >> D365 批处理 多任务批处理 方法1
  详细解决方案

D365 批处理 多任务批处理 方法1

热度:60   发布时间:2023-10-25 09:12:55.0

多任务批处理需要5个类:

1.Contract      新建参数

2.Task             任务逻辑

3.Service        调用 Task

4.Controller    调用Service

5.Scheduler    调用 controller 并传参

1.Contract

[DataContractAttribute]
class ContractClass implements SysPackable
{str   departCode;#define.CurrentVersion(1)#localmacro.CurrentListdepartCode#endmacropublic container pack(){return [#currentVersion, #currentList];}public boolean unpack(container _packedClass){int         version = RunBase::getVersion(_packedClass);boolean     result  = false;switch (version){case #CurrentVersion:[version, #CurrentList] = _packedClass;result = true;break;default :result = false;}return result;}[DataMemberAttribute, SysOperationControlVisibilityAttribute(false)]public str parmDepartCode(str _DepartCode = departCode){departCode = _DepartCode;return departCode;}

2.Task

class Task extends RunBaseBatch
{str  DepartCode;#define.CurrentVersion(2)#define.Version1(2)public boolean canGoBatchJournal(){return true;}public boolean init(){return true;}protected void new(){super();}public container pack(){return [#CurrentVersion];}public void run(){#OCCRetryCountif (! this.validate())throw error("");try{ttsbegin;..//需要增加的逻辑ttscommit;}catch{retry;}}public boolean runsImpersonated(){return true;}public boolean unpack(container packedClass){Version version = RunBase::getVersion(packedClass);;switch (version){case #CurrentVersion:[version] = packedClass;break;default:return false;}return true;}public boolean validate(Object _calledFrom = null){if (false)return checkFailed("");return true;}server static Task construct(){return new Task();}static ClassDescription description(){return "标题";}static void main(Args args){}protected boolean canRunInNewSession(){return false;}public static Task newTask(Contract dataContract){Task task;task = Task::construct();task.parmDepartCode(dataContract.parmDepartCode());task.runOperation();return task;}public str parmdepartCode(str _DepartCode = DepartCode){DepartCode = _DepartCode;return DepartCode;}}

3.Service

class Service  extends SysOperationServiceBase
{[SysEntryPointAttribute]public void startOperation(Contract _dataContract){Task::newTask(_dataContract);}}

4.Controller

class Cotnroller extends SysOperationServiceController
{str     taskCaption;public void new(){super();this.parmClassName(classStr(Service));this.parmMethodName(methodStr(Service, startOperation));this.parmDialogCaption("标题");}public str parmCaption(str _caption = taskCaption){taskCaption = _caption;return taskCaption;}public ClassDescription caption(){if (taskCaption)return taskCaption;elsereturn "标题";}}

5.Scheduler

class Scheduler extends SysOperationServiceController
{str                             depart,userId;public ClassDescription caption(){return '批处理详细信息';}public static void main(Args args){DialogField                  dialogDepart;FormBuildStringControl       depCtrl;Scheduler pro = new Scheduler();Dialog dialog = new Dialog("标题");dialogDepart    = dialog.addFieldValue(extendedTypeStr(Str4095),'101','部门');depCtrl = dialogDepart.control();depCtrl.limitText(10000);depCtrl.replaceOnLookup(false);depCtrl.registerOverrideMethod(methodStr(FormStringControl, lookup), methodStr(Scheduler, lookupDepart),pro);if(dialog.run()){pro.depart             = dialogDepart.value();pro.userId             = dialogUserId.value();pro.run();}}protected void lookupDepart(FormStringControl _control){Query query = new Query();QueryBuildDataSource  queryBuildDataSource;SysTableLookup sysTableLookup = SysTableLookup::newParameters(tableNum(DepartTable), _control);sysTableLookUp.addLookupfield(fieldNum(DepartTable, DepartCode));sysTableLookUp.addLookupfield(fieldNum(DepartTable, DepartName));queryBuildDataSource = query.addDataSource(tableNum(DepartTable));sysTableLookup.parmQuery(query);sysTableLookup.performFormLookup();}/// <summary>////// </summary>public void run(){BatchHeader                                 batchHeader;Controller    controller;Contract      dataContract;Query                                       q = new Query();QueryBuildDataSource                        qbds,qbdsTable;QueryRun                                    qr;SalesLine                                   salesLine;Set             set         = new Set(Types::Class);SetEnumerator   se;Batchable       batchTask;int             i = 0;;str jobCaption = '标题';qbds = q.addDataSource(tableNum(TestTable));qr = new QueryRun(q);while(qr.next()){salesLine = qr.get(tableNum(SalesLine));if(!batchHeader){batchHeader = BatchHeader::construct();}controller = new Controller();controller.parmCaption(strFmt('批处理任务描述'));dataContract = controller.getDataContractObject();dataContract.parmDepartCode('101');batchHeader.addTask(controller);set.add(controller);i++;}if(i) //此处一般不需要写,作用:当所有任务跑完后才运行下面的这个任务{controller = new Controller();controller.parmCaption('汇总:Test');dataContract = controller.getDataContractObject();dataContract.parmDepartCode('101');batchHeader.addTask(controller);se = set.getEnumerator();while (se.moveNext()){batchTask = se.current(); // Task to make dependent on
batchHeader.addDependency(controller,batchTask,BatchDependencyStatus::FinishedOrError);}if(batchHeader){// save the batchheader if it has been createdbatchHeader.parmCaption(jobCaption);batchHeader.save();info(strFmt("已生成批处理'));}}elseerror("未查询到满足条件的数据!");}}