zss
2025-02-17 087991c76f078defe5eb55d84223021b4199fb3d
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
package com.yuanchu.mom.service.impl;
 
import com.baomidou.mybatisplus.core.metadata.IPage;
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.yuanchu.mom.exception.ErrorException;
import com.yuanchu.mom.mapper.DeviceAcceptanceFileMapper;
import com.yuanchu.mom.mapper.DeviceAcceptanceMapper;
import com.yuanchu.mom.mapper.DeviceMapper;
import com.yuanchu.mom.pojo.Device;
import com.yuanchu.mom.pojo.DeviceAcceptance;
import com.yuanchu.mom.pojo.DeviceAcceptanceFile;
import com.yuanchu.mom.service.DeviceAcceptanceService;
import com.yuanchu.mom.utils.HackLoopTableRenderPolicy;
import com.yuanchu.mom.utils.QueryWrappers;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;
import org.springframework.web.multipart.MultipartFile;
 
import javax.annotation.Resource;
import javax.servlet.http.HttpServletResponse;
import java.io.File;
import java.io.InputStream;
import java.io.OutputStream;
import java.net.URLEncoder;
import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;
import java.util.HashMap;
 
/**
 * <p>
 * 设备验收(装备) 服务实现类
 * </p>
 *
 * @author 江苏鵷雏网络科技有限公司
 * @since 2024-12-20 01:45:14
 */
@Service
public class DeviceAcceptanceServiceImpl extends ServiceImpl<DeviceAcceptanceMapper, DeviceAcceptance> implements DeviceAcceptanceService {
 
    @Resource
    private DeviceAcceptanceFileMapper deviceAcceptanceFileMapper;
 
    @Resource
    private DeviceMapper deviceMapper;
 
    @Value("${file.path}")
    private String imgUrl;
 
    @Value("${wordUrl}")
    private String wordUrl;
 
    /**
     * 设备验收列表
     * @param page
     * @param deviceAcceptance
     * @return
     */
    @Override
    public IPage<DeviceAcceptance> pageDeviceAcceptance(Page page, DeviceAcceptance deviceAcceptance) {
        return baseMapper.pageDeviceAcceptance(page, QueryWrappers.queryWrappers(deviceAcceptance));
    }
 
    /**
     * 设备验收附件
     * @param acceptanceId
     * @param file
     * @return
     */
    @Override
    public boolean uploadDeviceAcceptanceFile(Integer acceptanceId, MultipartFile file) {
        if (acceptanceId == null) {
            throw new ErrorException("缺少验收id");
        }
 
        String urlString;
        String pathName;
        String path;
        String filename = file.getOriginalFilename();
        String contentType = file.getContentType();
        DeviceAcceptanceFile acceptanceFile = new DeviceAcceptanceFile();
        acceptanceFile.setAcceptanceId(acceptanceId);
        acceptanceFile.setFileName(filename);
        if (contentType != null && contentType.startsWith("image/")) {
            // 是图片
            path = imgUrl;
            acceptanceFile.setType(1);
        } else {
            // 是文件
            path = wordUrl;
            acceptanceFile.setType(2);
        }
        try {
            File realpath = new File(path);
            if (!realpath.exists()) {
                realpath.mkdirs();
            }
            pathName = LocalDateTime.now().format(DateTimeFormatter.ofPattern("yyMMddHHmmss")) + "_" + file.getOriginalFilename();
            urlString = realpath + "/" + pathName;
            file.transferTo(new File(urlString));
            acceptanceFile.setFileUrl(pathName);
            deviceAcceptanceFileMapper.insert(acceptanceFile);
            return true;
        } catch (Exception e) {
            e.printStackTrace();
            System.err.println("附件上传错误");
            return false;
        }
    }
 
    /**
     * 设备验收导出
     * @param acceptanceId  设备验收id
     * @param response   响应体
     * @return
     */
    @Override
    public void exportDeviceAcceptance(Integer acceptanceId, HttpServletResponse response) {
        DeviceAcceptance deviceAcceptance = baseMapper.selectById(acceptanceId);
        if (deviceAcceptance == null) {
            throw new ErrorException("设备验收不存在");
        }
        Device device = null;
        if (deviceAcceptance.getDeviceId() != null) {
            device = deviceMapper.selectById(deviceAcceptance.getDeviceId());
        }
 
 
        // 获取路径
        InputStream inputStream = this.getClass().getResourceAsStream("/static/word/acceptance-certificate.docx");
        Configure configure = Configure.builder()
                .bind("deviceInspectionRecordDetailsList", new HackLoopTableRenderPolicy())
                .build();
        Device finalDevice = device;
        String deviceName = device.getDeviceName() == null ? "" : device.getDeviceName();
        XWPFTemplate template = XWPFTemplate.compile(inputStream, configure).render(
                new HashMap<String, Object>() {{
                    put("deviceAcceptance", deviceAcceptance);
                    put("device", finalDevice);
                }});
 
        try {
            response.setContentType("application/msword");
            String fileName = URLEncoder.encode(
                    deviceName+ "验收单", "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("导出失败");
        }
    }
}