From c020bb8a0e827f2e98afbfd209a3ff5c383413d6 Mon Sep 17 00:00:00 2001
From: lxw <810412026@qq.com>
Date: 星期二, 13 九月 2022 11:45:52 +0800
Subject: [PATCH] 台站管理

---
 src/main/java/com/whyc/service/StationInfoService.java |   69 ++++++++++++++++++++++++++++++----
 1 files changed, 60 insertions(+), 9 deletions(-)

diff --git a/src/main/java/com/whyc/service/StationInfoService.java b/src/main/java/com/whyc/service/StationInfoService.java
index 8ab5578..98b9f38 100644
--- a/src/main/java/com/whyc/service/StationInfoService.java
+++ b/src/main/java/com/whyc/service/StationInfoService.java
@@ -2,15 +2,19 @@
 
 import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
 import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
+import com.whyc.dto.FileOpreate;
 import com.whyc.dto.Response;
+import com.whyc.dto.XmlFileOpreate;
 import com.whyc.mapper.FileInfoMapper;
 import com.whyc.mapper.StationInfoMapper;
+import com.whyc.pojo.FileInfo;
 import com.whyc.pojo.StationInfo;
 import org.springframework.beans.factory.annotation.Autowired;
 import org.springframework.stereotype.Service;
 import org.springframework.transaction.annotation.Transactional;
 
 import java.io.File;
+import java.util.List;
 
 @Service
 public class StationInfoService {
@@ -60,21 +64,68 @@
     @Transactional
     public Response addFileInStation(String stationName,String filePath) {
         File file=new File(filePath);
-        //1.鏌ヨ鍑烘渶澶х殑鍙扮珯id鍜屾渶澶ф枃浠秈d
+        boolean flag=true;
+        //1.鏌ヨ鍑烘渶澶х殑鍙扮珯id(鍙湁鏈烘埧涓嬫坊鍔犱簡鏂囦欢鎵嶄細鐢熸垚鏈烘埧id)鍜屾渶澶ф枃浠秈d
         int stationIdMax=mapper.selectMaxId();
         int fileIdMax=infoMapper.selectMaxId();
         if(file.exists()){
-            //2.缁欏彴绔欒祴浜坰tationId
-            UpdateWrapper uwrapper=new UpdateWrapper();
-            uwrapper.set("station_id",stationIdMax+1);
-            mapper.update(null,uwrapper);
             if(file.isFile()){
-                //3.瑙f瀽鏂囦欢
-
+                //2.瑙f瀽鏂囦欢骞惰ˉ鍏ㄦ枃浠秈d
+                FileInfo fileInfo=XmlFileOpreate.readXml(filePath);
+                fileInfo.setFileUrl(file.getPath());
+                fileInfo.setFileName(file.getName());
+                //4.灏嗗崟鏂囦欢鐨勬暟鎹瓨鍏ユ暟鎹簱
+                insertFileInDataBase(stationName,stationIdMax,fileIdMax,fileInfo);
             }else{
-
+                //3.濡傛灉鏄枃浠跺す锛氳幏鍙栨枃浠跺す涓嬫墍鏈夌殑鏂囦欢
+                List<File> allFile= FileOpreate.getAllFile(filePath);
+                //4.灏嗘枃浠惰矾寰勪笅鐨勬墍鏈夋枃浠惰В鏋愬苟瀛樺叆鏁版嵁搴�
+                insertAllFileInDataBase(stationName,stationIdMax,fileIdMax,allFile);
+            }
+        }else{
+            flag=false;
+        }
+        return new Response().set(1,flag,flag?"鍙扮珯涓嬫坊鍔犳枃浠舵垚鍔�":"鍙扮珯涓嬫坊鍔犳枃浠跺け璐�");
+    }
+    //4.灏嗘枃浠惰矾寰勪笅鐨勬墍鏈夋枃浠惰В鏋愬苟瀛樺叆鏁版嵁搴�
+    private void insertAllFileInDataBase(String stationName, int stationIdMax, int fileIdMax, List<File> allFile) {
+        if(allFile!=null&&allFile.size()>0){
+            for (int i=0;i< allFile.size();i++){
+                File file=allFile.get(i);
+                //2.瑙f瀽鏂囦欢骞惰ˉ鍏ㄦ枃浠秈d
+                FileInfo fileInfo=XmlFileOpreate.readXml(file.getPath());
+                fileInfo.setFileUrl(file.getPath());
+                fileInfo.setFileName(file.getName());
+                //4.灏嗗崟鏂囦欢鐨勬暟鎹瓨鍏ユ暟鎹簱
+                insertFileInDataBase(stationName,stationIdMax,fileIdMax+i,fileInfo);
             }
         }
-        return new Response().set(1);
+    }
+
+    //4.灏嗗崟鏂囦欢鐨勬暟鎹瓨鍏ユ暟鎹簱
+    private void insertFileInDataBase(String stationName,int stationIdMax,int fileIdMax, FileInfo fileInfo) {
+        int count=0;
+        int stationId=0;
+        if(fileInfo!=null){
+            //鏌ヨ绔欑偣鐨勬満鎴挎槸鍚﹀瓨鍦╯tationId
+            QueryWrapper wrapper=new QueryWrapper();
+            stationId=mapper.selectIdByName(stationName);
+            if(stationId!=0){
+                fileInfo.setStationId(String.valueOf(stationId));
+            }else{
+                fileInfo.setStationId(String.valueOf(stationIdMax+1));
+            }
+            fileInfo.setFileId(String.valueOf(fileIdMax+1));
+            count=infoMapper.insert(fileInfo);
+        }
+        if(count>0){
+            if(stationId==0){
+                //鎿嶄綔鎴愬姛缁欏彴绔欒祴浜坰tationId
+                UpdateWrapper uwrapper=new UpdateWrapper();
+                uwrapper.set("station_id",stationIdMax+1);
+                uwrapper.eq("station_name",stationName);
+                mapper.update(null,uwrapper);
+            }
+        }
     }
 }

--
Gitblit v1.9.1