package com.sonic.lion.lib.client;

import com.alibaba.fastjson.TypeReference;
import com.google.common.collect.Lists;
import com.sonic.common.AppRuntime;
import com.sonic.common.rpc.Result;
import com.sonic.common.rpc.RpcClient;
import com.sonic.lion.lib.input.GetSubscribeInput;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * @author code
 */
@Slf4j
@Service
public class SubscribeClient {

    private static final String SUBSCRIBE_QUERY = "/api/pay/subscribe/query";

    private RpcClient rpcClient;
    private String host;

    public SubscribeClient(RpcClient rpcClient, AppRuntime appRuntime) {
        this.rpcClient = rpcClient;
        switch (appRuntime.getEnv()) {
            case dev:
                this.host = "http://localhost:8080";
                break;
            case local:
                this.host = "http://localhost:8080";
                break;
            case test:
                this.host = "http://test-lion-svc:8080";
                break;
            case product:
            default:
                this.host = "http://prod-lion-svc:8080";
        }
    }

    /**
     * 获取用户是否是会员
     * @param userId
     * @return
     */
    public Boolean queryUserIsSubscribe(Long userId) {
        List<Long> userIdList = queryUserIsSubscribe(Lists.newArrayList(userId));
        return userIdList.contains(userId);
    }

    /**
     * 批量获取用户是否是会员
     * @param userIdList
     * @return
     */
    public List<Long> queryUserIsSubscribe(List<Long> userIdList) {
        GetSubscribeInput input = new GetSubscribeInput();
        input.setUserIdList(userIdList);
        return rpcClient.post(host + SUBSCRIBE_QUERY, input, new TypeReference<Result<List<Long>>>(){});
    }

}
