云
5 天以前 9265065bea5a40bcaced67857cb3ba22324ce06c
yudao-module-mes/src/main/java/cn/iocoder/yudao/module/mes/service/pd/seal/PdSealService.java
@@ -30,8 +30,39 @@
    public List<Map<String,Object>> seals(boolean activeOnly) {
        return jdbc.queryForList("SELECT id,name,enabled,create_time FROM pd_seal WHERE tenant_id=? AND deleted=0" + (activeOnly ? " AND enabled=1" : "") + " ORDER BY id DESC", tenant());
    }
    public byte[] getImage(long id) {
        var rows = jdbc.queryForList("SELECT image_data FROM pd_seal WHERE id=? AND tenant_id=? AND deleted=0", id, tenant());
        if (rows.isEmpty()) throw fail("印章不存在");
        return (byte[]) rows.get(0).get("image_data");
    }
    public long upload(String name, MultipartFile file, Long userId) throws Exception {
        checkName(name);
        byte[] data = readImage(file);
        String imageHash = hash(data);
        var keys = new GeneratedKeyHolder();
        jdbc.update(c -> { var ps=c.prepareStatement("INSERT INTO pd_seal(name,image_data,image_hash,enabled,created_by,tenant_id) VALUES(?,?,?,1,?,?)", Statement.RETURN_GENERATED_KEYS); ps.setString(1,name.trim()); ps.setBytes(2,data); ps.setString(3,imageHash); ps.setLong(4,userId); ps.setLong(5,tenant()); return ps; }, keys);
        return Objects.requireNonNull(keys.getKey()).longValue();
    }
    public void update(long id, String name, MultipartFile file) throws Exception {
        checkName(name);
        int updated;
        if (file == null || file.isEmpty()) {
            updated = jdbc.update("UPDATE pd_seal SET name=? WHERE id=? AND tenant_id=? AND deleted=0", name.trim(), id, tenant());
        } else {
            byte[] data = readImage(file);
            updated = jdbc.update("UPDATE pd_seal SET name=?,image_data=?,image_hash=? WHERE id=? AND tenant_id=? AND deleted=0", name.trim(), data, hash(data), id, tenant());
        }
        if (updated != 1) throw fail("印章不存在");
    }
    public void delete(long id) {
        Integer pending = jdbc.queryForObject("SELECT COUNT(*) FROM pd_seal_application WHERE seal_id=? AND tenant_id=? AND status=1", Integer.class, id, tenant());
        if (pending != null && pending > 0) throw fail("该印章有进行中的用印审批申请,请待审批完成后再删除");
        if (jdbc.update("UPDATE pd_seal SET deleted=1 WHERE id=? AND tenant_id=? AND deleted=0", id, tenant()) != 1) throw fail("印章不存在");
    }
    private void checkName(String name) {
        if (name == null || name.isBlank() || name.length() > 100) throw fail("印章名称不能为空且不能超过100字");
    }
    private byte[] readImage(MultipartFile file) throws Exception {
        if (file == null || file.isEmpty() || file.getSize() > 2 * 1024 * 1024) throw fail("请上传不超过2MB的PNG印章图片");
        byte[] data = file.getBytes();
        byte[] sig = {(byte)137,80,78,71,13,10,26,10};
@@ -43,10 +74,10 @@
            try { reader.setInput(input); if (reader.getWidth(0) > 2048 || reader.getHeight(0) > 2048) throw fail("图片尺寸不能超过2048×2048"); reader.read(0); }
            finally { reader.dispose(); }
        }
        String hash = HexFormat.of().formatHex(MessageDigest.getInstance("SHA-256").digest(data));
        var keys = new GeneratedKeyHolder();
        jdbc.update(c -> { var ps=c.prepareStatement("INSERT INTO pd_seal(name,image_data,image_hash,enabled,created_by,tenant_id) VALUES(?,?,?,1,?,?)", Statement.RETURN_GENERATED_KEYS); ps.setString(1,name.trim()); ps.setBytes(2,data); ps.setString(3,hash); ps.setLong(4,userId); ps.setLong(5,tenant()); return ps; }, keys);
        return Objects.requireNonNull(keys.getKey()).longValue();
        return data;
    }
    private String hash(byte[] data) throws Exception {
        return HexFormat.of().formatHex(MessageDigest.getInstance("SHA-256").digest(data));
    }
    public void setEnabled(long id, boolean enabled) {
        if (jdbc.update("UPDATE pd_seal SET enabled=? WHERE id=? AND tenant_id=? AND deleted=0", enabled, id, tenant()) != 1) throw fail("印章不存在");
@@ -80,11 +111,11 @@
        if (instance == null || instance.isBlank()) throw fail("审批记录不完整,无法下载");
        boolean humanApproval = taskService.getTaskListByProcessInstanceId(instance, true).stream().anyMatch(task -> {
            var vars = task.getTaskLocalVariables();
            if (vars == null || !Integer.valueOf(2).equals(vars.get("TASK_STATUS")) || task.getAssignee() == null || task.getAssignee().equals(String.valueOf(userId))) return false;
            if (vars == null || !Integer.valueOf(2).equals(vars.get("TASK_STATUS")) || task.getAssignee() == null) return false;
            Object reason = vars.get("TASK_REASON");
            return Arrays.stream(cn.iocoder.yudao.module.bpm.enums.task.BpmReasonEnum.values()).noneMatch(item -> Objects.equals(item.getReason(), reason));
        });
        if (!humanApproval) throw fail("未找到其他审批人的人工通过记录,无法下载");
        if (!humanApproval) throw fail("未找到人工审批通过记录,无法下载");
        byte[] snapshot=(byte[])rows.get(0).get("document_snapshot"), image=(byte[])rows.get(0).get("seal_image");
        var out=new ByteArrayOutputStream(); boolean found=false;
        try(var zin=new ZipInputStream(new ByteArrayInputStream(snapshot)); var zout=new ZipOutputStream(out)) { ZipEntry entry; while((entry=zin.getNextEntry())!=null){byte[] bytes=zin.readAllBytes(); if(entry.getName().equals("word/media/seal.png")){bytes=image;found=true;} zout.putNextEntry(new ZipEntry(entry.getName())); zout.write(bytes); zout.closeEntry();} }