如何将源单附件面板的附件复制到目标单据
# 1业务场景
在源单已保存状态下,切换到目标单,并复制源单附件面板上的所有附件到目标单。
# 2实现方案
(1)编写源单的表单插件代码,实现点击切换按钮(自定义按钮),通过FormShowParameter携带当前单据pkId,表单标识,切换到目标单。
(2)编写目标单的表单插件代码,通过源单插件传递的单据pkId,formId,调用接口查询出源单中的附件面板上的附件信息集合。
(3)获取到附件信息集合后,获取到附件URL,通过URL获取到文件流,重新上传成临时附件,获取新的临时附件URL,重新构造新的临时附件信息集合,使用AttachmentPanel的upload()方法模拟附件上传到目标单据。
# 3示例代码
## 3.1源单据插件示例代码
```
public class SourceAttachPlugin extends AbstractFormPlugin {
//按钮名
private static final String BUTTON = "button";
private static final String ATTACHMENT_PANEL_NAME = "attachmentpanel";
private static final String SHOW_FORM_ID = "targetformid";
@Override
public void registerListener(EventObject e) {
addItemClickListeners("tbmain");
}
@Override
public void itemClick(ItemClickEvent evt) {
String itemKey = evt.getItemKey();
if (!StringUtils.equals(itemKey, BUTTON)) {
return;
}
//点击按钮打开目标单页面
showForm();
}
private void showForm() {
FormShowParameter parameter = new BillShowParameter();
parameter.setFormId(SHOW_FORM_ID);
parameter.getOpenStyle().setShowType(ShowType.Modal);
//源单pkId
parameter.setCustomParam("pkId", getModel().getDataEntity().getPkValue());
//源单formId
parameter.setCustomParam("preFormId", getModel().getDataEntity().getDataEntityType().getName());
getView().showForm(parameter);
}
}
```
## 3.2目标单据插件示例代码
```
public class TargetAttachPlugin extends AbstractFormPlugin {
//源单的附件面板标识
private static final String SOURCE_ATTACHMENT_PANEL_NAME = "attachmentpanel";
private static final String ATTACHMENT_PANEL_NAME = "attachmentpanel";
private static final String FORM_ID = "targetformid";
private static final FileService fileService = FileServiceFactory.getAttachmentFileService();
private static final TempFileCache tempFileCache = CacheFactory.getCommonCacheFactory().getTempFileCache();
@Override
public void afterCreateNewData(EventObject e) {
FormShowParameter parameter = getView().getFormShowParameter();
//获取源单pkId
Object pkId = parameter.getCustomParam("pkId");
//获取源单formId
Object preFormId = parameter.getCustomParam("preFormId");
if (pkId == null || "0".equals(pkId) || StringUtils.isBlank(preFormId)) {
return;
}
//获取目标单附件面板控件
AttachmentPanel panel = getControl(ATTACHMENT_PANEL_NAME);
//通过源单formId,pkId,源单的附件面板标识,获取源单上该附件面板的附件信息集合
List<Map<String, Object>> attachments = AttachmentServiceHelper.getAttachments((String) preFormId, pkId, SOURCE_ATTACHMENT_PANEL_NAME);
List<Map<String, Object>> newAttachments = new ArrayList<>(attachments.size());
for (Map<String, Object> attachment : attachments) {
Map<String, Object> newAttachment = new HashMap<>(16);
String url = (String) attachment.get("url");
String name = (String) attachment.get("name");
//将url关联的文件服务器文件存储成临时文件
String tempUrl = saveAsTempFile(url, name);
newAttachment.put("url", tempUrl);
// 重新设置uid(附件唯一标识)
newAttachment.put("uid", "rc-upload-" + System.currentTimeMillis() + "-" + new SecureRandom().nextInt(100));
newAttachment.put("name", name);
newAttachment.put("size", attachment.get("size"));
newAttachment.put("status", attachment.get("status"));
//目标单formId
newAttachment.put("entityNum", FORM_ID);
//目标单pkId,此时还未生成,传入'0'
newAttachment.put("billPkId", "0");
newAttachments.add(newAttachment);
}
panel.upload(newAttachments);
}
/**
* 将url关联的文件服务器文件存储成临时文件
* (未保存状态下单据的附件必须是临时文件)
* @param url 文件地址
* @param name 文件名
* @return
*/
private String saveAsTempFile(String url, String name) {
String path = url.split("download.do\\?path=")[1];
//通过path参数获取文件流
InputStream inputStream = fileService.getInputStream(path);
//保存文件流返回
String tempUrl = tempFileCache.saveAsUrl(name, inputStream, 2 * 60 * 60);
//域名路径
String address = RequestContext.get().getClientFullContextPath();
if (!address.endsWith("/")) {
address = address + "/";
}
tempUrl = address + tempUrl;
return tempUrl;
}
}
```
# 4展示效果
![切换复制附件面板附件.webp](/download/010048487c611f4145679e175572fbe527f9.webp)
如何将源单附件面板的附件复制到目标单据
# 1业务场景在源单已保存状态下,切换到目标单,并复制源单附件面板上的所有附件到目标单。# 2实现方案(1)编写源单的表单插件代码,实...
点击下载文档
上一篇:如何在通过插件自定义水印内容下一篇:移动端附件配置介绍
本文2024-09-23 00:38:09发表“云苍穹知识”栏目。
本文链接:https://wenku.my7c.com/article/kingdee-cangqiong-140738.html
您需要登录后才可以发表评论, 登录登录 或者 注册
最新文档
热门文章