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.CreditBl;
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 javax.annotation.Resource;

/**
 * 额度接口
 * @author wangdy
 * @date 2018-08-28 11:13
 **/
@GRpcService
public class CreditGrpcServiceImpl extends ICreditGrpcServiceGrpc.ICreditGrpcServiceImplBase {
    private static final Logger LOGGER = LoggerFactory.getLogger(CreditGrpcServiceImpl.class);

    @Resource
    private CreditBl creditBl;

    /**
     * 获取额度申请记录信息
     * @param request
     * @param responseObserver
     */
    @Override
    public void getCreditApplyInfo(GetCreditApplyInfoRequest request, StreamObserver<GetCreditApplyInfoResponse> responseObserver) {
        GetCreditApplyInfoResponse.Builder builder = GetCreditApplyInfoResponse.newBuilder();
        try {
            builder = creditBl.getCreditApplyInfo(request);
            LOGGER.info("获取额度申请记录信息[ICreditGrpcServiceImpl.getCreditApplyInfo]正常，参数：{}", ProtoBufUtil.toJSONHasTryCatch(request));
        } catch (FinanceGrpcRuntimeException e) {
            LOGGER.warn("获取额度申请记录信息[ICreditGrpcServiceImpl.getCreditApplyInfo]告警，参数：{}", ProtoBufUtil.toJSONHasTryCatch(request), e);
            ResponseHeader responseHeader = ResponseUtil.getResponseHeader(e.getErrorCode(), e.getErrorMessage());
            builder.setResponseHeader(responseHeader);
        } catch (Exception e) {
            LOGGER.error("获取额度申请记录信息[ICreditGrpcServiceImpl.getCreditApplyInfo]异常，参数：{}", 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 auditCreditApplyInfo(AuditCreditApplyInfoRequest request, StreamObserver<AuditCreditApplyInfoResponse> responseObserver) {
        AuditCreditApplyInfoResponse.Builder builder = AuditCreditApplyInfoResponse.newBuilder();
        try {
            builder = creditBl.auditCreditApplyInfo(request);
            LOGGER.info("审核额度申请[ICreditGrpcServiceImpl.auditCreditApplyInfo]正常，参数：{}", ProtoBufUtil.toJSONHasTryCatch(request));
        } catch (FinanceGrpcRuntimeException e) {
            LOGGER.warn("审核额度申请[ICreditGrpcServiceImpl.auditCreditApplyInfo]告警，参数：{}", ProtoBufUtil.toJSONHasTryCatch(request), e);
            ResponseHeader responseHeader = ResponseUtil.getResponseHeader(e.getErrorCode(), e.getErrorMessage());
            builder.setResponseHeader(responseHeader);
        } catch (Exception e) {
            LOGGER.error("审核额度申请[ICreditGrpcServiceImpl.auditCreditApplyInfo]异常，参数：{}", 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 auditAddCreditApplyInfo(AuditAddCreditApplyInfoRequest request, StreamObserver<AuditAddCreditApplyInfoResponse> responseObserver) {
        AuditAddCreditApplyInfoResponse.Builder builder = AuditAddCreditApplyInfoResponse.newBuilder();
        try {
            builder = creditBl.auditAddCreditApplyInfo(request);
            LOGGER.info("审核增额申请[ICreditGrpcServiceImpl.auditAddCreditApplyInfo]正常，参数：{}", ProtoBufUtil.toJSONHasTryCatch(request));
        } catch (FinanceGrpcRuntimeException e) {
            LOGGER.warn("审核增额申请[ICreditGrpcServiceImpl.auditAddCreditApplyInfo]告警，参数：{}", ProtoBufUtil.toJSONHasTryCatch(request), e);
            ResponseHeader responseHeader = ResponseUtil.getResponseHeader(e.getErrorCode(), e.getErrorMessage());
            builder.setResponseHeader(responseHeader);
        } catch (Exception e) {
            LOGGER.error("审核增额申请[ICreditGrpcServiceImpl.auditAddCreditApplyInfo]异常，参数：{}", 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 checkCreditApplyInfo(CheckCreditApplyInfoRequest request, StreamObserver<CheckCreditApplyInfoResponse> responseObserver) {
        CheckCreditApplyInfoResponse.Builder builder = CheckCreditApplyInfoResponse.newBuilder();
        try {
            builder = creditBl.checkCreditApplyInfo(request);
            LOGGER.info("验证额度申请[ICreditGrpcServiceImpl.checkCreditApplyInfo]正常，参数：{}", ProtoBufUtil.toJSONHasTryCatch(request));
        } catch (FinanceGrpcRuntimeException e) {
            LOGGER.warn("验证额度申请[ICreditGrpcServiceImpl.checkCreditApplyInfo]告警，参数：{}", ProtoBufUtil.toJSONHasTryCatch(request), e);
            ResponseHeader responseHeader = ResponseUtil.getResponseHeader(e.getErrorCode(), e.getErrorMessage());
            builder.setResponseHeader(responseHeader);
        } catch (Exception e) {
            LOGGER.error("验证额度申请[ICreditGrpcServiceImpl.checkCreditApplyInfo]异常，参数：{}", 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 saveCreditApplyUserinfo(SaveCreditApplyUserinfoRequest request, StreamObserver<SaveCreditApplyUserinfoResponse> responseObserver) {
        SaveCreditApplyUserinfoResponse.Builder builder = SaveCreditApplyUserinfoResponse.newBuilder();
        try {
            builder = creditBl.saveCreditApplyUserinfo(request);
            LOGGER.info("保存用户资料[ICreditGrpcServiceImpl.saveCreditApplyUserinfo]正常，参数：{}", ProtoBufUtil.toJSONHasTryCatch(request));
        } catch (FinanceGrpcRuntimeException e) {
            LOGGER.warn("保存用户资料[ICreditGrpcServiceImpl.saveCreditApplyUserinfo]告警，参数：{}", ProtoBufUtil.toJSONHasTryCatch(request), e);
            ResponseHeader responseHeader = ResponseUtil.getResponseHeader(e.getErrorCode(), e.getErrorMessage());
            builder.setResponseHeader(responseHeader);
        } catch (Exception e) {
            LOGGER.error("保存用户资料[ICreditGrpcServiceImpl.saveCreditApplyUserinfo]异常，参数：{}", 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 getCreditApplyUserFiles(GetCreditApplyUserFilesRequest request, StreamObserver<GetCreditApplyUserFilesResponse> responseObserver) {
        GetCreditApplyUserFilesResponse.Builder builder = GetCreditApplyUserFilesResponse.newBuilder();
        try {
            builder = creditBl.getCreditApplyUserFiles(request);
            LOGGER.info("获取额度申请上传资料列表[ICreditGrpcServiceImpl.getCreditApplyUserFiles]正常，参数：{}", ProtoBufUtil.toJSONHasTryCatch(request));
        } catch (FinanceGrpcRuntimeException e) {
            LOGGER.warn("获取额度申请上传资料列表[ICreditGrpcServiceImpl.getCreditApplyUserFiles]告警，参数：{}", ProtoBufUtil.toJSONHasTryCatch(request), e);
            ResponseHeader responseHeader = ResponseUtil.getResponseHeader(e.getErrorCode(), e.getErrorMessage());
            builder.setResponseHeader(responseHeader);
        } catch (Exception e) {
            LOGGER.error("获取额度申请上传资料列表[ICreditGrpcServiceImpl.getCreditApplyUserFiles]异常，参数：{}", 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 saveCreditApplyUserFiles(SaveCreditApplyUserFilesRequest request, StreamObserver<SaveCreditApplyUserFilesResponse> responseObserver) {
        SaveCreditApplyUserFilesResponse.Builder builder = SaveCreditApplyUserFilesResponse.newBuilder();
        try {
            builder = creditBl.saveCreditApplyUserFiles(request);
            LOGGER.info("保存额度申请[ICreditGrpcServiceImpl.saveCreditApplyUserFiles]正常，参数：{}", ProtoBufUtil.toJSONHasTryCatch(request));
        } catch (FinanceGrpcRuntimeException e) {
            LOGGER.warn("保存额度申请[ICreditGrpcServiceImpl.saveCreditApplyUserFiles]告警，参数：{}", ProtoBufUtil.toJSONHasTryCatch(request), e);
            ResponseHeader responseHeader = ResponseUtil.getResponseHeader(e.getErrorCode(), e.getErrorMessage());
            builder.setResponseHeader(responseHeader);
        } catch (Exception e) {
            LOGGER.error("保存额度申请[ICreditGrpcServiceImpl.saveCreditApplyUserFiles]异常，参数：{}", 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 submitCreditApply(SubmitCreditApplyRequest request, StreamObserver<SubmitCreditApplyResponse> responseObserver) {
        SubmitCreditApplyResponse.Builder builder = SubmitCreditApplyResponse.newBuilder();
        try {
            builder = creditBl.submitCreditApply(request);
            LOGGER.info("提交额度申请[ICreditGrpcServiceImpl.submitCreditApply]正常，参数：{}", ProtoBufUtil.toJSONHasTryCatch(request));
        } catch (FinanceGrpcRuntimeException e) {
            LOGGER.warn("提交额度申请[ICreditGrpcServiceImpl.submitCreditApply]告警，参数：{}", ProtoBufUtil.toJSONHasTryCatch(request), e);
            ResponseHeader responseHeader = ResponseUtil.getResponseHeader(e.getErrorCode(), e.getErrorMessage());
            builder.setResponseHeader(responseHeader);
        } catch (Exception e) {
            LOGGER.error("提交额度申请[ICreditGrpcServiceImpl.submitCreditApply]异常，参数：{}", 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 checkAddCreditApplyInfo(CheckAddCreditApplyInfoRequest request, StreamObserver<CheckAddCreditApplyInfoResponse> responseObserver) {
        CheckAddCreditApplyInfoResponse.Builder builder = CheckAddCreditApplyInfoResponse.newBuilder();
        try {
            builder = creditBl.checkAddCreditApplyInfo(request);
            LOGGER.info("验证增额申请[ICreditGrpcServiceImpl.checkAddCreditApplyInfo]正常，参数：{}", ProtoBufUtil.toJSONHasTryCatch(request));
        } catch (FinanceGrpcRuntimeException e) {
            LOGGER.warn("验证增额申请[ICreditGrpcServiceImpl.checkAddCreditApplyInfo]告警，参数：{}", ProtoBufUtil.toJSONHasTryCatch(request), e);
            ResponseHeader responseHeader = ResponseUtil.getResponseHeader(e.getErrorCode(), e.getErrorMessage());
            builder.setResponseHeader(responseHeader);
        } catch (Exception e) {
            LOGGER.error("验证增额申请[ICreditGrpcServiceImpl.checkAddCreditApplyInfo]异常，参数：{}", 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 saveAddCreditApplyUserFiles(SaveAddCreditApplyUserFilesRequest request, StreamObserver<SaveAddCreditApplyUserFilesResponse> responseObserver) {
        SaveAddCreditApplyUserFilesResponse.Builder builder = SaveAddCreditApplyUserFilesResponse.newBuilder();
        try {
            builder = creditBl.saveAddCreditApplyUserFiles(request);
            LOGGER.info("保存增额申请[ICreditGrpcServiceImpl.saveAddCreditApplyUserFiles]正常，参数：{}", ProtoBufUtil.toJSONHasTryCatch(request));
        } catch (FinanceGrpcRuntimeException e) {
            LOGGER.warn("保存增额申请[ICreditGrpcServiceImpl.saveAddCreditApplyUserFiles]告警，参数：{}", ProtoBufUtil.toJSONHasTryCatch(request), e);
            ResponseHeader responseHeader = ResponseUtil.getResponseHeader(e.getErrorCode(), e.getErrorMessage());
            builder.setResponseHeader(responseHeader);
        } catch (Exception e) {
            LOGGER.error("保存增额申请[ICreditGrpcServiceImpl.saveAddCreditApplyUserFiles]异常，参数：{}", 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 submitAddCreditApplyUserFiles(SubmitAddCreditApplyUserFilesRequest request, StreamObserver<SubmitAddCreditApplyUserFilesResponse> responseObserver) {
        SubmitAddCreditApplyUserFilesResponse.Builder builder = SubmitAddCreditApplyUserFilesResponse.newBuilder();
        try {
            builder = creditBl.submitAddCreditApply(request);
            LOGGER.info("提交增额申请[ICreditGrpcServiceImpl.submitAddCreditApplyUserFiles]正常，参数：{}", ProtoBufUtil.toJSONHasTryCatch(request));
        } catch (FinanceGrpcRuntimeException e) {
            LOGGER.warn("提交增额申请[ICreditGrpcServiceImpl.submitAddCreditApplyUserFiles]告警，参数：{}", ProtoBufUtil.toJSONHasTryCatch(request), e);
            ResponseHeader responseHeader = ResponseUtil.getResponseHeader(e.getErrorCode(), e.getErrorMessage());
            builder.setResponseHeader(responseHeader);
        } catch (Exception e) {
            LOGGER.error("提交增额申请[ICreditGrpcServiceImpl.submitAddCreditApplyUserFiles]异常，参数：{}", 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 getMyCreditInfos(GetMyCreditInfosRequest request, StreamObserver<GetMyCreditInfosResponse> responseObserver) {
        GetMyCreditInfosResponse.Builder builder = GetMyCreditInfosResponse.newBuilder();
        try {
            builder = creditBl.getMyCreditInfos(request);
            LOGGER.info("获取我的额度列表[ICreditGrpcServiceImpl.getMyCreditInfos]正常，参数：{}", ProtoBufUtil.toJSONHasTryCatch(request));
        } catch (FinanceGrpcRuntimeException e) {
            LOGGER.warn("获取我的额度列表[ICreditGrpcServiceImpl.getMyCreditInfos]告警，参数：{}", ProtoBufUtil.toJSONHasTryCatch(request), e);
            ResponseHeader responseHeader = ResponseUtil.getResponseHeader(e.getErrorCode(), e.getErrorMessage());
            builder.setResponseHeader(responseHeader);
        } catch (Exception e) {
            LOGGER.error("获取我的额度列表[ICreditGrpcServiceImpl.getMyCreditInfos]异常，参数：{}", 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 getMyUsableCreditInfos(GetMyUsableCreditInfosRequest request, StreamObserver<GetMyUsableCreditInfosResponse> responseObserver) {
        GetMyUsableCreditInfosResponse.Builder builder = GetMyUsableCreditInfosResponse.newBuilder();
        try {
            builder = creditBl.getMyUsableCreditInfos(request);
            LOGGER.info("获取我的剩余可用额度[ICreditGrpcServiceImpl.getMyUsableCreditInfos]正常，参数：{}", ProtoBufUtil.toJSONHasTryCatch(request));
        } catch (FinanceGrpcRuntimeException e) {
            LOGGER.warn("获取我的剩余可用额度[ICreditGrpcServiceImpl.getMyUsableCreditInfos]告警，参数：{}", ProtoBufUtil.toJSONHasTryCatch(request), e);
            ResponseHeader responseHeader = ResponseUtil.getResponseHeader(e.getErrorCode(), e.getErrorMessage());
            builder.setResponseHeader(responseHeader);
        } catch (Exception e) {
            LOGGER.error("获取我的剩余可用额度[ICreditGrpcServiceImpl.getMyUsableCreditInfos]异常，参数：{}", 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 getCreditApplyPagingInfo(GetCreditApplyPagingInfoRequest request, StreamObserver<GetCreditApplyPagingInfoResponse> responseObserver) {
        GetCreditApplyPagingInfoResponse.Builder builder = GetCreditApplyPagingInfoResponse.newBuilder();
        try {
            builder = creditBl.getCreditApplyPagingInfo(request);
            LOGGER.info("获取额度申请列表[ICreditGrpcServiceImpl.getCreditApplyPagingInfo]正常，参数：{}", ProtoBufUtil.toJSONHasTryCatch(request));
        } catch (FinanceGrpcRuntimeException e) {
            LOGGER.warn("获取额度申请列表[ICreditGrpcServiceImpl.getCreditApplyPagingInfo]告警，参数：{}", ProtoBufUtil.toJSONHasTryCatch(request), e);
            ResponseHeader responseHeader = ResponseUtil.getResponseHeader(e.getErrorCode(), e.getErrorMessage());
            builder.setResponseHeader(responseHeader);
        } catch (Exception e) {
            LOGGER.error("获取额度申请列表[ICreditGrpcServiceImpl.getCreditApplyPagingInfo]异常，参数：{}", 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 checkWhiteList(CheckWhiteListRequest request, StreamObserver<CheckWhiteListResponse> responseObserver) {
        CheckWhiteListResponse.Builder builder = CheckWhiteListResponse.newBuilder();
        try {
            builder = creditBl.checkWhiteList(request);
            LOGGER.info("验证用户白名单[ICreditGrpcServiceImpl.checkWhiteList]正常，参数：{}", ProtoBufUtil.toJSONHasTryCatch(request));
        } catch (FinanceGrpcRuntimeException e) {
            LOGGER.warn("验证用户白名单[ICreditGrpcServiceImpl.checkWhiteList]告警，参数：{}", ProtoBufUtil.toJSONHasTryCatch(request), e);
            ResponseHeader responseHeader = ResponseUtil.getResponseHeader(e.getErrorCode(), e.getErrorMessage());
            builder.setResponseHeader(responseHeader);
        } catch (Exception e) {
            LOGGER.error("验证用户白名单[ICreditGrpcServiceImpl.checkWhiteList]异常，参数：{}", ProtoBufUtil.toJSONHasTryCatch(request), e);
            ResponseHeader responseHeader = ResponseUtil.getResponseHeader(FinanceGrpcRuntimeException.ERROR_CODE_100, e.getMessage());
            builder.setResponseHeader(responseHeader);
        }
        responseObserver.onNext(builder.build());
        responseObserver.onCompleted();
    }
}
