package com.sonic.frog.lib.client;

import com.alibaba.fastjson.TypeReference;
import com.sonic.common.AppRuntime;
import com.sonic.common.rpc.Result;
import com.sonic.common.rpc.RpcClient;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;

import java.util.HashMap;
import java.util.Map;

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

    private static final String GET_TOTAL_DEDUCTION_AMOUNT = "/api/user/get-total-deduction-amount";

    private RpcClient rpcClient;
    private String host;

    public UserDeductionClient(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-frog-svc:8080";
                break;
            case product:
            default:
                this.host = "http://prod-frog-svc:8080";
        }
    }


    /**
     * 获取用户总预扣金额
     *
     * @param userId
     * @return
     */
    public Long getTotalDeductionAmount(Long userId) {
        Map<String, Object> input = new HashMap<>();
        input.put("userId", userId);
        return rpcClient.post(host + GET_TOTAL_DEDUCTION_AMOUNT, input, new TypeReference<Result<Long>>() {
        });
    }

}
