package com.YuyeTech.HeartRate.game.gameInfo.service;

import com.YuyeTech.HeartRate.common.service.CommonServiceImpl;
import com.YuyeTech.HeartRate.game.gameInfo.projo.GameTag;
import com.YuyeTech.HeartRate.game.gameInfo.projo.GameTagReference;
import com.YuyeTech.HeartRate.dto.GameTagDTO;
import com.YuyeTech.HeartRate.dto.PageInfoDTO;
import com.YuyeTech.HeartRate.game.gameInfo.repository.GameTagReferenceRepository;
import com.YuyeTech.HeartRate.game.gameInfo.repository.GameTagRepository;
import com.YuyeTech.HeartRate.game.gameInfo.vo.GameTagReferenceVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Pageable;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.StringUtils;

import java.util.List;
import java.util.stream.Collectors;


/**
 * @author:slambb
 * @date:2020/6/24
 */
@Service
@Transactional
public class GameTagReferenceServiceImpl extends CommonServiceImpl<GameTagReferenceVo, GameTagReference, Integer> implements GameTagReferenceService {
    @Autowired
    private GameTagReferenceRepository gameTagReferenceRepository;

    @Autowired
    private GameTagRepository gameTagRepository;

    @Override
    public List<PageInfoDTO> findByGameId(String gameId, Pageable pageable) {
        return gameTagReferenceRepository.findByGameId(gameId,pageable).stream().map(e->new PageInfoDTO(e.getGameId(),"1","1","1")).collect(Collectors.toList());

    }

    @Override
    public List<GameTagDTO> findGameTagByGameId(String gameRankingId, String gameId) {
      String id = StringUtils.isEmpty(gameRankingId)?gameId:gameRankingId;
        //返回游戏对应的gameTag
      List<GameTagDTO> gameTagList =  gameTagReferenceRepository.findByGameId(id).stream().map(
              e->{
                  GameTag gt = gameTagRepository.findById(e.getTagId()).orElse(null);
                  return new GameTagDTO(gt.getId(),gt.getTitle(),gt.getMode(),gt.getIsDefault());
              }).collect(Collectors.toList());

        return gameTagList;
    }

    @Override
    public List<GameTagDTO> findGameTagByGameId(String gameId) {
        //返回游戏对应的gameTag
        List<GameTagDTO> gameTagList =  gameTagReferenceRepository.findByGameId(gameId).stream().map(e->
                {
                    GameTag gt = gameTagRepository.findById(e.getTagId()).orElse(null);
                    return new GameTagDTO(gt.getId(),gt.getTitle(),gt.getMode(),gt.getIsDefault());
                }).collect(Collectors.toList());

        return gameTagList;
    }

    @Override
    public void saveGameTagReference(List<GameTagReference> gameTagReferenceList) {
        gameTagReferenceRepository.saveAll(gameTagReferenceList);
    }

    @Override
    public void deleteGameTagByGameId(String gameId) {
        gameTagReferenceRepository.deleteAllByGameId(gameId);
    }
}
