whyclxw
3 小时以前 e196f42e9a977c0b2f8452dce875b110fb76e92f
src/main/java/com/whyc/service/DeviceSpareService.java
@@ -1,5 +1,8 @@
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;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
@@ -12,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 {
@@ -69,10 +74,10 @@
        if (file != null && file.size() > 0) {
            for (int i = 0; i < file.size(); i++) {
                MultipartFile multipartFile = file.get(i);
                String fileName = multipartFile.getOriginalFilename();
                //String fileName = multipartFile.getOriginalFilename();
                //将fileName中可能存在的,去掉
                fileName = fileName.replace(",","");
                String filePath = fileDirPath + File.separator + timeFormat+"_"+fileName;
                //fileName = fileName.replace(",","");
                String filePath = fileDirPath + File.separator + spare.getName()+"_"+spare.getModel()+"_"+spare.getVersion() + "_" + (i+1) + "_"+ timeFormat+".png";
                multipartFile.transferTo(new File(filePath));
                String split = "pis_file"+File.separator+"deviceSpare";
@@ -235,7 +240,7 @@
        }
        //第七列为图片-浮动式图片 TODO 待处理 处理已存在的物料
        //第七列为图片-浮动式图片
        //获取绘图对象中的所有图形
        XSSFDrawing drawing = (XSSFDrawing) sheet.getDrawingPatriarch();
        if (drawing == null) {
@@ -261,7 +266,7 @@
                if (!fileDir.exists()) {
                    fileDir.mkdirs();
                }
                String filePath = fileDirPath + File.separator + timeFormat+"_"+spare.getName()+"_"+spare.getModel()+"_"+spare.getVersion()+".png";
                String filePath = fileDirPath + File.separator + spare.getName()+"_"+spare.getModel()+"_"+spare.getVersion() + "_"+ timeFormat+".png";
                // 保存图片到本地
                try (FileOutputStream fos = new FileOutputStream(filePath)) {
@@ -271,12 +276,104 @@
                String picUrl = File.separator + filePath.substring(filePath.indexOf(split));
                spare.setPictureUrl(picUrl);
            }
        }
        //新增列表
        List<DeviceSpare> spareListNew = new ArrayList<>();
        //更新列表
        List<DeviceSpare> spareListUpdate = new ArrayList<>();
        //查询库中的所有设备器件
        List<DeviceSpare> deviceSpareListInDB = mapper.selectList((Wrapper<DeviceSpare>) CommonUtil.nullObject);
        for (DeviceSpare spare : spareList){
            boolean isExist = false;
            for (DeviceSpare spareInDB : deviceSpareListInDB){
                if (spare.getName().equals(spareInDB.getName())
                        && spare.getModel().equals(spareInDB.getModel())
                        && spare.getVersion().equals(spareInDB.getVersion())
                        && spare.getBrand().equals(spareInDB.getBrand())
                        && spare.getType().equals(spareInDB.getType())
                        && spare.getSupplier().equals(spareInDB.getSupplier())
                ){
                    isExist = true;
                    spare.setId(spareInDB.getId());
                    spare.setQuantity(spareInDB.getQuantity()+spare.getQuantity());
                    if (spare.getPictureUrl() != null && spareInDB.getPictureUrl()!=null){
                        spare.setPictureUrl(spareInDB.getPictureUrl()+","+spare.getPictureUrl());
                    }
                    spareListUpdate.add(spare);
                }
            }
            if(!isExist){
                //不存在则新增
                spareListNew.add(spare);
            }
        }
        //更新
        if(spareListUpdate.size()>0) {
            updateQuantityAndPictureBatch(spareListUpdate);
        }
        //新增
        if(spareListNew.size()>0) {
            addBatch(spareListNew);
        }
        //addBatch(spareList);
        return new Response().setII(1,"导入完成");
    }
        return null;
    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,"新增或者替换图片完成");
    }
}