Преглед изворни кода

1、修复配偶写入excel文件错行的bug。

jessie пре 5 година
родитељ
комит
94acb50226

+ 9 - 0
src/main/java/com/sprivacy/pms/common/Constants.java

@@ -0,0 +1,9 @@
+package com.sprivacy.pms.common;
+
+/**
+ * 通用常量
+ */
+public class Constants {
+
+    public static final String DEFAULT_DATETIME_FORMAT = "yyyy-MM-dd HH:mm:ss";
+}

+ 7 - 0
src/main/java/com/sprivacy/pms/controller/TestController.java

@@ -97,6 +97,13 @@ public class TestController {
     @Value("${pms.common.base-url}")
     private String baseUrl;
 
+    @ApiOperation("red info")
+    @GetMapping(value = "/ver/get")
+    public HttpResponse getVer(){
+        return HttpResponseUtils.ok(1,"修复配偶信息错行。",
+                com.sprivacy.pms.util.DateUtils.now());
+    }
+
     @ApiOperation("red info")
     @GetMapping(value = "/test/red")
     public HttpResponse uploadImageTest() throws Exception{

+ 17 - 1
src/main/java/com/sprivacy/pms/service/impl/PerServiceImpl.java

@@ -2120,7 +2120,7 @@ public class PerServiceImpl extends ServiceImpl<PerMapper, Per> implements PerSe
 
         List<PerFamRel> familyRelations = famRelService.findByPid(id);
         if (CollectionUtils.isNotEmpty(familyRelations)) {
-            perForm.setFamRelations(familyRelations);
+            perForm.setFamRelations(toFamilyRelations(familyRelations));
         }
 
         List<PerSocRel> socialRelations = socRelService.findByPid(id);
@@ -2138,6 +2138,22 @@ public class PerServiceImpl extends ServiceImpl<PerMapper, Per> implements PerSe
         return form;
     }
 
+    public List<PerFamRel> toFamilyRelations(List<PerFamRel> familyRelations){
+        if (null != familyRelations && familyRelations.size() > 0) {
+            if (!familyRelations.get(0).getRelation().contains("配偶")){
+                log.info("配偶调整。");
+                for (int index = 0, length = familyRelations.size(); index < length; index++) {
+                    if ("配偶".equals(familyRelations.get(index).getRelation())) {
+                        PerFamRel temp = familyRelations.get(0);
+                        familyRelations.set(0, familyRelations.get(index));
+                        familyRelations.set(index, temp);
+                    }
+                }
+            }
+        }
+        return familyRelations;
+    }
+
     public String getDepartureTime(String birthday, String sex, String perNature) {
         if (!TextUtils.isEmpty(birthday) && !TextUtils.isEmpty(sex)){
             if (sex.trim().equals("男")) {

+ 6 - 0
src/main/java/com/sprivacy/pms/util/DateUtils.java

@@ -1,6 +1,8 @@
 package com.sprivacy.pms.util;
 
+import com.sprivacy.pms.common.Constants;
 import org.apache.commons.lang3.StringUtils;
+import org.joda.time.DateTime;
 
 import java.text.DateFormat;
 import java.text.ParseException;
@@ -17,6 +19,10 @@ import java.util.Map;
  */
 public class DateUtils {
 
+    public static String now() {
+        return DateTime.now().toString(Constants.DEFAULT_DATETIME_FORMAT);
+    }
+
     /**
      * 获取当前格式化日期
      *

+ 45 - 39
src/main/java/com/sprivacy/pms/util/ExportExcelUtil.java

@@ -3,12 +3,14 @@ package com.sprivacy.pms.util;
 import com.alibaba.fastjson.JSON;
 import com.sprivacy.pms.form.per.PerForm;
 import com.sprivacy.pms.mapper.PerMapper;
+import org.apache.commons.collections.CollectionUtils;
 import org.apache.commons.lang3.StringUtils;
 import org.apache.poi.hssf.usermodel.HSSFClientAnchor;
 import org.apache.poi.hssf.usermodel.HSSFPatriarch;
 import org.apache.poi.hssf.usermodel.HSSFSheet;
 import org.apache.poi.hssf.usermodel.HSSFWorkbook;
 import org.apache.poi.ss.usermodel.ClientAnchor;
+import org.apache.poi.xssf.usermodel.XSSFWorkbook;
 import org.apache.tomcat.util.http.fileupload.IOUtils;
 import org.slf4j.Logger;
 import org.slf4j.LoggerFactory;
@@ -185,17 +187,16 @@ public class ExportExcelUtil {
 
         }
 
-
         HSSFSheet sheetAnother = wbs.getSheetAt(1);
         int totalRowAnother = sheetAnother.getLastRowNum();
 
-
         for (int i = 0; i < totalRowAnother; i++) {
             for (PerForm perForm : perForms) {
                 if (null != perForm.getFamRelations()){
                     int famRelationsNum = perForm.getFamRelations().size();
                     if (famRelationsNum > 0){
                         if (perForm.getFamRelations().get(0).getRelation().equals("配偶")){
+                            System.err.println("配偶来了。。。");
                             for (int i1 = 1; i1 < famRelationsNum; i1++) {
                                 if (i == (0 + i1) && perForm.getFamRelations().get(i1-1).getRelation().equals("配偶")) {
                                     sheetAnother.getRow(i).getCell(2).setCellValue(perForm.getFamRelations().get(0).getName());
@@ -304,10 +305,6 @@ public class ExportExcelUtil {
                     }
                 }
 
-
-
-
-
                 if (null != perForm.getSocRelations()){
                     int socRelationsNum = perForm.getSocRelations().size();
                     if (socRelationsNum > 0){
@@ -389,23 +386,28 @@ public class ExportExcelUtil {
         String currDateStr = DateUtils.getCurrentFromatTimeBySelf();
         // // 获取重复文件
         List<String> fileNames = getFileNames(path,Long.valueOf(StringUtils.substringBefore(excelFileName,"_")));
-        for (String fileName : fileNames) {
-            File file = new File(path + fileName);
-            log.info("fileName<>"+fileName);
-
-            log.info(fileName+","+checkSameName);
-            if (fileName.contains(checkSameName)){
-                log.info("file 存在>"+ JSON.toJSONString(file));
-                if (file.exists()) {
-                    if (!fileName.equals(checkSameName+currDateStr+".xls")){
-                        boolean d = file.delete();
-                        log.info("重复文件是否删除成功>" + d);
-                    }
 
+        if (CollectionUtils.isNotEmpty(fileNames)){
+            System.err.println(fileNames.size() + ","+JSON.toJSONString(fileNames));
+            for (String fileName : fileNames) {
+                File file = new File(path + fileName);
+                log.info("fileName<>"+fileName);
+
+                log.info(fileName+","+checkSameName);
+                if (fileName.contains(checkSameName)){
+                    log.info("file 存在>"+ JSON.toJSONString(file));
+                    if (file.exists()) {
+                        if (!fileName.equals(checkSameName+currDateStr+".xls")){
+                            boolean d = file.delete();
+                            log.info("重复文件是否删除成功>" + d);
+                        }
+
+                    }
                 }
             }
         }
         wbs.write(fileOutputStream);
+        System.err.println(path + excelFileName + ".xls");
         try {
             fileOutputStream.close(); // 关闭流
             fileOutputStream.close(); // 关闭工作簿
@@ -426,22 +428,25 @@ public class ExportExcelUtil {
      */
     private static void drawPictureInfoExcel(String pictureUrl,HSSFWorkbook wb,HSSFPatriarch patriarch,int rowIndex){
         try {
-            URL url = new URL(pictureUrl);
-            BufferedImage bufferImg = ImageIO.read(url);
-            ByteArrayOutputStream byteArrayOut = new ByteArrayOutputStream();
-
-            // 构建图片格式
-            String tempedFormatName = StringUtils.substringAfter(pictureUrl,"/pms/img/");
-            String formatName = StringUtils.substringAfter(tempedFormatName,".");
-
-            ImageIO.write(bufferImg, formatName, byteArrayOut);
-            byte[] data = byteArrayOut.toByteArray();
-
-            HSSFClientAnchor anchor = new HSSFClientAnchor(0, 0, 1023, 250,(short) rowIndex, 2, (short) rowIndex, 6);
-            // Sets the anchor type (图片在单元格的位置)
-            // 0 = Move and size with Cells, 2 = Move but don't size with cells, 3 = Don't move or size with cells.
-            anchor.setAnchorType(ClientAnchor.AnchorType.byId(0));
-            patriarch.createPicture(anchor, wb.addPicture(data, HSSFWorkbook.PICTURE_TYPE_JPEG));
+            // pictureUrl = "http://mms.s-privacy.cn:8087/pms/img/3734fd40-6521-4fb5-91d7-651f78028ad4.png";
+            if (null != pictureUrl && !StringUtils.isEmpty(pictureUrl)){
+                URL url = new URL(pictureUrl);
+                BufferedImage bufferImg = ImageIO.read(url);
+                ByteArrayOutputStream byteArrayOut = new ByteArrayOutputStream();
+
+                // 构建图片格式
+                String tempedFormatName = StringUtils.substringAfter(pictureUrl,"/pms/img/");
+                String formatName = StringUtils.substringAfter(tempedFormatName,".");
+
+                ImageIO.write(bufferImg, formatName, byteArrayOut);
+                byte[] data = byteArrayOut.toByteArray();
+
+                HSSFClientAnchor anchor = new HSSFClientAnchor(0, 0, 1023, 250,(short) rowIndex, 2, (short) rowIndex, 6);
+                // Sets the anchor type (图片在单元格的位置)
+                // 0 = Move and size with Cells, 2 = Move but don't size with cells, 3 = Don't move or size with cells.
+                anchor.setAnchorType(ClientAnchor.AnchorType.byId(0));
+                patriarch.createPicture(anchor, wb.addPicture(data, HSSFWorkbook.PICTURE_TYPE_JPEG));
+            }
         } catch (IOException e) {
             e.printStackTrace();
         } catch (Exception e) {
@@ -502,9 +507,11 @@ public class ExportExcelUtil {
         String zipFileName = DateUtils.getCurrentFromatTimeBySelf();
         for (Long id : ids) {
             fileName = getFileName(path,id);
+            System.out.println(fileName);
+            System.err.println(path + fileName);
             FileInputStream is = new FileInputStream(path + fileName);
-            HSSFWorkbook wbs = new HSSFWorkbook(is);
-
+            // HSSFWorkbook wbs = new HSSFWorkbook(is);
+            XSSFWorkbook wbs = new XSSFWorkbook(is);
             ZipEntry entry = new ZipEntry(fileName);
             zip.putNextEntry(entry);
 
@@ -521,14 +528,11 @@ public class ExportExcelUtil {
                 response.addHeader("Content-Disposition", "attachment;filename=" + zipFileName + ".zip");
             }
             response.flushBuffer();
-
             wbs.write(zip);
         }
         zip.flush();
         zip.close();
 
-
-
     }
 
     public static String getFileName(String path,Long id){
@@ -556,7 +560,9 @@ public class ExportExcelUtil {
             if (tempList[i].isFile()) {
                 System.out.println("文     件:" + tempList[i]);
                 String tempStr = StringUtils.substringBefore(tempList[i].getName(),"_");
+                System.err.println(tempStr+","+id);
                 if (tempStr.equals(String.valueOf(id))){
+                    System.err.println("same");
                     fileStrs.add(tempList[i].getName());
                     return fileStrs;
                 }

+ 4 - 2
src/main/resources/application-dev.yml

@@ -12,5 +12,7 @@ pms:
     base-url: http://192.168.1.73:8087/pms/img/
     upload-dir: E:\\mnt\\pms\\rar\\decompress\\
     decompress-dir: E:\\mnt\\pms\\rar\\upload\\
-    saved-dir: E:\\mnt\\pms\\rar\\saved\\
-    model-dir: E:\\mnt\\pms\\rar\\model\\model.xls
+#    saved-dir: E:\\mnt\\pms\\rar\\saved\\
+    saved-dir: E:\\tmp\\tmp_pro\\pms\\rar\\saved\\
+#    model-dir: E:\\mnt\\pms\\rar\\model\\model.xls
+    model-dir: E:\\tmp\\tmp_pro\\pms\\rar\\model\\model.xls

+ 2 - 2
src/main/resources/application-prod.yml

@@ -2,7 +2,7 @@ spring:
   datasource:
     type: com.alibaba.druid.pool.DruidDataSource
     druid:
-      url: jdbc:mysql://180.76.139.81:3308/pms_test?useUnicode=true&characterEncoding=utf8&characterSetResults=utf8
+      url: jdbc:mysql://mysql:3306/pms_test?useUnicode=true&characterEncoding=utf8&characterSetResults=utf8
       username: root
       password: Dc@123456
       driver-class-name: com.mysql.jdbc.Driver
@@ -11,7 +11,7 @@ spring:
 pms:
   common:
     file-dir: /mnt/pms/img/
-    base-url: http://180.76.139.81:8087/pms/img/
+    base-url: http://mms.s-privacy.cn:8087/pms/img/
     upload-dir: /mnt/pms/upload/decompress/
     decompress-dir: /mnt/pms/upload/rar/
     saved-dir: /mnt/pms/upload/saved/

+ 2 - 2
src/main/resources/application.yml

@@ -13,8 +13,8 @@ server:
 spring:
   # 环境 dev|prod
   profiles:
-      active: dev
-#     active: prod
+#      active: dev
+     active: prod
   jackson:
     time-zone: GMT+8
     date-format: yyyy-MM-dd HH:mm:ss

+ 162 - 7
src/test/java/com/sprivacy/pms/PmsTest.java

@@ -2,17 +2,26 @@
 //
 // import com.alibaba.fastjson.JSON;
 // import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
+// import com.sprivacy.pms.form.per.PerForm;
 // import com.sprivacy.pms.mapper.BirthRemindMapper;
-// import com.sprivacy.pms.model.BirthRemind;
-// import com.sprivacy.pms.model.Per;
+// import com.sprivacy.pms.mapper.PerMapper;
+// import com.sprivacy.pms.model.*;
+// import com.sprivacy.pms.service.*;
 // import com.sprivacy.pms.util.DateUtils;
+// import com.sprivacy.pms.util.ExportExcelUtil;
 // import lombok.extern.slf4j.Slf4j;
+// import org.apache.commons.collections4.CollectionUtils;
 // import org.junit.Test;
 // import org.junit.runner.RunWith;
 // import org.springframework.beans.factory.annotation.Autowired;
+// import org.springframework.beans.factory.annotation.Value;
 // import org.springframework.boot.test.context.SpringBootTest;
 // import org.springframework.test.context.junit4.SpringRunner;
 //
+// import java.io.File;
+// import java.io.FileWriter;
+// import java.io.IOException;
+// import java.util.ArrayList;
 // import java.util.HashMap;
 // import java.util.List;
 // import java.util.Map;
@@ -31,19 +40,165 @@
 //     private BirthRemindMapper birthRemindMapper;
 //
 //     @Test
-//     public void testQueryWrapper(){
+//     public void testQueryWrapper() {
 //         QueryWrapper<Per> perQueryWrapper = new QueryWrapper<>();
 //         perQueryWrapper.isNotNull("id");
 //         System.err.println(JSON.toJSONString(perQueryWrapper));
 //     }
 //
 //     @Test
-//     public void testQueryMap(){
+//     public void testQueryMap() {
 //         String currDate = DateUtils.getCurrentFromatDate();
-//         Map<String,Object> selectedMap = new HashMap<>();
-//         selectedMap.put("remind_time",currDate);
-//         selectedMap.put("remind_status","0");
+//         Map<String, Object> selectedMap = new HashMap<>();
+//         selectedMap.put("remind_time", currDate);
+//         selectedMap.put("remind_status", "0");
 //         List<BirthRemind> birthReminds = birthRemindMapper.selectByMap(selectedMap);
 //         System.err.println(JSON.toJSONString(birthReminds));
 //     }
+//
+//     @Autowired
+//     private EduRecService eduRecService;
+//
+//     @Autowired
+//     private WorkRecService workRecService;
+//
+//     @Autowired
+//     private FamRelService famRelService;
+//
+//     @Autowired
+//     private SocRelService socRelService;
+//
+//     @Autowired
+//     private RewRecService rewRecService;
+//
+//     @Autowired
+//     private PerMapper perMapper;
+//
+//     @Value("${pms.common.saved-dir}")
+//     private String savedDir;
+//
+//     @Value("${pms.common.model-dir}")
+//     private String modelDir;
+//
+//     @Test
+//     public void testFile() throws IOException {
+//         try {
+//             // List<PerForm> perForms = getPerForms(25L);
+//             // List<PerForm> perForms = getPerForms(6L);
+//             List<PerForm> perForms = getPerForms(896L);
+//             System.err.println();
+//             System.err.println();
+//             System.err.println();
+//             System.err.println();
+//             for (PerForm perForm : perForms) {
+//                 System.err.println(perForm.getFamRelations().get(0).getRelation());
+//                 System.err.println(JSON.toJSONString(perForm.getFamRelations()));
+//             }
+//             ExportExcelUtil.writeExcelFile(savedDir, modelDir, perForms);
+//
+//         }catch (Exception ex){
+//             System.err.println(ex.getMessage());
+//         }
+//
+//     }
+//
+//     @Test
+//     public void testExcel() throws IOException {
+//         QueryWrapper<Per> perQueryWrapper = new QueryWrapper<>();
+//         perQueryWrapper.lambda()
+//                 .isNotNull(Per::getId);
+//         List<Per> pers = perMapper.selectList(perQueryWrapper);
+//         String txtPath = "E:\\tmp\\pms\\ex\\";
+//         for (Per per : pers) {
+//             try {
+//                 List<PerForm> perForms = getPerForms(per.getId());
+//                 ExportExcelUtil.writeExcelFile(savedDir, modelDir, perForms);
+//             }catch (Exception ex){
+//                 System.err.println(ex.getMessage());
+//                 txt(txtPath,JSON.toJSONString(per),String.valueOf(per.getId()));
+//                 continue;
+//             }
+//
+//         }
+//     }
+//     public void txt(String path,String txtStr, String uploadId) {
+//         // String str = "verygood";
+//         File file = new File(path + uploadId + ".txt");
+//         try {
+//             FileWriter writer = new FileWriter(file, true);
+//             writer.write("\r\n" + txtStr);
+//             writer.close();
+//         } catch (Exception ex) {
+//             ex.printStackTrace();
+//             ex.getMessage();
+//         }
+//     }
+//
+//     @Test
+//     public void testPms() {
+//         // List<PerForm> perForms = getPerForms(25L);
+//         List<PerFamRel> familyRelations = famRelService.findByPid(25L);
+//
+//         System.out.println(JSON.toJSONString(familyRelations));
+//         System.err.println(JSON.toJSONString(toFamilyRelations(familyRelations)));
+//     }
+//
+//     public List<PerFamRel> toFamilyRelations(List<PerFamRel> familyRelations){
+//         if (null != familyRelations && familyRelations.size() > 0) {
+//             if (!familyRelations.get(0).getRelation().contains("配偶")){
+//                 log.info("配偶调整。");
+//                 for (int index = 0, length = familyRelations.size(); index < length; index++) {
+//                     if ("配偶".equals(familyRelations.get(index).getRelation())) {
+//                         PerFamRel temp = familyRelations.get(0);
+//                         familyRelations.set(0, familyRelations.get(index));
+//                         familyRelations.set(index, temp);
+//                     }
+//                 }
+//             }
+//         }
+//         return familyRelations;
+//     }
+//
+//     /**
+//      * TODO 根据id获取信息列表方法。
+//      *
+//      * @param id
+//      * @return
+//      */
+//     public List<PerForm> getPerForms(long id) {
+//         List<PerForm> form = new ArrayList<>();
+//         PerForm perForm = new PerForm();
+//         Per personnel = perMapper.selectById(id);
+//         if (null != personnel) {
+//             perForm.setPer(personnel);
+//         }
+//         List<PerEduRec> eduRecords = eduRecService.findByPid(id);
+//         if (CollectionUtils.isNotEmpty(eduRecords)) {
+//             perForm.setEduRecords(eduRecords);
+//         }
+//
+//         List<PerWorkRec> workRecords = workRecService.findByPid(id);
+//         if (CollectionUtils.isNotEmpty(workRecords)) {
+//             perForm.setWorkRecords(workRecords);
+//         }
+//
+//         List<PerFamRel> familyRelations = famRelService.findByPid(id);
+//         if (CollectionUtils.isNotEmpty(familyRelations)) {
+//             perForm.setFamRelations(toFamilyRelations(familyRelations));
+//         }
+//
+//         List<PerSocRel> socialRelations = socRelService.findByPid(id);
+//         if (CollectionUtils.isNotEmpty(socialRelations)) {
+//
+//             perForm.setSocRelations(socialRelations);
+//         }
+//
+//         List<PerRewRec> rewards = rewRecService.findByPid(id);
+//         if (CollectionUtils.isNotEmpty(rewards)) {
+//             perForm.setRewRecords(rewards);
+//         }
+//         form.add(perForm);
+//         log.info("getPerForms:{}" , JSON.toJSONString(form));
+//         return form;
+//     }
 // }