package com.lesingle.creation.mapper;

import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import com.lesingle.creation.entity.Permission;
import org.apache.ibatis.annotations.Mapper;
import org.apache.ibatis.annotations.Param;
import org.apache.ibatis.annotations.Select;

/**
 * 权限 Mapper 接口
 */
@Mapper
public interface PermissionMapper extends BaseMapper<Permission> {

    /**
     * 根据租户 ID 和权限编码查询权限
     *
     * @param tenantId 租户 ID
     * @param code 权限编码
     * @return 权限实体
     */
    @Select("SELECT * FROM t_auth_permission WHERE tenant_id = #{tenantId} AND code = #{code} AND deleted = 0")
    Permission selectByCode(@Param("tenantId") Long tenantId, @Param("code") String code);

    /**
     * 根据租户 ID 和资源操作查询权限
     *
     * @param tenantId 租户 ID
     * @param resource 资源名称
     * @param action 操作类型
     * @return 权限实体
     */
    @Select("SELECT * FROM t_auth_permission WHERE tenant_id = #{tenantId} AND resource = #{resource} AND action = #{action} AND deleted = 0")
    Permission selectByResourceAction(@Param("tenantId") Long tenantId, @Param("resource") String resource, @Param("action") String action);

    /**
     * 根据租户 ID 和权限编码查询权限（排除指定 ID）
     *
     * @param tenantId 租户 ID
     * @param code 权限编码
     * @param excludeId 排除的 ID
     * @return 权限实体
     */
    @Select("SELECT * FROM t_auth_permission WHERE tenant_id = #{tenantId} AND code = #{code} AND id != #{excludeId} AND deleted = 0")
    Permission selectByCodeExclude(@Param("tenantId") Long tenantId, @Param("code") String code, @Param("excludeId") Long excludeId);

    /**
     * 根据租户 ID 和资源操作查询权限（排除指定 ID）
     *
     * @param tenantId 租户 ID
     * @param resource 资源名称
     * @param action 操作类型
     * @param excludeId 排除的 ID
     * @return 权限实体
     */
    @Select("SELECT * FROM t_auth_permission WHERE tenant_id = #{tenantId} AND resource = #{resource} AND action = #{action} AND id != #{excludeId} AND deleted = 0")
    Permission selectByResourceActionExclude(@Param("tenantId") Long tenantId, @Param("resource") String resource, @Param("action") String action, @Param("excludeId") Long excludeId);
}
