Browse Source

文件路径增加上日期

lgs 11 months ago
parent
commit
6a3b36f120

+ 8 - 0
src/main/java/com/kexun/service/CrHeadInfService.java

@@ -20,4 +20,12 @@ public interface CrHeadInfService extends IService<CrHeadInfEntity> {
      */
     Long saveHeadInfo(HeadInfo headInfo);
 
+
+    /**
+     * 保存或者修改信息
+     * @param crHeadInfEntity
+     * @return
+     */
+    Long saveUpdateHeadInfo(CrHeadInfEntity crHeadInfEntity);
+
 }

+ 13 - 0
src/main/java/com/kexun/service/impl/CrHeadInfServiceImpl.java

@@ -38,4 +38,17 @@ public class CrHeadInfServiceImpl extends ServiceImpl<CrHeadInfMapper, CrHeadInf
         crHeadInfMapper.insert(crHeadInfEntity);
         return crHeadInfEntity.getId();
     }
+
+    /**
+     * 保存或者修改报文头信息
+     * @param crHeadInfEntity
+     * @return
+     */
+    @Override
+    public Long saveUpdateHeadInfo(CrHeadInfEntity crHeadInfEntity) {
+        super.saveOrUpdate(crHeadInfEntity);
+        return crHeadInfEntity.getId();
+    }
+
+
 }

+ 26 - 2
src/main/java/com/kexun/service/impl/FileServiceImpl.java

@@ -1,15 +1,19 @@
 package com.kexun.service.impl;
 
+import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
 import com.kexun.entity.ReportFileEntity;
 import com.kexun.entity.ReportInfo;
 import com.kexun.service.ReportFileService;
+import com.kexun.utils.DateUtils;
 import org.apache.commons.io.IOUtils;
 import org.springframework.beans.factory.annotation.Autowired;
 import org.springframework.beans.factory.annotation.Value;
 import org.springframework.stereotype.Service;
 
+import java.io.File;
 import java.io.FileOutputStream;
 import java.io.IOException;
+import java.util.Date;
 
 /**
  * Description: 文件处理实现类,包括文件的读取、写入
@@ -60,20 +64,40 @@ public class FileServiceImpl {
      * @param fileName
      */
     public void saveAndWriteFile(ReportInfo reportInfo,String xmlContext,String jsonContext,String fileName){
+        //根据HeadInfId查询是否存在
         ReportFileEntity reportFileEntity = new ReportFileEntity();
+        if (reportInfo.getHeadInfId()!=null){
+            QueryWrapper<ReportFileEntity> queryWrapper = new QueryWrapper<>();
+            queryWrapper.eq("head_inf_id",reportInfo.getHeadInfId());
+            ReportFileEntity reportFileEntityResult = reportFileService.getOne(queryWrapper);
+            reportFileEntity.setId(reportFileEntityResult.getId());
+        }
         reportFileEntity.setHeadInfId(reportInfo.getHeadInfId());
         reportFileEntity.setRptNo(reportInfo.getRptNo());
         reportFileEntity.setBusinessId(reportInfo.getBusinessId());
         reportFileEntity.setBusinessNum(reportInfo.getBusinessNum());
-        reportFileEntity.setFileNameXml(fileName+".xml");
-        reportFileEntity.setFileNameTxt(fileName+".txt");
+        String dateName = DateUtils.format(new Date(),DateUtils.YYYYMMDD);
+        reportFileEntity.setFileNameXml(dateName+ File.separator+fileName+".xml");
+        reportFileEntity.setFileNameTxt(dateName+File.separator+fileName+".txt");
         //保存文件信息表
         reportFileService.saveOrUpdate(reportFileEntity);
         //上传文件到服务器的目录
         //xml文件
+        xmlTargetPath = xmlTargetPath+File.separator+dateName+File.separator;
+        File file = new File(xmlTargetPath);
+        if (!file.exists()){
+            file.mkdirs();
+        }
+        jsonTargetPath = jsonTargetPath+File.separator+dateName+File.separator;
+        File targetPathFile = new File(jsonTargetPath);
+        if (!targetPathFile.exists()){
+            targetPathFile.mkdirs();
+        }
         writeFile(xmlContext,fileName+".xml",xmlTargetPath);
         //json文件
         writeFile(jsonContext,fileName+".txt",jsonTargetPath);
     }
 
+
+
 }

+ 26 - 1
src/main/java/com/kexun/service/impl/XmlFileHandlerServiceImpl.java

@@ -2,12 +2,17 @@ package com.kexun.service.impl;
 
 import com.alibaba.fastjson.JSON;
 import com.kexun.constant.ReportConsts;
+import com.kexun.entity.CrHeadInfEntity;
+import com.kexun.entity.ReportFileEntity;
 import com.kexun.entity.ReportInfo;
 import com.kexun.enums.ReportStatusEnum;
 import com.kexun.model.ReportStruct;
 import com.kexun.model.Request;
+import com.kexun.service.CrHeadInfService;
+import com.kexun.service.ReportFileService;
 import com.kexun.service.ReportInfoService;
 import com.kexun.service.XmlFileHandlerService;
+import com.kexun.utils.DateUtils;
 import lombok.extern.log4j.Log4j;
 import org.apache.commons.io.FileUtils;
 import org.dom4j.*;
@@ -41,6 +46,12 @@ public class XmlFileHandlerServiceImpl implements XmlFileHandlerService {
     @Autowired
     ReportInfoService reportInfoService;
 
+    @Autowired
+    CrHeadInfService crHeadInfService;
+
+    @Autowired
+    ReportFileService reportFileService;
+
     /**
      * 读取指定目录下报文文件
      */
@@ -55,6 +66,7 @@ public class XmlFileHandlerServiceImpl implements XmlFileHandlerService {
                 File[] fileLists = files.listFiles();
                 if (fileLists != null) {
                     //创建目录文件路径
+                    String dateName = DateUtils.format(new Date(), DateUtils.YYYYMMDD);
                     for (File fileList : fileLists) {
                         //只处理xml文件
                         if (fileList.isFile() && fileList.getName().endsWith(".xml")) {
@@ -70,7 +82,11 @@ public class XmlFileHandlerServiceImpl implements XmlFileHandlerService {
                             reportStruct = JSON.parseObject(xmlJson, ReportStruct.class);
                             saveReportInfo(reportStruct);
                             //将该目录下的文件进行移动,移动到指定的备份目录
-                            File targetFile = new File(xmlBackPath+name);
+                            File fileDir = new File(xmlBackPath + dateName);
+                            if (!fileDir.exists()){
+                                fileDir.mkdirs();
+                            }
+                            File targetFile = new File(xmlBackPath+dateName+File.separator+name);
                             try {
                                 FileUtils.moveFile(file,targetFile);
                             } catch (IOException e) {
@@ -142,7 +158,16 @@ public class XmlFileHandlerServiceImpl implements XmlFileHandlerService {
         }
         //文件名
         reportInfo.setFileName(reportStruct.getFileName());
+        //保存cr_head_inf报文头信息
+        CrHeadInfEntity crHeadInfEntity = new CrHeadInfEntity();
+        crHeadInfEntity.setName(reportInfo.getCustomerName());
+        Long headInfoId = crHeadInfService.saveUpdateHeadInfo(crHeadInfEntity);
+        //报文文件信息
+        ReportFileEntity reportFileEntity = new ReportFileEntity();
+        reportFileEntity.setHeadInfId(headInfoId);
+        reportFileService.saveOrUpdate(reportFileEntity);
         //保存报文信息
+        reportInfo.setHeadInfId(headInfoId);
         reportInfoService.saveReportInfo(reportInfo);
     }
 

+ 0 - 1
src/main/java/com/kexun/service/xml/ParseHandleService.java

@@ -295,7 +295,6 @@ public class ParseHandleService {
         commonParam.setIdNum(creNumber);
         commonParam.setRptNo(rptNo);
         HeadInfo headInfo = headJsonObject.toJavaObject(HeadInfo.class);
-
         //返回主键ID
         Long id = crHeadInfService.saveHeadInfo(headInfo);
         commonParam.setHeadInfId(id);

+ 10 - 0
src/main/java/com/kexun/utils/DateUtils.java

@@ -31,6 +31,16 @@ public class DateUtils {
     public static final String UNIT_DAYS = "D";
     public static final String UNIT_YEARS = "Y";
 
+    /**
+     * 日期格式化
+     * @param date
+     * @param pattern
+     * @return
+     */
+    public static String format(final Date date, final String pattern) {
+        final DateFormat format = new SimpleDateFormat(pattern);
+        return format.format(date);
+    }
 
     /**
      * 将带有T的时间字符串转换成yyyy-MM-dd HH:mm:ss

+ 27 - 2
src/test/java/BaseTest.java

@@ -1,7 +1,9 @@
 import com.kexun.utils.DateUtils;
+import org.apache.commons.io.FileUtils;
 import org.apache.commons.io.IOUtils;
 import org.junit.jupiter.api.Test;
 
+import java.io.File;
 import java.io.FileOutputStream;
 import java.io.IOException;
 import java.math.BigDecimal;
@@ -74,12 +76,35 @@ public class BaseTest {
     @Test
     public void testFile(){
         String context = "json文件写入操作";
-        String jsonTargetPath = "/Users/lgs/work/code/zxjx/";
+        String format = DateUtils.format(new Date(), DateUtils.YYYYMMDD);
+        String jsonTargetPath = "/Users/lgs/work/zxjx/test/"+File.separator+format+File.separator;
+        File file = new File(jsonTargetPath);
+        if (!file.exists()){
+            file.mkdirs();
+        }
         try {
-            IOUtils.write(context,new FileOutputStream(jsonTargetPath+"test.json"),"UTF-8");
+            IOUtils.write(context,new FileOutputStream(jsonTargetPath+"测试.txt"),"UTF-8");
             System.out.println("写入成功");
         } catch (IOException e) {
             e.printStackTrace();
         }
     }
+
+
+    /**
+     * 测试移动文件,是否会创建目录
+     */
+    @Test
+    public void testMoveFile() throws Exception{
+        String fileName ="test.txt";
+        String jsonTargetPath = "/Users/lgs/work/zxjx/";
+        File file = new File(jsonTargetPath + "");
+        String format = DateUtils.format(new Date(), DateUtils.YYYYMMDD);
+        File targetFile = new File(jsonTargetPath+format+File.separator);
+        if (!targetFile.exists()){
+            boolean mkdir = targetFile.mkdirs();
+            System.out.println(mkdir);
+        }
+       // FileUtils.moveFile(file,targetFile);
+    }
 }