value
2024-06-06 c1233db43e24655c28d6bfaab853df068f05c0ba
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
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
package com.yuanchu.mom.service.impl;
 
import com.aspose.words.License;
import com.aspose.words.SaveFormat;
import com.baomidou.mybatisplus.core.toolkit.ObjectUtils;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
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.data.Pictures;
import com.yuanchu.mom.common.GetLook;
import com.yuanchu.mom.common.PrintChina;
import com.yuanchu.mom.dto.ReportPageDto;
import com.yuanchu.mom.exception.ErrorException;
import com.yuanchu.mom.mapper.InsOrderMapper;
import com.yuanchu.mom.mapper.InsReportMapper;
import com.yuanchu.mom.mapper.UserMapper;
import com.yuanchu.mom.pojo.InsOrder;
import com.yuanchu.mom.pojo.InsReport;
import com.yuanchu.mom.service.InsReportService;
import com.yuanchu.mom.utils.QueryWrappers;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.core.io.ClassPathResource;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
 
import javax.annotation.Resource;
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.nio.file.Files;
import java.nio.file.Paths;
import java.time.LocalDateTime;
import java.util.HashMap;
import java.util.Map;
import java.util.concurrent.CompletableFuture;
 
/**
* @author Administrator
* @description 针对表【ins_report(检验报告)】的数据库操作Service实现
* @createDate 2024-03-17 22:10:02
*/
@Service
public class InsReportServiceImpl extends ServiceImpl<InsReportMapper, InsReport>
    implements InsReportService{
 
    @Resource
    private GetLook getLook;
 
    @Resource
    private UserMapper userMapper;
 
    @Resource
    private InsReportMapper insReportMapper;
 
    @Value("${wordUrl}")
    private String wordUrl;
 
    @Value("${file.path}")
    private String imgUrl;
 
    @Resource
    private InsOrderMapper insOrderMapper;
 
    @Override
    public Map<String, Object> pageInsReport(Page page, ReportPageDto reportPageDto) {
        Map<String, Object> map = new HashMap<>();
        map.put("head", PrintChina.printChina(ReportPageDto.class));
        Map<String, Integer> map1 = getLook.selectPowerByMethodAndUserId("pageInsReport");
        if (map1.get("look") == 1) reportPageDto.setCreateUser(map1.get("userId"));
        map.put("body", insReportMapper.pageInsReport(page, QueryWrappers.queryWrappers(reportPageDto)));
        return map;
    }
 
    @Override
    public int inReport(String url, Integer id) {
        InsReport insReport = new InsReport();
        insReport.setId(id);
        insReport.setUrlS(url);
        return insReportMapper.updateById(insReport);
    }
 
    @Override
    public int upReportUrl(Integer id) {
        return insReportMapper.update(null, Wrappers.<InsReport>lambdaUpdate().eq(InsReport::getId, id).set(InsReport::getUrlS, null));
    }
 
    //提交
    @Override
    public int writeReport(Integer id) {
        InsReport insReport = insReportMapper.selectById(id);
        insReport.setId(id);
        insReport.setState(1);
        insReport.setWriteUserId(getLook.selectPowerByMethodAndUserId(null).get("userId"));//提交人
        insReport.setWriteTime(LocalDateTime.now());//提交时间
        //获取提交人的签名地址
        String signatureUrl;
        try {
            signatureUrl = userMapper.selectById(insReport.getWriteUserId()).getSignatureUrl();
        }catch (Exception e){
            throw new ErrorException("找不到编制人的签名");
        }
        //系统生成报告地址
        String url = insReport.getUrl();
        //手动上传报告地址
        String urlS = insReport.getUrlS();
        wordInsertUrl(new HashMap<String, Object>(){{
            put("writeUrl", Pictures.ofLocal(imgUrl+"/"+signatureUrl).create());
        }}, (urlS==null?url:urlS).replace("/word", wordUrl));
        return insReportMapper.updateById(insReport);
    }
 
    //审核
    @Override
    public int examineReport(Integer id, Integer isExamine, String examineTell) {
        InsReport insReport = insReportMapper.selectById(id);
        insReport.setIsExamine(isExamine);
        if (ObjectUtils.isNotEmpty(examineTell)) {
            insReport.setExamineTell(examineTell);
        }
        insReport.setExamineUserId(getLook.selectPowerByMethodAndUserId(null).get("userId"));//审核人
        insReport.setExamineTime(LocalDateTime.now());//审核时间
        if (isExamine==0){
            //如果审核不通过
            insReport.setState(0);//提交状态改为待提交
            return insReportMapper.updateById(insReport);
        }
        //获取审核人的签名地址
        String signatureUrl;
        try {
            signatureUrl = userMapper.selectById(insReport.getExamineUserId()).getSignatureUrl();
        }catch (Exception e){
            throw new ErrorException("找不到审核人的签名");
        }
        //系统生成报告地址
        String url = insReport.getUrl();
        //手动上传报告地址
        String urlS = insReport.getUrlS();
        wordInsertUrl(new HashMap<String, Object>(){{
            put("examineUrl", Pictures.ofLocal(imgUrl+"/"+signatureUrl).create());
        }}, (urlS==null?url:urlS).replace("/word", wordUrl));
        return insReportMapper.updateById(insReport);
    }
 
    //批准
    @Override
    @Transactional(rollbackFor = Exception.class)
    public int ratifyReport(Integer id, Integer isRatify, String ratifyTell) {
        InsReport insReport = insReportMapper.selectById(id);
        insReport.setIsRatify(isRatify);
        if (ObjectUtils.isNotEmpty(ratifyTell)) {
            insReport.setRatifyTell(ratifyTell);
        }
        insReport.setRatifyUserId(getLook.selectPowerByMethodAndUserId(null).get("userId"));//批准人
        insReport.setRatifyTime(LocalDateTime.now());//批准时间
        if (isRatify==0){
            //如果批准不通过
            insReport.setState(0);//提交状态改为待提交
            return insReportMapper.updateById(insReport);
        }
        //获取批准人的签名地址
        String signatureUrl;
        try {
            signatureUrl = userMapper.selectById(insReport.getRatifyUserId()).getSignatureUrl();
        }catch (Exception e){
            throw new ErrorException("找不到批准人的签名");
        }
        String sealUrl;
        try {
            String laboratory = insOrderMapper.selectById(insReport.getInsOrderId()).getLaboratory();
            sealUrl = insReportMapper.getLaboratoryByName(laboratory);
        }catch (Exception e){
            throw new ErrorException("找不到报告专用章");
        }
        if(sealUrl==null) throw new ErrorException("找不到报告专用章");
        //系统生成报告地址
        String url = insReport.getUrl();
        //手动上传报告地址
        String urlS = insReport.getUrlS();
        wordInsertUrl(new HashMap<String, Object>(){{
            put("ratifyUrl", Pictures.ofLocal(imgUrl+"/"+signatureUrl).create());
            put("seal1", Pictures.ofLocal(imgUrl+"/"+sealUrl).create());
            put("seal2", Pictures.ofLocal(imgUrl+"/"+sealUrl).create());
        }}, (urlS==null?url:urlS).replace("/word", wordUrl));
        wordToPdf((urlS == null ? url : urlS).replace("/word", wordUrl));
        InsOrder insOrder = new InsOrder();
        insOrder.setId(insReportMapper.selectById(id).getInsOrderId());
        insOrder.setState(4);
        insOrderMapper.updateById(insOrder);
        return insReportMapper.updateById(insReport);
    }
 
    @Override
    public int wordInsertUrl(Map<String, Object> map, String url) {
        XWPFTemplate template = XWPFTemplate.compile(url).render(map);
        try {
            template.writeAndClose(Files.newOutputStream(Paths.get(url)));
        } catch (IOException e) {
            throw new RuntimeException(e);
        }
        return 1;
    }
 
    @Override
    public void wordToPdf(String path) {
        CompletableFuture.supplyAsync(() -> {
            try {
                wordToPdf(path, path.replace(".docx", ".pdf"));
                return null;
            } catch (Exception e) {
                throw new ErrorException("转换失败");
            }
        }).thenAccept(res -> {
        }).exceptionally(e -> {
            e.printStackTrace();
            return null;
        });
    }
 
    public String wordToPdf(String wordPath,String pdfPath) {
        FileOutputStream os = null;
        try {
            //凭证 不然切换后有水印
//            InputStream inputStream = this.getClass().getResourceAsStream("/lib/license.xml");
            /*String url;
            try {
                InputStream inputStream = this.getClass().getResourceAsStream("/lib/license.xml");
                File file = File.createTempFile("temp", ".tmp");
                OutputStream outputStream = new FileOutputStream(file);
                IOUtils.copy(inputStream, outputStream);
                url = file.getAbsolutePath();
            } catch (FileNotFoundException e) {
                throw new ErrorException("找不到模板文件");
            } catch (IOException e) {
                throw new RuntimeException(e);
            }*/
            InputStream is = new ClassPathResource("/lib/license.xml").getInputStream();
            License license = new License();
            license.setLicense(is);
            if (!license.getIsLicensed()) {
                System.out.println("License验证不通过...");
                return null;
            }
            //生成一个空的PDF文件
            File file = new File(pdfPath);
            os = new FileOutputStream(file);
            //要转换的word文件
            com.aspose.words.Document doc = new com.aspose.words.Document(wordPath);
            doc.save(os, SaveFormat.PDF);
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            if (os != null) {
                try {
                    os.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        }
        return null;
    }
}