附件字段、附件面板等问题处理原创
金蝶云社区-lilinpo
lilinpo
1人赞赏了该文章 726次浏览 未经作者许可,禁止转载编辑于2024年09月03日 15:08:48

1、删除单据中具体某个附件面板附件

        基本逻辑说明:  仿照AttachmentServiceHelper工具类中remove 方法重写


    /**
     * @description 重写附件删除
     *
     */
    public static void remove(String formId, Object pkId, String fattachmentpanel) {
        if (pkId != null) {
            pkId = pkId instanceof String ? pkId : "" + pkId;
        }
        QFilter[] filters = new QFilter[]{new QFilter("fattachmentpanel", "=", fattachmentpanel), new QFilter("FInterID", "=", pkId), new QFilter("FBillType", "=", formId)};
        removeAttachByQFilter(filters);
    }

    /**
     * @description 重写附件删除
     *
     */
    public static void removeAttachByQFilter(final QFilter[] filters) {
        final ORM orm = ORM.create();
        DynamicObjectCollection dynamicObjectCollection = QueryServiceHelper.query("bos_attachment", "ID,FInterId,FFileId", filters);
        if (null != dynamicObjectCollection && dynamicObjectCollection.size() > 0) {
            final Set<String> urlSet = new HashSet<>();
            for (DynamicObject dynamicObject : dynamicObjectCollection) {
                String url = dynamicObject.getString("FFileId");
                if (StringUtils.isBlank(url)) {
                    continue;
                }
                if (url.contains("tempfile/download.do?configKey")) {
                    CacheFactory.getCommonCacheFactory().getTempFileCache().remove(url);
                    continue;
                }
                urlSet.add(url);
            }
            threadPool.execute(() -> {
                orm.delete("bos_attachment", filters);
                for (String url : urlSet) {
                    boolean canDelte = !AttachmentServiceHelper.checkRefByPath(url);
                    if (canDelte) {
                        FileServiceFactory.getAttachmentFileService().delete(url);
                        BillFileMappingWriter.removeMappingRecord(url, "attach");
                        removeYzjPreviewFile(url);
                    }
                }
            });
        }
    }

    /**
     * @description 移除预览文件
     */
    private static void removeYzjPreviewFile(String path) {
        if ("1".equals(ParamUtil.getPreviewType())) {
            try {
                FileServiceFactory.getAttachmentFileService().deletePreviewWps(path, ParamUtil.getYzjParam());
            } catch (Exception var2) {
                logger.error("移除元之家预览文件失败,meg:" + var2.getMessage());
            }
        }
    }


2、移动单据体附件字段到另外一个单据体附件字段

    // 需要附件的单据
    DynamicObject targetObj = BusinessDataServiceHelper.loadSingle(pkid, "***");
    DynamicObjectCollection targetEntry = targetObj .getDynamicObjectCollection("targetEntry ");
    targetEntry .clear();
    // 被移动附件的单据
    DynamicObject sourceObj = BusinessDataServiceHelper.loadSingle(id, "****");
    DynamicObjectCollection sourceEntry= sourceObj.getDynamicObjectCollection("sourceEntry");
    for (DynamicObject item : sourceEntry) {
        DynamicObject addRow = targetEntry .addNew();
        DynamicObjectCollection sourceAttachcol = (DynamicObjectCollection) item.get("fj");
        for (DynamicObject dObj : sourceAttachcol) {
            // 获取单据D上的附件字段信息
            DynamicObjectCollection dAttCol = addRow.getDynamicObjectCollection("fj");
            // 遍历物品的附件字段的附件信息,赋值给单据D的附件 字段
            dAttCol.addNew().set("fbasedataid", dObj.get("fbasedataid"));
        }
    }
    SaveServiceHelper.save(new DynamicObject[]{targetObj });


3、单据体附件字段的删除 和  上传

        删除参考文章:     https://developer.kingdee.com/article/322323966637938176?productLineId=29&isKnowledge=2&lang=zh-CN

                   如下示例:

// 获取单据
DynamicObject obj= BusinessDataServiceHelper.loadSingle(pkid, billtype);
DynamicObjectCollection entry= obj.getDynamicObjectCollection("entry");

// 删除所有数据库附件信息与附件
for (DynamicObject dynamicObject : entry) {
    List<Object> attPkIds = new ArrayList<>(16);
    List<Object> entryPkIds = new ArrayList<>(16);
    entryPkIds.add(dynamicObject.getPkValue());
    DynamicObjectCollection zywj = dynamicObject.getDynamicObjectCollection("zywj");
    for (DynamicObject dynamicObject1 : zywj) {
        DynamicObject fbasedataid = (DynamicObject) dynamicObject1.get("fbasedataid");
        Object pkValue = fbasedataid.getPkValue();
        attPkIds.add(pkValue);
    }
    // 注意:第二个参数是附件字段的  table 表名
    AttachmentFieldServiceHelper.batchRemoveAtt(billtype, "tk_****", attPkIds.toArray(), entryPkIds.toArray(), AttachmentRemoveSource.ENTRY, false);
}

        上传参考文章:https://developer.kingdee.com/article/535454240743670528?productLineId=29&lang=zh-CN


4、附件面板上传附件

public void uploadToBill(String formId, Object pkId, String attachKey, List<Map<String, Object>> fjlist) {
        try {
            // 提前清除附件
            AttachmentServiceHelper.remove(formId, pkId);
            // 附件上传到指定单据
            List<Map<String, Object>> attachments = new ArrayList<>();
            FileService service = FileServiceFactory.getAttachmentFileService();
            RequestContext requestContext = RequestContext.get();
            if (!ObjectUtils.isEmpty(fjlist)) {
                for (Map<String, Object> xyFileModel : fjlist) {
                    String fjdz = (String) xyFileModel.get("fjdz");
                    String fjmc = (String) xyFileModel.get("fjmc");
                    if (!StringUtils.isEmpty(fjdz)) {
                        // 单据附件处理
                        // fjdz
                        String uuid = UUID.randomUUID().toString().replace("_", "");
                        // 生成文件路径-上传附件时远程服务器需要存储文件的位置
                        String pathParam = FileNameUtils.getAttachmentFileName(requestContext.getTenantId(), requestContext.getAccountId(), uuid, fjmc);
                        URL testurl = new URL(fjdz);
                        URLConnection connection = testurl.openConnection();
                        InputStream fileInputStream = connection.getInputStream();
                        FileItem fileItem = new FileItem(fjmc, pathParam, fileInputStream);
                        String downUrl = service.upload(fileItem);
                        Map<String, Object> attachItem = new HashMap<>();
                        attachItem.put("uid", uuid);
                        attachItem.put("name", fjmc);
                        attachItem.put("size", connection.getContentLength());
                        attachItem.put("url", downUrl);
                        attachments.add(attachItem);
                    }
                    if (!attachments.isEmpty()) {
                        AttachmentServiceHelper.upload(formId, pkId, attachKey, attachments);
                    }
                }
            }
        } catch (IOException e) {
            throw new KDBizException("附件处理发生异常!");
        }
    }


    5.附件字段到附件面板

  /**
     * 根据附件字段数据构造附件面板数据
     * @param sourceAttachCol
     * @return
     */
    private List<Map<String, Object>> buildAttachmentDataFromEdit(DynamicObjectCollection sourceAttachCol, String billPkId, String billname) {
        // 生成文件路径-上传附件时远程服务器需要存储文件的位置
        List<Map<String, Object>> attachDataList = new ArrayList<>();
        FileService service = FileServiceFactory.getAttachmentFileService();
        sourceAttachCol.forEach(attach -> {
            DynamicObject attachObj = attach.getDynamicObject("fbasedataid");
            RequestContext requestContext = RequestContext.get();
            Map<String, Object> attachMap = new HashMap<>();
            String uuid = UUID.randomUUID().toString().replace("_", "");
            String fjmc = attachObj.getString("name");
            String pathParam = FileNameUtils.getAttachmentFileName(requestContext.getTenantId(), requestContext.getAccountId(), uuid, fjmc);
            InputStream inputStream = FileServiceFactory.getAttachmentFileService().getInputStream(attachObj.getString("url"));
            FileItem fileItem = new FileItem(fjmc, pathParam, inputStream);
            String downUrl = service.upload(fileItem);
            attachMap.put("uid", uuid);
            attachMap.put("name", fjmc);
            attachMap.put("size", attachObj.get("size"));
            attachMap.put("url", downUrl);
            attachDataList.add(attachMap);
        });
        return attachDataList;
    }





3、单据体附件字段的删除 和  上传
图标赞 1
1人点赞
还没有人点赞,快来当第一个点赞的人吧!
图标打赏
0人打赏
还没有人打赏,快来当第一个打赏的人吧!