package com.usoft.credit.bl;

import com.google.protobuf.InvalidProtocolBufferException;
import com.usoft.credit.dal.entity.PersonJsonWithBLOBs;
import com.usoft.credit.dal.mapper.PersonJsonMapper;
import com.usoft.credit.exception.CreditRuntimeException;
import com.usoft.credit.grpc.api.protobuf.PersonJsonResponse;
import com.usoft.credit.grpc.api.protobuf.PersonQueryRequest;
import com.usoft.credit.grpc.api.query.entity.PersonQuery;
import com.usoft.credit.grpc.api.result.entity.PersonJson;
import com.usoft.credit.service.person.PersonCorporationService;
import com.usoft.credit.service.person.PersonalService;
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 PersonJsonBL {

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

    @SuppressWarnings("SpringJavaAutowiringInspection")
    @Autowired
    private PersonJsonMapper personJsonMapper;

    @Autowired
    private PersonCorporationService personCorporationService;

    @Autowired
    private PersonalService personalService;

    /**
     * erp对个人进行查询
     */
    public PersonJsonResponse.Builder queryPerson(PersonQueryRequest request) throws InvalidProtocolBufferException {


        // 获得查询条件并向数据库中取数据
        PersonQuery personQuery = request.getPersonQuery();

        if (personQuery.getDocumentNo() == null || personQuery.getName() == null) {
            throw new CreditRuntimeException(10002, "个人信用查询条件不正确");
        }

        PersonJsonResponse.Builder response = PersonJsonResponse.newBuilder();
        PersonJson.Builder personJsonbuilder = PersonJson.newBuilder();
        PersonJsonWithBLOBs personJsonWithBLOBs = personJsonMapper.findByCreateTime(DateUtils.date(new Date()), personQuery.getName(), personQuery.getDocumentNo());

        // 从数据库查询到，返回
        if (personJsonWithBLOBs != null && personJsonWithBLOBs.getPnidentity() != null) {
            personJsonbuilder = ProtoBufUtil.toProtoBuf(personJsonbuilder, personJsonWithBLOBs);
            response.setPersonJson(personJsonbuilder);
            logger.info("从数据库中查询到缓存的数据；{}", personJsonWithBLOBs);
            return response;
        }

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

        // 查询个人身份信息
        String policeCheckRoot = personalService.queryPoliceCheck(erpQuery);
        // 查询个人股东信息
        String personCorporationRoot = personCorporationService.queryPersonCorporation(erpQuery);

        PersonJsonWithBLOBs pJsonWithBLOBs = new PersonJsonWithBLOBs();
        pJsonWithBLOBs.setPnidentity(policeCheckRoot);
        pJsonWithBLOBs.setPnshareholder(personCorporationRoot);
        pJsonWithBLOBs.setCode(RandomCodeUtils.timeStampCode());
        pJsonWithBLOBs.setCreateDate(new Date());
        pJsonWithBLOBs.setPersonname(erpQuery.getName());
        pJsonWithBLOBs.setDocumentno(erpQuery.getDocumentNo());

        personJsonMapper.insertSelective(pJsonWithBLOBs);
        personJsonbuilder = ProtoBufUtil.toProtoBuf(personJsonbuilder, pJsonWithBLOBs);
        response.setPersonJson(personJsonbuilder);
        return response;
    }
}
