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.ConfigUnitDivisionTemplateDO;
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 ConfigUnitDivisionTemplateMapper extends BaseMapperX<ConfigUnitDivisionTemplateDO> {

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

    /** 按 catalogItemId + tabType 查询模板列表 */
    default List<ConfigUnitDivisionTemplateDO> selectListByCatalogItemIdAndTabType(Long catalogItemId, String tabType) {
        return selectList(new com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper<ConfigUnitDivisionTemplateDO>()
                .eq(ConfigUnitDivisionTemplateDO::getCatalogItemId, catalogItemId)
                .eq(ConfigUnitDivisionTemplateDO::getTabType, tabType)
                .orderByAsc(ConfigUnitDivisionTemplateDO::getSortOrder));
    }

    default List<ConfigUnitDivisionTemplateDO> selectListByParentId(Long parentId) {
        return selectList(new com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper<ConfigUnitDivisionTemplateDO>()
                .eq(ConfigUnitDivisionTemplateDO::getParentId, parentId)
                .orderByAsc(ConfigUnitDivisionTemplateDO::getSortOrder));
    }

    @Select("<script>" +
            "SELECT COALESCE(MAX(sort_order), 0) FROM yhy_config_unit_division_template " +
            "WHERE catalog_item_id = #{catalogItemId} " +
            "AND tab_type = #{tabType} " +
            "<if test='parentId != null'>AND parent_id = #{parentId} </if>" +
            "<if test='parentId == null'>AND parent_id IS NULL </if>" +
            "AND deleted = 0" +
            "</script>")
    Integer selectMaxSortOrderByParentId(@Param("catalogItemId") Long catalogItemId,
                                         @Param("parentId") Long parentId,
                                         @Param("tabType") String tabType);

    default List<ConfigUnitDivisionTemplateDO> selectListByParentIdAndSortOrderGe(Long parentId, Integer sortOrder) {
        return selectList(new com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper<ConfigUnitDivisionTemplateDO>()
                .eq(ConfigUnitDivisionTemplateDO::getParentId, parentId)
                .ge(ConfigUnitDivisionTemplateDO::getSortOrder, sortOrder)
                .orderByAsc(ConfigUnitDivisionTemplateDO::getSortOrder));
    }
}
