<?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="com.xunhong.erp.turbo.rbac.infrastructure.mapper.MenuMapper">
	<resultMap id="BaseResultMap"
			   type="com.xunhong.erp.turbo.rbac.infrastructure.entity.MenuDO">
		<result property="menuId" column="menu_id"/>
		<result property="platformId" column="platform_id"/>
		<result property="name" column="name"/>
		<result property="path" column="path"/>
		<result property="component" column="component"/>
		<result property="pid" column="pid"/>
		<result property="type" column="type"/>
		<result property="sort" column="sort"/>
		<result property="hideInMenu" column="hide_in_menu"/>
		<result property="permissionId" column="permission_id"/>
		<result property="icon" column="icon"/>
		<result property="iconColor" column="icon_color"/>
		<result property="bgColorClass" column="bg_color_class"/>
		<result property="createdAt" column="created_at"/>
		<result property="updatedAt" column="updated_at"/>
		<result property="isDelete" column="is_delete"/>
		<result property="version" column="version"/>
	</resultMap>
	
	<update id="batchResetSort">
		UPDATE menu t 
		JOIN (SELECT menu_id, (ROW_NUMBER() OVER (ORDER BY sort) - 1) * 1000 AS new_sort 
		FROM menu WHERE menu_id = #{menuId} AND platform_id = #{platformId} 
		<if test='pid != null'>
			AND pid = #{pid}
		</if>
		<if test='pid == null'>
			AND pid IS NULL
		</if>
		) AS sorted_rows 
		ON t.menu_id = sorted_rows.menu_id 
		SET t.sort = sorted_rows.new_sort WHERE t.menu_id = #{menuId} AND platform_id = #{platformId} 
		<if test='pid != null'>
			AND pid = #{pid}
		</if>
		<if test='pid == null'>
			AND pid IS NULL
		</if>
	</update>
</mapper>