package com.zanxiang.game.module.manage.rpc.impl;

import com.baomidou.mybatisplus.core.toolkit.CollectionUtils;
import com.zanxiang.game.module.base.pojo.dto.GameDTO;
import com.zanxiang.game.module.base.rpc.IGameRpc;
import com.zanxiang.game.module.manage.service.IGameService;
import com.zanxiang.game.module.manage.service.IGameSupperService;
import com.zanxiang.game.module.mybatis.entity.Game;
import com.zanxiang.game.module.mybatis.entity.GameSupper;
import com.zanxiang.module.util.bean.BeanUtil;
import com.zanxiang.module.util.pojo.ResultVO;
import org.apache.dubbo.config.annotation.DubboService;
import org.springframework.beans.factory.annotation.Autowired;

import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
import java.util.Objects;
import java.util.stream.Collectors;

/**
 * @author : lingfeng
 * @time : 2024-05-23
 * @description : 游戏相关接口
 */
@DubboService
public class GameRpcImpl implements IGameRpc {

    @Autowired
    private IGameSupperService gameSupperService;

    @Autowired
    private IGameService gameService;

    @Override
    public ResultVO<GameDTO> getAllGameList() {
        //超父游戏列表
        List<GameSupper> gameSupperList = gameSupperService.list();
        //游戏列表
        List<Game> gameList = gameService.list();
        //不存在数据
        if (CollectionUtils.isEmpty(gameSupperList) || CollectionUtils.isEmpty(gameList)) {
            return ResultVO.ok(GameDTO.builder().gameSupperList(Collections.emptyList()).build());
        }
        List<GameDTO.GameSupperBean> gameSupperBeanList = new ArrayList<>();
        gameSupperList.forEach(gameSupper -> {
            List<GameDTO.GameBean> gameBeanList = gameList.stream()
                    .filter(game -> Objects.equals(game.getSuperGameId(), gameSupper.getId()))
                    .map(game -> BeanUtil.copy(game, GameDTO.GameBean.class))
                    .collect(Collectors.toList());
            GameDTO.GameSupperBean gameSupperBean = BeanUtil.copy(gameSupper, GameDTO.GameSupperBean.class);
            gameSupperBean.setGameList(gameBeanList);
            gameSupperBeanList.add(gameSupperBean);
        });
        return ResultVO.ok(GameDTO.builder().gameSupperList(gameSupperBeanList).build());
    }
}
