本文介绍了一个使用“套打”表单插件实现当前业务对象套打的功能,重点解决了跨业务对象更新打印次数的问题。通过确保List
上一篇当前业务对象套打的例子【套打】表单插件实现当前业务对象套打
上一篇中提到的跨业务对象更新打印次数的问题,最后在检查了下,打印次数更新是以List<PrintJob>中第一个的业务对象标识进行对应业务对象更新,所以应该是支持到的,但是一定要确保整个打印任务中所有内码都为其业务对象标识的内码,否则会更新错误。
这一篇应该是问的比较多的跨业务对象套打的逻辑了,严格依照List<PrintJob>中业务对象标识唯一,内码对应,因此使用了本地打印队列实现,直接上示例:
上面代码中两个逻辑比较重要,生成打印任务队列,和队列打印,
<1>生成打印任务队列的逻辑,每个业务对象生成独自的打印任务,以确保取数、打印次数更新的逻辑正常。有人可能就问如果我把两个PrintJob对应的formId不一样放在一个List<PrintJob>里面可以吗,答案是不可以的。正如文章的开头,套打设计的就是按照List<PrintJob>第一个的业务对象标识作为整个打印任务的业务对象标识。
<2>打印完成逻辑,插件二开事件OnAfterPrint,打印机打印完成(当前批次数据单应完成,批次的理解参考【套打】批次打印页数与上限截断页数)后进行调用,这里简单判断了上一个打印有成功打印的数据后就开始下一个打印任务的打印,数据量少的时候可以这样简单判断;如果因为数据量过大导致的分批打印,那么应该判断上一个打印任务是否所有单据都打印完成。
这里强调下,我们B业务对象(这里指代在采购订单界面中打印的采购入库单),是无法调用其B表单插件的套打干预数据包事件,而会调用当前视图的表单插件的套打干预数据包事件OnPrepareNotePrintData,所以如果进行了干预数据包,建议严格判断套打模板标识,避免因为代码逻辑不严谨导致的B业务对象数据被错误修改。
/// <summary>
/// 默认打印机
/// </summary>
const string DefaultPrinter = "Doro PDF Writer";
/// <summary>
/// 采购订单的套打模板标识
/// </summary>
const string PurOrderTemplateId = "734cffd8-26ef-4131-b0c2-1d26b29d90c0";
/// <summary>
/// 采购入库单的套打模板标识
/// </summary>
const string STKInStockTemplateId = "bbfb0bd1-bd12-4681-9a02-00fb63caa1c6";
public override void BarItemClick(BarItemClickEventArgs e)
{
if (e.BarItemKey.EqualsIgnoreCase("CustomPrint"))
{
GeneratePrintJobsQueue();
}
}
/// <summary>
/// 打印队列
/// </summary>
Queue<List<PrintJob>> printJobsQueue = new Queue<List<Kingdee.BOS.Core.NotePrint.PrintJob>>();
/// <summary>
/// 生成打印任务队列
/// </summary>
private void GeneratePrintJobsQueue()
{
if (printJobsQueue.Count > 0)
{
this.View.ShowMessage("正在打印");
return;
}
///采购订单的打印任务
var purOrderIds = new List<string>() { "100765", "100760" };
var purOrderPrintJob = GeneratePrintJob(purOrderIds, PurOrderTemplateId, "PUR_PurchaseOrder");
printJobsQueue.Enqueue(purOrderPrintJob);
///采购入库单的打印任务
var inStockIds = new List<string>() { "100971", "100972", "100973", "100974", "100975" };
var stkInStockprintJobs = GeneratePrintJob(inStockIds, STKInStockTemplateId, "STK_InStock");
printJobsQueue.Enqueue(stkInStockprintJobs);
if (printJobsQueue.Count <= 0)
return;
var currPringJob = printJobsQueue.Dequeue();
string printJobsKey = Guid.NewGuid().ToString();
this.View.Session[printJobsKey] = currPringJob;
NoteTemplateOperation(printJobsKey, "print", DefaultPrinter);
}
public override void OnAfterPrint(AfterPrintEventArgs e)
{
base.OnAfterPrint(e);
if (e.NoteIDPairs.Count <= 0)//上一次打印中没有成功打印的数据,打印失败后续不打
{
printJobsQueue.Clear();
return;
}
if (printJobsQueue.Count <= 0)
return;
var currPringJob = printJobsQueue.Dequeue();//打印队列打印
string printJobsKey = Guid.NewGuid().ToString();
this.View.Session[printJobsKey] = currPringJob;
NoteTemplateOperation(printJobsKey, "print", DefaultPrinter);
}
/// <summary>
/// 生成打印任务(打印任务中所有子项formId唯一,如果需要不同业务对象应该创建不同的打印任务)
/// </summary>
private List<PrintJob> GeneratePrintJob(List<string> billIdList, string templateId, string formId)
{
List<PrintJob> result = new List<PrintJob>();
PrintJob printJob = new Kingdee.BOS.Core.NotePrint.PrintJob();
printJob.FormId = formId;
printJob.PrintJobItems = new List<Kingdee.BOS.Core.NotePrint.PrintJobItem>();
foreach (var billId in billIdList)
{
printJob.PrintJobItems.Add(new PrintJobItem(billId, templateId));
}
result.Add(printJob);
return result;
}
/// <summary>
/// 套打模板的执行操作
/// </summary>
/// <param name="printJobId">打印任务ID</param>
/// <param name="printType">打印类型</param>
/// <param name="printer">打印机ID</param>
public void NoteTemplateOperation(string printJobId, string printType, object printer = null)
{
JSONObject jsonObj = new JSONObject();
jsonObj.Put("pageID", this.View.PageId);
jsonObj.Put("printJobId", printJobId);
jsonObj.Put("action", printType);
string action = string.Empty;
if (printType.EqualsIgnoreCase("preview") || printType.EqualsIgnoreCase("PrintMergePreview"))
{
action = JSAction.printPreview;
}
else if (printType.EqualsIgnoreCase("print") || printType.EqualsIgnoreCase("PrintMerge"))
{
action = JSAction.print;
}
jsonObj.Put("printerAddress", printer.IsNullOrEmpty() == true ? string.Empty : Convert.ToString(printer));
this.View.AddAction(action, jsonObj);
}
using Kingdee.BOS.Core.Const; using Kingdee.BOS.Core.DynamicForm.PlugIn; using Kingdee.BOS.Core.DynamicForm.PlugIn.Args; using Kingdee.BOS.Core.NotePrint; using Kingdee.BOS.JSON; using Kingdee.BOS.Orm.DataEntity; using Kingdee.BOS.Util; using System; using System.Collections.Generic; namespace Test.BillPlugIn { class test : AbstractDynamicFormPlugIn { /// <summary> /// 默认打印机 /// </summary> const string DefaultPrinter = "Doro PDF Writer"; /// <summary> /// 采购订单的套打模板标识 /// </summary> const string PurOrderTemplateId = "734cffd8-26ef-4131-b0c2-1d26b29d90c0"; /// <summary> /// 采购入库单的套打模板标识 /// </summary> const string STKInStockTemplateId = "bbfb0bd1-bd12-4681-9a02-00fb63caa1c6"; public override void BarItemClick(BarItemClickEventArgs e) { if (e.BarItemKey.EqualsIgnoreCase("CustomPrint")) { GeneratePrintJobsQueue(); } } /// <summary> /// 打印队列 /// </summary> Queue<List<PrintJob>> printJobsQueue = new Queue<List<Kingdee.BOS.Core.NotePrint.PrintJob>>(); /// <summary> /// 生成打印任务队列 /// </summary> private void GeneratePrintJobsQueue() { if (printJobsQueue.Count > 0) { this.View.ShowMessage("正在打印"); return; } ///采购订单的打印任务 var purOrderIds = new List<string>() { "100765", "100760" }; var purOrderPrintJob = GeneratePrintJob(purOrderIds, PurOrderTemplateId, "PUR_PurchaseOrder"); printJobsQueue.Enqueue(purOrderPrintJob); ///采购入库单的打印任务 var inStockIds = new List<string>() { "100971", "100972", "100973", "100974", "100975" }; var stkInStockprintJobs = GeneratePrintJob(inStockIds, STKInStockTemplateId, "STK_InStock"); printJobsQueue.Enqueue(stkInStockprintJobs); if (printJobsQueue.Count <= 0) return; var currPringJob = printJobsQueue.Dequeue(); string printJobsKey = Guid.NewGuid().ToString(); this.View.Session[printJobsKey] = currPringJob; NoteTemplateOperation(printJobsKey, "print", DefaultPrinter); } public override void OnAfterPrint(AfterPrintEventArgs e) { base.OnAfterPrint(e); if (e.NoteIDPairs.Count <= 0)//上一次打印中没有成功打印的数据,打印失败后续不打 { printJobsQueue.Clear(); return; } if (printJobsQueue.Count <= 0) return; var currPringJob = printJobsQueue.Dequeue();//打印队列打印 string printJobsKey = Guid.NewGuid().ToString(); this.View.Session[printJobsKey] = currPringJob; NoteTemplateOperation(printJobsKey, "print", DefaultPrinter); } /// <summary> /// 生成打印任务(打印任务中所有子项formId唯一,如果需要不同业务对象应该创建不同的打印任务) /// </summary> private List<PrintJob> GeneratePrintJob(List<string> billIdList, string templateId, string formId) { List<PrintJob> result = new List<PrintJob>(); PrintJob printJob = new Kingdee.BOS.Core.NotePrint.PrintJob(); printJob.FormId = formId; printJob.PrintJobItems = new List<Kingdee.BOS.Core.NotePrint.PrintJobItem>(); foreach (var billId in billIdList) { printJob.PrintJobItems.Add(new PrintJobItem(billId, templateId)); } result.Add(printJob); return result; } /// <summary> /// 套打模板的执行操作 /// </summary> /// <param name="printJobId">打印任务ID</param> /// <param name="printType">打印类型</param> /// <param name="printer">打印机ID</param> public void NoteTemplateOperation(string printJobId, string printType, object printer = null) { JSONObject jsonObj = new JSONObject(); jsonObj.Put("pageID", this.View.PageId); jsonObj.Put("printJobId", printJobId); jsonObj.Put("action", printType); string action = string.Empty; if (printType.EqualsIgnoreCase("preview") || printType.EqualsIgnoreCase("PrintMergePreview")) { action = JSAction.printPreview; } else if (printType.EqualsIgnoreCase("print") || printType.EqualsIgnoreCase("PrintMerge")) { action = JSAction.print; } jsonObj.Put("printerAddress", printer.IsNullOrEmpty() == true ? string.Empty : Convert.ToString(printer)); this.View.AddAction(action, jsonObj); } public override void OnPrepareNotePrintData(PreparePrintDataEventArgs e) { base.OnPrepareNotePrintData(e); if (e.DataSourceId.EqualsIgnoreCase("FPOOrderEntry")) { if (e.DataObjects.Length <= 0) return; var dataObj = e.DataObjects[0]; List<DynamicObject> listData = new List<DynamicObject>(); for (int i = 0; i < 2000; ++i) { listData.Add(dataObj); } e.DataObjects = listData.ToArray(); } } } }
推荐阅读