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
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
package com.yuanchu.mom.service.impl;
 
import cn.hutool.core.util.StrUtil;
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.PersonSupervisionControlSheetExportDto;
import com.yuanchu.mom.exception.ErrorException;
import com.yuanchu.mom.mapper.UserMapper;
import com.yuanchu.mom.pojo.PersonSupervisionControlSheet;
import com.yuanchu.mom.mapper.PersonSupervisionControlSheetMapper;
import com.yuanchu.mom.service.PersonSupervisionControlSheetService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
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.util.HashMap;
import java.util.List;
 
/**
 * <p>
 * 监督记录 - 控制单 服务实现类
 * </p>
 *
 * @author
 * @since 2024-10-10 05:18:57
 */
@Service
public class PersonSupervisionControlSheetServiceImpl extends ServiceImpl<PersonSupervisionControlSheetMapper, PersonSupervisionControlSheet> implements PersonSupervisionControlSheetService {
 
    @Resource
    private UserMapper userMapper;
 
    @Value("${file.path}")
    private String imgUrl;
 
    /**
     * 导出人员监督记录纠正控制单
     * @param supervisionRecordId
     * @param response
     */
    @Override
    public void exportSupervisionControlSheet(Integer supervisionRecordId, HttpServletResponse response) {
        PersonSupervisionControlSheetExportDto controlSheetExportDto = baseMapper.selectSupervisionControl(supervisionRecordId);
 
        // 判断不符合工作发现途径是否为空
        if (StringUtils.isNotBlank(controlSheetExportDto.getDiscoveryApproach())) {
            List<String> splitList = StrUtil.split(controlSheetExportDto.getDiscoveryApproach(), ',');
            for (String s : splitList) {
                switch (s) {
                    case "0":
                        controlSheetExportDto.setDiscoveryApproach0("☑");
                        break;
                    case "1":
                        controlSheetExportDto.setDiscoveryApproach1("☑");
                        break;
                    case "2":
                        controlSheetExportDto.setDiscoveryApproach2("☑");
                        break;
                    case "3":
                        controlSheetExportDto.setDiscoveryApproach3("☑");
                        break;
                    case "4":
                        controlSheetExportDto.setDiscoveryApproach4("☑");
                        break;
                    case "5":
                        controlSheetExportDto.setDiscoveryApproach5("☑");
                        break;
                    case "6":
                        controlSheetExportDto.setDiscoveryApproach6("☑");
                        break;
                    case "7":
                        controlSheetExportDto.setDiscoveryApproach7("☑");
                        break;
                }
            }
        }
        // 发现部门
        String discovererUrl = null;
        if (controlSheetExportDto.getDepartmentHeadId() != null) {
            discovererUrl = userMapper.selectById(controlSheetExportDto.getDepartmentHeadId()).getSignatureUrl();
            if (StringUtils.isBlank(discovererUrl)) {
                throw new ErrorException("找不到发现部门人的签名");
            }
        }
 
        // 处理措施负责人
        String responsibleUrl = null;
        if (controlSheetExportDto.getResponsibleDepartmentPersonId() != null) {
            responsibleUrl = userMapper.selectById(controlSheetExportDto.getResponsibleDepartmentPersonId()).getSignatureUrl();
            if (StringUtils.isBlank(responsibleUrl)) {
                throw new ErrorException("找不到处理措施负责人的签名");
            }
        }
 
        // 技术负责人
        String correctiveUrl = null;
        if (controlSheetExportDto.getResponsibleDepartmentPersonId() != null) {
            correctiveUrl = userMapper.selectById(controlSheetExportDto.getResponsibleDepartmentPersonId()).getSignatureUrl();
            if (StringUtils.isBlank(correctiveUrl)) {
                throw new ErrorException("找不到技术负责人的签名");
            }
        }
 
        // 质量负责人
        String qualityUrl = null;
        if (controlSheetExportDto.getQualitySupervisorId() != null) {
            qualityUrl = userMapper.selectById(controlSheetExportDto.getQualitySupervisorId()).getSignatureUrl();
            if (StringUtils.isBlank(qualityUrl)) {
                throw new ErrorException("找不到质量负责人的签名");
            }
        }
 
        // 获取路径
        InputStream inputStream = this.getClass().getResourceAsStream("/static/supervision-control-sheet.docx");
        ConfigureBuilder builder = Configure.builder();
        builder.useSpringEL(true);
        String finalDiscovererUrl = discovererUrl;
        String finalResponsibleUrl = responsibleUrl;
        String finalCorrectiveUrl = correctiveUrl;
        String finalQualityUrl = qualityUrl;
        XWPFTemplate template = XWPFTemplate.compile(inputStream, builder.build()).render(
                new HashMap<String, Object>() {{
                    put("control", controlSheetExportDto);
                    put("discovererUrl", StringUtils.isNotBlank(finalDiscovererUrl) ? Pictures.ofLocal(imgUrl + "/" + finalDiscovererUrl).create() : null);
                    put("responsibleUrl", StringUtils.isNotBlank(finalResponsibleUrl) ? Pictures.ofLocal(imgUrl + "/" + finalResponsibleUrl).create() : null);
                    put("correctiveUrl", StringUtils.isNotBlank(finalCorrectiveUrl) ? Pictures.ofLocal(imgUrl + "/" + finalCorrectiveUrl).create() : null);
                    put("qualityUrl", StringUtils.isNotBlank(finalQualityUrl) ? Pictures.ofLocal(imgUrl + "/" + finalQualityUrl).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("导出失败");
        }
 
 
    }
}