车间能级提升-智能设备管理系统
baoshiwei
2025-04-12 beaed6d077e7c3e9abfad68acb8c587835b5a406
eims/ruoyi-modules/lb-eims/src/main/java/org/dromara/eims/controller/EimsMaintPlanController.java
@@ -1,11 +1,17 @@
package org.dromara.eims.controller;
import java.io.IOException;
import java.io.InputStream;
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 +27,7 @@
import org.dromara.eims.domain.bo.EimsMaintPlanBo;
import org.dromara.eims.service.IEimsMaintPlanService;
import org.dromara.common.mybatis.core.page.TableDataInfo;
import org.springframework.web.multipart.MultipartFile;
/**
 * 保养计划
@@ -65,7 +72,7 @@
    @SaCheckPermission("eims:maintPlan:query")
    @GetMapping("/{id}")
    public R<EimsMaintPlanVo> getInfo(@NotNull(message = "主键不能为空")
                                     @PathVariable Long id) {
                                      @PathVariable Long id) {
        return R.ok(eimsMaintPlanService.queryById(id));
    }
@@ -103,4 +110,55 @@
                          @PathVariable Long[] ids) {
        return toAjax(eimsMaintPlanService.deleteWithValidByIds(List.of(ids), true));
    }
    /**
     * 导入数据
     *
     * @param file          导入文件
     * @param updateSupport 是否更新已存在数据
     */
    @Log(title = "保养计划", businessType = BusinessType.IMPORT)
    @SaCheckPermission("eims:maintPlan:import")
    @PostMapping(value = "/importData", consumes = MediaType.MULTIPART_FORM_DATA_VALUE)
    public R<Void> importData(@RequestPart("file") MultipartFile file, boolean updateSupport) throws Exception {
        //ExcelResult<MaintCheckItemVo> result = ExcelUtil.importExcel(file.getInputStream(), MaintCheckItemVo.class, new MaintCheckItemImportListener(updateSupport));
        String res = eimsMaintPlanService.importData(file, updateSupport);
        return R.ok(res);
    }
    /**
     * 获取导入模板
     */
    @PostMapping("/importTemplate")
    public void importTemplate(HttpServletResponse response) throws IOException {
        // 设置响应类型
        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 + ".xls");
        // 读取模板文件
        ClassPathResource templateResource = new ClassPathResource("template/by.xls");
        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);
        }
    }
}