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.QuotaRateFieldLabelDO;
import java.util.List;
import org.apache.ibatis.annotations.Mapper;

/**
 * 定额费率字段标签字典 Mapper
 *
 * @author yhy
 */
@Mapper
public interface QuotaRateFieldLabelMapper extends BaseMapperX<QuotaRateFieldLabelDO> {

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

    /**
     * 根据模式节点ID和标签名称查询
     */
    default QuotaRateFieldLabelDO selectByCatalogItemIdAndName(Long catalogItemId, String labelName) {
        return selectOne(new LambdaQueryWrapperX<QuotaRateFieldLabelDO>()
                .eq(QuotaRateFieldLabelDO::getCatalogItemId, catalogItemId)
                .eq(QuotaRateFieldLabelDO::getLabelName, labelName));
    }

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

    /**
     * 更新排序值
     */
    default void updateSortOrder(Long id, Integer sortOrder) {
        QuotaRateFieldLabelDO updateObj = new QuotaRateFieldLabelDO();
        updateObj.setId(id);
        updateObj.setSortOrder(sortOrder);
        updateById(updateObj);
    }
}
