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
package com.ruoyi.device.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.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.deepoove.poi.XWPFTemplate;
import com.deepoove.poi.config.Configure;
import com.ruoyi.basic.mapper.LaboratoryMapper;
import com.ruoyi.basic.pojo.Laboratory;
import com.ruoyi.device.dto.DeviceStateDto;
import com.ruoyi.device.mapper.DeviceStateMapper;
import com.ruoyi.device.pojo.DeviceState;
import com.ruoyi.device.service.DeviceStateService;
import com.ruoyi.inspect.util.UserUtils;
import org.springframework.stereotype.Service;
 
import jakarta.annotation.Resource;
import jakarta.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-09-26 09:51:40
 */
@Service
public class DeviceStateServiceImpl extends ServiceImpl<DeviceStateMapper, DeviceState> implements DeviceStateService {
 
    @Resource
    private LaboratoryMapper laboratoryMapper;
 
    @Override
    public IPage<DeviceStateDto> getDeviceStatePage(Integer deviceId, Page page, String processNumber) {
        return baseMapper.getDeviceStatePage(deviceId, page, processNumber);
    }
 
    @Override
    public void exportDeviceStatus(Integer deviceId, String processNumber, HttpServletResponse response) {
        // 根据流程编号 查询cnas设备状态
        DeviceStateDto deviceStateDto = baseMapper.getDeviceStatePage(deviceId,new Page<DeviceStateDto>(1,1), processNumber).getRecords().get(0);
 
        // 对时间进行修改
        DateTimeFormatter format = DateTimeFormatter.ofPattern("yyyy年MM月dd日");
        deviceStateDto.setSubmitDateString(deviceStateDto.getSubmitDate() != null ? deviceStateDto.getSubmitDate().format(format) : "  年 月 日");
        deviceStateDto.setDepartmentDateString(deviceStateDto.getDepartmentDate() != null? deviceStateDto.getDepartmentDate().format(format) : "  年 月 日");
        deviceStateDto.setMeasuringRoomDateString(deviceStateDto.getMeasuringRoomDate() != null? deviceStateDto.getMeasuringRoomDate().format(format) : "  年 月 日");
        deviceStateDto.setApprovalDateString(deviceStateDto.getApprovalDate() != null? deviceStateDto.getApprovalDate().format(format) : "  年 月 日");
 
        // 查询设备属于哪个实验室
        String laboratoryName;
        String largeCategory = deviceStateDto.getLargeCategory();
        if (StringUtils.isNotBlank(largeCategory)) {
            largeCategory = largeCategory.substring(0, 1);
            Laboratory laboratory = laboratoryMapper.selectOne(Wrappers.<Laboratory>lambdaQuery()
                    .eq(Laboratory::getLaboratoryNumber, largeCategory)
                    .select(Laboratory::getLaboratoryName));
            laboratoryName = laboratory != null ? laboratory.getLaboratoryName() : "";
        } else {
            laboratoryName = "";
        }
 
        // 获取路径
        InputStream inputStream = this.getClass().getResourceAsStream("/static/word/device-status.docx");
        Configure configure = Configure.builder()
                .build();
        XWPFTemplate template = XWPFTemplate.compile(inputStream, configure).render(
                new HashMap<String, Object>() {{
                    put("deviceStateDto", deviceStateDto);
                    // 签名兜底: 人员未配置签名或签名文件缺失时返回透明图片, 保证导出不因空图片报错
                    put("submitOperatingPersonnelUrl", UserUtils.getFinalUserSignatureOrTransparent(deviceStateDto.getSubmitOperatingPersonnel()));
                    put("departmentNextPesponsibleUrl", UserUtils.getFinalUserSignatureOrTransparent(deviceStateDto.getDepartmentNextPesponsible()));
                    put("measuringRoomNextPesponsibleUrl", UserUtils.getFinalUserSignatureOrTransparent(deviceStateDto.getMeasuringRoomNextPesponsible()));
                    put("approvalNextPesponsibleUrl", UserUtils.getFinalUserSignatureOrTransparent(deviceStateDto.getApprovalNextPesponsible()));
                    put("laboratory", laboratoryName);
                }});
 
        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();
            inputStream.close();
        } catch (Exception e) {
            e.printStackTrace();
            throw new RuntimeException("导出失败");
        }
    }
}