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

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

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