套打.二开案例.自定义记录打印次数
【场景】自定义记录打印次数
【案例】采购订单,列表和单据,记录打印次数
<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>表单插件、列表插件
![`E7G4ZMFRPLEGAYK0.webp](/download/0100da2bafc0805841269bf8fc6b425792da.webp)
```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.Transactions;
namespace DynamicFormPlugIn.NotePrint
{
public class NotePrintRecordHandler
{
private readonly AbstractDynamicFormPlugIn PlugIn;
public NotePrintRecordHandler(AbstractDynamicFormPlugIn plugIn)
{
PlugIn = plugIn;
}
public void Execute(BeforeNotePrintEventArgs e)
{
//<0>把预览界面的打印取消,预览界面的打印目前无法插件干预
e.ShowPreviewButton = false;
//<1> 仅针对打印操作记录
if (!IsPrintOp(e.PrintType))
return;
var templateBillCntMap = GetPrintBillSet(e, PlugIn.View.BillBusinessInfo);
if(!UpdatePrintCount(templateBillCntMap))
{
e.Cancel = true;
}
//<2>如果需要控制次数,可针对不满足的取消打印
//e.Cancel = true;
}
///
/// 是否打印操作
///
/// <param name="printType"></param>
/// <returns></returns>
private static bool IsPrintOp(string printType)
{
if (printType == null)
return false;
const string print = "print";
const string PrintMerge = "PrintMerge";
if (string.Equals(printType, print, StringComparison.OrdinalIgnoreCase) ||
string.Equals(printType, PrintMerge, StringComparison.OrdinalIgnoreCase))
return true;
return false;
}
///
/// 获取 模板、单据内码的打印次数
///
/// <param name="e"></param>
/// <param name="billBusinessInfo"></param>
/// <returns></returns>
private static Dictionary<string, Dictionary<string, int>> GetPrintBillSet(BeforeNotePrintEventArgs e, BusinessInfo billBusinessInfo)
{
string curFormId = billBusinessInfo.GetForm().Id;
Dictionary<string, Dictionary<string, int>> templateBillCntMap = new Dictionary<string, Dictionary<string, int>>();
foreach (var printJob in e.PrintJobs)
{
if (printJob == null || printJob.PrintJobItems == null)
continue;
//不是当前表单的任务不处理,理论上应该都是相同表单的,但是有可能二开会指向其他表单
if (!string.Equals(printJob.FormId, curFormId))
continue;
foreach (var printJobItem in printJob.PrintJobItems)
{
if (printJobItem == null)
continue;
//任务中包含单据内码和模板ID,这里只需要记录单据内码
if (!templateBillCntMap.ContainsKey(printJobItem.TemplateId))
{
templateBillCntMap[printJobItem.TemplateId] = new Dictionary<string, int>();
}
if (!templateBillCntMap[printJobItem.TemplateId].ContainsKey(printJobItem.BillId))
{
templateBillCntMap[printJobItem.TemplateId][printJobItem.BillId] = 1;
}
else
{
templateBillCntMap[printJobItem.TemplateId][printJobItem.BillId] += 1;
}
}
}
return templateBillCntMap;
}
///
/// 更新模板打印次数
///
/// <param name="templateBillCntMap"></param>
/// <returns></returns>
private bool UpdatePrintCount(Dictionary<string, Dictionary<string, int>> templateBillCntMap)
{
string curFormId = this.PlugIn.View.BillBusinessInfo.GetForm().Id;
string strLock = "UPDATE T_Test_NotePrintRecord SET FCOUNT = -1 WHERE FID = 'LOCK'";
string insertOrUpdateSql = @"IF NOT EXISTS (SELECT 1 FROM T_Test_NotePrintRecord WHERE FOBJECTID = @FOBJECTID AND FINTERID = @FINTERID AND FTEMPTELAEID = @FTEMPTELAEID)
INSERT INTO T_Test_NotePrintRecord(FID, FOBJECTID, FINTERID, FTEMPTELAEID, FCOUNT)
VALUES(NEWID(), @FOBJECTID, @FINTERID, @FTEMPTELAEID, @FCOUNT)
ELSE
UPDATE T_Test_NotePrintRecord SET FCOUNT = FCOUNT + @FCOUNT
WHERE FOBJECTID = @FOBJECTID AND FINTERID = @FINTERID AND FTEMPTELAEID = @FTEMPTELAEID";
List<SqlObject> sqlObjects = new List<SqlObject>();
foreach(var templateItem in templateBillCntMap)
{
string templateId = templateItem.Key;
foreach(var billItem in templateItem.Value)
{
string billId = billItem.Key;
int count = billItem.Value;
List<SqlParam> paramList = new List<SqlParam>();
paramList.Add(new SqlParam("@FOBJECTID", KDDbType.AnsiString, curFormId));
paramList.Add(new SqlParam("@FINTERID", KDDbType.AnsiString, billId));
paramList.Add(new SqlParam("@FTEMPTELAEID", KDDbType.AnsiString, templateId));
paramList.Add(new SqlParam("@FCOUNT", KDDbType.Int32, count));
sqlObjects.Add(new SqlObject(insertOrUpdateSql, paramList));
}
}
using (KDTransactionScope scope = new KDTransactionScope(TransactionScopeOption.RequiresNew))
{
DBUtils.Execute(PlugIn.Context, strLock);
DBUtils.ExecuteBatch(PlugIn.Context, sqlObjects);
scope.Complete();
}
return true;
}
}
[Kingdee.BOS.Util.HotUpdate]
[System.ComponentModel.Description("表单插件")]
public class RecordBillPlugIn : AbstractBillPlugIn
{
private NotePrintRecordHandler handler;
public override void BeforeNotePrintCommand(BeforeNotePrintEventArgs e)
{
if(handler == null)
{
handler = new NotePrintRecordHandler(this);
}
handler.Execute(e);
}
}
[Kingdee.BOS.Util.HotUpdate]
[System.ComponentModel.Description("列表插件")]
public class RecordListPlugIn : AbstractListPlugIn
{
private NotePrintRecordHandler handler;
public override void BeforeNotePrintCommand(BeforeNotePrintEventArgs e)
{
if (handler == null)
{
handler = new NotePrintRecordHandler(this);
}
handler.Execute(e);
}
}
}
```
【效果】
点击套打打印后,记录套打模板的打印次数
![Image_20221110141605.webp](/download/0100772ad30281234fae94b65786be927bee.webp)
套打.二开案例.自定义记录打印次数
【场景】自定义记录打印次数【案例】采购订单,列表和单据,记录打印次数<0>对象、单据、模板打印次数表```sql--ksql/****** 对象、单据、...
点击下载文档
上一篇:套打导出所选分录下一篇:套打.二开案例.自定义套打导出次数
本文2024-09-23 04:12:26发表“云星空知识”栏目。
本文链接:https://wenku.my7c.com/article/kingdee-k3cloud-163812.html
您需要登录后才可以发表评论, 登录登录 或者 注册
最新文档
热门文章