ソースを参照

update 曲库导入支持图片

周箭河 5 年 前
コミット
35b1278166

+ 24 - 5
edu-user/edu-user-biz/src/main/java/com/keao/edu/user/service/impl/ExamSongServiceImpl.java

@@ -3,6 +3,7 @@ package com.keao.edu.user.service.impl;
 import com.keao.edu.auth.api.client.SysUserFeignService;
 import com.keao.edu.auth.api.entity.SysUser;
 import com.keao.edu.common.dal.BaseDAO;
+import com.keao.edu.common.entity.UploadReturnBean;
 import com.keao.edu.common.exception.BizException;
 import com.keao.edu.common.page.PageInfo;
 import com.keao.edu.common.page.QueryInfo;
@@ -13,24 +14,24 @@ import com.keao.edu.user.dao.ExamSongDao;
 import com.keao.edu.user.dao.SubjectDao;
 import com.keao.edu.user.entity.ExamSong;
 import com.keao.edu.user.entity.Subject;
-import com.keao.edu.user.entity.Teacher;
 import com.keao.edu.user.enums.SongTypeEnum;
-import com.keao.edu.user.enums.TeacherSettlementTypeEnum;
 import com.keao.edu.user.page.ExamSongQueryInfo;
 import com.keao.edu.user.service.ExamSongService;
+import com.keao.edu.user.service.UploadFileService;
 import com.keao.edu.util.collection.MapUtil;
 import com.keao.edu.util.excel.POIUtil;
 import com.keao.edu.util.iniFile.IniFileEntity;
 import com.keao.edu.util.iniFile.IniFileUtil;
 import org.apache.commons.lang3.StringUtils;
+import org.apache.poi.ss.usermodel.PictureData;
 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 java.io.ByteArrayInputStream;
 import java.io.InputStream;
-import java.math.BigDecimal;
 import java.util.*;
 
 @Service
@@ -44,6 +45,8 @@ public class ExamSongServiceImpl extends BaseServiceImpl<Integer, ExamSong> impl
     private SysUserFeignService sysUserFeignService;
     @Autowired
     private SubjectDao subjectDao;
+    @Autowired
+    private UploadFileService uploadFileService;
 
     @Override
     public BaseDAO<Integer, ExamSong> getDAO() {
@@ -142,13 +145,13 @@ public class ExamSongServiceImpl extends BaseServiceImpl<Integer, ExamSong> impl
                                 throw new BizException("专业不能为空");
                             }
                             String[] subjects = row.get(s).toString().split(",");
-                            String subjectIds = "";
+                            StringBuilder subjectIds = new StringBuilder();
                             for (String subject : subjects) {
                                 boolean has = false;
                                 for (Subject subSubject : subSubjects) {
                                     if (subject.equals(subSubject.getName())) {
                                         has = true;
-                                        subjectIds = subjectIds + "," + subSubject.getId();
+                                        subjectIds.append(",").append(subSubject.getId());
                                         break;
                                     }
                                 }
@@ -159,6 +162,22 @@ public class ExamSongServiceImpl extends BaseServiceImpl<Integer, ExamSong> impl
                             objectMap.put("subjectList", subjectIds.substring(1));
                         }
 
+                        StringBuilder pics = new StringBuilder();
+                        if (column.getValue().equals("fileUrlList")) {
+                            List<PictureData> pictureDataList = (ArrayList<PictureData>) row.get(s);
+                            for (PictureData pictureData : pictureDataList) {
+                                InputStream in = new ByteArrayInputStream(pictureData.getData());
+                                UploadReturnBean bean = uploadFileService.uploadFile(in, pictureData.suggestFileExtension());
+                                if (!bean.isStatus()) {
+                                    throw new BizException(objectMap.get("songName") + bean.getMessage());
+                                }
+                                pics.append(",").append(bean.getUrl());
+                            }
+                            if (pics.length() > 0) {
+                                objectMap.put("fileUrlList", pics.substring(1));
+                            }
+                        }
+
                         if (column.getValue().equals("type")) {
                             for (SongTypeEnum songType : SongTypeEnum.values()) {
                                 if (songType.getMsg().equals(row.get(s).toString())) {

+ 1 - 0
edu-user/edu-user-server/src/main/resources/columnMapper.ini

@@ -4,6 +4,7 @@
 曲目专业(英文逗号分隔) = subjectList
 曲目级别(填1-10的数字) = levelList
 曲目类别(练习曲or演奏曲) = type
+曲谱图片(插入图片) = fileUrlList
 
 [organization]
 合作机构名字 = name

BIN
edu-user/edu-user-server/src/main/resources/excelTemplate/examSong.xls


+ 86 - 18
edu-util/src/main/java/com/keao/edu/util/excel/POIUtil.java

@@ -1,9 +1,6 @@
 package com.keao.edu.util.excel;
 
-import java.io.File;
-import java.io.FileInputStream;
-import java.io.IOException;
-import java.io.InputStream;
+import java.io.*;
 import java.lang.reflect.InvocationTargetException;
 import java.lang.reflect.Method;
 import java.text.SimpleDateFormat;
@@ -18,21 +15,12 @@ import java.util.Map.Entry;
 import org.apache.commons.beanutils.NestedNullException;
 import org.apache.commons.beanutils.PropertyUtils;
 import org.apache.commons.lang3.StringUtils;
-import org.apache.poi.hssf.usermodel.HSSFCell;
-import org.apache.poi.hssf.usermodel.HSSFCellStyle;
-import org.apache.poi.hssf.usermodel.HSSFFont;
-import org.apache.poi.hssf.usermodel.HSSFRow;
-import org.apache.poi.hssf.usermodel.HSSFSheet;
-import org.apache.poi.hssf.usermodel.HSSFWorkbook;
-import org.apache.poi.ss.usermodel.Cell;
-import org.apache.poi.ss.usermodel.CellStyle;
-import org.apache.poi.ss.usermodel.Font;
-import org.apache.poi.ss.usermodel.IndexedColors;
-import org.apache.poi.ss.usermodel.Row;
-import org.apache.poi.ss.usermodel.Sheet;
-import org.apache.poi.ss.usermodel.Workbook;
+import org.apache.poi.POIXMLDocumentPart;
+import org.apache.poi.hssf.usermodel.*;
+import org.apache.poi.ss.usermodel.*;
 import org.apache.poi.xssf.streaming.SXSSFWorkbook;
-import org.apache.poi.xssf.usermodel.XSSFWorkbook;
+import org.apache.poi.xssf.usermodel.*;
+import org.openxmlformats.schemas.drawingml.x2006.spreadsheetDrawing.CTMarker;
 import org.slf4j.Logger;
 import org.slf4j.LoggerFactory;
 
@@ -481,6 +469,14 @@ public class POIUtil {
             currentRowNum = 0;
             // 顺序取sheet
             sheet = workbook.getSheetAt(i);
+
+            Map<String, List<PictureData>> picMap = null;
+            if (extName.endsWith(".xlsx")) {
+                picMap = getXlsxPictures((XSSFSheet) sheet);
+            } else if (extName.endsWith(".xls")) {
+                picMap = getXlsPictures((HSSFSheet) sheet);
+            }
+
             List<Map<String, Object>> datas = new ArrayList<Map<String, Object>>();
             rowIter = sheet.iterator();
             while (rowIter.hasNext()) {
@@ -530,6 +526,13 @@ public class POIUtil {
                         currentCellNum++;
                     }
                 }
+                if (picMap != null) {
+                    for (Entry<String, List<PictureData>> pics : picMap.entrySet()) {
+                        String key = pics.getKey();
+                        String[] split = key.split("-");
+                        obj.put(fieldsName[Integer.parseInt(split[1])], pics.getValue());
+                    }
+                }
                 if (obj != null && hasVal)
                     datas.add(obj);
 
@@ -629,4 +632,69 @@ public class POIUtil {
         return list;
     }
 
+    /**
+     * 获取图片和位置 (xls)
+     *
+     * @param sheet
+     * @return
+     * @throws IOException
+     */
+    public static Map<String, List<PictureData>> getXlsPictures(HSSFSheet sheet) throws IOException {
+        Map<String, List<PictureData>> map = new HashMap<>();
+        List<HSSFShape> list = sheet.getDrawingPatriarch().getChildren();
+        for (HSSFShape shape : list) {
+            if (shape instanceof HSSFPicture) {
+                HSSFPicture picture = (HSSFPicture) shape;
+                HSSFClientAnchor cAnchor = (HSSFClientAnchor) picture.getAnchor();
+                PictureData pdata = picture.getPictureData();
+                String key = cAnchor.getRow1() + "-" + cAnchor.getCol1(); // 行号-列号
+                if (map.containsKey(key)) {
+                    List<PictureData> pics = map.get(key);
+                    pics.add(pdata);
+                    map.put(key, pics);
+                } else {
+                    List<PictureData> pics = new ArrayList<>();
+                    pics.add(pdata);
+                    map.put(key, pics);
+                }
+            }
+        }
+        return map;
+    }
+
+    /**
+     * 获取图片和位置 (xlsx)
+     *
+     * @param sheet
+     * @return
+     * @throws IOException
+     */
+    public static Map<String, List<PictureData>> getXlsxPictures(XSSFSheet sheet) throws IOException {
+        Map<String, List<PictureData>> map = new HashMap<>();
+        List<POIXMLDocumentPart> list = sheet.getRelations();
+        for (POIXMLDocumentPart part : list) {
+            if (part instanceof XSSFDrawing) {
+                XSSFDrawing drawing = (XSSFDrawing) part;
+                List<XSSFShape> shapes = drawing.getShapes();
+                for (XSSFShape shape : shapes) {
+                    XSSFPicture picture = (XSSFPicture) shape;
+                    XSSFClientAnchor anchor = picture.getPreferredSize();
+                    CTMarker marker = anchor.getFrom();
+                    String key = marker.getRow() + "-" + marker.getCol();
+
+                    if (map.containsKey(key)) {
+                        List<PictureData> pics = map.get(key);
+                        pics.add(picture.getPictureData());
+                        map.put(key, pics);
+                    } else {
+                        List<PictureData> pics = new ArrayList<>();
+                        pics.add(picture.getPictureData());
+                        map.put(key, pics);
+                    }
+                }
+            }
+        }
+        return map;
+    }
+
 }