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 40594a094..1ace273c0 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,12 +82,4 @@ 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 efbd4df49..a22c50f85 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,10 +25,7 @@ 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; @@ -37,7 +34,9 @@ import com.baomidou.mybatisplus.extension.toolkit.SqlRunner; import jakarta.annotation.Resource; 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; /** @@ -97,33 +96,4 @@ 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); - } - } diff --git a/kernel-d-tree/pom.xml b/kernel-d-tree/pom.xml index 7e36d44c6..386d709cf 100644 --- a/kernel-d-tree/pom.xml +++ b/kernel-d-tree/pom.xml @@ -24,10 +24,10 @@ ${roses.version} - + com.javaguns.roses - db-api + auth-api ${roses.version} diff --git a/kernel-d-tree/src/main/java/cn/stylefeng/roses/kernel/rule/pidset/CalcParentIdListUtil.java b/kernel-d-tree/src/main/java/cn/stylefeng/roses/kernel/rule/pidset/CalcParentIdListUtil.java index e5a791a21..96f8d0ea9 100644 --- a/kernel-d-tree/src/main/java/cn/stylefeng/roses/kernel/rule/pidset/CalcParentIdListUtil.java +++ b/kernel-d-tree/src/main/java/cn/stylefeng/roses/kernel/rule/pidset/CalcParentIdListUtil.java @@ -1,13 +1,18 @@ package cn.stylefeng.roses.kernel.rule.pidset; +import cn.hutool.core.date.DateUtil; 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.TreeConstants; 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.mapper.CommonUpdatePidMapper; import cn.stylefeng.roses.kernel.rule.pidset.pojo.ParentIdInfoPojo; +import java.util.Date; + /** * pid和pids的填充计算 * @@ -69,14 +74,21 @@ public class CalcParentIdListUtil { // 计算出被更新的pids 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 - 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结构 - DbOperatorApi dbOperatorApi = SpringUtil.getBean(DbOperatorApi.class); - dbOperatorApi.updateSubParentIdListString(tableName, pidsFieldName, oldPids, newParentIds); + CommonUpdatePidMapper commonUpdatePidMapper = SpringUtil.getBean(CommonUpdatePidMapper.class); + commonUpdatePidMapper.updateSubParentIdListString(tableName, pidsFieldName, oldPids, newParentIds, DateUtil.formatDateTime(new Date()), loginUserId); } } diff --git a/kernel-d-tree/src/main/java/cn/stylefeng/roses/kernel/rule/pidset/mapper/CommonUpdatePidMapper.java b/kernel-d-tree/src/main/java/cn/stylefeng/roses/kernel/rule/pidset/mapper/CommonUpdatePidMapper.java new file mode 100644 index 000000000..d33f81106 --- /dev/null +++ b/kernel-d-tree/src/main/java/cn/stylefeng/roses/kernel/rule/pidset/mapper/CommonUpdatePidMapper.java @@ -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); + +} \ No newline at end of file diff --git a/kernel-d-tree/src/main/java/cn/stylefeng/roses/kernel/rule/pidset/mapper/mapping/CommonUpdatePidMapping.xml b/kernel-d-tree/src/main/java/cn/stylefeng/roses/kernel/rule/pidset/mapper/mapping/CommonUpdatePidMapping.xml new file mode 100644 index 000000000..ef0b3ea43 --- /dev/null +++ b/kernel-d-tree/src/main/java/cn/stylefeng/roses/kernel/rule/pidset/mapper/mapping/CommonUpdatePidMapping.xml @@ -0,0 +1,13 @@ + + + + + + UPDATE ${tableName} + SET ${pidsFieldName} = replace(${pidsFieldName}, #{oldParentIdListString}, #{newParentIdListString}), + update_time = #{updateTime}, + update_user = #{updateUser} + WHERE ${pidsFieldName} LIKE CONCAT('%', #{oldParentIdListString}, '%') + + + \ No newline at end of file