package com.xunhong.erp.turbo.rbac.infrastructure.mapper;

import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import com.xunhong.erp.turbo.rbac.infrastructure.entity.MenuDO;
import org.apache.ibatis.annotations.Mapper;
import org.apache.ibatis.annotations.Param;
import org.apache.ibatis.annotations.Update;

/**
 * @author shenyifei
 */
@Mapper
public interface MenuMapper extends BaseMapper<MenuDO> {
	// 批量更新排序值（用于间隙重整）
	@Update("UPDATE menu t " +
		"JOIN (SELECT menu_id, (ROW_NUMBER() OVER (ORDER BY sort) - 1) * 1000 AS new_sort " +
		"FROM menu WHERE menu_id = #{menuId}) AS sorted_rows " +
		"ON t.menu_id = sorted_rows.menu_id " +
		"SET t.sort = sorted_rows.new_sort WHERE t.menu_id = #{menuId}")
	void batchResetSort(@Param("menuId") Long menuId);
}
