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

import com.YuyeTech.TPlat.game.gameInfo.projo.GameTag;
import com.YuyeTech.TPlat.game.gameInfo.projo.GameTagReference;
import com.YuyeTech.TPlat.dto.GameTagDTO;
import com.YuyeTech.TPlat.dto.PageInfoDTO;
import com.YuyeTech.TPlat.game.gameInfo.repository.GameTagReferenceRepository;
import com.YuyeTech.TPlat.game.gameInfo.repository.GameTagRepository;
import com.YuyeTech.TPlat.game.gameInfo.service.GameTagReferenceService;
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
public class GameTagReferenceServiceImpl 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
    @Transactional
    public void saveGameTagReference(List<GameTagReference> gameTagReferenceList) {
        gameTagReferenceRepository.saveAll(gameTagReferenceList);
    }

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