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

import cn.iocoder.yudao.framework.mybatis.core.mapper.BaseMapperX;
import com.yhy.module.core.dal.dataobject.config.ConfigUnitTabRefDO;
import java.util.List;
import org.apache.ibatis.annotations.Mapper;
import org.apache.ibatis.annotations.Param;
import org.apache.ibatis.annotations.Select;

@Mapper
public interface ConfigUnitTabRefMapper extends BaseMapperX<ConfigUnitTabRefDO> {

    default List<ConfigUnitTabRefDO> selectListByCatalogAndTab(Long catalogItemId, String tabType) {
        return selectList(new com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper<ConfigUnitTabRefDO>()
                .eq(ConfigUnitTabRefDO::getCatalogItemId, catalogItemId)
                .eq(ConfigUnitTabRefDO::getTabType, tabType)
                .orderByAsc(ConfigUnitTabRefDO::getSortOrder));
    }

    default List<ConfigUnitTabRefDO> selectListByCatalogItemId(Long catalogItemId) {
        return selectList(new com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper<ConfigUnitTabRefDO>()
                .eq(ConfigUnitTabRefDO::getCatalogItemId, catalogItemId)
                .orderByAsc(ConfigUnitTabRefDO::getSortOrder));
    }

    /** 删除指定模板节点的所有引用（级联删除用） */
    default void deleteByTemplateNodeId(Long templateNodeId) {
        delete(new com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper<ConfigUnitTabRefDO>()
                .eq(ConfigUnitTabRefDO::getTemplateNodeId, templateNodeId));
    }

    @Select("SELECT COALESCE(MAX(sort_order), 0) FROM yhy_config_unit_tab_ref WHERE catalog_item_id = #{catalogItemId} AND tab_type = #{tabType} AND deleted = 0")
    Integer selectMaxSortOrder(@Param("catalogItemId") Long catalogItemId, @Param("tabType") String tabType);
}
