清帐单、余额调节表、理财预提、存款预提保存pdf至附件、附件地址
This commit is contained in:
parent
11219db0fb
commit
147f84bffd
|
|
@ -0,0 +1,240 @@
|
|||
package shjh.jhzj7.fi.fi.plugin.task;
|
||||
|
||||
import com.fasterxml.jackson.databind.ObjectMapper;
|
||||
import kd.bos.context.RequestContext;
|
||||
import kd.bos.dataentity.entity.DynamicObject;
|
||||
import kd.bos.exception.KDException;
|
||||
import kd.bos.fileservice.FileService;
|
||||
import kd.bos.fileservice.FileServiceFactory;
|
||||
import kd.bos.logging.Log;
|
||||
import kd.bos.logging.LogFactory;
|
||||
import kd.bos.orm.query.QCP;
|
||||
import kd.bos.orm.query.QFilter;
|
||||
import kd.bos.print.api.PrintTask;
|
||||
import kd.bos.print.api.PrintWork;
|
||||
import kd.bos.print.core.service.PrtAttach;
|
||||
import kd.bos.print.service.BosPrintServiceHelper;
|
||||
import kd.bos.schedule.executor.AbstractTask;
|
||||
import kd.bos.servicehelper.AttachmentServiceHelper;
|
||||
import kd.bos.servicehelper.BusinessDataServiceHelper;
|
||||
import kd.bos.servicehelper.QueryServiceHelper;
|
||||
import kd.bos.servicehelper.operation.SaveServiceHelper;
|
||||
import kd.sdk.plugin.Plugin;
|
||||
import shjh.jhzj7.fi.fi.plugin.operate.RecRedPushOperation;
|
||||
|
||||
import java.io.ByteArrayOutputStream;
|
||||
import java.io.IOException;
|
||||
import java.io.InputStream;
|
||||
import java.util.*;
|
||||
|
||||
/**
|
||||
* 后台任务插件
|
||||
* 清帐单、余额调节表、存款预提处理、理财预提处理,打印表单存入附件并保存附件地址
|
||||
*/
|
||||
public class ClearBillSaveUrlTask extends AbstractTask implements Plugin {
|
||||
|
||||
private final static Log log = LogFactory.getLog(ClearBillSaveUrlTask.class);
|
||||
|
||||
|
||||
@Override
|
||||
public void execute(RequestContext requestContext, Map<String, Object> map) throws KDException {
|
||||
|
||||
// 为每个表单创建独立的任务,互不干扰
|
||||
for (Map.Entry<String, Object> config : map.entrySet()) {
|
||||
String formId = config.getKey();
|
||||
String printTemplateCode = (String) config.getValue();
|
||||
|
||||
try {
|
||||
processForm(formId, printTemplateCode);
|
||||
log.info("表单 {} 处理完成", formId);
|
||||
} catch (Exception e) {
|
||||
// 捕获异常,确保一个表单失败不影响其他表单
|
||||
log.error("处理表单 {} 时发生错误: {}", formId, e.getMessage(), e);
|
||||
// 可以在这里添加错误通知或其他处理逻辑
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* 处理单个表单
|
||||
*/
|
||||
private void processForm(String formId, String printTemplateCode) throws KDException {
|
||||
// 1. 获取数据(已审核、无附件地址)
|
||||
QFilter statusFilter = new QFilter("billstatus", QCP.equals, "C");
|
||||
QFilter urlFilter = new QFilter("shjh_url", QCP.equals, "");
|
||||
QFilter[] filters = new QFilter[]{statusFilter, urlFilter};
|
||||
|
||||
DynamicObject[] bills = BusinessDataServiceHelper.load(formId, "id,billstatus,shjh_url,billno", filters);
|
||||
|
||||
if (bills.length == 0) {
|
||||
log.info("表单 {} 没有需要处理的单据", formId);
|
||||
return;
|
||||
}
|
||||
|
||||
log.info("开始处理表单 {},共 {} 个单据", formId, bills.length);
|
||||
|
||||
// 批量加载数据到缓存,提高性能
|
||||
Long[] ids = Arrays.stream(bills)
|
||||
.filter(bill -> {
|
||||
Object shjhUrl = bill.get("shjh_url");
|
||||
// 过滤掉 shjh_url 不为 null 且不为空字符串的数据
|
||||
return shjhUrl == null || "".equals(shjhUrl.toString().trim());
|
||||
})
|
||||
.map(bill -> (Long) bill.getPkValue())
|
||||
.toArray(Long[]::new);
|
||||
|
||||
Map<Object, DynamicObject> billMap = BusinessDataServiceHelper.loadFromCache(ids, formId);
|
||||
|
||||
// 获取打印模板ID(只查询一次,提高性能)
|
||||
DynamicObject printMeta = QueryServiceHelper.queryOne("bos_print_meta", "id",
|
||||
new QFilter[]{new QFilter("number", QCP.equals, printTemplateCode)});
|
||||
|
||||
if (printMeta == null) {
|
||||
log.error("打印模板 {} 不存在", printTemplateCode);
|
||||
return;
|
||||
}
|
||||
|
||||
String tplId = printMeta.getString("id");
|
||||
|
||||
// 处理每个单据
|
||||
for (DynamicObject bill : billMap.values()) {
|
||||
processSingleBill(bill, formId, tplId);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* 处理单个单据
|
||||
*/
|
||||
private void processSingleBill(DynamicObject bill, String formId, String tplId) {
|
||||
try {
|
||||
String billNo = bill.getString("billno");
|
||||
log.debug("处理单据: {}", billNo);
|
||||
|
||||
//查看是否存在附件
|
||||
List<Map<String, Object>> list = AttachmentServiceHelper.getAttachments(formId, bill.getPkValue(), "attachmentpanel");
|
||||
if (list.size()==0){
|
||||
// 生成打印任务
|
||||
PrintTask printTask = createPrintTask(bill, formId, tplId);
|
||||
PrintWork printWork = createPrintWork(printTask);
|
||||
|
||||
// 生成PDF
|
||||
PrtAttach printResult = BosPrintServiceHelper.doPrint(printWork);
|
||||
PrtAttach.AttachDetail attachDetail = printResult.getAttachDetail().get(0);
|
||||
|
||||
// 创建附件信息并上传
|
||||
Map<String, Object> attInfo = createAttInfo(attachDetail, billNo);
|
||||
uploadAttachment(formId, bill, attInfo);
|
||||
}
|
||||
|
||||
// 更新单据
|
||||
updateBill(bill,formId);
|
||||
|
||||
log.debug("单据 {} 处理完成", billNo);
|
||||
|
||||
} catch (Exception e) {
|
||||
log.error("处理单据 {} 时发生错误: {}", bill.getString("billno"), e.getMessage(), e);
|
||||
// 单个单据失败不影响其他单据
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* 创建打印任务
|
||||
*/
|
||||
private PrintTask createPrintTask(DynamicObject bill, String formId, String tplId) {
|
||||
ArrayList<Object> pkIds = new ArrayList<>();
|
||||
pkIds.add(bill.getPkValue());
|
||||
|
||||
PrintTask printTask = new PrintTask();
|
||||
printTask.setTplId(tplId);
|
||||
printTask.setPkIds(pkIds);
|
||||
printTask.setPrintType("billForm");
|
||||
printTask.setFormId(formId);
|
||||
|
||||
return printTask;
|
||||
}
|
||||
|
||||
/**
|
||||
* 创建打印作业
|
||||
*/
|
||||
private PrintWork createPrintWork(PrintTask printTask) {
|
||||
ArrayList<PrintTask> taskList = new ArrayList<>();
|
||||
taskList.add(printTask);
|
||||
|
||||
PrintWork printWork = new PrintWork();
|
||||
printWork.setPrintLang("zh_CN");
|
||||
printWork.setExpType("pdf");
|
||||
printWork.setTaskList(taskList);
|
||||
|
||||
return printWork;
|
||||
}
|
||||
|
||||
/**
|
||||
* 上传附件
|
||||
*/
|
||||
private void uploadAttachment(String formId, DynamicObject bill, Map<String, Object> attInfo) {
|
||||
List<Map<String, Object>> attachments = new ArrayList<>();
|
||||
attachments.add(attInfo);
|
||||
|
||||
AttachmentServiceHelper.upload(formId, bill.getPkValue(), "attachmentpanel", attachments);
|
||||
}
|
||||
|
||||
/**
|
||||
* 更新单据
|
||||
*/
|
||||
private void updateBill(DynamicObject bill, String formId) {
|
||||
List<Map<String, Object>> list = AttachmentServiceHelper.getAttachments(formId, bill.getPkValue(), "attachmentpanel");
|
||||
if (list.size()!=0){
|
||||
String url = (String) list.get(0).get("url");
|
||||
bill.set("shjh_url", url);
|
||||
bill.set("shjh_url_tag", url);
|
||||
SaveServiceHelper.update(bill);
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
/**
|
||||
* 创建附件信息(优化版本)
|
||||
*/
|
||||
private Map<String, Object> createAttInfo(PrtAttach.AttachDetail attachDetail, String billNo) {
|
||||
long timestamp = System.currentTimeMillis();
|
||||
|
||||
Map<String, Object> att = new HashMap<>();
|
||||
att.put("createdate", timestamp);
|
||||
att.put("lastModified", timestamp);
|
||||
att.put("status", "success");
|
||||
att.put("type", attachDetail.getFileType());
|
||||
att.put("name", billNo + "_" + attachDetail.getFileName());
|
||||
att.put("uid", "rc-upload-" + timestamp + "-1");
|
||||
att.put("url", attachDetail.getFilePath());
|
||||
|
||||
// 异步或延迟计算文件大小,避免阻塞
|
||||
att.put("size", calculateFileSize(attachDetail.getFilePath()));
|
||||
|
||||
return att;
|
||||
}
|
||||
|
||||
/**
|
||||
* 计算文件大小(优化性能)
|
||||
*/
|
||||
private long calculateFileSize(String filePath) {
|
||||
try {
|
||||
FileService fs = FileServiceFactory.getAttachmentFileService();
|
||||
try (InputStream in = fs.getInputStream(filePath);
|
||||
ByteArrayOutputStream out = new ByteArrayOutputStream()) {
|
||||
|
||||
byte[] buffer = new byte[8192]; // 使用缓冲区提高读取效率
|
||||
int bytesRead;
|
||||
long totalSize = 0;
|
||||
|
||||
while ((bytesRead = in.read(buffer)) != -1) {
|
||||
totalSize += bytesRead;
|
||||
}
|
||||
return totalSize;
|
||||
}
|
||||
} catch (IOException e) {
|
||||
log.warn("计算文件大小时发生错误: {}", e.getMessage());
|
||||
return 0L;
|
||||
}
|
||||
}
|
||||
}
|
||||
Loading…
Reference in New Issue