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

/**
 * 统一取费单价 Mapper
 */
@Mapper
public interface QuotaUnifiedFeeMapper extends BaseMapperX<QuotaUnifiedFeeDO> {

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

    /**
     * 根据模式节点ID查询父节点列表
     */
    default List<QuotaUnifiedFeeDO> selectParentListByCatalogItemId(Long catalogItemId) {
        return selectList(new LambdaQueryWrapperX<QuotaUnifiedFeeDO>()
                .eq(QuotaUnifiedFeeDO::getCatalogItemId, catalogItemId)
                .isNull(QuotaUnifiedFeeDO::getParentId)
                .orderByAsc(QuotaUnifiedFeeDO::getSortOrder));
    }

    /**
     * 根据父节点ID查询子节点列表
     */
    default List<QuotaUnifiedFeeDO> selectChildListByParentId(Long parentId) {
        return selectList(new LambdaQueryWrapperX<QuotaUnifiedFeeDO>()
                .eq(QuotaUnifiedFeeDO::getParentId, parentId)
                .orderByAsc(QuotaUnifiedFeeDO::getSortOrder));
    }

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

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

    /**
     * 检查是否有子节点
     */
    default boolean hasChildren(Long parentId) {
        return selectCount(new LambdaQueryWrapperX<QuotaUnifiedFeeDO>()
                .eq(QuotaUnifiedFeeDO::getParentId, parentId)) > 0;
    }

    /**
     * 根据feeItemId和catalogItemId查询
     */
    default QuotaUnifiedFeeDO selectByFeeItemIdAndCatalogItemId(Long feeItemId, Long catalogItemId) {
        return selectOne(new LambdaQueryWrapperX<QuotaUnifiedFeeDO>()
                .eq(QuotaUnifiedFeeDO::getFeeItemId, feeItemId)
                .eq(QuotaUnifiedFeeDO::getCatalogItemId, catalogItemId));
    }
}
