6c
10 天以前 cfd0903dc0cfc7ffd39c2caa101f31a50441d39c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
package com.product.print.util;
 
import cn.hutool.extra.spring.SpringUtil;
import com.deepoove.poi.converter.ObjectToPictureRenderDataConverter;
import com.deepoove.poi.converter.ToRenderDataConverter;
import com.deepoove.poi.data.PictureRenderData;
import com.deepoove.poi.data.PictureType;
import com.deepoove.poi.data.Pictures;
import com.deepoove.poi.policy.PictureRenderPolicy;
import com.deepoove.poi.render.RenderContext;
import com.product.common.lang.StringUtils;
import com.product.core.dao.BaseDao;
import com.product.core.entity.FieldSetEntity;
import com.product.core.spring.context.SpringMVCContextHolder;
import com.product.file.service.FileManagerService;
import com.product.print.config.CmnCode;
 
import java.io.File;
import java.io.FileInputStream;
import java.util.Arrays;
 
/**
 * Copyright © 6c
 *
 * @Date 2023年11月16日 17:19
 * @Author 6c
 * @Description
 */
public class CustomPictureRenderPolicy extends PictureRenderPolicy {
 
    private static ToRenderDataConverter<Object, PictureRenderData> converter = new ObjectToPictureRenderDataConverter();
 
    @Override
    public PictureRenderData cast(Object source) throws Exception {
        FileManagerService fileManagerService = SpringUtil.getBean(FileManagerService.class);
        String templateUid = source.toString();
        if (StringUtils.isEmpty(templateUid)) {
            return null;
        }
        int width = 0;
        int height = 0;
        if (templateUid.contains(" ")) {
            String[] infoArr = templateUid.split(" ");
            templateUid = infoArr[0];
            width = Integer.parseInt(infoArr[1]);
            height = Integer.parseInt(infoArr[2]);
        }
        BaseDao baseDao = SpringUtil.getBean(BaseDao.class);
        FieldSetEntity attachmentFse = baseDao.getFieldSetEntity("product_sys_attachments", templateUid, false);
 
        // 遇错跳过,默认使用透明背景不拦截,记录日志到文件
        try {
            fileManagerService.getFile(templateUid);
        } catch (Exception e) {
            attachmentFse = baseDao.getFieldSetEntityByFilter("product_sys_attachments", "attachment_title=?", new Object[]{"sys_sp_deal_透明背景"}, false);
            fileManagerService.getFile(attachmentFse.getUUID());
            SpringMVCContextHolder.getSystemLogger().error(String.format("%s-附件uuid:%s", CmnCode.EXISTS_FILE_RECORD_BUT_NO_FILE.getText(), templateUid));
        }
 
        String fileName = attachmentFse.getString("file_name");
        String type = fileName.substring(fileName.lastIndexOf(".") + 1);
        File file = fileManagerService.getFile(attachmentFse);
        PictureType imgType = PictureType.PNG;
        if (Arrays.asList("jpg", "jpeg").contains(type)) {
            imgType = PictureType.JPEG;
        } else if ("bmp".equals(type)) {
            imgType = PictureType.BMP;
        } else if ("gif".equals(type)) {
            imgType = PictureType.GIF;
        } else if ("svg".equals(type)) {
            imgType = PictureType.SVG;
        }
        if (width > 0 && height > 0) {
            return converter.convert(Pictures.ofStream(new FileInputStream(file), imgType).size(width, height).create());
        } else {
            return converter.convert(Pictures.ofStream(new FileInputStream(file), imgType).create());
        }
    }
 
    @Override
    public void doRender(RenderContext<PictureRenderData> context) throws Exception {
        Helper.renderPicture(context.getRun(), context.getData());
    }
}