package com.usoft.credit.bl;

import com.google.protobuf.InvalidProtocolBufferException;
import com.usoft.credit.dal.entity.EnterpriseJsonWithBLOBs;
import com.usoft.credit.dal.mapper.EnterpriseJsonMapper;
import com.usoft.credit.exception.CreditRuntimeException;
import com.usoft.credit.grpc.api.protobuf.EnterpriseJsonResponse;
import com.usoft.credit.grpc.api.protobuf.EnterpriseQueryRequest;
import com.usoft.credit.grpc.api.query.entity.EnterpriseQuery;
import com.usoft.credit.grpc.api.result.entity.EnterpriseJson;
import com.usoft.credit.service.enterprise.EnCreditInfoService;
import com.usoft.credit.service.enterprise.EnRiskInfoService;
import com.usoft.credit.service.enterprise.EnexceptionService;
import com.usoft.credit.service.enterprise.EnterpriseDeptService;
import com.usoft.credit.util.DateUtils;
import com.usoft.credit.util.ProtoBufUtil;
import com.usoft.credit.util.RandomCodeUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;

/**
 * @author liuam
 * @since 2018/8/7 0007 下午 14:31
 */
@Service
public class EnterPriseJsonBL {

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

    @SuppressWarnings("SpringJavaAutowiringInspection")
    @Autowired
    private EnterpriseJsonMapper enterpriseJsonMapper;

    @Autowired
    private EnCreditInfoService creditInfoService;

    @Autowired
    private EnexceptionService enexceptionService;

    @Autowired
    private EnRiskInfoService enRiskInfoService;

    @Autowired
    private EnterpriseDeptService enterpriseDeptService;
    /**
     * 返回企业信用信息
     * @param request
     * @return
     * @throws Exception
     */
    public EnterpriseJsonResponse.Builder queryEn(EnterpriseQueryRequest request) throws InvalidProtocolBufferException {
        // 获得查询条件并向数据库中取数据
        EnterpriseQuery enterpriseQuery = request.getEnterpriseQuery();
        if (enterpriseQuery.getCreditCode() == null || enterpriseQuery.getEnname() == null || enterpriseQuery.getRegisterNo() == null) {
            throw new CreditRuntimeException(10001, "企业信用查询条件不正确");
        }

        EnterpriseJsonResponse.Builder response = EnterpriseJsonResponse.newBuilder();
        EnterpriseJson.Builder enterpriseJsonbuilder = EnterpriseJson.newBuilder();


        EnterpriseJsonWithBLOBs enterpriseJson = enterpriseJsonMapper.findByCreateTime(DateUtils.date(new Date()), enterpriseQuery.getEnname(), enterpriseQuery.getRegisterNo(), enterpriseQuery.getCreditCode());
        // 从数据库查询到，返回
        if (enterpriseJson != null && enterpriseJson.getEndept() != null) {
            enterpriseJsonbuilder = ProtoBufUtil.toProtoBuf(enterpriseJsonbuilder, enterpriseJson);
            response.setEnterpriseJson(enterpriseJsonbuilder);
            logger.info("从数据库中查询到缓存的数据；{}", enterpriseJson);
            return response;
        }

        // 将protobuf 的enterpriseQuery 转换成 com.usoft.credit.modal.EnterpriseQuery
        com.usoft.credit.modal.EnterpriseQuery erpQuery = ProtoBufUtil.toBean(enterpriseQuery, com.usoft.credit.modal.EnterpriseQuery.class);

        // 查询企业债务信息
        String endeptinfoRoot = enterpriseDeptService.queryEnterpriseDept(erpQuery);
        // 查询企业经营异常信息
        String abnormalRoot = enexceptionService.queryEnexception(erpQuery);
        // 查询企业信用信息
        String encreditinfoRoot = creditInfoService.queryEnCreditInfo(erpQuery);
        // 查询企业风险信息
        String enRiskInfoRoot = enRiskInfoService.queryEnRiskInfo(erpQuery);

        EnterpriseJsonWithBLOBs enterpriseJsonWithBLOBs = new EnterpriseJsonWithBLOBs();
        enterpriseJsonWithBLOBs.setEndept(endeptinfoRoot);
        enterpriseJsonWithBLOBs.setEnexception(abnormalRoot);
        enterpriseJsonWithBLOBs.setEncredit(encreditinfoRoot);
        enterpriseJsonWithBLOBs.setEnrisk(enRiskInfoRoot);
        enterpriseJsonWithBLOBs.setCreateDate(new Date());
        enterpriseJsonWithBLOBs.setEnname(erpQuery.getEnname());
        enterpriseJsonWithBLOBs.setCreditcode(erpQuery.getCreditCode());
        enterpriseJsonWithBLOBs.setRegisterno(erpQuery.getRegisterNo());
        enterpriseJsonWithBLOBs.setCode(RandomCodeUtils.timeStampCode());


        enterpriseJsonMapper.insertSelective(enterpriseJsonWithBLOBs);
        enterpriseJsonbuilder = ProtoBufUtil.toProtoBuf(enterpriseJsonbuilder, enterpriseJsonWithBLOBs);
        response.setEnterpriseJson(enterpriseJsonbuilder);
        return response;
    }

}
