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

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

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

