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.metadata.IPage;
import com.baomidou.mybatisplus.core.toolkit.StringUtils;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.deepoove.poi.XWPFTemplate;
import com.deepoove.poi.config.Configure;
import com.deepoove.poi.data.Pictures;
import com.yuanchu.mom.dto.FePowerStableDto;
import com.yuanchu.mom.dto.FePowerStableExportDto;
import com.yuanchu.mom.exception.ErrorException;
import com.yuanchu.mom.mapper.FeMeasuredQuantityMapper;
import com.yuanchu.mom.mapper.UserMapper;
import com.yuanchu.mom.pojo.FeMeasuredQuantity;
import com.yuanchu.mom.pojo.FePowerStable;
import com.yuanchu.mom.mapper.FePowerStableMapper;
import com.yuanchu.mom.service.FePowerStableService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.yuanchu.mom.utils.HackLoopTableRenderPolicy;
import com.yuanchu.mom.utils.DateImageUtil;
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.*;
 
/**
 * <p>
 * 设施和环境条件-设施和环境条件要求-电源稳定性 服务实现类
 * </p>
 *
 * @author 
 * @since 2024-11-07 04:16:52
 */
@Service
public class FePowerStableServiceImpl extends ServiceImpl<FePowerStableMapper, FePowerStable> implements FePowerStableService {
 
    @Resource
    private FeMeasuredQuantityMapper feMeasuredQuantityMapper;
    @Resource
    private UserMapper userMapper;
 
    @Value("${file.path}")
    private String imgUrl;
 
    @Override
    public IPage<FePowerStableDto> getLaboratoryFacilityPowerStablePage(Page page) {
        return baseMapper.getLaboratoryFacilityPowerStablePage(page);
    }
 
    @Override
    public Map<String, Objects> getCalibrationDate(Integer deviceId) {
        return baseMapper.getCalibrationDate(deviceId);
    }
 
    /**
     * 导出电源稳定性
     * @param powerStableId
     * @param response
     */
    @Override
    public void exportFePowerStable(Integer powerStableId, HttpServletResponse response) {
        FePowerStableExportDto powerStable = baseMapper.selectPowerStable(powerStableId);
 
        // 检测人
        String testerUrl = null;
        if (powerStable.getTesterId() != null) {
            testerUrl = userMapper.selectById(powerStable.getTesterId()).getSignatureUrl();
            if (StringUtils.isBlank(testerUrl)) {
                throw new ErrorException("找不到检测人的签名");
            }
        }
 
        // 核查人
        String checkerUrl = null;
        if (powerStable.getCheckerId() != null) {
            checkerUrl = userMapper.selectById(powerStable.getCheckerId()).getSignatureUrl();
            if (StringUtils.isBlank(checkerUrl)) {
                throw new ErrorException("找不到核查人的签名");
            }
        }
 
        // 查询详情
        List<FeMeasuredQuantity> feMeasuredQuantities = feMeasuredQuantityMapper.selectList(Wrappers.<FeMeasuredQuantity>lambdaQuery()
                .eq(FeMeasuredQuantity::getPowerStableId, powerStableId));
 
        // 获取路径
        InputStream inputStream = this.getClass().getResourceAsStream("/static/power-stable.docx");
        Configure configure = Configure.builder()
                .bind("measuredQuantityList", new HackLoopTableRenderPolicy())
                .build();
        String finalTesterUrl = testerUrl;
        String finalCheckerUrl = checkerUrl;
        XWPFTemplate template = XWPFTemplate.compile(inputStream, configure).render(
                new HashMap<String, Object>() {{
                    put("stable", powerStable);
                    put("measuredQuantityList", feMeasuredQuantities);
                    put("testerUrl", StringUtils.isNotBlank(finalTesterUrl) ? Pictures.ofLocal(imgUrl + "/" + finalTesterUrl).create() : null);
                    put("checkerUrl", StringUtils.isNotBlank(finalCheckerUrl) ? Pictures.ofLocal(imgUrl + "/" + finalCheckerUrl).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("导出失败");
        }
    }
}