package com.lesingle.creation.mapper;

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

/**
 * 用户 Mapper 接口
 */
@Mapper
public interface UserMapper extends BaseMapper<User> {

    /**
     * 根据用户名查询用户
     *
     * @param tenantId 租户 ID
     * @param username 用户名
     * @return 用户实体
     */
    @Select("SELECT * FROM t_sys_user WHERE tenant_id = #{tenantId} AND username = #{username} AND deleted = 0")
    User selectByusername(@Param("tenantId") Long tenantId, @Param("username") String username);

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

    /**
     * 根据邮箱查询用户
     *
     * @param tenantId 租户 ID
     * @param email 邮箱
     * @return 用户实体
     */
    @Select("SELECT * FROM t_sys_user WHERE tenant_id = #{tenantId} AND email = #{email} AND deleted = 0")
    User selectByEmail(@Param("tenantId") Long tenantId, @Param("email") String email);

    /**
     * 根据手机号查询用户
     *
     * @param phone 手机号
     * @return 用户实体
     */
    @Select("SELECT * FROM t_sys_user WHERE phone = #{phone} AND deleted = 0")
    User selectByPhone(@Param("phone") String phone);

    /**
     * 根据邮箱查询用户（排除指定 ID，用于更新时检查重复）
     *
     * @param tenantId  租户 ID
     * @param email     邮箱
     * @param excludeId 排除的 ID
     * @return 用户实体
     */
    @Select("SELECT * FROM t_sys_user WHERE tenant_id = #{tenantId} AND email = #{email} AND id != #{excludeId} AND deleted = 0")
    User selectByEmailExclude(@Param("tenantId") Long tenantId, @Param("email") String email, @Param("excludeId") Long excludeId);

    /**
     * 根据用户名查询用户（不限制租户，用于公众用户注册登录）
     *
     * @param username 用户名
     * @return 用户实体
     */
    @Select("SELECT * FROM t_sys_user WHERE username = #{username} AND deleted = 0")
    User selectByUsername(@Param("username") String username);
}
