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

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

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