package com.lesingle.creation.mapper;

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

/**
 * 租户 Mapper 接口
 */
@Mapper
public interface TenantMapper extends BaseMapper<Tenant> {

    /**
     * 根据编码查询租户
     *
     * @param code 租户编码
     * @return 租户实体
     */
    @Select("SELECT * FROM t_sys_tenant WHERE code = #{code} AND deleted = 0")
    Tenant selectByCode(@Param("code") String code);

    /**
     * 根据域名查询租户
     *
     * @param domain 租户域名
     * @return 租户实体
     */
    @Select("SELECT * FROM t_sys_tenant WHERE domain = #{domain} AND deleted = 0")
    Tenant selectByDomain(@Param("domain") String domain);

    /**
     * 根据编码查询租户（排除指定 ID，用于更新时检查重复）
     *
     * @param code  租户编码
     * @param excludeId 排除的 ID
     * @return 租户实体
     */
    @Select("SELECT * FROM t_sys_tenant WHERE code = #{code} AND id != #{excludeId} AND deleted = 0")
    Tenant selectByCodeExclude(@Param("code") String code, @Param("excludeId") Long excludeId);

    /**
     * 根据域名查询租户（排除指定 ID，用于更新时检查重复）
     *
     * @param domain  租户域名
     * @param excludeId 排除的 ID
     * @return 租户实体
     */
    @Select("SELECT * FROM t_sys_tenant WHERE domain = #{domain} AND id != #{excludeId} AND deleted = 0")
    Tenant selectByDomainExclude(@Param("domain") String domain, @Param("excludeId") Long excludeId);
}
