From c7795eb09f3059f1a0310bc817e2fa0e9e64d29e Mon Sep 17 00:00:00 2001
From: whyclxw <810412026@qq.com>
Date: 星期四, 26 六月 2025 21:15:01 +0800
Subject: [PATCH] Merge branch 'master' of http://118.89.139.230:10101/r/powerIntelligenceSystem

---
 src/main/java/com/whyc/service/DeviceSpareService.java |  164 ++++++++++++++++++++++++++++++++++++++++++++++++++----
 1 files changed, 151 insertions(+), 13 deletions(-)

diff --git a/src/main/java/com/whyc/service/DeviceSpareService.java b/src/main/java/com/whyc/service/DeviceSpareService.java
index 72a0fea..94ada48 100644
--- a/src/main/java/com/whyc/service/DeviceSpareService.java
+++ b/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;
@@ -13,17 +16,22 @@
 import com.whyc.util.ThreadLocalUtil;
 import org.apache.commons.lang.StringUtils;
 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 {
@@ -66,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();
                 //灏唂ileName涓彲鑳藉瓨鍦ㄧ殑,鍘绘帀
-                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";
@@ -229,13 +237,143 @@
             spare.setSupplier(supplier);
 
             spareList.add(spare);
-            //绗竷鍒椾负鍥剧墖 TODO 寰呭鐞�
-            /*Cell cell = row.getCell(7);
-            CellType cellType = cell.getCellType();
-            System.out.println("-");*/
-        }
-        addBatch(spareList);
 
-        return null;
+        }
+
+        //绗竷鍒椾负鍥剧墖-娴姩寮忓浘鐗�
+        //鑾峰彇缁樺浘瀵硅薄涓殑鎵�鏈夊浘褰�
+        XSSFDrawing drawing = (XSSFDrawing) sheet.getDrawingPatriarch();
+        if (drawing == null) {
+            drawing = (XSSFDrawing) sheet.createDrawingPatriarch();
+        }
+        List<XSSFShape> shapes = drawing.getShapes();
+        for (XSSFShape shape : shapes) {
+            if (shape instanceof XSSFPicture) {
+                XSSFPicture pic = (XSSFPicture) shape;
+                XSSFClientAnchor anchor = pic.getClientAnchor();
+                //鑾峰彇鍥剧墖鎵�鍦ㄧ殑璧峰琛�
+                int rowIndex = anchor.getRow1();
+                byte[] data = pic.getPictureData().getData();
+
+                DeviceSpare spare = spareList.get(rowIndex - 1);
+                //瀵瑰瓨鍌ㄨ矾寰勮繘琛屽畾涔�
+                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";
+
+                // 淇濆瓨鍥剧墖鍒版湰鍦�
+                try (FileOutputStream fos = new FileOutputStream(filePath)) {
+                    fos.write(data);
+                }
+                String split = "pis_file"+File.separator+"deviceSpare";
+                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);
+        }
+
+        return new Response().setII(1,"瀵煎叆瀹屾垚");
+    }
+
+    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,"鏂板鎴栬�呮浛鎹㈠浘鐗囧畬鎴�");
     }
 }

--
Gitblit v1.9.1