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.DrawBl;
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 liusw
 * @date 2018-08-28 11:13
 **/
@GRpcService
public class DrawServiceImpl extends IDrawGrpcServiceGrpc.IDrawGrpcServiceImplBase {

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

    @Autowired
    private DrawBl drawBl;

    /**
     * 获取提款申请信息
     * @param request
     * @param responseObserver
     */
    @Override
    public void getDrawInfo(GetDrawInfoRequest request, StreamObserver<GetDrawInfoResponse> responseObserver) {
        GetDrawInfoResponse.Builder builder = GetDrawInfoResponse.newBuilder();
        try {
            builder = drawBl.getDrawInfo(request);
            LOGGER.info("获取提款申请信息[IDrawServiceImpl.getDrawInfo]正常，参数：{}", ProtoBufUtil.toJSONHasTryCatch(request));
        } catch (FinanceGrpcRuntimeException e) {
            LOGGER.warn("获取提款申请信息[IDrawServiceImpl.getDrawInfo]告警，参数：{}", ProtoBufUtil.toJSONHasTryCatch(request), e);
            ResponseHeader responseHeader = ResponseUtil.getResponseHeader(e.getErrorCode(), e.getErrorMessage());
            builder.setResponseHeader(responseHeader);
        } catch (Exception e) {
            LOGGER.error("获取提款申请信息[IDrawServiceImpl.getDrawInfo]异常，参数：{}", 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 auditDrawInfo(AuditDrawInfoRequest request, StreamObserver<AuditDrawInfoResponse> responseObserver) {
        AuditDrawInfoResponse.Builder builder = AuditDrawInfoResponse.newBuilder();
        try {
            builder = drawBl.auditDrawInfo(request);
            LOGGER.info("审核提款申请[IDrawServiceImpl.auditDrawInfo]正常，参数：{}", ProtoBufUtil.toJSONHasTryCatch(request));
        } catch (FinanceGrpcRuntimeException e) {
            LOGGER.warn("审核提款申请[IDrawServiceImpl.auditDrawInfo]告警，参数：{}", ProtoBufUtil.toJSONHasTryCatch(request), e);
            ResponseHeader responseHeader = ResponseUtil.getResponseHeader(e.getErrorCode(), e.getErrorMessage());
            builder.setResponseHeader(responseHeader);
        } catch (Exception e) {
            LOGGER.error("审核提款申请[IDrawServiceImpl.auditDrawInfo]异常，参数：{}", 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 drawDrawInfo(DrawDrawInfoRequest request, StreamObserver<DrawDrawInfoResponse> responseObserver) {
        DrawDrawInfoResponse.Builder builder = DrawDrawInfoResponse.newBuilder();
        try {
            builder = drawBl.drawDrawInfo(request);
            LOGGER.info("打款提款申请[IDrawServiceImpl.drawDrawInfo]正常，参数：{}", ProtoBufUtil.toJSONHasTryCatch(request));
        } catch (FinanceGrpcRuntimeException e) {
            LOGGER.warn("打款提款申请[IDrawServiceImpl.drawDrawInfo]告警，参数：{}", ProtoBufUtil.toJSONHasTryCatch(request), e);
            ResponseHeader responseHeader = ResponseUtil.getResponseHeader(e.getErrorCode(), e.getErrorMessage());
            builder.setResponseHeader(responseHeader);
        } catch (Exception e) {
            LOGGER.error("打款提款申请[IDrawServiceImpl.drawDrawInfo]异常，参数：{}", 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 submitDrawApplyFiles(SubmitDrawApplyFilesRequst request, StreamObserver<SubmitDrawApplyFilesResponse> responseObserver) {
        SubmitDrawApplyFilesResponse.Builder builder = SubmitDrawApplyFilesResponse.newBuilder();
        try {
            builder = drawBl.submitDrawApplyFiles(request);
            LOGGER.info("提交提款申请-上传资料[IDrawServiceImpl.submitDrawApplyFiles]正常，参数：{}", ProtoBufUtil.toJSONHasTryCatch(request));
        } catch (FinanceGrpcRuntimeException e) {
            LOGGER.warn("提交提款申请-上传资料[IDrawServiceImpl.submitDrawApplyFiles]告警，参数：{}", ProtoBufUtil.toJSONHasTryCatch(request), e);
            ResponseHeader responseHeader = ResponseUtil.getResponseHeader(e.getErrorCode(), e.getErrorMessage());
            builder.setResponseHeader(responseHeader);
        } catch (Exception e) {
            LOGGER.error("提交提款申请-上传资料[IDrawServiceImpl.submitDrawApplyFiles]异常，参数：{}", 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 checkDrawApplyInfo(CheckDrawApplyInfoRequest request, StreamObserver<CheckDrawApplyInfoResponse> responseObserver) {
        CheckDrawApplyInfoResponse.Builder builder = CheckDrawApplyInfoResponse.newBuilder();
        try {
            builder = drawBl.checkDrawApplyInfo(request);
            LOGGER.info("验证提款申请[IDrawServiceImpl.checkDrawApplyInfo]正常，参数：{}", ProtoBufUtil.toJSONHasTryCatch(request));
        } catch (FinanceGrpcRuntimeException e) {
            LOGGER.warn("验证提款申请[IDrawServiceImpl.checkDrawApplyInfo]告警，参数：{}", ProtoBufUtil.toJSONHasTryCatch(request), e);
            ResponseHeader responseHeader = ResponseUtil.getResponseHeader(e.getErrorCode(), e.getErrorMessage());
            builder.setResponseHeader(responseHeader);
        } catch (Exception e) {
            LOGGER.error("验证提款申请[IDrawServiceImpl.checkDrawApplyInfo]异常，参数：{}", 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 getDrawApplyFiles(GetDrawApplyFilesRequest request, StreamObserver<GetDrawApplyFilesResponse> responseObserver) {
        GetDrawApplyFilesResponse.Builder builder = GetDrawApplyFilesResponse.newBuilder();
        try {
            builder = drawBl.getDrawApplyFiles(request);
            LOGGER.info("获取提款申请-上传资料项列表[IDrawServiceImpl.getDrawApplyFiles]正常，参数：{}", ProtoBufUtil.toJSONHasTryCatch(request));
        } catch (FinanceGrpcRuntimeException e) {
            LOGGER.warn("获取提款申请-上传资料项列表[IDrawServiceImpl.getDrawApplyFiles]告警，参数：{}", ProtoBufUtil.toJSONHasTryCatch(request), e);
            ResponseHeader responseHeader = ResponseUtil.getResponseHeader(e.getErrorCode(), e.getErrorMessage());
            builder.setResponseHeader(responseHeader);
        } catch (Exception e) {
            LOGGER.error("获取提款申请-上传资料项列表[IDrawServiceImpl.getDrawApplyFiles]异常，参数：{}", 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 getDrawApplyPagingInfo(GetDrawApplyPagingInfoRequest request, StreamObserver<GetDrawApplyPagingInfoResponse> responseObserver) {
        GetDrawApplyPagingInfoResponse.Builder builder = GetDrawApplyPagingInfoResponse.newBuilder();
        try {
            builder = drawBl.getDrawApplyPagingInfo(request);
            LOGGER.info("获取提款申请列表[IDrawServiceImpl.getDrawApplyPagingInfo]正常，参数：{}", ProtoBufUtil.toJSONHasTryCatch(request));
        } catch (FinanceGrpcRuntimeException e) {
            LOGGER.warn("获取提款申请列表[IDrawServiceImpl.getDrawApplyPagingInfo]告警，参数：{}", ProtoBufUtil.toJSONHasTryCatch(request), e);
            ResponseHeader responseHeader = ResponseUtil.getResponseHeader(e.getErrorCode(), e.getErrorMessage());
            builder.setResponseHeader(responseHeader);
        } catch (Exception e) {
            LOGGER.error("获取提款申请列表[IDrawServiceImpl.getDrawApplyPagingInfo]异常，参数：{}", 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 getDrawApplyInfo(GetDrawApplyInfoRequest request, StreamObserver<GetDrawApplyInfoResponse> responseObserver) {
        GetDrawApplyInfoResponse.Builder builder = GetDrawApplyInfoResponse.newBuilder();
        try {
            builder = drawBl.getDrawApplyInfo(request);
            LOGGER.info("获取提款申请信息[IDrawServiceImpl.getDrawApplyInfo]正常，参数：{}", ProtoBufUtil.toJSONHasTryCatch(request));
        } catch (FinanceGrpcRuntimeException e) {
            LOGGER.warn("获取提款申请信息[IDrawServiceImpl.getDrawApplyInfo]告警，参数：{}", ProtoBufUtil.toJSONHasTryCatch(request), e);
            ResponseHeader responseHeader = ResponseUtil.getResponseHeader(e.getErrorCode(), e.getErrorMessage());
            builder.setResponseHeader(responseHeader);
        } catch (Exception e) {
            LOGGER.error("获取提款申请信息[IDrawServiceImpl.getDrawApplyInfo]异常，参数：{}", ProtoBufUtil.toJSONHasTryCatch(request), e);
            ResponseHeader responseHeader = ResponseUtil.getResponseHeader(FinanceGrpcRuntimeException.ERROR_CODE_100, e.getMessage());
            builder.setResponseHeader(responseHeader);
        }
        responseObserver.onNext(builder.build());
        responseObserver.onCompleted();
    }
}
