package com.zanxiang.game.gs.data.serve.service.impl;


import com.mybatisflex.core.query.QueryWrapper;
import com.mybatisflex.spring.service.impl.ServiceImpl;
import com.zanxiang.game.gs.data.serve.dao.mapper.game.order.GameAuthRoleGroupMapper;
import com.zanxiang.game.gs.data.serve.pojo.entity.game.order.GameAuthGroup;
import com.zanxiang.game.gs.data.serve.pojo.entity.game.order.GameAuthRoleGroup;
import com.zanxiang.game.gs.data.serve.service.IGameAuthRoleGroupService;
import lombok.Data;
import org.springframework.stereotype.Service;

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

import static com.zanxiang.game.gs.data.serve.pojo.entity.game.order.table.GameAuthGroupTableDef.GAME_AUTH_GROUP;
import static com.zanxiang.game.gs.data.serve.pojo.entity.game.order.table.GameAuthRoleGroupTableDef.GAME_AUTH_ROLE_GROUP;

/**
 * 游戏授权角色分组 服务层实现。
 *
 * @author shaosong
 * @since 1.0
 */
@Service
public class GameAuthRoleGroupServiceImpl extends ServiceImpl<GameAuthRoleGroupMapper, GameAuthRoleGroup> implements IGameAuthRoleGroupService {

    @Override
    public Map<Long, List<String>> getGsGroupMap(String sourceSystem, Collection<Long> gsIdList) {
        QueryWrapper queryWrapper = QueryWrapper.create()
                .select(GAME_AUTH_GROUP.GROUP_NAME)
                .select(GAME_AUTH_ROLE_GROUP.USER_ID)
                .from(GAME_AUTH_ROLE_GROUP)
                .leftJoin(GAME_AUTH_GROUP)
                .on(GAME_AUTH_GROUP.ID.eq(GAME_AUTH_ROLE_GROUP.GROUP_ID))
                .eq(GameAuthRoleGroup::getSourceSystem, sourceSystem)
                .eq(GameAuthGroup::getSourceSystem, sourceSystem)
                .in(GameAuthRoleGroup::getUserId, gsIdList);
        List<GsGroup> gsGroups = getMapper().selectListByQueryAs(queryWrapper, GsGroup.class);
        return gsGroups.stream().collect(Collectors.groupingBy(GsGroup::getUserId, Collectors.mapping(GsGroup::getGroupName, Collectors.toList())));
    }

    @Data
    public static class GsGroup {
        private Long userId;
        private String groupName;
    }
}