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

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.seer.teach.user.entity.LevelEntity;
import com.seer.teach.user.entity.UserExtendEntity;
import com.seer.teach.user.mapper.UserExtendMapper;
import com.seer.teach.user.service.ILevelService;
import com.seer.teach.user.service.IUserExtendService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;
import java.util.Set;

/**
 * <p>
 * 服务实现类
 * </p>
 *
 * @author Wang
 * @since 2025-07-16 17:01:56
 */
@Service
@RequiredArgsConstructor
public class UserExtendServiceImpl extends ServiceImpl<UserExtendMapper, UserExtendEntity> implements IUserExtendService {

    private final ILevelService levelService;

    @Override
    public UserExtendEntity getUserExtendByUserId(Integer userId) {
        return getOne(new LambdaQueryWrapper<UserExtendEntity>().eq(UserExtendEntity::getUserId, userId));
    }

    /**
     * 为用户增加经验值并处理升级逻辑
     *
     * @param userId          用户ID
     * @param experienceValue 要增加的经验值
     * @return
     */
    @Override
    public UserExtendEntity extraPoints(Integer userId, Integer experienceValue) {
        // 获取用户扩展信息
        UserExtendEntity userExtend = getUserExtendByUserId(userId);
        Integer currentLevelId = userExtend.getLevelId();
        LevelEntity currentLevel = levelService.getById(currentLevelId);
        userExtend.setExperience(userExtend.getExperience() + experienceValue);

        // 检查是否达到升级条件
        if (userExtend.getExperience() >= currentLevel.getPoint()) {
            // 扣除升级所需经验值
            userExtend.setExperience(userExtend.getExperience() - currentLevel.getPoint());
            // 获取下一个等级信息
            LevelEntity nextLevel = levelService.getOne(new LambdaQueryWrapper<LevelEntity>()
                    .eq(LevelEntity::getSort, currentLevel.getSort() + 1));
            // 更新用户等级
            userExtend.setLevelId(nextLevel.getId());
        }
        // 更新用户扩展信息
        updateById(userExtend);
        return userExtend;
    }

    @Override
    public List<UserExtendEntity> getListByUserIds(Set<Integer> userIds) {
        return super.list(new LambdaQueryWrapper<>(UserExtendEntity.class).in(UserExtendEntity::getUserId, userIds));
    }

    /**
     * 根据用户ID获取用户扩展信息
     * @param userId
     * @return
     */
    @Override
    public UserExtendEntity getByUserId(Integer userId) {
        return getOne(new LambdaQueryWrapper<UserExtendEntity>().eq(UserExtendEntity::getUserId, userId));
    }

    /**
     * 根据年级Id获取用户扩信息
     * @param id
     * @return
     */
    @Override
    public List<UserExtendEntity> getByGradeId(Integer id) {
        List<UserExtendEntity> list = list(new LambdaQueryWrapper<UserExtendEntity>()
                .eq(UserExtendEntity::getGradeId, id));
        if(list != null && !list.isEmpty()){
            return list;
        }
        return new ArrayList<>();
    }
}