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

提交关于地籍入库回退上版本

chenendian пре 2 месеци
родитељ
комит
d9e72818c9

+ 14 - 0
siwei-modules/siwei-apply/src/main/java/com/siwei/apply/controller/cadastre/CadastreManageController.java

@@ -174,6 +174,20 @@ public class CadastreManageController extends BaseController {
     }
 
 
+
+    @PostMapping("/backToOld")
+    public R<String> backToOld(String uploadId) {
+        try {
+            String res = cadastreManageService.backToOldData(uploadId);
+            return R.ok(res);
+        } catch (Exception e) {
+            e.printStackTrace();
+            return R.fail(e.getMessage());
+        }
+    }
+
+
+
     @RequestMapping("/testRun")
     public R<Object> testRun() {
         try {

+ 6 - 1
siwei-modules/siwei-apply/src/main/java/com/siwei/apply/mapper/CadastreFileMapper.java

@@ -4,7 +4,6 @@ import com.siwei.apply.domain.CadastreFile;
 import org.apache.ibatis.annotations.Mapper;
 import org.apache.ibatis.annotations.Param;
 
-import java.util.Collection;
 import java.util.List;
 import java.util.Map;
 
@@ -82,5 +81,11 @@ public interface CadastreFileMapper {
      */
     void addValidFlagColumn(@Param("tableName") String tableName);
 
+    void updateValidFlag(@Param("validFlag") String validFlag, @Param("oldValidFlag") String oldValidFlag, @Param("tableName") String tableName);
 
+
+    void deleteByValidFlag(@Param("validFlag") String validFlag,@Param("tableName") String tableName);
+
+
+    Integer countByValidFlag(@Param("validFlag") String validFlag, @Param("tableName") String tableName);
 }

+ 1 - 0
siwei-modules/siwei-apply/src/main/java/com/siwei/apply/service/cadastre/CadastreManageService.java

@@ -33,5 +33,6 @@ public interface CadastreManageService {
 
     void testRun();
 
+    String backToOldData(String uploadId);
 
 }

+ 113 - 9
siwei-modules/siwei-apply/src/main/java/com/siwei/apply/service/cadastre/impl/CadastreManageServiceImpl.java

@@ -12,10 +12,12 @@ import com.siwei.common.core.exception.ServiceException;
 import lombok.extern.slf4j.Slf4j;
 import org.apache.commons.collections4.CollectionUtils;
 import org.apache.commons.lang3.StringUtils;
+import org.apache.ibatis.annotations.Param;
 import org.gdal.ogr.*;
 import org.gdal.osr.SpatialReference;
 import org.springframework.beans.factory.annotation.Autowired;
 import org.springframework.stereotype.Service;
+import org.springframework.transaction.annotation.Propagation;
 import org.springframework.transaction.annotation.Transactional;
 
 import java.nio.file.Files;
@@ -100,14 +102,12 @@ public class CadastreManageServiceImpl implements CadastreManageService {
                         s -> s.get("data_type"),
                         (existing, replacement) -> existing // 如果有重复键,保留第一个值
                 ));
-
                 List<Map<String, Object>> theValueList = getValueList(layer,dbFieldNameAndTypeMap, haveGeom);
                 //这里直接数据入库,区分增量或者全量
                 Boolean addRes = fullDataStorage(layerName, theValueList);
                 log.error("当前图层{},入库结果 {}", layerName, addRes);
                 //Boolean checkRes2 = incrementalDataStorage(layer);
             }
-
         } catch (Exception e) {
             log.error("矢量数据入库异常,uploadId: {}", uploadId, e);
             throw new ServiceException("矢量数据入库异常," + e.getMessage());
@@ -116,6 +116,13 @@ public class CadastreManageServiceImpl implements CadastreManageService {
     }
 
 
+
+
+
+
+
+
+
     /**
      * 获取比较数据
      *
@@ -334,7 +341,6 @@ public class CadastreManageServiceImpl implements CadastreManageService {
         //这里移除校验字段(非属mdb中的字段)
         dbFieldNameList.remove("valid_flag");
 
-
         List<String> layerFieldNameList = this.getFieldName(layer, haveGeom);
         // 判断是否包含相同的元素(不考虑顺序)
         boolean areCollectionsEqual = CollectionUtils.isEqualCollection(dbFieldNameList, layerFieldNameList);
@@ -347,7 +353,7 @@ public class CadastreManageServiceImpl implements CadastreManageService {
         //todo 这里根据表名获取相关校验字段信息
         List<String> checkFieldList = new ArrayList<>();
         //checkFieldList.add("111");
-        //checkFieldList.add("bz");
+        checkFieldList.add("bz");
         //仅获取图层属性
         List<Map<String, Object>> theValueList = getValueList(layer,new LinkedHashMap<>(), false);
         for (Map<String, Object> theMap : theValueList) {
@@ -360,6 +366,11 @@ public class CadastreManageServiceImpl implements CadastreManageService {
         }
         checkInfoVo.setCheckInfoList(checkInfoList);
         //todo 这里需要放入到缓存中,并且可以下载为txt内容
+        if(checkInfoVo.getCheckStatus().equals(true)){
+            checkInfoList.add("当前矢量数据:"+name + "---" + "数据格式为gdb");
+            checkInfoList.add("当前矢量数据:"+name + "---" + "坐标系是CGCS2000");
+            checkInfoList.add("当前矢量数据:"+name + "---" + "检查校验通过!");
+        }
         return checkInfoVo;
     }
 
@@ -393,10 +404,10 @@ public class CadastreManageServiceImpl implements CadastreManageService {
      */
     private List<Map<String, Object>> getValueList(Layer layer,Map<String,String> fieldNameAndTypeMap , Boolean haveGeom) {
         List<Map<String, Object>> list = new ArrayList<>();
-        Map<String, Object> map = new LinkedHashMap<>();
         layer.ResetReading();
         Feature feature;
         while ((feature = layer.GetNextFeature()) != null) {
+            Map<String, Object> map = new LinkedHashMap<>();
             for (int i = 0; i < feature.GetFieldCount(); i++) {
                 String fieldName = feature.GetFieldDefnRef(i).GetName();
                 if(fieldNameAndTypeMap.containsKey(fieldName.toLowerCase())) {
@@ -428,6 +439,11 @@ public class CadastreManageServiceImpl implements CadastreManageService {
 //                    // 为几何体设置空间参考(SRID 4326)
 //                    geom.AssignSpatialReference(spatialRef);
                     // 将几何对象转换为 WKT 格式
+                    geom = geom.MakeValid();
+                    int type = geom.GetGeometryType() & 0xFF;
+                    if (type != ogr.wkbPolygon && type != ogr.wkbMultiPolygon) {
+                        continue; // 直接跳过
+                    }
                     ewkt = "SRID=" + 4326 + ";" + geom.ExportToWkt();;
                 }
                 map.put("geom", ewkt);
@@ -437,7 +453,6 @@ public class CadastreManageServiceImpl implements CadastreManageService {
         return list;
     }
 
-
     /**
      * 校验当前图层坐标系是否为:CGCS2000
      *
@@ -487,13 +502,40 @@ public class CadastreManageServiceImpl implements CadastreManageService {
     }
 
 
+
     /**
      * 获取对比的矢量图层
      *
      * @param layer
      * @return
      */
-    private List<String> getLayerGeom(Layer layer) {
+    private static List<String> getLayerGeom(Layer layer) {
+        List<String> list = new ArrayList<>();
+        layer.ResetReading();
+        Feature feature;
+        while ((feature = layer.GetNextFeature()) != null) {
+            Geometry geom = feature.GetGeometryRef();
+            String ewkt = "";
+            if (geom != null) {
+                // 将几何对象转换为 WKT 格式
+                ewkt = "SRID=" + 4326 + ";" + geom.ExportToWkt();;
+            }
+            list.add(ewkt);
+        }
+        return list;
+    }
+
+
+
+
+
+    /**
+     * 获取对比的矢量图层
+     *
+     * @param layer
+     * @return
+     */
+    private List<String> getLayerGeom_del(Layer layer) {
         List<String> list = new ArrayList<>();
         // 循环处理每个要素
         for (int i = 0; i < layer.GetFeatureCount(0); i++) {
@@ -535,13 +577,19 @@ public class CadastreManageServiceImpl implements CadastreManageService {
      * @param theValueList
      * @return
      */
-    private Boolean fullDataStorage(String tableName, List<Map<String, Object>> theValueList) {
+    @Transactional(propagation = Propagation.REQUIRED,rollbackFor = Exception.class)
+    public Boolean fullDataStorage(String tableName, List<Map<String, Object>> theValueList) {
         boolean addRes = false;
+        Integer shpDbSRID = 4326;
         if (!CollectionUtils.isEmpty(theValueList)) {
+            //先删除数据
+            cadastreFileMapper.deleteByValidFlag("1",tableName);
+            //先把当前版本都修改为历史版本
+            cadastreFileMapper.updateValidFlag("1","0",tableName);
             for (Map<String, Object> map : theValueList) {
 //                Collection<Object> values = map.values();
 //                Collection<String> keys = map.keySet();
-                Integer shpDbSRID = 4326;
+                map.put("valid_flag", 0);//有效数据标识
                 cadastreFileMapper.insertTableData(tableName,map,shpDbSRID);
             }
         }
@@ -613,10 +661,66 @@ public class CadastreManageServiceImpl implements CadastreManageService {
             cadastreFileMapper.addValidFlagColumn(tableName);
             System.out.println("正在处理表: " + tableName + " 完成");
         }
+    }
 
+    @Transactional(propagation = Propagation.REQUIRED, rollbackFor = Exception.class)
+    @Override
+    public String backToOldData(String uploadId) {
+        //1.获取所有表名。
+        //2.校验是否都有历史版本。
+        //3.如果都有历史版本,则把当前版本删除,把历史版本修改为当前版本。
+        List<String>  layerNameList = getLayerNameList(uploadId);
+        for(String layerName : layerNameList){
+            if (!layerName.equalsIgnoreCase("T_CGZJ_CWTC_M")) {
+                continue;
+            }
+
+            //校验是否都有历史版本
+            Integer count = cadastreFileMapper.countByValidFlag("1", layerName.toLowerCase(Locale.ROOT));
+            if(count == 0){
+                throw new ServiceException("表"+layerName+"没有历史版本,无法回退");
+            }
+        }
+        for(String layerName : layerNameList){
+            if (!layerName.equalsIgnoreCase("T_CGZJ_CWTC_M")) {
+                continue;
+            }
+            cadastreFileMapper.deleteByValidFlag("0", layerName.toLowerCase(Locale.ROOT));
+            cadastreFileMapper.updateValidFlag("0","1", layerName.toLowerCase(Locale.ROOT));
+            log.info("正在回退表:{}。。。", layerName);
+        }
+        return "当前批次数据已回退到上一个版本!:"+uploadId;
     }
 
 
 
 
+    /**
+     * 获取图层名称列表
+     * @param uploadId
+     * @return
+     */
+    public List<String> getLayerNameList(String uploadId) {
+        List<String> nameList = new ArrayList<>();
+        CadastreFile cadastreFile = cadastreFileMapper.get(uploadId);
+        String gdbPath = cadastreFile.getUnzipPath();
+        DataSource ds = ogr.Open(gdbPath + "/" + cadastreFile.getName(), 0); // 0 = 只读
+        if (ds == null) {
+            throw new ServiceException("无法打开 gdb" );
+        }
+        //这里进行遍历每个图层
+        for (int i = 0; i < ds.GetLayerCount(); i++) {
+            Layer layer = ds.GetLayer(i);
+            String layerName = layer.GetName();
+            nameList.add(layerName);
+        }
+        return nameList;
+    }
+
+
+
+
+
+
+
 }

+ 16 - 0
siwei-modules/siwei-apply/src/main/resources/mapper/cadastre/CadastreFileMapper.xml

@@ -158,6 +158,12 @@
         from vector.${tableName} gd
     </select>
 
+    <select id="countByValidFlag" resultType="int">
+        SELECT COUNT(*)
+        FROM vector.${tableName}
+        WHERE valid_flag = ${validFlag}
+    </select>
+
     <update id="addValidFlagColumn">
             COMMENT ON COLUMN vector.${tableName}."valid_flag" IS '有效标识:0-当前版本,1-历史版本';
     </update>
@@ -214,4 +220,14 @@
         </where>
     </select>
 
+    <update id="updateValidFlag">
+        UPDATE vector.${tableName}  SET  valid_flag  = ${validFlag} WHERE 1=1
+        <if test="oldValidFlag != null and oldValidFlag != ''">
+            and valid_flag= ${oldValidFlag}
+        </if>
+    </update>
+
+    <delete id="deleteByValidFlag">
+        DELETE  FROM  vector.${tableName}   WHERE valid_flag = ${validFlag}
+    </delete>
 </mapper>