Selaa lähdekoodia

新增config.json 的删除方法

wuweihao 5 vuotta sitten
vanhempi
commit
4be9980436

+ 9 - 0
src/main/java/com/fd/entity/OutputFileEntity.java

@@ -71,6 +71,8 @@ public class OutputFileEntity extends BaseEntity implements Serializable {
      * 8: 服务已发布
      * 9:判断中
      * 10:发布失败
+     * 11: 修改样式完成
+     * 12:样式修改失败
      */
     @Column
     private Integer status;
@@ -93,4 +95,11 @@ public class OutputFileEntity extends BaseEntity implements Serializable {
     @Column
     private String coordType;
 
+
+    /**
+     * 发布服务路径
+     */
+    @Column
+    private String servicePath;
+
 }

+ 7 - 1
src/main/java/com/fd/entity/StyleEntity.java

@@ -43,7 +43,7 @@ public class StyleEntity extends BaseEntity implements Serializable {
     /**
      * 图层
      */
-    @Column
+    @Column(columnDefinition = "LONGTEXT")
     private String layer;
 
 
@@ -60,5 +60,11 @@ public class StyleEntity extends BaseEntity implements Serializable {
     @Column
     private String latitude;
 
+//    /**
+//     * 图层name
+//     */
+//    @Column
+//    private String key;
+
 
 }

+ 13 - 1
src/main/java/com/fd/repository/StyleRepository.java

@@ -1,14 +1,18 @@
 package com.fd.repository;
 
 import com.fd.entity.StyleEntity;
+import org.springframework.data.jpa.repository.JpaRepository;
+import org.springframework.data.jpa.repository.Modifying;
 import org.springframework.data.jpa.repository.Query;
+import org.springframework.transaction.annotation.Transactional;
 
 
 /**
  * Created by Owen on 2019/10/28 0028 11:36
  *
  */
-public interface StyleRepository extends IBaseRepository<StyleEntity, Long> {
+//public interface StyleRepository extends IBaseRepository<StyleEntity, Long> {
+public interface StyleRepository extends JpaRepository<StyleEntity, Long> {
 
 
     /**
@@ -17,4 +21,12 @@ public interface StyleRepository extends IBaseRepository<StyleEntity, Long> {
      */
     @Query(value = "SELECT * from t_style where output_file_id = ?1 order by create_time desc LIMIT 0,1", nativeQuery = true)
     StyleEntity findByOutputFileIdTop(Long id);
+
+    @Transactional
+    @Modifying
+    @Query(value = "DELETE FROM t_style WHERE output_file_id = ?1", nativeQuery = true)
+    void deleteByOutputFileId(Long id);
+
+
+
 }

+ 65 - 12
src/main/java/com/fd/server/impl/VectorServerImpl.java

@@ -24,7 +24,6 @@ import com.fd.util.R;
 import com.fd.util.RegexUtils;
 import lombok.extern.log4j.Log4j2;
 import org.apache.commons.lang3.StringUtils;
-import org.springframework.beans.BeanUtils;
 import org.springframework.beans.factory.annotation.Autowired;
 import org.springframework.beans.factory.annotation.Value;
 import org.springframework.data.domain.Page;
@@ -33,6 +32,7 @@ import org.springframework.data.domain.Sort;
 import org.springframework.stereotype.Service;
 import org.springframework.web.multipart.MultipartFile;
 
+import javax.transaction.Transactional;
 import java.io.BufferedReader;
 import java.io.File;
 import java.io.IOException;
@@ -46,6 +46,7 @@ import java.util.*;
  */
 @Log4j2
 @Service
+@Transactional
 public class VectorServerImpl implements VectorServer {
 
     @Value("${input.file.path.vector}")
@@ -77,9 +78,6 @@ public class VectorServerImpl implements VectorServer {
     @Autowired
     private StyleRepository styleRepository;
 
-//    @Autowired
-//    private LayerRepository layerRepository;
-
     @Override
     public R moveFileToServer(Long fileId, ConfigJsonDto param) {
         Optional<OutputFileEntity> o = outputFileRepository.findById(fileId);
@@ -104,6 +102,7 @@ public class VectorServerImpl implements VectorServer {
 
 
             entity.setStatus(8);
+            entity.setServicePath(slice);
             entity.setUpdateTime(new Date());
             outputFileRepository.save(entity);
             return new R(200, MsgCode.SUCCESS);
@@ -146,6 +145,7 @@ public class VectorServerImpl implements VectorServer {
             deleteFolder(path, entity);
         }
 
+        // 文件类型
         if (entity.getGeojsonPath() != null) {
             String path = entity.getGeojsonPath();
             deleteFolder(path, entity);
@@ -155,10 +155,22 @@ public class VectorServerImpl implements VectorServer {
             FileUtils.delFolder(entity.getSlicePath());
         }
 
+        // 目录类型
+        if (entity.getServicePath() != null) {
+            FileUtils.delFolder(entity.getServicePath());
+        }
+
+
+        // 删除配置文件config.json制定行
+        StyleEntity styleEntity = styleRepository.findByOutputFileIdTop(fileId);
+        deleteRowConfigJson(styleEntity);
+
 
         // 删除数据库记录
         outputFileRepository.deleteById(fileId);
         fileRepository.deleteById(entity.getUploadId());
+        styleRepository.deleteByOutputFileId(fileId);
+
 
         return new R(200, MsgCode.SUCCESS);
     }
@@ -338,11 +350,14 @@ public class VectorServerImpl implements VectorServer {
 
     @Override
     public R saveStyle(StyleDto entity) {
-//        StyleEntity styleEntity = new StyleEntity();
         StyleEntity styleEntity = styleRepository.findByOutputFileIdTop(entity.getOutputFileId());
-
-
-//        BeanUtils.copyProperties(entity, styleEntity);
+        Optional<OutputFileEntity> o = outputFileRepository.findById(entity.getOutputFileId());
+        if (o.isPresent()) {
+            OutputFileEntity outputFileEntity = o.get();
+            outputFileEntity.setStatus(11);
+            outputFileEntity.setUpdateTime(new Date());
+            outputFileRepository.save(outputFileEntity);
+        }
         styleEntity.setUpdateTime(new Date());
         styleEntity.setContent(entity.getContent());
         StyleEntity save = styleRepository.save(styleEntity);
@@ -502,6 +517,9 @@ public class VectorServerImpl implements VectorServer {
      * 修改config.json
      */
     private void writeJsonFile(ConfigJsonDto param, String lastName, OutputFileEntity entity) {
+
+        StyleEntity styleEntity = styleRepository.findByOutputFileIdTop(entity.getId());
+
         String s = FileUtils.readFile(CONFIG_JSON_PATH);
 
         JSONObject original = JSON.parseObject(s);
@@ -512,8 +530,9 @@ public class VectorServerImpl implements VectorServer {
 
         JSONObject subJson = new JSONObject();
         long cu = System.currentTimeMillis();
+        String name = "vector_" + cu;
         // 需要唯一
-        subJson.put("name", "vector_" + cu);
+        subJson.put("name", name);
 
         subJson.put("text", param.getText());
         // vector 就用这个类型geodata
@@ -521,10 +540,9 @@ public class VectorServerImpl implements VectorServer {
         subJson.put("checked", false);
         subJson.put("show", true);
         subJson.put("url", "../data/" + lastName);
+        subJson.put("style", JSON.parse(styleEntity.getContent()));
         layers.add(subJson);
 
-
-
         original.put("layers", layers);
 
         log.info("original update: {}", original.toJSONString());
@@ -533,10 +551,13 @@ public class VectorServerImpl implements VectorServer {
 
             // 将图层信息保存到db
 
-            StyleEntity styleEntity = styleRepository.findByOutputFileIdTop(entity.getId());
+            log.info("check test 1");
             styleEntity.setLayer(subJson.toJSONString());
             styleEntity.setUpdateTime(new Date());
+            styleEntity.setName(name);
+            log.info("check test 2");
             styleRepository.save(styleEntity);
+            log.info("check test 3");
 
         } catch (IOException e) {
             e.printStackTrace();
@@ -544,6 +565,38 @@ public class VectorServerImpl implements VectorServer {
     }
 
 
+    /**
+     * 删除制定行的config.json 数据
+     */
+    private void deleteRowConfigJson(StyleEntity entity){
+        String s = FileUtils.readFile(CONFIG_JSON_PATH);
+        JSONObject original = JSON.parseObject(s);
+        log.info("original: {}", s);
+
+        JSONArray layers = JSON.parseArray(original.getString("layers"));
+
+        for (int i = 0; i < layers.size(); i++) {
+            JSONObject o = (JSONObject) layers.get(i);
+            if (o.getString("name").equals(entity.getName())) {
+                // 删除对象
+                layers.remove(i);
+            }
+        }
+
+        // 更新json
+        original.put("layers", layers);
+        log.info("original update: {}", original.toJSONString());
+
+        // 更新config.json
+        try {
+            FileUtils.fileWriter(JSON.toJSONString(original), CONFIG_JSON_PATH);
+        } catch (IOException e) {
+            e.printStackTrace();
+        }
+
+    }
+
+
     public static void main(String[] args) throws IOException {
 
         String inPath = "F:\\test\\clip\\aaa\\wwww";

+ 26 - 3
src/main/java/com/fd/util/JsonUtils.java

@@ -5,6 +5,8 @@ import com.alibaba.fastjson.JSONArray;
 import com.alibaba.fastjson.JSONObject;
 
 import java.io.IOException;
+import java.util.Arrays;
+import java.util.List;
 
 /**
  * Created by Owen on 2019/11/22 0022 16:35
@@ -14,16 +16,33 @@ public class JsonUtils {
 
     public static void main(String[] args) {
         String path = "static/config.json";
-        String s = readFile(path);
-        System.out.println(s);
+        deleteContent(path);
 
-        writeJsonFile( s);
+    }
 
+    /**
+     * 删除一行json内容
+     * @param path
+     * @return
+     */
+    public static String deleteContent(String path){
+        String s = readFile(path);
+        JSONObject original = JSON.parseObject(s);
 
+        JSONArray layers = JSON.parseArray(original.getString("layers"));
 
+        for (int i = 0; i < layers.size(); i++) {
+            JSONObject o = (JSONObject) layers.get(i);
+            if (o.getString("name").equals("road")) {
+                layers.remove(i);
+            }
+        }
+        System.out.println(layers);
 
+        return null;
     }
 
+
     /**
      * 读取json 文件, windows版
      */
@@ -34,6 +53,10 @@ public class JsonUtils {
 
     }
 
+    /**
+     * 写json
+     * @param str
+     */
     public static void writeJsonFile(String str){
         JSONObject original = JSON.parseObject(str);
 //        String layers = original.getString("layers");

+ 1 - 1
src/main/resources/static/config.json

@@ -3,6 +3,6 @@
     "layers": [
           { "name": "house", "text": "建筑", "type": "geodata", "checked": false, "show": true, "url": "house" },
         { "name": "road", "text": "道路", "type": "geodata", "checked": false, "show": true, "url": "road" },
-        { "name": "heatmap", "text": "热力图", "type": "heatmap", "checked": false, "show": true, "url": "" } 
+        { "name": "heatmap", "text": "热力图", "type": "heatmap", "checked": false, "show": true, "url": "","style":"" }
      ]
 }