From 116476a873901b54f5f8478b239d23f1c276d324 Mon Sep 17 00:00:00 2001 From: stylefeng Date: Fri, 30 Aug 2024 18:30:49 +0800 Subject: [PATCH] =?UTF-8?q?=E3=80=908.3.0=E3=80=91=E3=80=90db=E3=80=91?= =?UTF-8?q?=E6=9B=B4=E6=96=B0=E9=80=9A=E7=94=A8=E6=9B=B4=E6=96=B0=E5=AD=90?= =?UTF-8?q?=E8=8A=82=E7=82=B9pids=E7=9A=84=E6=96=B9=E6=B3=95?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../roses/kernel/db/api/DbOperatorApi.java | 8 +++++ .../db/mp/dboperator/DbOperatorImpl.java | 36 +++++++++++++++++-- 2 files changed, 41 insertions(+), 3 deletions(-) diff --git a/kernel-d-db/db-api/src/main/java/cn/stylefeng/roses/kernel/db/api/DbOperatorApi.java b/kernel-d-db/db-api/src/main/java/cn/stylefeng/roses/kernel/db/api/DbOperatorApi.java index 1ace273c0..40594a094 100644 --- a/kernel-d-db/db-api/src/main/java/cn/stylefeng/roses/kernel/db/api/DbOperatorApi.java +++ b/kernel-d-db/db-api/src/main/java/cn/stylefeng/roses/kernel/db/api/DbOperatorApi.java @@ -82,4 +82,12 @@ public interface DbOperatorApi { */ Long getMaxSortByTableName(String tableName, String fieldName); + /** + * 更新子节点的parentIdListString + * + * @author fengshuonan + * @since 2024/8/30 17:22 + */ + void updateSubParentIdListString(String tableName, String pidsFieldName, String oldParentIdListString, String newParentIdListString); + } diff --git a/kernel-d-db/db-sdk-mp/src/main/java/cn/stylefeng/roses/kernel/db/mp/dboperator/DbOperatorImpl.java b/kernel-d-db/db-sdk-mp/src/main/java/cn/stylefeng/roses/kernel/db/mp/dboperator/DbOperatorImpl.java index a22c50f85..efbd4df49 100644 --- a/kernel-d-db/db-sdk-mp/src/main/java/cn/stylefeng/roses/kernel/db/mp/dboperator/DbOperatorImpl.java +++ b/kernel-d-db/db-sdk-mp/src/main/java/cn/stylefeng/roses/kernel/db/mp/dboperator/DbOperatorImpl.java @@ -25,7 +25,10 @@ package cn.stylefeng.roses.kernel.db.mp.dboperator; import cn.hutool.core.convert.Convert; +import cn.hutool.core.date.DateUtil; import cn.hutool.core.util.StrUtil; +import cn.stylefeng.roses.kernel.auth.api.context.LoginContext; +import cn.stylefeng.roses.kernel.auth.api.pojo.login.LoginUser; import cn.stylefeng.roses.kernel.db.api.DbOperatorApi; import cn.stylefeng.roses.kernel.db.api.pojo.druid.DruidProperties; import cn.stylefeng.roses.kernel.rule.enums.DbTypeEnum; @@ -34,9 +37,7 @@ import com.baomidou.mybatisplus.extension.toolkit.SqlRunner; import jakarta.annotation.Resource; import org.springframework.stereotype.Service; -import java.util.List; -import java.util.Map; -import java.util.Set; +import java.util.*; import java.util.stream.Collectors; /** @@ -96,4 +97,33 @@ public class DbOperatorImpl implements DbOperatorApi { return Convert.toLong(maxSort); } + @Override + public void updateSubParentIdListString(String tableName, String pidsFieldName, String oldParentIdListString, String newParentIdListString) { + + // 1. 配置sql的模板 + String sqlTemplate = "UPDATE " + " {tableName} " + " SET {pidsFieldName} = replace({pidsFieldName}, ?, ?)," + + " update_time = {updateTime}," + + " update_user = {updateUser}" + + " WHERE {pidsFieldName} LIKE CONCAT('%', ?, '%')"; + + // 2. 装填模板参数 + HashMap paramValueMap = new HashMap<>(); + paramValueMap.put("tableName", tableName); + paramValueMap.put("pidsFieldName", pidsFieldName); + paramValueMap.put("updateTime", DateUtil.formatDateTime(new Date())); + + LoginUser loginUserNullable = LoginContext.me().getLoginUserNullable(); + if (loginUserNullable != null) { + paramValueMap.put("updateUser", String.valueOf(loginUserNullable.getUserId())); + } else { + paramValueMap.put("updateUser", "-1"); + } + + // 3. 拼装sql + String finalSql = StrUtil.format(sqlTemplate, paramValueMap); + + // 4. 执行SQL + SqlRunner.db().update(finalSql, oldParentIdListString, newParentIdListString, oldParentIdListString); + } + }