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

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

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