车间能级提升-智能设备管理系统
zhuguifei
2025-06-06 00cf6c13531447af59a8d2121bbfb1a3a1f68c0d
eims/ruoyi-modules/lb-eims/src/main/java/org/dromara/eims/controller/EimsInspectPlanController.java
@@ -1,11 +1,18 @@
package org.dromara.eims.controller;
import java.io.IOException;
import java.io.InputStream;
import java.io.UnsupportedEncodingException;
import java.net.URLEncoder;
import java.util.List;
import jakarta.servlet.ServletOutputStream;
import lombok.RequiredArgsConstructor;
import jakarta.servlet.http.HttpServletResponse;
import jakarta.validation.constraints.*;
import cn.dev33.satoken.annotation.SaCheckPermission;
import org.springframework.core.io.ClassPathResource;
import org.springframework.http.MediaType;
import org.springframework.web.bind.annotation.*;
import org.springframework.validation.annotation.Validated;
import org.dromara.common.idempotent.annotation.RepeatSubmit;
@@ -21,6 +28,7 @@
import org.dromara.eims.domain.bo.EimsInspectPlanBo;
import org.dromara.eims.service.IEimsInspectPlanService;
import org.dromara.common.mybatis.core.page.TableDataInfo;
import org.springframework.web.multipart.MultipartFile;
/**
 * 点检计划
@@ -103,4 +111,52 @@
                          @PathVariable Long[] ids) {
        return toAjax(eimsInspectPlanService.deleteWithValidByIds(List.of(ids), true));
    }
    /**
     * 导入数据
     *
     * @param file          导入文件
     * @param updateSupport 是否更新已存在数据
     */
    @Log(title = "点检计划", businessType = BusinessType.IMPORT)
    @SaCheckPermission("eims:inspectPlan:import")
    @PostMapping(value = "/importData", consumes = MediaType.MULTIPART_FORM_DATA_VALUE)
    public R<Void> importData(@RequestPart("files") List<MultipartFile> files, boolean updateSupport) throws Exception {
        StringBuilder successMsg = new StringBuilder();
        for (MultipartFile file : files) {
            String res = eimsInspectPlanService.importData(file, updateSupport);
            successMsg.append(res).append(";");
        }
        return R.ok(successMsg.toString());
    }
    /**
     * 获取导入模板
     */
    @PostMapping("/importTemplate")
    public void importTemplate(HttpServletResponse response) throws UnsupportedEncodingException {
        // 设置响应类型
        response.setContentType("application/vnd.openxmlformats-officedocument.spreadsheetml.sheet");
        response.setCharacterEncoding("utf-8");
        // 设置文件名
        String fileName = URLEncoder.encode("点检计划导入模板", "UTF-8");
        response.setHeader("Content-Disposition",
            "attachment;filename=" + fileName + ".xlsx");
        // 读取模板文件
        ClassPathResource templateResource = new ClassPathResource("template/dj.xlsx");
        try (InputStream inputStream = templateResource.getInputStream();
             ServletOutputStream outputStream = response.getOutputStream()) {
            // 流拷贝
            byte[] buffer = new byte[1024];
            int bytesRead;
            while ((bytesRead = inputStream.read(buffer)) != -1) {
                outputStream.write(buffer, 0, bytesRead);
            }
        } catch (IOException e) {
            throw new RuntimeException("模板文件读取失败", e);
        }
    }
}