package com.seer.teach.user.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.seer.teach.user.entity.UserAuthEntity;
import com.seer.teach.user.mapper.UserAuthMapper;
import com.seer.teach.user.service.IUserAuthService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;

import java.util.Collection;
import java.util.Collections;
import java.util.List;

/**
 * <p>
 * 用户第三方认证表（状态增强版） 服务实现类
 * </p>
 *
 * @author Wang
 * @since 2025-08-10 17:42:43
 */
@Slf4j
@Service
public class UserAuthServiceImpl extends ServiceImpl<UserAuthMapper, UserAuthEntity> implements IUserAuthService {

    @Override
    public UserAuthEntity getOneByOpenIdAndUnionId(String openid, String unionid) {
        UserAuthEntity one = getOne(new LambdaQueryWrapper<UserAuthEntity>()
                .eq(UserAuthEntity::getOpenId, openid)
                .eq(UserAuthEntity::getUnionId, unionid));
        log.info("根据openId [{}] 和 unionId [{}] 获取用户信息:{}", openid, unionid, one);
        return one;
    }

    @Override
    public UserAuthEntity getOneByOpenId(String openid) {
        UserAuthEntity one = getOne(new LambdaQueryWrapper<UserAuthEntity>()
                .eq(UserAuthEntity::getOpenId, openid));
        log.info("根据openId [{}] 获取用户信息:{}", openid, one);
        return one;
    }

    @Override
    public UserAuthEntity getOneByUnionId(String unionid) {
        UserAuthEntity one = getOne(new LambdaQueryWrapper<UserAuthEntity>()
                .eq(UserAuthEntity::getUnionId, unionid));
        log.info("根据 unionId [{}] 获取用户信息:{}",  unionid, one);
        return one;
    }

    @Override
    public List<UserAuthEntity> getUserAuthListByUserIdsAndAppId(Collection<Integer> userIds, String appId) {
        return super.list(new LambdaQueryWrapper<>(UserAuthEntity.class).eq(UserAuthEntity::getAppId, appId).in(UserAuthEntity::getUserId, userIds));
    }

    @Override
    public List<UserAuthEntity> getUserAuthListByUserId(Integer userId) {
        return super.list(new LambdaQueryWrapper<>(UserAuthEntity.class).eq(UserAuthEntity::getUserId, userId));
    }

    @Override
    public UserAuthEntity getOneByUserIdAndAppId(Integer userId, String appId) {
        // 根据项目规范，对于可能返回多条记录的查询，使用list()方法配合排序参数
        // 按更新时间倒序排列并取最新的一条记录，确保查询的健壮性和数据的新鲜度
        List<UserAuthEntity> userAuthList = list(new LambdaQueryWrapper<UserAuthEntity>()
                .eq(UserAuthEntity::getUserId, userId)
                .eq(UserAuthEntity::getAppId, appId)
                .eq(UserAuthEntity::getStatus, 1) // 只查询正常状态的记录
                .orderByDesc(UserAuthEntity::getUpdateTime) // 按更新时间倒序
                .last("LIMIT 1")); // 限制只取一条最新记录
        
        UserAuthEntity result = userAuthList.isEmpty() ? null : userAuthList.get(0);
        log.info("userId [{}] 和 appId [{}] 获取用户信息:{}, 共找到{}条记录", userId, appId, result, userAuthList.size());
        return result;
    }
}
