package com.yaoai.domain.mapper;

import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.yaoai.domain.entity.User;
import org.apache.ibatis.annotations.Mapper;

import java.util.Optional;

@Mapper
public interface UserMapper extends BaseMapper<User> {

    default Optional<User> findActiveByEmail(String email) {
        return Optional.ofNullable(selectOne(
                Wrappers.<User>lambdaQuery()
                        .eq(User::getEmail, email)
                        .eq(User::getStatus, 1)
                        .isNull(User::getDeletedAt)));
    }

    default boolean existsByEmail(String email) {
        return selectCount(
                Wrappers.<User>lambdaQuery()
                        .eq(User::getEmail, email)
                        .isNull(User::getDeletedAt)) > 0;
    }

    default boolean existsByUsernameAndTenant(String username, Long tenantId) {
        return selectCount(
                Wrappers.<User>lambdaQuery()
                        .eq(User::getUsername, username)
                        .eq(User::getTenantId, tenantId)
                        .isNull(User::getDeletedAt)) > 0;
    }

    default java.util.List<User> findByTenant(Long tenantId) {
        return selectList(Wrappers.<User>lambdaQuery()
                .eq(User::getTenantId, tenantId)
                .isNull(User::getDeletedAt)
                .orderByAsc(User::getCreatedAt));
    }
}
