【8.3.0】【db】更新通用更新子节点pids的方法

master
stylefeng 2024-08-30 18:30:49 +08:00
parent ae760ae09a
commit 116476a873
2 changed files with 41 additions and 3 deletions

View File

@ -82,4 +82,12 @@ public interface DbOperatorApi {
*/ */
Long getMaxSortByTableName(String tableName, String fieldName); 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);
} }

View File

@ -25,7 +25,10 @@
package cn.stylefeng.roses.kernel.db.mp.dboperator; package cn.stylefeng.roses.kernel.db.mp.dboperator;
import cn.hutool.core.convert.Convert; import cn.hutool.core.convert.Convert;
import cn.hutool.core.date.DateUtil;
import cn.hutool.core.util.StrUtil; 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.DbOperatorApi;
import cn.stylefeng.roses.kernel.db.api.pojo.druid.DruidProperties; import cn.stylefeng.roses.kernel.db.api.pojo.druid.DruidProperties;
import cn.stylefeng.roses.kernel.rule.enums.DbTypeEnum; import cn.stylefeng.roses.kernel.rule.enums.DbTypeEnum;
@ -34,9 +37,7 @@ import com.baomidou.mybatisplus.extension.toolkit.SqlRunner;
import jakarta.annotation.Resource; import jakarta.annotation.Resource;
import org.springframework.stereotype.Service; import org.springframework.stereotype.Service;
import java.util.List; import java.util.*;
import java.util.Map;
import java.util.Set;
import java.util.stream.Collectors; import java.util.stream.Collectors;
/** /**
@ -96,4 +97,33 @@ public class DbOperatorImpl implements DbOperatorApi {
return Convert.toLong(maxSort); 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<String, String> 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);
}
} }