zss
6 天以前 51ec98113c6d49d0f7eec4e3c030e55e337e97db
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
package com.yuanchu.mom.service.impl;
 
import com.baomidou.mybatisplus.core.toolkit.StringUtils;
import com.deepoove.poi.XWPFTemplate;
import com.deepoove.poi.config.Configure;
import com.deepoove.poi.config.ConfigureBuilder;
import com.deepoove.poi.data.Pictures;
import com.yuanchu.mom.dto.PersonSupervisionProcessingSheetDto;
import com.yuanchu.mom.exception.ErrorException;
import com.yuanchu.mom.mapper.UserMapper;
import com.yuanchu.mom.pojo.PersonSupervisionProcessingSheet;
import com.yuanchu.mom.mapper.PersonSupervisionProcessingSheetMapper;
import com.yuanchu.mom.service.PersonSupervisionProcessingSheetService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;
 
import javax.annotation.Resource;
import javax.servlet.http.HttpServletResponse;
import java.io.InputStream;
import java.io.OutputStream;
import java.net.URLEncoder;
import java.time.format.DateTimeFormatter;
import java.util.HashMap;
 
/**
 * <p>
 * 监督记录 - 处理单 服务实现类
 * </p>
 *
 * @author 
 * @since 2024-10-10 05:35:58
 */
@Service
public class PersonSupervisionProcessingSheetServiceImpl extends ServiceImpl<PersonSupervisionProcessingSheetMapper, PersonSupervisionProcessingSheet> implements PersonSupervisionProcessingSheetService {
 
    @Resource
    private UserMapper userMapper;
 
    @Value("${file.path}")
    private String imgUrl;
 
    /**
     * 导出人员监督记录纠正处理单
     * @param supervisionRecordId
     * @param response
     */
    @Override
    public void exportSupervisionProcessingSheet(Integer supervisionRecordId, HttpServletResponse response) {
        PersonSupervisionProcessingSheetDto processingSheetDto = baseMapper.selectProcessingSheet(supervisionRecordId);
 
        // 提出人签名
        String raiseUrl = null;
        if (processingSheetDto.getProposingDepartmentPersonId() != null) {
            raiseUrl = userMapper.selectById(processingSheetDto.getProposingDepartmentPersonId()).getSignatureUrl();
            if (StringUtils.isBlank(raiseUrl)) {
                throw new ErrorException("找不到提出人的签名");
            }
        }
 
        // 原因分析人
        String causeUrl = null;
        if (processingSheetDto.getCauseAnalysisPersonId() != null) {
            causeUrl = userMapper.selectById(processingSheetDto.getCauseAnalysisPersonId()).getSignatureUrl();
            if (StringUtils.isBlank(causeUrl)) {
                throw new ErrorException("找不到原因分析人的签名");
            }
        }
 
        // 纠正人
        String correctUrl = null;
        if (processingSheetDto.getCorrectiveActionId() != null) {
            correctUrl = userMapper.selectById(processingSheetDto.getCorrectiveActionId()).getSignatureUrl();
            if (StringUtils.isBlank(correctUrl)) {
                throw new ErrorException("找不到纠正人的签名");
            }
        }
 
        // 验证人
        String validationUrl = null;
        if (processingSheetDto.getVerificationDepartmentPersonId() != null) {
            validationUrl = userMapper.selectById(processingSheetDto.getVerificationDepartmentPersonId()).getSignatureUrl();
            if (StringUtils.isBlank(validationUrl)) {
                throw new ErrorException("找不到验证人的签名");
            }
        }
 
        // 获取路径
        InputStream inputStream = this.getClass().getResourceAsStream("/static/supervision-processing-sheet.docx");
        ConfigureBuilder builder = Configure.builder();
        builder.useSpringEL(true);
        String finalRaiseUrl = raiseUrl;
        String finalCauseUrl = causeUrl;
        String finalCorrectUrl = correctUrl;
        String finalValidationUrl = validationUrl;
        XWPFTemplate template = XWPFTemplate.compile(inputStream, builder.build()).render(
                new HashMap<String, Object>() {{
                    put("processing", processingSheetDto);
                    put("raiseUrl", StringUtils.isNotBlank(finalRaiseUrl) ? Pictures.ofLocal(imgUrl + "/" + finalRaiseUrl).create() : null);
                    put("causeUrl", StringUtils.isNotBlank(finalCauseUrl) ? Pictures.ofLocal(imgUrl + "/" + finalCauseUrl).create() : null);
                    put("correctUrl", StringUtils.isNotBlank(finalCorrectUrl) ? Pictures.ofLocal(imgUrl + "/" + finalCorrectUrl).create() : null);
                    put("validationUrl", StringUtils.isNotBlank(finalValidationUrl) ? Pictures.ofLocal(imgUrl + "/" + finalValidationUrl).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("导出失败");
        }
    }
}