diff --git a/shkd-cosmic-debug/src/main/java/shkd/fi/em/plugin/ContractBillExtList.java b/shkd-cosmic-debug/src/main/java/shkd/fi/em/plugin/ContractBillExtList.java index 72aefb3..9a47acd 100644 --- a/shkd-cosmic-debug/src/main/java/shkd/fi/em/plugin/ContractBillExtList.java +++ b/shkd-cosmic-debug/src/main/java/shkd/fi/em/plugin/ContractBillExtList.java @@ -80,7 +80,7 @@ public class ContractBillExtList extends AbstractListPlugin implements Plugin, U public void beforeDoOperation(BeforeDoOperationEventArgs args) { FormOperate operate = (FormOperate)args.getSource(); - // 下推预付单 or 对公报销 or 下推暂估单 检查补充附件的数量,0件的情况不允许操作 + // 下推预付单 or 对公报销 or 下推暂估单 检查归档附件的数量,0件的情况不允许操作 if("pushtoprepay".equals(operate.getOperateKey()) || "publicreimburse".equals(operate.getOperateKey()) || "pushcostestimatebill".equals(operate.getOperateKey())){ diff --git a/shkd-cosmic-debug/src/main/java/shkd/fi/er/plugin/ListUploadPlugin.java b/shkd-cosmic-debug/src/main/java/shkd/fi/er/plugin/ListUploadPlugin.java new file mode 100644 index 0000000..de5f6a8 --- /dev/null +++ b/shkd-cosmic-debug/src/main/java/shkd/fi/er/plugin/ListUploadPlugin.java @@ -0,0 +1,252 @@ +package shkd.fi.er.plugin; + +import kd.bos.bill.BillShowParameter; +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.entity.datamodel.IDataModel; +import kd.bos.entity.datamodel.ListSelectedRow; +import kd.bos.entity.datamodel.ListSelectedRowCollection; +import kd.bos.entity.operate.IOperationResult; +import kd.bos.fileservice.FileItem; +import kd.bos.fileservice.FileService; +import kd.bos.fileservice.FileServiceFactory; +import kd.bos.form.*; +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.BeforeDoOperationEventArgs; +import kd.bos.form.operate.FormOperate; +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.orm.ORM; +import kd.bos.servicehelper.AttachmentServiceHelper; +import kd.bos.servicehelper.operation.SaveServiceHelper; +import kd.bos.util.FileNameUtils; +import kd.sdk.plugin.Plugin; + +import java.io.InputStream; +import java.util.*; +import java.util.stream.Collectors; + +/** + * 标准单据列表插件 + */ +public class ListUploadPlugin extends AbstractListPlugin implements Plugin, UploadListener { + private static final Log log = LogFactory.getLog(ListUploadPlugin.class); + + @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(); + boolean isError = false; + String errMsg = ""; + if (selectRows.size() != 1) { + isError = true; + errMsg = "请选择一条要执行的数据!"; + } else if (!"E".equals(selectRows.get(0).getBillStatus()) + && !"F".equals(selectRows.get(0).getBillStatus()) + && !"G".equals(selectRows.get(0).getBillStatus())) { + isError = true; + errMsg = "单据审核通过后才能上传付款附件!"; + } + if (isError) { + //UploadListener.super.beforeUpload(evt); + Map param = new HashMap<>(1); + param.put("status", 0); + // 传递前端中止指令, 不继续执行文件上传 + ((IClientViewProxy) getView().getService(IClientViewProxy.class)).addAction("beforeUploadResult", param); + this.getView().showTipNotification(errMsg); + } + } + + @Override + public void upload(UploadEvent evt) { + // UploadListener.super.upload(evt); + Object[] urls = evt.getUrls(); + Object[] names = evt.getNames(); + String callbackKey = evt.getCallbackKey(); + // String entityNumber = "er_contractbill";//当前单据实体编码 + // String attKey = "shkd_attachmentpanelap"; // 单据面板标识 attachmentpanel + if ("shkd_baritemap16".equals(callbackKey)) { + //执行业务逻辑 + Map attDataItem = new HashMap(); + attDataItem.put("url", urls[0]); + attDataItem.put("name", names[0]); + doUpload(attDataItem, "er_publicreimbursebill", "shkd_attachmentpanelap"); + } else if ("shkd_baritemap17".equals(callbackKey)) { + //执行业1务逻辑 + Map attDataItem = new HashMap(); + attDataItem.put("url", urls[0]); + attDataItem.put("name", names[0]); + doUpload(attDataItem, "er_dailyreimbursebill", "shkd_attachmentpanelap"); + } + } + + private void doUpload(Map attachment, String entityNumber, String attKey) { + 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 formDataErr = new ArrayList(); + List attBillRelList = new ArrayList<>(); + List> attachmentDataUploaded = new ArrayList<>(); + //----------------------------------------------------------------- + Map result = this.uploadFileServer(attachment); + if (result.get("path") == null || "".equals(result.get("path"))) { + hasAttachmentDataUploadedErr = true; + } + Map 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 uploadFileServer(Map attDataItem) { + Map result = new HashMap(); + 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> 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 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; + } +} \ No newline at end of file