package com.yhy.module.core.dal.mysql.quota;

import cn.iocoder.yudao.framework.mybatis.core.mapper.BaseMapperX;
import cn.iocoder.yudao.framework.mybatis.core.query.LambdaQueryWrapperX;
import com.yhy.module.core.dal.dataobject.quota.QuotaVariableSettingDO;
import java.util.List;
import org.apache.ibatis.annotations.Mapper;
import org.apache.ibatis.annotations.Param;

/**
 * 单位工程变量设置 Mapper
 */
@Mapper
public interface QuotaVariableSettingMapper extends BaseMapperX<QuotaVariableSettingDO> {

    /**
     * 根据费率模式节点ID和类别查询列表
     */
    default List<QuotaVariableSettingDO> selectListByCatalogItemIdAndCategory(Long catalogItemId, String category) {
        return selectList(new LambdaQueryWrapperX<QuotaVariableSettingDO>()
                .eq(QuotaVariableSettingDO::getCatalogItemId, catalogItemId)
                .eq(QuotaVariableSettingDO::getCategory, category)
                .orderByAsc(QuotaVariableSettingDO::getSortOrder));
    }

    /**
     * 根据费率模式节点ID查询所有类别的列表
     */
    default List<QuotaVariableSettingDO> selectListByCatalogItemId(Long catalogItemId) {
        return selectList(new LambdaQueryWrapperX<QuotaVariableSettingDO>()
                .eq(QuotaVariableSettingDO::getCatalogItemId, catalogItemId)
                .orderByAsc(QuotaVariableSettingDO::getCategory)
                .orderByAsc(QuotaVariableSettingDO::getSortOrder));
    }

    /**
     * 根据费率模式节点ID、类别和代号查询
     */
    default QuotaVariableSettingDO selectByCode(Long catalogItemId, String category, String code) {
        return selectOne(new LambdaQueryWrapperX<QuotaVariableSettingDO>()
                .eq(QuotaVariableSettingDO::getCatalogItemId, catalogItemId)
                .eq(QuotaVariableSettingDO::getCategory, category)
                .eq(QuotaVariableSettingDO::getCode, code));
    }

    /**
     * 根据费率模式节点ID和代号查询（跨类别，不限定 category）
     */
    default QuotaVariableSettingDO selectByCodeAcrossCategories(Long catalogItemId, String code) {
        return selectOne(new LambdaQueryWrapperX<QuotaVariableSettingDO>()
                .eq(QuotaVariableSettingDO::getCatalogItemId, catalogItemId)
                .eq(QuotaVariableSettingDO::getCode, code)
                .last("LIMIT 1"));
    }

    /**
     * 查询同类别最大排序值
     */
    default Integer selectMaxSortOrder(Long catalogItemId, String category) {
        QuotaVariableSettingDO maxItem = selectOne(new LambdaQueryWrapperX<QuotaVariableSettingDO>()
                .eq(QuotaVariableSettingDO::getCatalogItemId, catalogItemId)
                .eq(QuotaVariableSettingDO::getCategory, category)
                .orderByDesc(QuotaVariableSettingDO::getSortOrder)
                .last("LIMIT 1"));
        return maxItem != null ? maxItem.getSortOrder() : 0;
    }

    /**
     * 将指定位置及之后的行的排序值都加1
     * 实现在 QuotaVariableSettingMapper.xml 中
     */
    void incrementSortOrderFrom(@Param("catalogItemId") Long catalogItemId, 
                                 @Param("category") String category, 
                                 @Param("sortOrder") Integer sortOrder);
}
