Parcourir la source

4/12 patentExport

xiexiang il y a 1 an
Parent
commit
24983045f1

+ 38 - 0
src/main/java/com/example/xiaoshiweixinback/business/utils/GenerateObjectUtil.java

@@ -0,0 +1,38 @@
+package com.example.xiaoshiweixinback.business.utils;
+
+import org.springframework.stereotype.Component;
+
+import java.lang.reflect.Field;
+import java.lang.reflect.InvocationTargetException;
+import java.lang.reflect.Method;
+
+@Component
+public class GenerateObjectUtil {
+
+
+    public static void setObjectProperty(Object obj, String propertyName, Object value) throws Exception {
+//        String setMethodName = "set" + propertyName.substring(0, 1).toUpperCase() + propertyName.substring(1);
+        Field field = obj.getClass().getDeclaredField(propertyName);
+        field.setAccessible(true);
+        if (value != null && field.getType() != value.getClass()) {
+            value = null;
+        }
+        field.set(obj, value);
+    }
+
+    /**
+     * @param obj
+     * @param propertyName
+     * @return
+     */
+    public static Object getPropertyValue(Object obj, String propertyName) {
+        try {
+            String getMethodName = "get" + propertyName.substring(0, 1).toUpperCase() + propertyName.substring(1);
+            Method method = obj.getClass().getMethod(getMethodName);
+            return method.invoke(obj);
+        } catch (NoSuchMethodException | IllegalAccessException | InvocationTargetException e) {
+            e.printStackTrace();
+            return null;
+        }
+    }
+}

+ 53 - 0
src/main/java/com/example/xiaoshiweixinback/business/utils/ParseByteToFileUtils.java

@@ -0,0 +1,53 @@
+package com.example.xiaoshiweixinback.business.utils;
+
+import cn.hutool.core.util.IdUtil;
+import com.example.xiaoshiweixinback.service.common.FileManagerService;
+import org.springframework.beans.factory.annotation.Autowired;
+import org.springframework.mock.web.MockMultipartFile;
+import org.springframework.stereotype.Component;
+import org.springframework.web.multipart.MultipartFile;
+
+import java.io.ByteArrayInputStream;
+import java.io.IOException;
+import java.io.InputStream;
+import java.net.URLEncoder;
+import java.nio.charset.StandardCharsets;
+import java.util.ArrayList;
+import java.util.List;
+
+/**
+ * @Author xiexiang
+ * @Date 2024/1/11
+ */
+@Component
+public class ParseByteToFileUtils {
+    @Autowired
+    private FileManagerService fileManagerService;
+
+    public static MultipartFile convertBytesToMultipartFile(byte[] fileData, String fileName) throws IOException {
+        InputStream inputStream = new ByteArrayInputStream(fileData);
+        MultipartFile multipartFile = new MockMultipartFile("file", fileName, "application/octet-stream", inputStream);
+        return multipartFile;
+    }
+
+    public String uploadFile(byte[] fileData, Integer type) throws IOException {
+        List<MultipartFile> multipartFiles = new ArrayList<>();
+        //保存生成地址
+        String fileName = "";
+        if (type.equals(1)) {
+            fileName = IdUtil.simpleUUID() + ".xls";
+        } else if (type.equals(2)) {
+            fileName = IdUtil.simpleUUID() + ".pdf";
+        }
+        //文件原始名中的中文字符可能会乱码,对文件名进行URL编码
+        String encodedFileName = URLEncoder.encode(fileName, StandardCharsets.UTF_8.toString());
+        MultipartFile multipartFile = this.convertBytesToMultipartFile(fileData, encodedFileName);
+        multipartFiles.add(multipartFile);
+        List<String> fileGuids = fileManagerService.uploadFileGetGuid(multipartFiles);
+        String fileGuid = null;
+        if (!fileGuids.isEmpty()) {
+            fileGuid = fileGuids.get(0);
+        }
+        return fileGuid;
+    }
+}

+ 41 - 0
src/main/java/com/example/xiaoshiweixinback/entity/vo/PatentExportVO.java

@@ -0,0 +1,41 @@
+package com.example.xiaoshiweixinback.entity.vo;
+
+import lombok.Data;
+
+/**
+ * @Author xiexiang
+ * @Date 2024/4/11
+ */
+@Data
+public class PatentExportVO {
+    /**
+     * 字段名
+     */
+    private String value;
+
+    /**
+     * 字段中文
+     */
+    private String name;
+
+    /**
+     *
+     */
+    private Integer order;
+
+    /**
+     * 是否选择
+     */
+    private Boolean selected;
+
+    /**
+     * 类型
+     */
+    private String type;
+
+    private Boolean ifShow;
+
+    private Boolean ifHidden;
+
+    private Integer createType;
+}

+ 8 - 0
src/main/java/com/example/xiaoshiweixinback/factorys/factorys.java

@@ -0,0 +1,8 @@
+package com.example.xiaoshiweixinback.factorys;
+
+/**
+ * @Author xiexiang
+ * @Date 2024/4/12
+ */
+public class factorys {
+}

+ 16 - 0
src/main/java/com/example/xiaoshiweixinback/factorys/patentExportFactory/GetCommonValue.java

@@ -0,0 +1,16 @@
+package com.example.xiaoshiweixinback.factorys.patentExportFactory;
+
+import org.springframework.stereotype.Component;
+
+/**
+ * @Author xiexiang
+ * @Date 2024/4/12
+ */
+@Component
+public class GetCommonValue implements GetValueImp {
+    @Override
+    public String getValue(Object value) {
+        String reValue = value.toString();
+        return reValue;
+    }
+}

+ 24 - 0
src/main/java/com/example/xiaoshiweixinback/factorys/patentExportFactory/GetDateValue.java

@@ -0,0 +1,24 @@
+package com.example.xiaoshiweixinback.factorys.patentExportFactory;
+
+import org.springframework.stereotype.Component;
+
+import java.text.SimpleDateFormat;
+import java.util.Date;
+
+/**
+ * @Author xiexiang
+ * @Date 2024/4/12
+ */
+@Component
+public class GetDateValue implements GetValueImp {
+    @Override
+    public String getValue(Object value) {
+        Date inputDate = (Date)value;
+        String reValue;
+        // 创建SimpleDateFormat对象,指定输出日期的格式
+        SimpleDateFormat outputDateFormat = new SimpleDateFormat("yyyy-MM-dd");
+        // 将Date对象格式化为字符串
+        reValue = outputDateFormat.format(inputDate);
+        return reValue;
+    }
+}

+ 21 - 0
src/main/java/com/example/xiaoshiweixinback/factorys/patentExportFactory/GetNameValue.java

@@ -0,0 +1,21 @@
+package com.example.xiaoshiweixinback.factorys.patentExportFactory;
+
+import org.springframework.stereotype.Component;
+
+/**
+ * @Author xiexiang
+ * @Date 2024/4/12
+ */
+@Component
+public class GetNameValue implements GetValueImp {
+    @Override
+    public String getValue(Object value) {
+        if (value != null) {
+            String reValue = value.toString();
+            String result = reValue.substring(1, reValue.length() - 1);
+            return result;
+        } else {
+            return null;
+        }
+    }
+}

+ 30 - 0
src/main/java/com/example/xiaoshiweixinback/factorys/patentExportFactory/GetTextContentValue.java

@@ -0,0 +1,30 @@
+package com.example.xiaoshiweixinback.factorys.patentExportFactory;
+
+import com.alibaba.fastjson.JSONObject;
+import com.example.xiaoshiweixinback.domain.es.Text;
+import org.springframework.stereotype.Component;
+
+import java.util.List;
+
+/**
+ * @Author xiexiang
+ * @Date 2024/4/12
+ */
+@Component
+public class GetTextContentValue implements GetValueImp {
+    @Override
+    public String getValue(Object value) {
+        try {
+            String text = JSONObject.toJSONString(value);
+            List<Text> texts = JSONObject.parseArray(text, Text.class);
+            if (!texts.isEmpty()) {
+                return texts.get(0).getTextContent();
+            } else {
+                return "";
+            }
+        } catch (Exception e) {
+            e.printStackTrace();
+            return "";
+        }
+    }
+}

+ 9 - 0
src/main/java/com/example/xiaoshiweixinback/factorys/patentExportFactory/GetValueImp.java

@@ -0,0 +1,9 @@
+package com.example.xiaoshiweixinback.factorys.patentExportFactory;
+
+/**
+ * @Author xiexiang
+ * @Date 2024/4/12
+ */
+public interface GetValueImp {
+    public String getValue(Object value);
+}

+ 22 - 0
src/main/java/com/example/xiaoshiweixinback/factorys/patentExportFactory/PatentExportFactory.java

@@ -0,0 +1,22 @@
+package com.example.xiaoshiweixinback.factorys.patentExportFactory;
+
+import org.springframework.beans.factory.annotation.Autowired;
+import org.springframework.stereotype.Component;
+
+import java.util.Map;
+
+/**
+ * @Author xiexiang
+ * @Date 2024/4/12
+ */
+@Component
+public class PatentExportFactory {
+    @Autowired
+    private Map<String, GetValueImp> getValueImpMap;
+
+
+    public GetValueImp getClass(String sqlType) {
+        GetValueImp bean1 = getValueImpMap.get(sqlType);
+        return bean1;
+    }
+}

+ 175 - 0
src/main/java/com/example/xiaoshiweixinback/service/exportPatent/PatentExportService.java

@@ -0,0 +1,175 @@
+package com.example.xiaoshiweixinback.service.exportPatent;
+
+import com.example.xiaoshiweixinback.business.utils.*;
+import com.example.xiaoshiweixinback.entity.vo.PatentExportVO;
+import com.example.xiaoshiweixinback.factorys.patentExportFactory.GetValueImp;
+import com.example.xiaoshiweixinback.factorys.patentExportFactory.PatentExportFactory;
+import com.example.xiaoshiweixinback.service.importPatent.CommonService;
+import lombok.RequiredArgsConstructor;
+import com.alibaba.fastjson.JSON;
+import org.apache.poi.hssf.usermodel.*;
+import org.apache.poi.hssf.util.HSSFColor;
+import org.apache.poi.ss.usermodel.FillPatternType;
+import org.apache.poi.ss.usermodel.HorizontalAlignment;
+import org.apache.poi.ss.usermodel.VerticalAlignment;
+import org.springframework.beans.factory.annotation.Autowired;
+import org.springframework.scheduling.annotation.Async;
+import org.springframework.stereotype.Service;
+
+import java.io.ByteArrayOutputStream;
+import java.io.FileNotFoundException;
+import java.io.IOException;
+import java.util.*;
+import java.util.stream.Collectors;
+
+/**
+ * 专利导出Excel
+ * @Author xiexiang
+ * @Date 2024/4/11
+ */
+@Service
+@RequiredArgsConstructor
+public class PatentExportService {
+    @Autowired
+    private PatentExportFactory patentExportFactory;
+    @Autowired
+    private ParseByteToFileUtils parseByteToFileUtils;
+
+//    /**
+//     * 导出专利
+//     *
+//     * @param exportTask
+//     * @return
+//     * @throws IOException
+//     */
+//    @Async
+//    public void exportPatent(ExportTaskDTO exportTask) throws IOException {
+//        EsCustomFieldBatchVO EsVO = exportTask.getEsVO();
+//        try (ByteArrayOutputStream out = new ByteArrayOutputStream()) {
+//            String esSelected = JsonUtils.objectToJson(EsVO.getSelected());
+//            //selected字符串转PatentExportVO集合
+//            List<PatentExportVO> selected = Optional.ofNullable(JsonUtils.jsonToList(esSelected, PatentExportVO.class))
+//                    .orElse(new ArrayList<>());
+//            //导出文件名
+//            if (selected.isEmpty()) {
+//                throw new IllegalArgumentException("没有选择要导出的字段数据");
+//            }
+//
+//            //key的集合(标头:例如patentNo)
+//            List<String> columns = selected.stream().filter(PatentExportVO::getSelected).map(PatentExportVO::getValue).distinct().collect(Collectors.toList());
+//            //name的集合
+//            List<String> headers = selected.stream().filter(PatentExportVO::getSelected).map(PatentExportVO::getName).distinct().collect(Collectors.toList());
+//            if (columns.size() != headers.size()) {
+//                throw new XiaoShiException("数量不匹配");
+//            }
+//            //新建工作簿
+//            HSSFWorkbook hssfWorkbook = new HSSFWorkbook();
+//            //新建sheet页
+//            HSSFSheet sheet = hssfWorkbook.createSheet();
+//            sheet.setDefaultColumnWidth(30);
+//            //新建标头行
+//            HSSFRow headerRow = sheet.createRow(0);
+//            headerRow.setHeight((short) 500);
+//            //新建标头行格式
+//            HSSFCellStyle headerCellStyle = hssfWorkbook.createCellStyle();
+//            //新建普通行格式
+//            HSSFCellStyle commonCellStyle = hssfWorkbook.createCellStyle();
+//            //遍历设置标头
+//            for (int i = 0; i < headers.size(); i++) {
+//                HSSFCell cell = headerRow.createCell(i);
+//                ExcelUtils.setExcelCellStyle(headerCellStyle);
+//                headerCellStyle.setAlignment(HorizontalAlignment.CENTER);
+//                headerCellStyle.setVerticalAlignment(VerticalAlignment.CENTER);
+//                headerCellStyle.setFillForegroundColor(HSSFColor.HSSFColorPredefined.SKY_BLUE.getIndex());
+//                headerCellStyle.setFillPattern(FillPatternType.SOLID_FOREGROUND);
+//                headerCellStyle.setWrapText(true);
+//                cell.setCellStyle(headerCellStyle);
+//                cell.setCellValue(headers.get(i));
+//            }
+//            //根据projectId查询专利信息
+//            List<String> patentNos = pdfExportFirstPageService.getPatentNo(EsVO);
+//            if (patentNos.isEmpty()) {
+//                throw new XiaoShiException("未获取到专利信息");
+//            }
+//            Integer total = patentNos.size();
+//            Integer defaultNum = 0;
+//            for (int i = 0; i < patentNos.size(); i++) {
+//                Map<String, Object> map = new LinkedHashMap<>();
+//                PatentNoVO patentNoVO = new PatentNoVO();
+//                patentNoVO.setPatentNo(patentNos.get(i));
+//                PatentColumnDTO patent = patentService.selectPatentDetail(patentNoVO);
+//                if (patent == null) {
+//                    defaultNum++;
+//                    continue;
+//                }
+//                for (int j = 0; j < columns.size(); j++) {
+//                    String column = columns.get(j);
+//                    PatentExportVO patentExportVO = selected.stream().filter(item -> item.getValue().equals(column)).findFirst().orElse(new PatentExportVO());
+//                    // 使用反射获取属性值
+//                    Object value = GenerateObjectUtil.getPropertyValue(patent, patentExportVO.getValue());
+//                    if (value != null) {
+//                        String json = CommonService.readJsonFile("patent.json");
+//                        List<PatentConfigVO> patentConfigVOS = JSON.parseArray(json, PatentConfigVO.class);
+//                        PatentConfigVO patentConfigVO = patentConfigVOS.stream().filter(item -> item.getValue().equals(column)).findFirst().orElse(null);
+//                        String exportClass = patentConfigVO.getExportClass();
+//                        GetValueImp getValueImp = patentExportFactory.getClass(exportClass);
+//                        if (getValueImp != null) {
+//                            String reValue = getValueImp.getValue(value);
+//                            map.put(patentExportVO.getName(), reValue);
+//                        }
+//                    }
+//                }
+//                //新建一普通行
+//                HSSFRow row = sheet.createRow(i + 1);
+//                row.setHeight((short) 800);
+//                for (String key : map.keySet()) {
+//                    int index = headers.indexOf(key);
+//                    if (index != -1) {
+//                        HSSFCell cell = row.createCell(index);
+//                        ExcelUtils.setExcelCellStyle(commonCellStyle);
+//                        commonCellStyle.setVerticalAlignment(VerticalAlignment.TOP);
+//                        commonCellStyle.setWrapText(true);
+//                        cell.setCellStyle(commonCellStyle);
+//                        if (StringUtils.isNotNull(map.get(key))) {
+//                            cell.setCellValue(String.valueOf(map.get(key)));
+//                        }
+//                    }
+//                }
+//                WebSocketMessageVO webSocketMessageVO = new WebSocketMessageVO();
+//                webSocketMessageVO.setProjectId(EsVO.getProjectId());
+//                webSocketMessageVO.setCreateId(exportTask.getCreateId());
+//                webSocketMessageVO.setCode(602);
+//                webSocketMessageVO.setAllNum(total);
+//                webSocketMessageVO.setCurrentNum(i+1);
+//                webSocketMessageVO.setTaskId(exportTask.getTaskId());
+//                webSocketMessageVO.setState(1);
+//                messageService.sendPatentExportMessage(webSocketMessageVO);
+//            }
+//            hssfWorkbook.write(out);
+//            WebSocketMessageVO webSocketMessageVO = new WebSocketMessageVO();
+//            webSocketMessageVO.setProjectId(EsVO.getProjectId());
+//            webSocketMessageVO.setCreateId(exportTask.getCreateId());
+//            webSocketMessageVO.setCode(603);
+//            webSocketMessageVO.setAllNum(total);
+//            webSocketMessageVO.setCurrentNum(total);
+//            webSocketMessageVO.setTaskId(exportTask.getTaskId());
+//            webSocketMessageVO.setState(2);
+//            messageService.sendPatentExportMessage(webSocketMessageVO);
+//            String fileGuid = "";
+//            if (out.toByteArray() != null && out.toByteArray().length != 0) {
+//                fileGuid = parseByteToFileUtils.uploadFile(out.toByteArray(), 1);
+//            }
+//            exportTask.setFileGuid(fileGuid);
+//            exportTask.setAllNum(total);
+//            exportTask.setDefaultNum(defaultNum);
+//            exportTask.setDoneNum(total - defaultNum);
+//            exportTask.setState(2);
+//            Integer taskId = importTaskService.updateExportTask(exportTask);
+//            if (taskId == null) {
+//                throw new XiaoShiException("导出记录失败");
+//            }
+//        } catch (FileNotFoundException e) {
+//            throw new FileNotFoundException();
+//        }
+//    }
+}