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

import cn.hutool.core.collection.CollectionUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.seer.teach.common.enums.ResultCodeEnum;
import com.seer.teach.common.enums.UserRelationEnum;
import com.seer.teach.common.utils.AssertUtils;
import com.seer.teach.user.api.dto.UserInfoDTO;
import com.seer.teach.user.entity.UserEntity;
import com.seer.teach.user.entity.UserRelationEntity;
import com.seer.teach.user.mapper.UserMapper;
import com.seer.teach.user.service.IUserAuthService;
import com.seer.teach.user.service.IUserRelationService;
import com.seer.teach.user.service.IUserService;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;

import java.util.Collection;
import java.util.Collections;
import java.util.List;
import java.util.Set;
import java.util.stream.Collectors;

/**
 * <p>
 * 用户表 服务实现类
 * </p>
 *
 * @author Wang
 * @since 2025-07-16 17:01:56
 */
@RequiredArgsConstructor
@Slf4j
@Service
public class UserServiceImpl extends ServiceImpl<UserMapper, UserEntity> implements IUserService {

    private final IUserRelationService userRelationService;

    private final IUserAuthService userAuthService;

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

    @Override
    public UserEntity getOneByMobile(String mobile) {
        return getOne(new LambdaQueryWrapper<UserEntity>()
                .eq(UserEntity::getMobile, mobile));
    }

    @Override
    public UserEntity getOneByUserName(String userName) {
        return getOne(new LambdaQueryWrapper<UserEntity>()
                .eq(UserEntity::getUserName, userName));
    }

    @Override
    public UserEntity getChildrenByUserId(Integer userId,Integer childId) {
        UserRelationEntity userRelation = userRelationService.getUserRelationByUserIdAndRelationId(userId,childId, UserRelationEnum.PARENT_CHILD);
        log.info("根据用户id[{}]获取孩子信息:{}", userId, userRelation);
        if (userRelation != null) {
            return getById(userRelation.getRelationId());
        }
        return null;
    }

    @Override
    public List<UserEntity> getListByUserIds(Collection<Integer> ids) {
        return super.list(new LambdaQueryWrapper<UserEntity>().in(UserEntity::getId, ids));
    }

    @Override
    public List<UserEntity> getDefaultChildrenByUserIds(Collection<Integer> ids) {
        List<UserRelationEntity> userRelations = userRelationService.getUserRelationListByUserIdsAndRelation(ids, UserRelationEnum.PARENT_CHILD);
        if(CollectionUtil.isEmpty(userRelations)){
            return Collections.emptyList();
        }
        Set<Integer> childrenIds = userRelations.stream()
                .filter(relation-> UserRelationEntity.IS_DEFAULT == relation.getIsDefault())
                .map(UserRelationEntity::getRelationId).collect(Collectors.toSet());
        return getListByUserIds(childrenIds);
    }

    @Override
    public UserEntity checkUserExistsByUserId(Integer userId) {
        UserEntity user = getById(userId);
        AssertUtils.notNull(user, ResultCodeEnum.USER_NOT_FOUND);
        return user;
    }
}