whyclxw
28 分钟以前 e196f42e9a977c0b2f8452dce875b110fb76e92f
src/main/java/com/whyc/service/DeviceSpareService.java
@@ -1,5 +1,7 @@
package com.whyc.service;
import cn.afterturn.easypoi.excel.ExcelExportUtil;
import cn.afterturn.easypoi.excel.entity.TemplateExportParams;
import com.baomidou.mybatisplus.core.conditions.Wrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
@@ -13,21 +15,23 @@
import com.whyc.util.CommonUtil;
import com.whyc.util.ThreadLocalUtil;
import org.apache.commons.lang.StringUtils;
import org.apache.poi.openxml4j.opc.OPCPackage;
import org.apache.poi.ss.usermodel.*;
import org.apache.poi.xssf.usermodel.*;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.core.io.ClassPathResource;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.multipart.MultipartFile;
import javax.annotation.Resource;
import javax.servlet.http.HttpServletResponse;
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.net.URLEncoder;
import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;
import java.util.*;
@Service
public class DeviceSpareService {
@@ -319,4 +323,57 @@
    private void updateQuantityAndPictureBatch(List<DeviceSpare> spareListUpdate) {
        mapper.updateQuantityAndPictureBatch(spareListUpdate);
    }
    public void excelExport(HttpServletResponse response) {
        //查询所有的设备器件
        List<DeviceSpare> deviceSpareList = mapper.selectList((Wrapper<DeviceSpare>) CommonUtil.nullObject);
        //获取导出模板地址
        ClassPathResource classPathResource = new ClassPathResource("excel_templates/template_device_spare.xlsx");
        String path = classPathResource.getPath();
        TemplateExportParams templateExportParams1 = new TemplateExportParams(path,true);
        // 构建导出数据模型
        Map<String, Object> map = new HashMap<>();
        map.put("deviceSpareList", deviceSpareList); // 假设模板中使用 ${deviceSpareList} 作为变量名
        Workbook wb = ExcelExportUtil.exportExcel(templateExportParams1, map);
        try {
            LocalDateTime now = LocalDateTime.now();
            String fileName = "维修管理器件库存_"+now.format(DateTimeFormatter.ofPattern("yyyyMMddHHmmss"))+".xlsx";
            response.setContentType("application/vnd.ms-excel");
            response.setHeader("Content-Disposition", "attachment;filename=" + URLEncoder.encode(fileName, "utf-8"));
            response.flushBuffer();
            wb.write(response.getOutputStream());
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
    public Response andOrChangePicture(Integer id, MultipartFile file) throws IOException {
        DeviceSpare spare = mapper.selectById(id);
        //对存储路径进行定义
        Date now = new Date();
        String timeFormat = ThreadLocalUtil.format(ThreadLocalUtil.TIME_YYYY_MM_DD_HH_MM_SS_UNION, now);
        String dirMonth = ThreadLocalUtil.format(ThreadLocalUtil.TIME_YYYY_MM, now);
        String fileDirPath = CommonUtil.getRootFile() + "deviceSpare" + File.separator + dirMonth;
        File fileDir = new File(fileDirPath);
        //如果文件夹不存在则创建
        if (!fileDir.exists()) {
            fileDir.mkdirs();
        }
        String filePath = fileDirPath + File.separator + spare.getName()+"_"+spare.getModel()+"_"+spare.getVersion() + "_"+ timeFormat+".png";
        // 保存图片到本地
        file.transferTo(new File(filePath));
        String split = "pis_file"+File.separator+"deviceSpare";
        String picUrl = File.separator + filePath.substring(filePath.indexOf(split));
        //更新图片
        UpdateWrapper<DeviceSpare> update = Wrappers.update();
        update.eq("id",id);
        update.set("picture_url",picUrl);
        mapper.update((DeviceSpare) CommonUtil.nullObject,update);
        //记录变更
        deviceSpareLogService.add(id,2,"更换图片",now);
        return new Response().setII(1,"新增或者替换图片完成");
    }
}