zhuo
2025-04-17 3baa0061112a555d75c6bc606d5271ec842ac607
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
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.Pictures;
 
import com.ruoyi.common.utils.DateImageUtil;
import com.ruoyi.framework.exception.ErrorException;
import com.ruoyi.inspect.util.HackLoopTableRenderPolicy;
import com.ruoyi.manage.mapper.ManageControlPlanListMapper;
 
import com.ruoyi.manage.pojo.ManageControlPlanList;
import com.ruoyi.manage.service.ManageControlPlanListService;
 
 
import com.ruoyi.manage.vo.ManageControlPlanListVo;
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.InputStream;
import java.io.OutputStream;
import java.net.URLEncoder;
import java.util.HashMap;
 
/**
 * <p>
 * 重大风险因素分析及控制计划清单 服务实现类
 * </p>
 *
 * @author
 * @since 2024-11-15 02:58:30
 */
@Service
public class ManageControlPlanListServiceImpl extends ServiceImpl<ManageControlPlanListMapper, ManageControlPlanList> implements ManageControlPlanListService {
 
    @Autowired
    private UserMapper userMapper;
 
    @Value("${file.path}")
    private String imgUrl;
 
    @Override
    public IPage<ManageControlPlanListVo> getPageList(Page page) {
        return baseMapper.getPageList(page, false);
    }
 
    @Override
    public void exportPersonTraining(HttpServletResponse response) {
        // 查询详情
        IPage<ManageControlPlanListVo> detailedDtos = baseMapper.getPageList(new Page(1, -1), true);
        if (detailedDtos.getRecords().isEmpty()) {
            throw new ErrorException("审核通过的数据为空!请审核通过后在导出");
        }
        ManageControlPlanListVo manageRiskAssessmentResultsVo = detailedDtos.getRecords().get(0);
 
        //获取编制人的签名地址
        String writeUrl = userMapper.selectById(manageRiskAssessmentResultsVo.getEditor()).getSignatureUrl();
        if (ObjectUtils.isEmpty(writeUrl) || writeUrl.equals("")) {
            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 (ManageControlPlanListVo detailedDto : detailedDtos.getRecords()) {
            detailedDto.setIndex(index);
            index++;
        }
        // 获取路径
        InputStream inputStream = this.getClass().getResourceAsStream("/static/analysis-risk-factors.docx");
        String finalExamineUrl = examineUrl;
        String finalRatifyUrl = ratifyUrl;
        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("导出失败");
        }
    }
}