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

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.zanxiang.erp.base.ErpServer;
import com.zanxiang.erp.base.pojo.vo.SysGameUserGroupRpcVO;
import com.zanxiang.erp.base.rpc.ISysGameUserGroupRpc;
import com.zanxiang.erp.security.util.SecurityUtil;
import com.zanxiang.game.module.base.pojo.enums.GameAuthEnum;
import com.zanxiang.game.module.base.pojo.vo.GameAuthUserVO;
import com.zanxiang.game.module.base.rpc.GameAuthRpc;
import com.zanxiang.game.module.manage.service.IGameAuthRoleService;
import com.zanxiang.game.module.manage.service.IGameAuthService;
import com.zanxiang.game.module.mybatis.entity.GameAuth;
import com.zanxiang.game.module.mybatis.entity.GameAuthRole;
import com.zanxiang.module.util.bean.BeanUtil;
import com.zanxiang.module.util.exception.BaseException;
import com.zanxiang.module.util.pojo.ResultVO;
import org.apache.dubbo.config.annotation.DubboReference;
import org.apache.dubbo.config.annotation.DubboService;
import org.springframework.beans.factory.annotation.Autowired;
import reactor.util.function.Tuple2;

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

/**
 * @author : lingfeng
 * @time : 2023-08-17
 * @description : 游戏授权
 */
@DubboService
public class GameAuthRpcImpl implements GameAuthRpc {

    @Autowired
    private IGameAuthService gameAuthService;

    @Autowired
    private IGameAuthRoleService gameAuthRoleService;

    @DubboReference(providedBy = ErpServer.SERVER_DUBBO_NAME)
    private ISysGameUserGroupRpc sysGameUserGroupRpc;

    @Override
    public ResultVO<GameAuthUserVO> getGameAuthByUserIds() {
        //游戏列表
        Tuple2<String, List<Long>> tuple2 = gameAuthService.getUserGameList(null);
        //角色
        GameAuthEnum gameAuthEnum = GameAuthEnum.getByValue(tuple2.getT1());
        if (gameAuthEnum == null) {
            throw new BaseException("参数错误, 未查询到相应的角色类型");
        }
        //投手列表
        List<Long> pitcherList = new ArrayList<>();
        //超管或者管理员, 返回所有角色人
        if (SecurityUtil.isAdmin() || Objects.equals(gameAuthEnum.getValue(), GameAuthEnum.MANAGE.getValue())) {
            pitcherList = gameAuthRoleService.list(new LambdaQueryWrapper<GameAuthRole>()
                    .eq(GameAuthRole::getAuthType, GameAuthEnum.PITCHER.getValue())
            ).stream().map(GameAuthRole::getUserId).collect(Collectors.toList());
        }
        //所有投手
        List<Long> pitcherIdList = gameAuthRoleService.list(new LambdaQueryWrapper<GameAuthRole>()
                .eq(GameAuthRole::getAuthType, GameAuthEnum.PITCHER.getValue())
        ).stream().map(GameAuthRole::getUserId).collect(Collectors.toList());
        //运营权限
        if (pitcherList.isEmpty() && Objects.equals(gameAuthEnum.getValue(), GameAuthEnum.OPERATE.getValue())) {
            pitcherList = gameAuthService.list(new LambdaQueryWrapper<GameAuth>()
                    .in(GameAuth::getGameId, tuple2.getT2())
                    .in(GameAuth::getUserId, pitcherIdList)
            ).stream().map(GameAuth::getUserId).collect(Collectors.toList());
        }
        //判断是否是组长, 返回组员
        SysGameUserGroupRpcVO sysGameUserGroupRpcVO = sysGameUserGroupRpc.getByGroupUser(SecurityUtil.getCompanyId(),
                SecurityUtil.getUserId()).getData();
        if (pitcherList.isEmpty() && sysGameUserGroupRpcVO != null) {
            Collection<Long> memberUserIdList = sysGameUserGroupRpc.memberUserIds(SecurityUtil.getCompanyId(),
                    SecurityUtil.getUserId()).getData();
            List<Long> userIdList = memberUserIdList.stream().filter(pitcherIdList::contains).collect(Collectors.toList());
            pitcherList.addAll(userIdList);
        }
        //投手必定返回自己
        if (Objects.equals(gameAuthEnum.getValue(), GameAuthEnum.PITCHER.getValue())
                && !pitcherList.contains(SecurityUtil.getUserId())) {
            pitcherList.add(SecurityUtil.getUserId());
        }
        //游戏授权列表
        List<GameAuthUserVO.GameAuthBean> gameAuthBeanList = gameAuthService.list(new LambdaQueryWrapper<GameAuth>()
                .eq(GameAuth::getUserId, SecurityUtil.getUserId()).in(GameAuth::getGameId, tuple2.getT2())
        ).stream().map(gameAuth -> BeanUtil.copy(gameAuth, GameAuthUserVO.GameAuthBean.class)).collect(Collectors.toList());
        //构造返回
        return ResultVO.ok(GameAuthUserVO.builder()
                .userId(SecurityUtil.getUserId())
                .gameIdList(tuple2.getT2())
                .gameAuthEnum(gameAuthEnum)
                .gameAuth(gameAuthEnum.getValue())
                .pitcherList(pitcherList)
                .gameAuthBeanList(gameAuthBeanList)
                .build());
    }
}
