package com.usoft.credit.grpc.impl;

import com.usoft.credit.bl.EnterPriseJsonBL;
import com.usoft.credit.bl.LogBL;
import com.usoft.credit.bl.PersonJsonBL;
import com.usoft.credit.bl.UserBL;
import com.usoft.credit.bl.util.ResponseUtil;
import com.usoft.credit.grpc.api.protobuf.*;
import com.usoft.credit.grpc.api.query.entity.ResponseHeader;
import com.usoft.credit.util.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 liuam
 * @since 2018/8/7 0007 下午 14:22
 */
@GRpcService
public class CreditQueryServiceImpl extends GrpcQueryServiceGrpc.GrpcQueryServiceImplBase {

    private static final Logger logger = LoggerFactory.getLogger(CreditQueryServiceImpl.class);

    @Autowired
    private LogBL logBL;

    @Autowired
    private EnterPriseJsonBL enterPriseJsonBL;

    @Autowired
    private PersonJsonBL personJsonBL;

    @Autowired
    private UserBL userBL;

    @Override
    public void queryEn(EnterpriseQueryRequest request, StreamObserver<EnterpriseJsonResponse> responseObserver) {
        // 将查询记录保存到数据库
        logBL.saveEnterpriseLog();

        EnterpriseJsonResponse.Builder builder = EnterpriseJsonResponse.newBuilder();
        if (!userBL.verifyEnterprise(request)) {
            logger.warn("查询企业信息[CreditQueryServiceImpl.queryEn]异常，user 不正确，参数：{}", ProtoBufUtil.toJSONHasTryCatch(request));
            ResponseHeader responseHeader = ResponseUtil.getResponseHeader(10004, "用户不正确");
            builder.setResponseHeader(responseHeader);
        } else {
            try {
                builder = enterPriseJsonBL.queryEn(request);
                logger.info("查询企业信用信息[CreditQueryServiceImpl.queryEn]正常，参数：{}", ProtoBufUtil.toJSONHasTryCatch(request));
            } catch (Exception e) {
                logger.error("查询企业信用信息[CreditQueryServiceImpl.queryEn]异常，参数：{}，异常信息：{}", ProtoBufUtil.toJSONHasTryCatch(request), e);
                ResponseHeader responseHeader = ResponseUtil.getResponseHeader(10003, e.getMessage());
                builder.setResponseHeader(responseHeader);
            }
        }

        responseObserver.onNext(builder.build());
        responseObserver.onCompleted();

    }

    @Override
    public void queryPerson(PersonQueryRequest request, StreamObserver<PersonJsonResponse> responseObserver) {
        // 将查询记录保存到数据库
        logBL.savePersonLog();

        PersonJsonResponse.Builder builder = PersonJsonResponse.newBuilder();
        if (!userBL.verifyPerson(request)) {
            logger.warn("查询个人信息[CreditQueryServiceImpl.queryPerson]异常，user 不正确，参数：{}", ProtoBufUtil.toJSONHasTryCatch(request));
            ResponseHeader responseHeader = ResponseUtil.getResponseHeader(10004, "用户不正确");
            builder.setResponseHeader(responseHeader);
        } else {
            try {
                builder = personJsonBL.queryPerson(request);
                logger.info("查询个人信用信息[CreditQueryServiceImpl.queryPerson]正常，参数：{}", ProtoBufUtil.toJSONHasTryCatch(request));
            } catch (Exception e) {
                logger.error("查询个人信用信息[CreditQueryServiceImpl.queryPerson]异常，参数：{}，异常信息：{}", ProtoBufUtil.toJSONHasTryCatch(request), e);
                ResponseHeader responseHeader = ResponseUtil.getResponseHeader(10003, e.getMessage());
                builder.setResponseHeader(responseHeader);
            }
        }

        responseObserver.onNext(builder.build());
        responseObserver.onCompleted();
    }
}
