package com.usoft.credit.controller;

import com.google.protobuf.InvalidProtocolBufferException;
import com.usoft.credit.grpc.api.protobuf.EnterpriseJsonResponse;
import com.usoft.credit.grpc.api.protobuf.EnterpriseQueryRequest;
import com.usoft.credit.grpc.api.protobuf.PersonJsonResponse;
import com.usoft.credit.grpc.api.protobuf.PersonQueryRequest;
import com.usoft.credit.grpc.api.query.entity.EnterpriseQuery;
import com.usoft.credit.grpc.api.query.entity.PersonQuery;
import com.usoft.credit.grpc.api.query.entity.ResponseHeader;
import com.usoft.credit.grpc.api.result.entity.User;
import com.usoft.credit.rest.domain.EnterPriseJsonDomain;
import com.usoft.credit.rest.domain.PersonJsonDomain;
import com.usoft.credit.rest.util.ProtoBufUtil;
import com.usoft.credit.rest.util.ResponseUtil;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

/**
 * @author liuam
 * @since 2018/8/7 0007 下午 19:24
 */
@RestController
@RequestMapping("erpquery")
public class CreditQueryController {

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


    @Autowired
    private EnterPriseJsonDomain enterPriseJsonDomain;

    @Autowired
    private PersonJsonDomain personJsonDomain;

    /**
     * ERP对企业进行查询
     * @param data enterpriseQuery 查询条件
     * @return
     */
    @RequestMapping(value = "/queryEn", method = RequestMethod.POST)
    public String queryEn(String data, int userUU, int enUU, String password) {

        // 创建返回的 enterpriseJson
        EnterpriseJsonResponse.Builder builder = EnterpriseJsonResponse.newBuilder();

        // 创建查询的 enterpriseQuery ，转换异常则进行记录并返回
        EnterpriseQuery.Builder enterpriseQuery = null;
        try {
            enterpriseQuery = ProtoBufUtil.toProtoBuf(EnterpriseQuery.newBuilder(), data);
        } catch (InvalidProtocolBufferException e) {
            logger.warn("查询企业信息[CreditQueryController.queryEn]异常，enterpriseQuery 参数错误，参数：{}", data);
            ResponseHeader responseHeader = ResponseUtil.getResponseHeader(10005, "enterpriseQuery不正确");
            builder.setResponseHeader(responseHeader);
            return ProtoBufUtil.toJSONHasTryCatch(builder.build());
        }

        // 将 user 和 enterpriseQuery 封装进 request，传递给domain
        EnterpriseQueryRequest.Builder request = EnterpriseQueryRequest.newBuilder();
        request.setEnterpriseQuery(enterpriseQuery);

        // 创建用户检验的user
        User.Builder user = User.newBuilder();
        user.setUseruu(userUU);
        user.setEnuu(enUU);
        user.setPassword(password);
        request.setUser(user);


        try {
            builder = enterPriseJsonDomain.queryEn(request.build());
            logger.info("查询企业信用信息[CreditQueryController.queryEn]正常，参数：{}", ProtoBufUtil.toJSONHasTryCatch(request));
        } catch (Exception e) {
            logger.error("查询企业信用信息[CreditQueryController.queryEn]异常，参数：{}，异常信息：{}", ProtoBufUtil.toJSONHasTryCatch(request), e);
            ResponseHeader responseHeader = ResponseUtil.getResponseHeader(10003, e.getMessage());
            builder.setResponseHeader(responseHeader);
        }


        return ProtoBufUtil.toJSONHasTryCatch(builder.build());
    }

    /**
     * erp对个人进行查询
     */
    @RequestMapping(value = "/queryPe", method = RequestMethod.POST)
    public String queryPerson(String data, int userUU, int enUU, String password) {

        // 创建返回的 enterpriseJson
        PersonJsonResponse.Builder builder = PersonJsonResponse.newBuilder();

        // 创建查询的 personQuery ，转换异常则进行记录并返回
        PersonQuery.Builder personQuery = null;
        try {
            personQuery = ProtoBufUtil.toProtoBuf(PersonQuery.newBuilder(), data);
        } catch (InvalidProtocolBufferException e) {
            logger.warn("查询企业信息[CreditQueryController.queryPerson]异常，personQuery 参数错误，参数：{}", data);
            ResponseHeader responseHeader = ResponseUtil.getResponseHeader(10005, "personQuery不正确");
            builder.setResponseHeader(responseHeader);
            return ProtoBufUtil.toJSONHasTryCatch(builder.build());
        }

        // 将 user 和 personQuery 封装进 request，传递给domain
        PersonQueryRequest.Builder request = PersonQueryRequest.newBuilder();
        request.setPersonQuery(personQuery);

        // 创建用户检验的user
        User.Builder user = User.newBuilder();
        user.setUseruu(userUU);
        user.setEnuu(enUU);
        user.setPassword(password);
        request.setUser(user);

        try {
            builder = personJsonDomain.queryPerson(request.build());
            logger.info("查询个人信用信息[CreditQueryController.queryPerson]正常，参数：{}", ProtoBufUtil.toJSONHasTryCatch(request));
        } catch (Exception e) {
            logger.error("查询个人信用信息[CreditQueryController.queryPerson]异常，参数：{}，异常信息：{}", ProtoBufUtil.toJSONHasTryCatch(request), e);
            ResponseHeader responseHeader = ResponseUtil.getResponseHeader(10003, e.getMessage());
            builder.setResponseHeader(responseHeader);
        }


        return ProtoBufUtil.toJSONHasTryCatch(builder.build());
    }

}
