package com.zanxiang.game.back.serve.rpc.impl;

import com.zanxiang.game.back.base.pojo.vo.GameBackPolicyRpcVO;
import com.zanxiang.game.back.base.rpc.IGameBackPolicyRpc;
import com.zanxiang.game.back.serve.pojo.entity.GameBackPolicy;
import com.zanxiang.game.back.serve.service.IGameBackPolicyService;
import com.zanxiang.module.util.bean.BeanUtil;
import com.zanxiang.module.util.pojo.ResultVO;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.collections4.CollectionUtils;
import org.apache.dubbo.config.annotation.DubboService;
import org.springframework.beans.factory.annotation.Autowired;

import java.util.Collections;
import java.util.List;
import java.util.Map;
import java.util.function.Function;
import java.util.stream.Collectors;

@Slf4j
@DubboService
public class GameBackPolicyRpcImpl implements IGameBackPolicyRpc {
    @Autowired
    private IGameBackPolicyService gameBackPolicyService;

    @Override
    public ResultVO<Map<Long, GameBackPolicyRpcVO>> listByIds(List<Long> backPolicyIds) {
        if (CollectionUtils.isEmpty(backPolicyIds)) {
            return ResultVO.ok(Collections.emptyMap());
        } else {
            List<GameBackPolicyRpcVO> policyList = gameBackPolicyService.listByIds(backPolicyIds)
                    .stream().map(this::toVOSimple).collect(Collectors.toList());
            return ResultVO.ok(policyList.stream().collect(Collectors.toMap(GameBackPolicyRpcVO::getId, Function.identity())));
        }
    }

    private GameBackPolicyRpcVO toVOSimple(GameBackPolicy gameBackPolicy) {
        if (gameBackPolicy == null) {
            return null;
        }
        return BeanUtil.copy(gameBackPolicy, GameBackPolicyRpcVO.class);
    }
}
