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

/**
 * 统一取费设置 Mapper
 */
@Mapper
public interface QuotaUnifiedFeeSettingMapper extends BaseMapperX<QuotaUnifiedFeeSettingDO> {

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

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

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

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

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

    /**
     * 将指定位置及之后的记录的 sortOrder 都 +1
     * 用于在指定位置插入新记录
     */
    default void incrementSortOrderFrom(Long catalogItemId, Long parentId, Integer fromSortOrder) {
        LambdaQueryWrapperX<QuotaUnifiedFeeSettingDO> wrapper = new LambdaQueryWrapperX<>();
        wrapper.eq(QuotaUnifiedFeeSettingDO::getCatalogItemId, catalogItemId);
        wrapper.ge(QuotaUnifiedFeeSettingDO::getSortOrder, fromSortOrder);
        if (parentId != null) {
            wrapper.eq(QuotaUnifiedFeeSettingDO::getParentId, parentId);
        } else {
            wrapper.isNull(QuotaUnifiedFeeSettingDO::getParentId);
        }
        List<QuotaUnifiedFeeSettingDO> records = selectList(wrapper);
        for (QuotaUnifiedFeeSettingDO record : records) {
            record.setSortOrder(record.getSortOrder() + 1);
            updateById(record);
        }
    }

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