From 9c4f97da16048451d3ffc0a428cfd9f8cb81a50c Mon Sep 17 00:00:00 2001
From: 13693261870 <252740454@qq.com>
Date: 星期日, 04 十二月 2022 11:29:43 +0800
Subject: [PATCH] 1

---
 src/main/java/com/lf/server/extend/UpdateBatchMethod.java |   51 ++++++++++++++++++++++++++++++++++++++++++++++++++-
 1 files changed, 50 insertions(+), 1 deletions(-)

diff --git a/src/main/java/com/lf/server/extend/UpdateBatchMethod.java b/src/main/java/com/lf/server/extend/UpdateBatchMethod.java
index e935fc0..d3f0855 100644
--- a/src/main/java/com/lf/server/extend/UpdateBatchMethod.java
+++ b/src/main/java/com/lf/server/extend/UpdateBatchMethod.java
@@ -1,9 +1,17 @@
 package com.lf.server.extend;
 
 import com.baomidou.mybatisplus.core.injector.AbstractMethod;
+import com.baomidou.mybatisplus.core.metadata.TableFieldInfo;
 import com.baomidou.mybatisplus.core.metadata.TableInfo;
+import com.baomidou.mybatisplus.core.toolkit.sql.SqlScriptUtils;
 import org.apache.ibatis.mapping.MappedStatement;
 import org.apache.ibatis.mapping.SqlSource;
+
+import java.util.ArrayList;
+import java.util.Arrays;
+import java.util.List;
+import java.util.Objects;
+import java.util.stream.Collectors;
 
 /**
  * 鎵归噺鏇存柊鏂规硶
@@ -11,6 +19,8 @@
  */
 @SuppressWarnings("ALL")
 public class UpdateBatchMethod extends AbstractMethod {
+    private static List<String> excludeFields = new ArrayList<>(Arrays.asList("objectid", "createuser", "createtime", "shape_leng", "shape_area"));
+
     /**
      * update user set name = "a", age = 17 where id = 1;
      * update user set name = "b", age = 18 where id = 2;
@@ -33,12 +43,51 @@
     public MappedStatement injectMappedStatement(Class<?> mapperClass, Class<?> modelClass, TableInfo tableInfo) {
         String sql = "<script>\n<foreach collection=\"list\" item=\"item\" separator=\";\">\nupdate %s %s where %s=#{%s} %s\n</foreach>\n</script>";
         String additional = tableInfo.isWithVersion() ? tableInfo.getVersionFieldInfo().getVersionOli("item", "item.") : "" + tableInfo.getLogicDeleteSql(true, true);
-        String setSql = sqlSet(tableInfo.isWithLogicDelete(), false, tableInfo, false, "item", "item.");
+        //String setSql = sqlSet(tableInfo.isWithLogicDelete(), false, tableInfo, false, "item", "item.");
+        String setSql = getSqlSet(tableInfo.isWithLogicDelete(), false, tableInfo, false, "item", "item.");
         String sqlResult = String.format(sql, tableInfo.getTableName(), setSql, tableInfo.getKeyColumn(), "item." + tableInfo.getKeyProperty(), additional);
+
+        // update %s %s where %s=#{%s} %s
+        // update tab set a=#{a} where gid=1
 
         SqlSource sqlSource = languageDriver.createSqlSource(configuration, sqlResult, modelClass);
 
         // 绗笁涓弬鏁板繀椤诲拰RootMapper鐨勮嚜瀹氫箟鏂规硶鍚嶄竴鑷�
         return this.addUpdateMappedStatement(mapperClass, modelClass, "updateBatch", sqlSource);
     }
+
+    private String getSqlSet(boolean logic, boolean ew, TableInfo table, boolean judgeAliasNull, final String alias, final String prefix) {
+        // String sqlScript = table.getAllSqlSet(logic, prefix);
+        String sqlScript = getSqlSet(table);
+        if (judgeAliasNull) {
+            sqlScript = SqlScriptUtils.convertIf(sqlScript, String.format("%s != null", alias), true);
+        }
+        if (ew) {
+            sqlScript = sqlScript + "\n";
+            sqlScript = sqlScript + this.convertIfEwParam("ew.sqlSet", false);
+        }
+
+        sqlScript = SqlScriptUtils.convertSet(sqlScript);
+
+        return sqlScript;
+    }
+
+    private String getSqlSet(TableInfo tableInfo) {
+        StringBuilder sb = new StringBuilder();
+        for (TableFieldInfo f : tableInfo.getFieldList()) {
+            if (excludeFields.contains(f.getColumn())) {
+                continue;
+            }
+
+            if ("geom".equals(f.getColumn())) {
+                sb.append("<if test=\"item['geom'] != null\">geom=${item.geom},</if>\n");
+                continue;
+            }
+
+            sb.append(String.format("<if test=\"item['%s'] != null\">%s=#{item.%s},</if>\n", f.getColumn(), f.getColumn(), f.getColumn()));
+        }
+        sb.deleteCharAt(sb.length() - 1);
+
+        return sb.toString();
+    }
 }

--
Gitblit v1.9.3