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

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

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