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.QuotaFeeItemDO;
import org.apache.ibatis.annotations.Mapper;

import java.util.List;

/**
 * 定额取费项 Mapper
 */
@Mapper
public interface QuotaFeeItemMapper extends BaseMapperX<QuotaFeeItemDO> {

    /**
     * 根据模式节点ID查询取费项列表
     */
    default List<QuotaFeeItemDO> selectListByCatalogItemId(Long catalogItemId) {
        return selectList(new LambdaQueryWrapperX<QuotaFeeItemDO>()
                .eq(QuotaFeeItemDO::getCatalogItemId, catalogItemId)
                .orderByAsc(QuotaFeeItemDO::getSortOrder));
    }

    /**
     * 根据模式节点ID和自定义序号查询
     */
    default QuotaFeeItemDO selectByCustomCode(Long catalogItemId, String customCode) {
        return selectOne(new LambdaQueryWrapperX<QuotaFeeItemDO>()
                .eq(QuotaFeeItemDO::getCatalogItemId, catalogItemId)
                .eq(QuotaFeeItemDO::getCustomCode, customCode));
    }

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