From 4f3a98f19143865cdc1de4791e8a95d96bd40c65 Mon Sep 17 00:00:00 2001
From: maven <2163098428@qq.com>
Date: 星期五, 01 八月 2025 13:27:59 +0800
Subject: [PATCH] yys 密码已重置

---
 cnas-device/src/main/java/com/ruoyi/device/service/impl/DeviceAcceptanceServiceImpl.java |  160 +++++++++++++++++++++++++++++++++++++++++++++++++++++
 1 files changed, 160 insertions(+), 0 deletions(-)

diff --git a/cnas-device/src/main/java/com/ruoyi/device/service/impl/DeviceAcceptanceServiceImpl.java b/cnas-device/src/main/java/com/ruoyi/device/service/impl/DeviceAcceptanceServiceImpl.java
new file mode 100644
index 0000000..2cdb61e
--- /dev/null
+++ b/cnas-device/src/main/java/com/ruoyi/device/service/impl/DeviceAcceptanceServiceImpl.java
@@ -0,0 +1,160 @@
+package com.ruoyi.device.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.ruoyi.common.utils.QueryWrappers;
+import com.ruoyi.device.mapper.DeviceAcceptanceFileMapper;
+import com.ruoyi.device.mapper.DeviceAcceptanceMapper;
+import com.ruoyi.device.mapper.DeviceMapper;
+import com.ruoyi.device.pojo.Device;
+import com.ruoyi.device.pojo.DeviceAcceptance;
+import com.ruoyi.device.pojo.DeviceAcceptanceFile;
+import com.ruoyi.device.service.DeviceAcceptanceService;
+import com.ruoyi.framework.exception.ErrorException;
+import com.ruoyi.inspect.util.HackLoopTableRenderPolicy;
+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();
+            inputStream.close();
+        } catch (Exception e) {
+            e.printStackTrace();
+            throw new RuntimeException("瀵煎嚭澶辫触");
+        }
+    }
+}

--
Gitblit v1.9.3