合同台账 补充附件上传改造

This commit is contained in:
李靖 2024-06-18 15:48:06 +08:00
parent 748620a4aa
commit 8687cf8a67
1 changed files with 220 additions and 4 deletions

View File

@ -1,26 +1,49 @@
package shkd.fi.em.plugin; package shkd.fi.em.plugin;
import com.alibaba.excel.util.StringUtils;
import kd.bos.bill.BillShowParameter; import kd.bos.bill.BillShowParameter;
import kd.bos.cache.CacheFactory;
import kd.bos.cache.TempFileCache;
import kd.bos.context.RequestContext;
import kd.bos.dataentity.entity.DynamicObject;
import kd.bos.dataentity.entity.DynamicObjectCollection;
import kd.bos.dataentity.metadata.dynamicobject.DynamicObjectType;
import kd.bos.dataentity.resource.ResManager; import kd.bos.dataentity.resource.ResManager;
import kd.bos.entity.datamodel.IDataModel; import kd.bos.entity.datamodel.IDataModel;
import kd.bos.entity.datamodel.ListSelectedRow; import kd.bos.entity.datamodel.ListSelectedRow;
import kd.bos.entity.datamodel.ListSelectedRowCollection; import kd.bos.entity.datamodel.ListSelectedRowCollection;
import kd.bos.entity.operate.IOperationResult; import kd.bos.entity.operate.IOperationResult;
import kd.bos.form.FormShowParameter; import kd.bos.fileservice.FileItem;
import kd.bos.form.ShowType; import kd.bos.fileservice.FileService;
import kd.bos.fileservice.FileServiceFactory;
import kd.bos.form.*;
import kd.bos.form.control.AttachmentPanel;
import kd.bos.form.control.Toolbar;
import kd.bos.form.control.events.BeforeUploadEvent;
import kd.bos.form.control.events.ItemClickEvent;
import kd.bos.form.control.events.UploadEvent;
import kd.bos.form.control.events.UploadListener;
import kd.bos.form.events.AfterDoOperationEventArgs; import kd.bos.form.events.AfterDoOperationEventArgs;
import kd.bos.form.events.BeforeDoOperationEventArgs; import kd.bos.form.events.BeforeDoOperationEventArgs;
import kd.bos.list.plugin.AbstractListPlugin; import kd.bos.list.plugin.AbstractListPlugin;
import kd.bos.logging.Log;
import kd.bos.logging.LogFactory;
import kd.bos.mvc.list.ListView; import kd.bos.mvc.list.ListView;
import kd.bos.orm.ORM;
import kd.bos.servicehelper.AttachmentServiceHelper;
import kd.bos.servicehelper.operation.SaveServiceHelper;
import kd.bos.util.FileNameUtils;
import kd.sdk.plugin.Plugin; import kd.sdk.plugin.Plugin;
import java.util.List; import java.io.InputStream;
import java.util.*;
import java.util.stream.Collectors; import java.util.stream.Collectors;
/** /**
* 标准单据列表插件 * 标准单据列表插件
*/ */
public class ContractBillExtList extends AbstractListPlugin implements Plugin { public class ContractBillExtList extends AbstractListPlugin implements Plugin, UploadListener {
private static final Log log = LogFactory.getLog(ContractBillExtList.class);
public void afterDoOperation(AfterDoOperationEventArgs e) { public void afterDoOperation(AfterDoOperationEventArgs e) {
super.afterDoOperation(e); super.afterDoOperation(e);
String key = e.getOperateKey(); String key = e.getOperateKey();
@ -57,4 +80,197 @@ public class ContractBillExtList extends AbstractListPlugin implements Plugin {
public void beforeDoOperation(BeforeDoOperationEventArgs args) { public void beforeDoOperation(BeforeDoOperationEventArgs args) {
} }
@Override
public void itemClick(ItemClickEvent evt) {
System.out.println(evt);
super.itemClick(evt);
}
@Override
public void initialize() {
Object cc = this.getControl("toolbar");
super.initialize();
}
@Override
public void registerListener(EventObject e) {
super.registerListener(e);
Toolbar toolbar = this.getControl("toolbarap");
toolbar.addUploadListener(this);
}
@Override
public void beforeUpload(BeforeUploadEvent evt) {
ListSelectedRowCollection selectRows = getSelectedRows();
if(selectRows.size() != 1) {
//UploadListener.super.beforeUpload(evt);
Map<String, Integer> param = new HashMap<>(1);
param.put("status", 0);
// 传递前端中止指令, 不继续执行文件上传
((IClientViewProxy) getView().getService(IClientViewProxy.class)).addAction("beforeUploadResult", param);
this.getView().showTipNotification("请选择一条要执行的数据!");
}
}
@Override
public void upload(UploadEvent evt) {
// UploadListener.super.upload(evt);
Object[] urls = evt.getUrls();
Object[] names = evt.getNames();
String callbackKey = evt.getCallbackKey();
if ("shkd_baritemap2".equals(callbackKey)) {
//执行业务逻辑
Map<String, Object> attDataItem = new HashMap<String, Object>();
attDataItem.put("url", urls[0]);
attDataItem.put("name", names[0]);
doUpload(attDataItem);
}
}
private void doUpload(Map<String, Object> attachment) {
String entityNumber = "er_contractbill";//当前单据实体编码
String attKey = "shkd_attachmentpanelap"; // 单据面板标识 attachmentpanel
FormShowParameter showParameter = this.getView().getFormShowParameter();
// Object pkidobj = showParameter.getCustomParam("pkid");
ListSelectedRowCollection selectRows = getSelectedRows();
Object pkiStr = selectRows.get(0).getPrimaryKeyValue();
boolean hasAttachmentDataUploadedErr = false, hasFormDataErr = false;
ORM orm = ORM.create();
// List<Object> formDataErr = new ArrayList<Object>();
List<DynamicObject> attBillRelList = new ArrayList<>();
List<Map<String, Object>> attachmentDataUploaded = new ArrayList<>();
//-----------------------------------------------------------------
Map<String, Object> result = this.uploadFileServer(attachment);
if (result.get("path") == null || "".equals(result.get("path"))) {
hasAttachmentDataUploadedErr = true;
}
Map<String, Object> map = new HashMap<>(attachment);
map.put("url", (result.get("path") + ""));
map.put("size", (result.get("fileSize") + ""));
map.put("uid", UUID.randomUUID());
attachmentDataUploaded.add(map);
try {
DynamicObjectCollection tempRel =
this.genAttachmentRel(entityNumber, String.valueOf(pkiStr), attKey, attachmentDataUploaded, orm);
attBillRelList.addAll(tempRel);
} catch (Exception e) {
hasFormDataErr = true;
log.error(e.getMessage());
}
SaveServiceHelper.save(attBillRelList.toArray(new DynamicObject[0]));
//-----------------------------------------------------------------
if (!hasAttachmentDataUploadedErr && !hasFormDataErr) {
this.getView().showConfirm("上传成功!", MessageBoxOptions.OK,
new ConfirmCallBackListener("upload_success", this));
} else if (hasAttachmentDataUploadedErr && !hasFormDataErr) {
// ResManager.LoadKDString("以下附件上传失败,请重新上传文件!", "UploadAtt2BillsFormPlugin_6")
this.getView().showTipNotification("附件上传失败,请重新上传文件!");
} else {
// ResManager.LoadKDString("附件上传出现未知异常,请联系管理员查询日志分析!", "UploadAtt2BillsFormPlugin_7")
this.getView().showErrorNotification("附件上传出现未知异常,请联系管理员查询日志分析!");
}
}
/**
* 保存临时文件到文件服务器进行持久化
* @see AttachmentServiceHelper#saveTempToFileService
* @param attDataItem 已上传的附件临时文件Map信息
* @return 上传文件服务器返回url
*/
private Map<String, Object> uploadFileServer(Map<String, Object> attDataItem) {
Map<String, Object> result = new HashMap<String, Object>();
try{
FileService fs = FileServiceFactory.getAttachmentFileService();
RequestContext requestContext = RequestContext.get();
// TempFileCache fileCache = CacheFactory.getCommonCacheFactory().getTempFileCache();
String filename = (String) attDataItem.get("name");
String tempUrl = (String) attDataItem.get("url");
String uuid = UUID.randomUUID().toString().replace("-", "");
String filepath = FileNameUtils.getAttachmentFileName( // 此处 attachmentpanel 用于文件路径的唯一标识
requestContext.getTenantId(), requestContext.getAccountId(), "attachmentpanel", uuid + "/" + filename);
// InputStream inputStream = fileCache.getInputStream(tempUrl);
InputStream inputStream = fs.getInputStream(tempUrl);
FileItem item = new FileItem(filename, filepath, inputStream);
String[] splits = filename.trim().split("\\.");
String fileType = splits[splits.length - 1];
long compressPicSize = 0L;
int fileSize = inputStream.available() / 1024;
result.put("fileSize", inputStream.available());
if ("jpg,jpeg,png,gif,bmp,tiff,tga,ico,dib,rle,emf,jpe,jfif,pcx,dcx,pic,tif,wmf".contains(fileType.toLowerCase())) {
compressPicSize = AttachmentServiceHelper.getCompressPicSize();
}
if (compressPicSize != 0L && fileSize > compressPicSize) {
result.put("path", fs.compressPicUpload(item, compressPicSize));
} else {
result.put("path", fs.upload(item));
}
return result;
} catch (Exception e){
log.error("uploadFileServer err: " + e.getMessage());
return null;
}
}
/**
* 绑定附件到单据的附件面板
* @see AttachmentServiceHelper#upload
* @param entityNumber 实体编码
* @param billPkId 单据主键
* @param attList 附件信息
* @param orm orm实例
* @return 附件面板实体数据用于入库记录
*/
private DynamicObjectCollection genAttachmentRel(String entityNumber, String billPkId, String attachKey, List<Map<String, Object>> attList, ORM orm){
DynamicObjectType entityType = (DynamicObjectType)orm.getDataEntityType("bos_attachment");
DynamicObjectCollection dynColl = new DynamicObjectCollection(entityType, null);
if (attList == null || attList.size() == 0) {
return dynColl;
}
long[] ids = orm.genLongIds(entityType, attList.size());
Date today = new Date();
for(int i = 0; i < attList.size(); i++) {
Map<String, Object> attach = attList.get(i);
DynamicObject dynamicObject = new DynamicObject(entityType);
dynamicObject.set("id", ids[i]);
dynamicObject.set("FNUMBER", attach.get("uid"));
dynamicObject.set("FBillType", entityNumber);
dynamicObject.set("FInterID", billPkId);
Object lastModified = attach.get("lastModified");
if (lastModified instanceof Date) {
dynamicObject.set("FModifyTime", lastModified);
} else if (lastModified instanceof Long) {
dynamicObject.set("FModifyTime", new Date((Long)lastModified));
} else {
dynamicObject.set("FModifyTime", today);
}
dynamicObject.set("fcreatetime", attach.getOrDefault("uploadTime", today));
String name = (String)attach.get("name");
dynamicObject.set("FaliasFileName", name);
dynamicObject.set("FAttachmentName", name);
String extName = name != null ? name.substring(name.lastIndexOf(46) + 1) : "";
dynamicObject.set("FExtName", extName);
long compressPicSize = AttachmentServiceHelper.getCompressPicSize();
if ("jpg,jpeg,png,gif,bmp,tiff,tga,ico,dib,rle,emf,jpe,jfif,pcx,dcx,pic,tif,wmf".contains(extName.toLowerCase()) && compressPicSize != 0L && Long.parseLong(attach.get("size").toString()) > compressPicSize * 1024L) {
dynamicObject.set("FATTACHMENTSIZE", compressPicSize * 1024L);
} else {
dynamicObject.set("FATTACHMENTSIZE", attach.get("size"));
}
dynamicObject.set("FFileId", attach.get("url"));
dynamicObject.set("FCREATEMEN", RequestContext.get().getCurrUserId());
dynamicObject.set("fattachmentpanel", attachKey);
dynamicObject.set("filesource", attach.get("filesource"));
if (attach.containsKey("description")) {
dynamicObject.set("fdescription", attach.get("description"));
}
dynColl.add(dynamicObject);
}
return dynColl;
}
} }