package com.erp.tenant.repository.feature;

import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import com.erp.tenant.entity.feature.FeatureFlag;
import org.apache.ibatis.annotations.Mapper;
import org.apache.ibatis.annotations.Param;
import org.apache.ibatis.annotations.Select;

import java.util.List;

@Mapper
public interface FeatureFlagRepository extends BaseMapper<FeatureFlag> {

    /**
     * 查询套餐的功能开关
     */
    @Select("SELECT * FROM feature_flags WHERE (package_id = #{packageId} OR package_id IS NULL) AND (tenant_id IS NULL) ORDER BY sort ASC")
    List<FeatureFlag> findByPackageId(@Param("packageId") Long packageId);

    /**
     * 查询租户的功能开关（包含套餐级别和租户级别）
     */
    @Select("SELECT * FROM feature_flags WHERE (tenant_id = #{tenantId} OR tenant_id IS NULL) ORDER BY sort ASC")
    List<FeatureFlag> findByTenantId(@Param("tenantId") Long tenantId);

    /**
     * 查询所有启用的功能开关
     */
    @Select("SELECT * FROM feature_flags WHERE enabled = true ORDER BY sort ASC")
    List<FeatureFlag> findAllEnabled();

    /**
     * 根据功能键查询
     */
    @Select("SELECT * FROM feature_flags WHERE feature_key = #{featureKey} LIMIT 1")
    FeatureFlag findByFeatureKey(@Param("featureKey") String featureKey);
}
