package com.td.Dawa.game.excelcharacterlevel.service;

import com.td.Dawa.common.service.*;
import com.td.Dawa.config.enums.GameEnum;
import com.td.Dawa.game.excelcharacterlevel.pojo.ExcelCharacterLevel;
import com.td.Dawa.game.excelcharacterlevel.vo.ExcelCharacterLevelVo;
import com.td.Dawa.game.excelcharacterlevel.repository.ExcelCharacterLevelRepository;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.persistence.EntityManager;
import javax.persistence.PersistenceContext;
import java.util.List;
import java.util.stream.Collectors;

@Service
@Transactional
public class ExcelCharacterLevelServiceImpl extends CommonServiceImpl<ExcelCharacterLevelVo, ExcelCharacterLevel, Integer> implements ExcelCharacterLevelService {

    @PersistenceContext
    private EntityManager em;
    @Autowired
    private ExcelCharacterLevelRepository excelCharacterLevelRepository;

    /**
     * 根据当前等级返回对应的升级信息，如果超过了最大等级，返回当前等级的数据
     *
     * @param levelId
     * @return
     */
    @Override
    public ExcelCharacterLevel getNextLevelInfo(Integer levelId) {
        List<ExcelCharacterLevel> levelList = super.getListFromCache(GameEnum.REDIS_KEY_EXCEL_CHARACTER_LEVEL.getMessage());
        List<Integer> levelIdList = levelList.stream().map(ExcelCharacterLevel::getId).collect(Collectors.toList());
        if (levelId < levelList.get(levelList.size() - 1).getId()) {
            return levelList.get(levelIdList.indexOf(levelId + 1));
        } else {
            return levelList.get(levelIdList.indexOf(levelId));
        }
    }
}
