package com.usoft.fin.grpc.service.impl;

import com.usoft.fin.grpc.api.entity.ResponseHeader;
import com.usoft.fin.grpc.api.protobuf.*;
import com.usoft.fin.grpc.bl.RepayBl;
import com.usoft.fin.grpc.utils.FinanceGrpcRuntimeException;
import com.usoft.fin.grpc.utils.ResponseUtil;
import com.usoft.protobuf.utils.ProtoBufUtil;
import io.grpc.stub.StreamObserver;
import org.lognet.springboot.grpc.GRpcService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;

/**
 * 还款接口
 * @author wangmh
 * @date 2018-08-23 15:02
 **/
@GRpcService
public class RepayGrpcServiceImpl extends IRepayGrpcServiceGrpc.IRepayGrpcServiceImplBase {

    private static final Logger LOGGER = LoggerFactory.getLogger(RepayGrpcServiceImpl.class);

    @Autowired
    private RepayBl repayBl;

    /**
     * 用户还款
     * @param request
     * @param responseObserver
     */
    @Override
    public void repayDrawInfo(RepayDrawInfoRequest request, StreamObserver<RepayDrawInfoResponse> responseObserver) {
        RepayDrawInfoResponse.Builder builder = RepayDrawInfoResponse.newBuilder();
        builder.setResponseHeader(ResponseHeader.newBuilder());
        try {
            builder = repayBl.repayDrawInfo(request);
            LOGGER.info("用户还款[IRepayGrpcServiceImpl.repayDrawInfo]正常，参数：{}", ProtoBufUtil.toJSONHasTryCatch(request));
        } catch (FinanceGrpcRuntimeException e) {
            LOGGER.warn("用户还款[IRepayGrpcServiceImpl.repayDrawInfo]告警，参数：{}", ProtoBufUtil.toJSONHasTryCatch(request), e);
            ResponseHeader responseHeader = ResponseUtil.getResponseHeader(e.getErrorCode(), e.getErrorMessage());
            builder.setResponseHeader(responseHeader);
        } catch (Exception e) {
            LOGGER.warn("用户还款[IRepayGrpcServiceImpl.repayDrawInfo]告警，参数：{}", ProtoBufUtil.toJSONHasTryCatch(request), e);
            ResponseHeader responseHeader = ResponseUtil.getResponseHeader(FinanceGrpcRuntimeException.ERROR_CODE_100, e.getMessage());
            builder.setResponseHeader(responseHeader);
        }
        responseObserver.onNext(builder.build());
        responseObserver.onCompleted();
    }

    /**
     * 获取还款计划列表
     * @param request
     * @param responseObserver
     */
    @Override
    public void getRepayPlanInfo(GetRepayPlanInfoRequest request, StreamObserver<GetRepayPlanInfoResponse> responseObserver) {
        GetRepayPlanInfoResponse.Builder builder = GetRepayPlanInfoResponse.newBuilder();
        builder.setResponseHeader(ResponseHeader.newBuilder());
        try {
            builder = repayBl.getRepayPlanInfo(request);
            LOGGER.info("获取还款计划列表[IRepayGrpcServiceImpl.getRepayPlans]正常，参数：{}", ProtoBufUtil.toJSONHasTryCatch(request));
        } catch (FinanceGrpcRuntimeException e) {
            LOGGER.warn("获取还款计划列表[IRepayGrpcServiceImpl.getRepayPlans]告警，参数：{}", ProtoBufUtil.toJSONHasTryCatch(request), e);
            ResponseHeader responseHeader = ResponseUtil.getResponseHeader(e.getErrorCode(), e.getErrorMessage());
            builder.setResponseHeader(responseHeader);
        } catch (Exception e) {
            LOGGER.warn("获取还款计划列表[IRepayGrpcServiceImpl.getRepayPlans]告警，参数：{}", ProtoBufUtil.toJSONHasTryCatch(request), e);
            ResponseHeader responseHeader = ResponseUtil.getResponseHeader(FinanceGrpcRuntimeException.ERROR_CODE_100, e.getMessage());
            builder.setResponseHeader(responseHeader);
        }
        responseObserver.onNext(builder.build());
        responseObserver.onCompleted();
    }

    /**
     * 获取我的还款
     * @param request
     * @param responseObserver
     */
    @Override
    public void getMyRepayPagingInfo(GetMyRepayPagingInfoRequest request, StreamObserver<GetMyRepayPagingInfoResponse> responseObserver) {
        GetMyRepayPagingInfoResponse.Builder builder = GetMyRepayPagingInfoResponse.newBuilder();
        try {
            builder = repayBl.getMyRepayPagingInfo(request);
            builder.setResponseHeader(ResponseHeader.newBuilder());
            LOGGER.info("获取我的还款[IRepayGrpcServiceImpl.getMyRepayPagingInfo]正常，参数：{}", ProtoBufUtil.toJSONHasTryCatch(request));
        } catch (FinanceGrpcRuntimeException e) {
            LOGGER.warn("获取我的还款[IRepayGrpcServiceImpl.getMyRepayPagingInfo]告警，参数：{}", ProtoBufUtil.toJSONHasTryCatch(request), e);
            ResponseHeader responseHeader = ResponseUtil.getResponseHeader(e.getErrorCode(), e.getErrorMessage());
            builder.setResponseHeader(responseHeader);
        } catch (Exception e) {
            LOGGER.warn("获取我的还款[IRepayGrpcServiceImpl.getMyRepayPagingInfo]告警，参数：{}", ProtoBufUtil.toJSONHasTryCatch(request), e);
            ResponseHeader responseHeader = ResponseUtil.getResponseHeader(FinanceGrpcRuntimeException.ERROR_CODE_100, e.getMessage());
            builder.setResponseHeader(responseHeader);
        }
        responseObserver.onNext(builder.build());
        responseObserver.onCompleted();
    }

    /**
     * 获取还款明细
     * @param request
     * @param responseObserver
     */
    @Override
    public void getRepayDetail(GetRepayDetailRequest request, StreamObserver<GetRepayDetailResponse> responseObserver) {
        GetRepayDetailResponse.Builder builder = GetRepayDetailResponse.newBuilder();
        try {
            builder = repayBl.getRepayDetail(request);
            builder.setResponseHeader(ResponseHeader.newBuilder());
            LOGGER.info("获取还款明细[IRepayGrpcServiceImpl.getRepayDetail]正常，参数：{}", ProtoBufUtil.toJSONHasTryCatch(request));
        } catch (FinanceGrpcRuntimeException e) {
            LOGGER.warn("获取还款明细[IRepayGrpcServiceImpl.getRepayDetail]告警，参数：{}", ProtoBufUtil.toJSONHasTryCatch(request), e);
            ResponseHeader responseHeader = ResponseUtil.getResponseHeader(e.getErrorCode(), e.getErrorMessage());
            builder.setResponseHeader(responseHeader);
        } catch (Exception e) {
            LOGGER.warn("获取还款明细[IRepayGrpcServiceImpl.getRepayDetail]告警，参数：{}", ProtoBufUtil.toJSONHasTryCatch(request), e);
            ResponseHeader responseHeader = ResponseUtil.getResponseHeader(FinanceGrpcRuntimeException.ERROR_CODE_100, e.getMessage());
            builder.setResponseHeader(responseHeader);
        }
        responseObserver.onNext(builder.build());
        responseObserver.onCompleted();
    }
}
