【8.3.0】【tree】更新设置pids的方法

master
stylefeng 2024-08-30 23:30:19 +08:00
parent 1d51be9bf8
commit 1eaca2a03f
6 changed files with 61 additions and 48 deletions

View File

@ -82,12 +82,4 @@ 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,10 +25,7 @@
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;
@ -37,7 +34,9 @@ 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.*; import java.util.List;
import java.util.Map;
import java.util.Set;
import java.util.stream.Collectors; import java.util.stream.Collectors;
/** /**
@ -97,33 +96,4 @@ 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);
}
} }

View File

@ -24,10 +24,10 @@
<version>${roses.version}</version> <version>${roses.version}</version>
</dependency> </dependency>
<!-- 数据库操作依赖,需要更新树节点的子节点 --> <!-- 获取当前用户 -->
<dependency> <dependency>
<groupId>com.javaguns.roses</groupId> <groupId>com.javaguns.roses</groupId>
<artifactId>db-api</artifactId> <artifactId>auth-api</artifactId>
<version>${roses.version}</version> <version>${roses.version}</version>
</dependency> </dependency>

View File

@ -1,13 +1,18 @@
package cn.stylefeng.roses.kernel.rule.pidset; package cn.stylefeng.roses.kernel.rule.pidset;
import cn.hutool.core.date.DateUtil;
import cn.hutool.extra.spring.SpringUtil; import cn.hutool.extra.spring.SpringUtil;
import cn.stylefeng.roses.kernel.db.api.DbOperatorApi; import cn.stylefeng.roses.kernel.auth.api.context.LoginContext;
import cn.stylefeng.roses.kernel.auth.api.pojo.login.LoginUser;
import cn.stylefeng.roses.kernel.rule.constants.SymbolConstant; import cn.stylefeng.roses.kernel.rule.constants.SymbolConstant;
import cn.stylefeng.roses.kernel.rule.constants.TreeConstants; import cn.stylefeng.roses.kernel.rule.constants.TreeConstants;
import cn.stylefeng.roses.kernel.rule.pidset.callback.PidGetterService; import cn.stylefeng.roses.kernel.rule.pidset.callback.PidGetterService;
import cn.stylefeng.roses.kernel.rule.pidset.callback.PidSettable; import cn.stylefeng.roses.kernel.rule.pidset.callback.PidSettable;
import cn.stylefeng.roses.kernel.rule.pidset.mapper.CommonUpdatePidMapper;
import cn.stylefeng.roses.kernel.rule.pidset.pojo.ParentIdInfoPojo; import cn.stylefeng.roses.kernel.rule.pidset.pojo.ParentIdInfoPojo;
import java.util.Date;
/** /**
* pidpids * pidpids
* *
@ -69,14 +74,21 @@ public class CalcParentIdListUtil {
// 计算出被更新的pids // 计算出被更新的pids
String oldPids = oldItem.getParentIdListString(); String oldPids = oldItem.getParentIdListString();
oldPids = oldPids + SymbolConstant.COMMA + SymbolConstant.LEFT_SQUARE_BRACKETS + oldItem.getCurrentId() + SymbolConstant.RIGHT_SQUARE_BRACKETS; oldPids = oldPids + SymbolConstant.LEFT_SQUARE_BRACKETS + oldItem.getCurrentId() + SymbolConstant.RIGHT_SQUARE_BRACKETS + SymbolConstant.COMMA;
// 将pids更改为新的pids // 将pids更改为新的pids
String newParentIds = newItem.getParentIdListString() + SymbolConstant.COMMA + SymbolConstant.LEFT_SQUARE_BRACKETS + newItem.getCurrentId() + SymbolConstant.RIGHT_SQUARE_BRACKETS; String newParentIds = newItem.getParentIdListString() + SymbolConstant.LEFT_SQUARE_BRACKETS + newItem.getCurrentId() + SymbolConstant.RIGHT_SQUARE_BRACKETS + SymbolConstant.COMMA;
// 获取用户id
Long loginUserId = -1L;
LoginUser loginUserNullable = LoginContext.me().getLoginUserNullable();
if (loginUserNullable != null) {
loginUserId = loginUserNullable.getUserId();
}
// 更新pids结构 // 更新pids结构
DbOperatorApi dbOperatorApi = SpringUtil.getBean(DbOperatorApi.class); CommonUpdatePidMapper commonUpdatePidMapper = SpringUtil.getBean(CommonUpdatePidMapper.class);
dbOperatorApi.updateSubParentIdListString(tableName, pidsFieldName, oldPids, newParentIds); commonUpdatePidMapper.updateSubParentIdListString(tableName, pidsFieldName, oldPids, newParentIds, DateUtil.formatDateTime(new Date()), loginUserId);
} }
} }

View File

@ -0,0 +1,26 @@
package cn.stylefeng.roses.kernel.rule.pidset.mapper;
import org.apache.ibatis.annotations.Param;
/**
*
*
* @author fengshuonan
* @since 2024/8/30 22:56
*/
public interface CommonUpdatePidMapper {
/**
* parentIdListString
*
* @author fengshuonan
* @since 2024/8/30 17:22
*/
void updateSubParentIdListString(@Param("tableName") String tableName,
@Param("pidsFieldName") String pidsFieldName,
@Param("oldParentIdListString") String oldParentIdListString,
@Param("newParentIdListString") String newParentIdListString,
@Param("updateTime") String updateTime,
@Param("updateUser") Long updateUser);
}

View File

@ -0,0 +1,13 @@
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE mapper PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN" "http://mybatis.org/dtd/mybatis-3-mapper.dtd">
<mapper namespace="cn.stylefeng.roses.kernel.rule.pidset.mapper.CommonUpdatePidMapper">
<update id="updateSubParentIdListString">
UPDATE ${tableName}
SET ${pidsFieldName} = replace(${pidsFieldName}, #{oldParentIdListString}, #{newParentIdListString}),
update_time = #{updateTime},
update_user = #{updateUser}
WHERE ${pidsFieldName} LIKE CONCAT('%', #{oldParentIdListString}, '%')
</update>
</mapper>