zouyu
2025-03-19 3647aa5008055528f075ee73002542a1399575ae
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
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
package com.ruoyi.manage.service.impl;
 
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.core.toolkit.ObjectUtils;
import com.baomidou.mybatisplus.core.toolkit.StringUtils;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.deepoove.poi.XWPFTemplate;
import com.deepoove.poi.config.Configure;
import com.deepoove.poi.data.PictureRenderData;
import com.deepoove.poi.data.Pictures;
import com.ruoyi.common.utils.DateImageUtil;
import com.ruoyi.framework.exception.ErrorException;
import com.ruoyi.framework.util.HackLoopTableRenderPolicy;
import com.ruoyi.manage.mapper.ManageRiskAssessmentResultsMapper;
import com.ruoyi.manage.pojo.ManageRiskAssessmentResults;
import com.ruoyi.manage.service.ManageRiskAssessmentResultsService;
import com.ruoyi.manage.vo.ManageRiskAssessmentResultsVo;
import com.ruoyi.system.mapper.UserMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;
 
import javax.servlet.http.HttpServletResponse;
import java.io.File;
import java.io.InputStream;
import java.io.OutputStream;
import java.net.URLEncoder;
import java.util.HashMap;
 
/**
 * <p>
 * 危险因素辨识与风险评价结果一览表 服务实现类
 * </p>
 *
 * @author 芯导软件(江苏)有限公司
 * @since 2024-11-15 02:58:51
 */
@Service
public class ManageRiskAssessmentResultsServiceImpl extends ServiceImpl<ManageRiskAssessmentResultsMapper, ManageRiskAssessmentResults> implements ManageRiskAssessmentResultsService {
 
    @Autowired
    private UserMapper userMapper;
 
    @Value("${file.path}")
    private String imgUrl;
 
    @Override
    public IPage<ManageRiskAssessmentResultsVo> getPageResults(Page page) {
        return baseMapper.getPageResults(page, false);
    }
 
    @Override
    public void exportPersonTraining(HttpServletResponse response) {
        // 查询详情
        IPage<ManageRiskAssessmentResultsVo> detailedDtos = baseMapper.getPageResults(new Page(1, -1), true);
        if (detailedDtos.getRecords().isEmpty()) {
            throw new ErrorException("审核通过的数据为空!请审核通过后在导出");
        }
        ManageRiskAssessmentResultsVo manageRiskAssessmentResultsVo = detailedDtos.getRecords().get(0);
 
        //获取编制人的签名地址
        String writeUrl = userMapper.selectById(manageRiskAssessmentResultsVo.getEditor()).getSignatureUrl();
        if (ObjectUtils.isEmpty(writeUrl) || writeUrl.isEmpty()) {
            throw new ErrorException("找不到检验人的签名");
        }
 
        //获取复核人的签名地址
        String examineUrl = null;
        if (manageRiskAssessmentResultsVo.getApproval() != null) {
            examineUrl = userMapper.selectById(manageRiskAssessmentResultsVo.getApproval()).getSignatureUrl();
            if (StringUtils.isBlank(examineUrl)) {
                throw new ErrorException("找不到复核人的签名");
            }
        }
 
        //获取批准人的签名地址
        String ratifyUrl = null;
        if (manageRiskAssessmentResultsVo.getApproval() != null) {
            ratifyUrl = userMapper.selectById(manageRiskAssessmentResultsVo.getApproval()).getSignatureUrl();
            if (StringUtils.isBlank(ratifyUrl)) {
                throw new ErrorException("找不到复核人的签名");
            }
        }
 
        int index = 1;
        for (ManageRiskAssessmentResultsVo detailedDto : detailedDtos.getRecords()) {
            detailedDto.setIndex(index);
            index++;
        }
 
        String finalExamineUrl = examineUrl;
        String finalRatifyUrl = ratifyUrl;
        File file = new File(imgUrl + File.separator + writeUrl);
        File examine = new File(imgUrl + File.separator + finalExamineUrl);
        File ratify = new File(imgUrl + File.separator + finalRatifyUrl);
        if(!file.exists()){
            throw new ErrorException("找不到检验人的签名");
        }
        if(!examine.exists()){
            throw new ErrorException("找不到复核人的签名");
        }
        if(!ratify.exists()){
            throw new ErrorException("找不到批准人的签名");
        }
 
        // 获取路径
        InputStream inputStream = this.getClass().getResourceAsStream("/static/risk-factor-identification-risk.docx");
        Configure configure = Configure.builder()
                .bind("trainingDetailedList", new HackLoopTableRenderPolicy())
                .build();
        XWPFTemplate template = XWPFTemplate.compile(inputStream, configure).render(
                new HashMap<String, Object>() {{
                    put("trainingDetailedList", detailedDtos.getRecords());
                    put("writeUrl", StringUtils.isNotBlank(writeUrl) ? Pictures.ofLocal(imgUrl + "/" + writeUrl).create() : null);
                    put("examineUrl", StringUtils.isNotBlank(finalExamineUrl) ? Pictures.ofLocal(imgUrl + "/" + finalExamineUrl).create() : null);
                    put("ratifyUrl", StringUtils.isNotBlank(finalRatifyUrl) ? Pictures.ofLocal(imgUrl + "/" + finalRatifyUrl).create() : null);
                    put("writeDateUrl", manageRiskAssessmentResultsVo.getEditorDate() != null ?
                            Pictures.ofStream(DateImageUtil.createDateImage(manageRiskAssessmentResultsVo.getEditorDate())).create() : null);
                    put("examineDateUrl", manageRiskAssessmentResultsVo.getApproveDate() != null ?
                            Pictures.ofStream(DateImageUtil.createDateImage(manageRiskAssessmentResultsVo.getApproveDate())).create() : null);
                    put("ratifyDateUrl", manageRiskAssessmentResultsVo.getApproveDate() != null ?
                            Pictures.ofStream(DateImageUtil.createDateImage(manageRiskAssessmentResultsVo.getApproveDate())).create() : null);
                }});
        try {
            response.setContentType("application/msword");
            String fileName = URLEncoder.encode(
                    "危险因素辨识与风险评价结果一览", "UTF-8");
            response.setHeader("Content-disposition",
                    "attachment;filename=" + fileName + ".docx");
            OutputStream os = response.getOutputStream();
            template.write(os);
            os.flush();
            os.close();
        } catch (Exception e) {
            e.printStackTrace();
            throw new RuntimeException("导出失败");
        }
    }
}