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

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.zanxiang.game.module.manage.service.IGameGiftPackConfigService;
import com.zanxiang.game.module.manage.service.IGameService;
import com.zanxiang.game.module.mybatis.entity.Game;
import com.zanxiang.game.module.mybatis.entity.GameGiftPackConfig;
import com.zanxiang.game.module.mybatis.mapper.GameGiftPackConfigMapper;
import com.zanxiang.module.util.JsonUtil;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.collections4.CollectionUtils;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import reactor.util.function.Tuple2;
import reactor.util.function.Tuples;

import java.util.*;
import java.util.stream.Collectors;

/**
 * @author : lingfeng
 * @time : 2022-06-22
 * @description : 游戏礼包码配置
 */
@Slf4j
@Service
public class GameGiftPackConfigServiceImpl extends ServiceImpl<GameGiftPackConfigMapper, GameGiftPackConfig> implements IGameGiftPackConfigService {

    @Autowired
    private IGameService gameService;

    @Override
    public List<Tuple2<Map<Long, String>, Map<String, String>>> getGameGiftPackConfig() {
        List<GameGiftPackConfig> gameGiftPackConfigList = super.list();
        if (CollectionUtils.isEmpty(gameGiftPackConfigList)) {
            return Collections.emptyList();
        }
        Map<Long, String> gameMap = gameService.list().stream().collect(Collectors.toMap(Game::getId, Game::getName));
        List<Tuple2<Map<Long, String>, Map<String, String>>> list = new ArrayList<>();
        gameGiftPackConfigList.forEach(config -> {
            String gameIds = config.getGameIds();
            String codeTypeConfig = config.getCodeTypeConfig();
            if (StringUtils.isAnyEmpty(gameIds, codeTypeConfig)) {
                return;
            }
            //类型配置
            Map<String, String> codeTypeMap = JsonUtil.toMap(codeTypeConfig, Map.class, String.class);
            //添加返回
            Arrays.stream(gameIds.split(",")).distinct()
                    .map(Long::valueOf)
                    .forEach(gameId -> list.add(Tuples.of(Collections.singletonMap(gameId, gameMap.get(gameId)), codeTypeMap)));
        });
        return list;
    }
}




