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.MessageBl;
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;

/**
 * U融链消息
 * @author: huyy
 * @date: 2018/9/6 17:01
 */
@GRpcService
public class MessageServiceImpl extends IMessageGrpcServiceGrpc.IMessageGrpcServiceImplBase {
    private static final Logger LOGGER = LoggerFactory.getLogger(RepayGrpcServiceImpl.class);

    @Autowired
    private MessageBl messageBl;

    /**
     * 获取消息
     *
     * @param request
     * @param responseObserver
     */
    @Override
    public void getMessages(GetMessagesRequest request, StreamObserver<GetMessagesResponse> responseObserver) {
        GetMessagesResponse.Builder builder = GetMessagesResponse.newBuilder();
        try {
            builder = messageBl.getMessages(request);
            LOGGER.info("获取消息[MessageServiceImpl.getMessages]正常，参数：{}", ProtoBufUtil.toJSONHasTryCatch(request));
        } catch (FinanceGrpcRuntimeException e) {
            LOGGER.warn("获取消息[MessageServiceImpl.getMessages]告警，参数：{}", ProtoBufUtil.toJSONHasTryCatch(request), e);
            ResponseHeader responseHeader = ResponseUtil.getResponseHeader(e.getErrorCode(), e.getErrorMessage());
            builder.setResponseHeader(responseHeader);
        } catch (Exception e) {
            LOGGER.error("获取消息[MessageServiceImpl.getMessages]异常，参数：{}", 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 getUnreadCount(GetUnreadCountRequest request, StreamObserver<GetUnreadCountResponse> responseObserver) {
        GetUnreadCountResponse.Builder builder = GetUnreadCountResponse.newBuilder();
        try {
            builder = messageBl.getUnreadCount(request);
            LOGGER.info("获取消息[MessageServiceImpl.getUnreadCount]正常，参数：{}", ProtoBufUtil.toJSONHasTryCatch(request));
        } catch (FinanceGrpcRuntimeException e) {
            LOGGER.warn("获取消息[MessageServiceImpl.getUnreadCount]告警，参数：{}", ProtoBufUtil.toJSONHasTryCatch(request), e);
            ResponseHeader responseHeader = ResponseUtil.getResponseHeader(e.getErrorCode(), e.getErrorMessage());
            builder.setResponseHeader(responseHeader);
        } catch (Exception e) {
            LOGGER.error("获取消息[MessageServiceImpl.getUnreadCount]异常，参数：{}", 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 setRead(SetReadRequest request, StreamObserver<SetReadResponse> responseObserver) {
        SetReadResponse.Builder builder = SetReadResponse.newBuilder();
        try {
            builder = messageBl.setRead(request);
            LOGGER.info("获取消息[MessageServiceImpl.setRead]正常，参数：{}", ProtoBufUtil.toJSONHasTryCatch(request));
        } catch (FinanceGrpcRuntimeException e) {
            LOGGER.warn("获取消息[MessageServiceImpl.setRead]告警，参数：{}", ProtoBufUtil.toJSONHasTryCatch(request), e);
            ResponseHeader responseHeader = ResponseUtil.getResponseHeader(e.getErrorCode(), e.getErrorMessage());
            builder.setResponseHeader(responseHeader);
        } catch (Exception e) {
            LOGGER.error("获取消息[MessageServiceImpl.setRead]异常，参数：{}", ProtoBufUtil.toJSONHasTryCatch(request), e);
            ResponseHeader responseHeader = ResponseUtil.getResponseHeader(FinanceGrpcRuntimeException.ERROR_CODE_100, e.getMessage());
            builder.setResponseHeader(responseHeader);
        }
        responseObserver.onNext(builder.build());
        responseObserver.onCompleted();
    }
}
