mirror of
				https://github.com/dromara/RuoYi-Vue-Plus.git
				synced 2025-11-04 08:13:44 +08:00 
			
		
		
		
	update 优化 部门修改子元素关系 使用批量更新
This commit is contained in:
		@@ -37,12 +37,4 @@ public interface SysDeptMapper extends BaseMapperPlus<SysDeptMapper, SysDept, Sy
 | 
			
		||||
     */
 | 
			
		||||
    List<Long> selectDeptListByRoleId(@Param("roleId") Long roleId, @Param("deptCheckStrictly") boolean deptCheckStrictly);
 | 
			
		||||
 | 
			
		||||
    /**
 | 
			
		||||
     * 修改子元素关系
 | 
			
		||||
     *
 | 
			
		||||
     * @param depts 子元素
 | 
			
		||||
     * @return 结果
 | 
			
		||||
     */
 | 
			
		||||
    int updateDeptChildren(@Param("depts") List<SysDept> depts);
 | 
			
		||||
 | 
			
		||||
}
 | 
			
		||||
 
 | 
			
		||||
@@ -21,6 +21,7 @@ import com.ruoyi.system.service.ISysDeptService;
 | 
			
		||||
import lombok.RequiredArgsConstructor;
 | 
			
		||||
import org.springframework.stereotype.Service;
 | 
			
		||||
 | 
			
		||||
import java.util.ArrayList;
 | 
			
		||||
import java.util.Arrays;
 | 
			
		||||
import java.util.List;
 | 
			
		||||
 | 
			
		||||
@@ -234,11 +235,15 @@ public class SysDeptServiceImpl implements ISysDeptService {
 | 
			
		||||
    public void updateDeptChildren(Long deptId, String newAncestors, String oldAncestors) {
 | 
			
		||||
        List<SysDept> children = baseMapper.selectList(new LambdaQueryWrapper<SysDept>()
 | 
			
		||||
            .apply("find_in_set({0},ancestors)", deptId));
 | 
			
		||||
        List<SysDept> list = new ArrayList<>();
 | 
			
		||||
        for (SysDept child : children) {
 | 
			
		||||
            child.setAncestors(child.getAncestors().replaceFirst(oldAncestors, newAncestors));
 | 
			
		||||
            SysDept dept = new SysDept();
 | 
			
		||||
            dept.setDeptId(child.getDeptId());
 | 
			
		||||
            dept.setAncestors(child.getAncestors().replaceFirst(oldAncestors, newAncestors));
 | 
			
		||||
            list.add(dept);
 | 
			
		||||
        }
 | 
			
		||||
        if (children.size() > 0) {
 | 
			
		||||
            baseMapper.updateDeptChildren(children);
 | 
			
		||||
        if (list.size() > 0) {
 | 
			
		||||
            baseMapper.updateBatchById(list);
 | 
			
		||||
        }
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
 
 | 
			
		||||
@@ -37,17 +37,4 @@
 | 
			
		||||
        order by d.parent_id, d.order_num
 | 
			
		||||
    </select>
 | 
			
		||||
 | 
			
		||||
    <update id="updateDeptChildren" parameterType="java.util.List">
 | 
			
		||||
        update sys_dept set ancestors =
 | 
			
		||||
        <foreach collection="depts" item="item" index="index"
 | 
			
		||||
                 separator=" " open="case dept_id" close="end">
 | 
			
		||||
            when #{item.deptId} then #{item.ancestors}
 | 
			
		||||
        </foreach>
 | 
			
		||||
        where dept_id in
 | 
			
		||||
        <foreach collection="depts" item="item" index="index"
 | 
			
		||||
                 separator="," open="(" close=")">
 | 
			
		||||
            #{item.deptId}
 | 
			
		||||
        </foreach>
 | 
			
		||||
    </update>
 | 
			
		||||
 | 
			
		||||
</mapper>
 | 
			
		||||
 
 | 
			
		||||
		Reference in New Issue
	
	Block a user