package com.xly.erp.module.mod.mapper;

import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import com.xly.erp.module.mod.entity.Module;
import org.apache.ibatis.annotations.Param;
import org.apache.ibatis.annotations.Select;

import java.util.List;

public interface ModuleMapper extends BaseMapper<Module> {

    @Select("SELECT 1 FROM tModule WHERE iIncrement = #{id} AND bDeleted = 0 LIMIT 1")
    Integer findActiveFlagById(@Param("id") Integer iIncrement);

    default boolean existsActiveById(Integer iIncrement) {
        return findActiveFlagById(iIncrement) != null;
    }

    @Select("SELECT iParentId FROM tModule WHERE iIncrement = #{id} AND bDeleted = 0")
    Integer selectParentIdById(@Param("id") Integer iIncrement);

    @Select("SELECT 1 FROM tModule WHERE iParentId = #{parentId} AND bDeleted = 0 LIMIT 1")
    Integer findActiveChildFlag(@Param("parentId") Integer parentId);

    default boolean hasActiveChildren(Integer parentId) {
        return findActiveChildFlag(parentId) != null;
    }

    @Select("SELECT iIncrement, sModuleNameZh, sDisplayType, sManageDeptEn, iParentId, iSortOrder "
            + "FROM tModule WHERE bDeleted = 0 AND sModuleNameZh LIKE CONCAT('%', #{keyword}, '%') "
            + "ORDER BY iSortOrder ASC, iIncrement ASC")
    List<Module> selectActiveByKeyword(@Param("keyword") String keyword);
}
