🔨 优化数据分组逻辑.

This commit is contained in:
lijiahangmax
2025-10-17 14:12:14 +08:00
parent 9d3b46e9b3
commit f648e18557
3 changed files with 44 additions and 20 deletions

View File

@@ -45,20 +45,27 @@ public interface DataGroupDAO extends IMapper<DataGroupDO> {
* *
* @param parentId parentId * @param parentId parentId
* @param type type * @param type type
* @param userId userId
* @return max(sort) * @return max(sort)
*/ */
Integer selectMaxSort(@Param("parentId") Long parentId, @Param("type") String type); Integer selectMaxSort(@Param("parentId") Long parentId,
@Param("type") String type,
@Param("userId") Long userId);
/** /**
* 修改排序 * 修改排序
* *
* @param parentId parentId * @param parentId parentId
* @param type type
* @param userId userId
* @param condition 条件 * @param condition 条件
* @param referSort 对比值 * @param referSort 对比值
* @param addition 自增步长 * @param addition 自增步长
* @return effect * @return effect
*/ */
Integer updateSort(@Param("parentId") Long parentId, Integer updateSort(@Param("parentId") Long parentId,
@Param("type") String type,
@Param("userId") Long userId,
@Param("condition") String condition, @Param("condition") String condition,
@Param("referSort") Integer referSort, @Param("referSort") Integer referSort,
@Param("addition") Integer addition); @Param("addition") Integer addition);

View File

@@ -82,7 +82,7 @@ public class DataGroupServiceImpl implements DataGroupService {
// 查询数据是否冲突 // 查询数据是否冲突
this.checkDataGroupPresent(record); this.checkDataGroupPresent(record);
// 查询最大排序 // 查询最大排序
Integer sort = dataGroupDAO.selectMaxSort(request.getParentId(), request.getType()); Integer sort = dataGroupDAO.selectMaxSort(request.getParentId(), request.getType(), request.getUserId());
record.setSort(sort + Const.DEFAULT_SORT); record.setSort(sort + Const.DEFAULT_SORT);
// 插入 // 插入
int effect = dataGroupDAO.insert(record); int effect = dataGroupDAO.insert(record);
@@ -130,14 +130,19 @@ public class DataGroupServiceImpl implements DataGroupService {
Assert.notNull(targetRecord, ErrorMessage.GROUP_ABSENT); Assert.notNull(targetRecord, ErrorMessage.GROUP_ABSENT);
// 更新 // 更新
String type = moveRecord.getType(); String type = moveRecord.getType();
Long userId = moveRecord.getUserId();
Long targetParentId = targetRecord.getParentId(); Long targetParentId = targetRecord.getParentId();
int effect = 0; int effect = 0;
// 修改排序 // 修改排序
if (MovePosition.TOP.equals(position)) { if (MovePosition.TOP.equals(position)) {
// 移动到元素上 将大于等于 targetRecord 的排序都加 10 // 移动到元素上 将大于等于 targetRecord 的排序都加 10
dataGroupDAO.updateSort(targetParentId, ">=", dataGroupDAO.updateSort(targetParentId,
targetRecord.getSort(), Const.DEFAULT_SORT); type,
// 修改 parentId sort userId,
">=",
targetRecord.getSort(),
Const.DEFAULT_SORT);
// 修改关联以及排序
DataGroupDO update = DataGroupDO.builder() DataGroupDO update = DataGroupDO.builder()
.id(id) .id(id)
.parentId(targetParentId) .parentId(targetParentId)
@@ -146,8 +151,8 @@ public class DataGroupServiceImpl implements DataGroupService {
effect = dataGroupDAO.updateById(update); effect = dataGroupDAO.updateById(update);
} else if (MovePosition.IN.equals(position)) { } else if (MovePosition.IN.equals(position)) {
// 移动到元素中 获取最大排序 // 移动到元素中 获取最大排序
Integer newSort = dataGroupDAO.selectMaxSort(targetId, type) + Const.DEFAULT_SORT; Integer newSort = dataGroupDAO.selectMaxSort(targetId, type, userId) + Const.DEFAULT_SORT;
// 修改 parentId sort // 修改关联以及排序
DataGroupDO update = DataGroupDO.builder() DataGroupDO update = DataGroupDO.builder()
.id(id) .id(id)
.parentId(targetId) .parentId(targetId)
@@ -156,9 +161,13 @@ public class DataGroupServiceImpl implements DataGroupService {
effect = dataGroupDAO.updateById(update); effect = dataGroupDAO.updateById(update);
} else if (MovePosition.BOTTOM.equals(position)) { } else if (MovePosition.BOTTOM.equals(position)) {
// 移动到元素下 将大于 targetRecord 的排序都加 10 // 移动到元素下 将大于 targetRecord 的排序都加 10
dataGroupDAO.updateSort(targetParentId, ">", dataGroupDAO.updateSort(targetParentId,
targetRecord.getSort(), Const.DEFAULT_SORT); type,
// 修改 parentId sort userId,
">",
targetRecord.getSort(),
Const.DEFAULT_SORT);
// 修改关联以及排序
DataGroupDO update = DataGroupDO.builder() DataGroupDO update = DataGroupDO.builder()
.id(id) .id(id)
.parentId(targetParentId) .parentId(targetParentId)
@@ -167,7 +176,7 @@ public class DataGroupServiceImpl implements DataGroupService {
effect = dataGroupDAO.updateById(update); effect = dataGroupDAO.updateById(update);
} }
// 删除缓存 // 删除缓存
this.deleteCache(type, moveRecord.getUserId()); this.deleteCache(type, userId);
// 添加日志参数 // 添加日志参数
OperatorLogs.add(OperatorLogs.SOURCE, moveRecord.getName()); OperatorLogs.add(OperatorLogs.SOURCE, moveRecord.getName());
OperatorLogs.add(OperatorLogs.TARGET, targetRecord.getName()); OperatorLogs.add(OperatorLogs.TARGET, targetRecord.getName());

View File

@@ -22,19 +22,27 @@
id, parent_id, type, user_id, name, sort, create_time, update_time, creator, updater, deleted id, parent_id, type, user_id, name, sort, create_time, update_time, creator, updater, deleted
</sql> </sql>
<update id="updateSort">
UPDATE data_group
SET sort = sort + #{addition}
WHERE parent_id = #{parentId}
AND sort ${condition} #{referSort}
</update>
<select id="selectMaxSort" resultType="java.lang.Integer"> <select id="selectMaxSort" resultType="java.lang.Integer">
SELECT IFNULL(MAX(sort), 0) SELECT IFNULL(MAX(sort), 0)
FROM data_group FROM data_group
WHERE deleted = 0 WHERE deleted = 0
AND type = #{type} AND type = #{type}
AND parent_id = #{parentId} AND parent_id = #{parentId}
<if test="userId != null">
AND user_id = #{userId}
</if>
</select> </select>
<update id="updateSort">
UPDATE data_group
SET sort = sort + #{addition}
WHERE deleted = 0
AND type = #{type}
AND parent_id = #{parentId}
AND sort ${condition} #{referSort}
<if test="userId != null">
AND user_id = #{userId}
</if>
</update>
</mapper> </mapper>