
【场景】自定义套打导出次数
【案例仿照】[套打.二开案例.自定义记录打印次数](https://vip.kingdee.com/article/378549008505683712?productLineId=1)
【案例】采购合同,列表和单据,记录导出次数
<0>对象、单据、模板打印次数表
```sql
--ksql
/****** 对象、单据、模板打印次数记录表 ******/
/****** 表结构 ******/
IF NOT EXISTS (SELECT 1 FROM KSQL_USERTABLES WHERE KSQL_TABNAME = 'T_Test_NotePrintRecord')
CREATE TABLE T_Test_NotePrintRecord
(
FID VARCHAR(36) DEFAULT ' ' NOT NULL,
FOBJECTID VARCHAR(50) DEFAULT ' ' NOT NULL,
FINTERID VARCHAR(50) DEFAULT ' ' NOT NULL,
FTEMPTELAEID NVARCHAR(50) DEFAULT ' ' NOT NULL,
FCOUNT INT DEFAULT 0 NOT NULL
);
/****** 索引 ******/
IF NOT EXISTS (SELECT 1 FROM KSQL_INDEXES WHERE KSQL_INDNAME = 'IDX_Test_NotePrintRecord')
CREATE INDEX IDX_Test_NotePrintRecord ON T_Test_NotePrintRecord ( FOBJECTID, FINTERID, FTEMPTELAEID);
/****** 主键 ******/
IF EXISTS (SELECT 1 FROM KSQL_USERTABLES WHERE KSQL_TABNAME = 'T_Test_NotePrintRecord')
EXEC p_AlterPK 'PK_Test_NotePrintRecord', 'T_Test_NotePrintRecord', 'FID', '1';
/****** 锁记录 ******/
INSERT T_Test_NotePrintRecord(FID) Values('LOCK')
```
<1>表单插件、列表插件

```csharp
using Kingdee.BOS;
using Kingdee.BOS.App.Data;
using Kingdee.BOS.Core.Bill.PlugIn;
using Kingdee.BOS.Core.DynamicForm.PlugIn;
using Kingdee.BOS.Core.DynamicForm.PlugIn.Args;
using Kingdee.BOS.Core.List.PlugIn;
using Kingdee.BOS.Core.Metadata;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Transactions;
namespace DynamicFormPlugIn.NotePrint
{
public class NoteExportRecordHandler
{
private readonly AbstractDynamicFormPlugIn PlugIn;
public NoteExportRecordHandler(AbstractDynamicFormPlugIn plugIn)
{
PlugIn = plugIn;
}
private bool IsExportOp;
public void SetIsExportOp(bool isExportOp)
{
IsExportOp = isExportOp;
}
public void Execute(BeforePrintExportEventArgs e)
{
//<0> 仅针对套打导出操作,因为发送邮件的套打附件和工作流的套打附件,均不是以操作的实现调用
if (!IsExportOp)
return;
if(e.ExportInfo == null)
{
return;
}
//<1>获取本次操作 模板和单据的导出次数
var templateBillCntMap = GetPrintBillSet(e, PlugIn.View.BillBusinessInfo);
if (!UpdatePrintCount(templateBillCntMap))
{
//取消操作的逻辑,将导出任务清空
PlugIn.View.ShowMessage("更新失败");
e.ExportInfo = null;
}
//<2>如果需要控制次数,可针对不满足的取消打印
//取消操作的逻辑,将导出任务清空
//PlugIn.View.ShowMessage("超出控制次数");
//e.ExportInfo = null;
}
/// <summary>
/// 获取 模板、单据内码的打印次数
/// </summary>
/// <param name="e"></param>
/// <param name="billBusinessInfo"></param>
/// <returns></returns>
private static Dictionary<string, Dictionary<string, int>> GetPrintBillSet(BeforePrintExportEventArgs e, BusinessInfo billBusinessInfo)
{
string curFormId = billBusinessInfo.GetForm().Id;
Dictionary<string, Dictionary<string, int>> templateBillCntMap = new Dictionary<string, Dictionary<string, int>>();
if(e.ExportInfo.ExportItems != null && e.ExportInfo.ExportItems.Count >0)
{
//套打高级设置
foreach (var pExportInfo in e.ExportInfo.ExportItems)
{
if (pExportInfo == null || pExportInfo.BillIds == null)
continue;
foreach (var billId in pExportInfo.BillIds)
{
if (billId == null)
continue;
//任务中包含单据内码和模板ID,这里只需要记录单据内码
if (!templateBillCntMap.ContainsKey(pExportInfo.TemplateId))
{
templateBillCntMap[pExportInfo.TemplateId] = new Dictionary<string, int>();
}
if (!templateBillCntMap[pExportInfo.TemplateId].ContainsKey(billId))
{
templateBillCntMap[pExportInfo.TemplateId][billId] = 1;
}
else
{
templateBillCntMap[pExportInfo.TemplateId][billId] += 1;
}
}
}
return templateBillCntMap;
}
if (e.ExportInfo.BillIds != null)
{
if(e.ExportInfo.OperationType == Kingdee.BOS.Core.NotePrint.PrintOperationType.SelectTempate)
{
//套打选择模板配对
for (int i = 0; i < e.ExportInfo.BillIds.Count; ++i)
{
string billId = e.ExportInfo.BillIds[i];
for (int j = 0; j < e.ExportInfo.TemplateIds.Count; ++j)
{
string templateId = e.ExportInfo.TemplateIds.Count > i ? e.ExportInfo.TemplateIds[j] : null;
if (billId == null || templateId == null)
continue;
//任务中包含单据内码和模板ID,这里只需要记录单据内码
if (!templateBillCntMap.ContainsKey(templateId))
{
templateBillCntMap[templateId] = new Dictionary<string, int>();
}
if (!templateBillCntMap[templateId].ContainsKey(billId))
{
templateBillCntMap[templateId][billId] = 1;
}
else
{
templateBillCntMap[templateId][billId] += 1;
}
}
}
}
else
{
//套打普通映射
for (int i = 0; i < e.ExportInfo.BillIds.Count; ++i)
{
string billId = e.ExportInfo.BillIds[i];
string templateId = e.ExportInfo.TemplateIds.Count > i ? e.ExportInfo.TemplateIds[i] : null;
if (billId == null || templateId == null)
continue;
//任务中包含单据内码和模板ID,这里只需要记录单据内码
if (!templateBillCntMap.ContainsKey(templateId))
{
templateBillCntMap[templateId] = new Dictionary<string, int>();
}
if (!templateBillCntMap