package com.heyu.api.controller.card;


import com.aliyun.ocr20191230.models.RecognizeBusinessLicenseResponse;
import com.aliyun.ocr20191230.models.RecognizeBusinessLicenseResponseBody;
import com.heyu.api.alibaba.handle.common.text.ARecognizeBusinessLicenseHandle;
import com.heyu.api.alibaba.request.common.text.ARecognizeBusinessLicenseRequest;
import com.heyu.api.baidu.handle.certificate.BBusinessLicenseHandle;
import com.heyu.api.baidu.request.certificate.BBusinessLicenseRequest;
import com.heyu.api.controller.BaseController;
import com.heyu.api.data.utils.MapUtils;
import com.heyu.api.request.card.BusinessLicenseRecognizeRequest;
import com.heyu.api.resp.card.BusinessLicenseRecognizeResp;
import com.heyu.api.data.annotation.CacheResult;
import com.heyu.api.data.annotation.NotIntercept;
import com.heyu.api.data.utils.ApiR;
import com.heyu.api.data.utils.R;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.Map;

/***
 * https://next.api.aliyun.com/api/ocr/2019-12-30/RecognizeBusinessLicense?tab=DOC&lang=JAVA
 *营业执照识别
 *
 * RecognizeBusinessLicense
 *
 * 功能描述
 * 营业执照识别能力可以识别营业执照关键字段内容，包括：公司地址、营业范围、注册资本、注册日期、公司法人、公司名称、统一社会信用代码、公司类型、公司营业期限日期。同时可以输出营业执照上二维码、印章位置。
 *
 *
 *
 *
 */
@Slf4j
@RestController
@RequestMapping("/business/license")
@NotIntercept
public class BusinessLicenseRecognizeController extends BaseController {


    @Autowired
    private ARecognizeBusinessLicenseHandle arRecognizeBusinessLicenseHandle;


    @Autowired
    private BBusinessLicenseHandle bBusinessLicenseHandle;


    @RequestMapping("/recognize")
    @CacheResult
    public R recognize(BusinessLicenseRecognizeRequest request) {
        BusinessLicenseRecognizeResp resp = new BusinessLicenseRecognizeResp();
        BBusinessLicenseRequest businessLicenseRequest = new BBusinessLicenseRequest();
        businessLicenseRequest.setImageUrl(request.getImageUrl());
        businessLicenseRequest.setImageBase64(request.getImageBase64());

        ApiR<Map> bR = bBusinessLicenseHandle.handle(businessLicenseRequest);
        if (bR.isSuccess()) {
            Map<String, Object> data = bR.getData();
            resp.setCompanyType(MapUtils.getByExpr(data, "words_result.类型.words"));
            resp.setRegisterDate(MapUtils.getByExpr(data, "words_result.成立日期.words"));
            resp.setValidDateEnd(MapUtils.getByExpr(data, "words_result.有效期.words"));
            resp.setBusiness(MapUtils.getByExpr(data, "words_result.经营范围.words"));
            resp.setRegisterNumber(MapUtils.getByExpr(data, "words_result.社会信用代码.words"));
            resp.setCompanyAddress(MapUtils.getByExpr(data, "words_result.地址.words"));
            resp.setRegisterCapital(MapUtils.getByExpr(data, "words_result.注册资本.words"));
            resp.setCompanyName(MapUtils.getByExpr(data, "words_result.单位名称.words"));
            resp.setCompositionType(MapUtils.getByExpr(data, "words_result.组成形式.words"));
            resp.setCertificateNumber(MapUtils.getByExpr(data, "words_result.证件编号.words"));
            resp.setRealCapital(MapUtils.getByExpr(data, "words_result.实收资本.words"));
            resp.setValidDateStart(MapUtils.getByExpr(data, "words_result.有效期起始日期.words"));
            resp.setApprovalDate(MapUtils.getByExpr(data, "words_result.核准日期.words"));
            resp.setTaxRegisterCode(MapUtils.getByExpr(data, "words_result.税务登记号.words"));
            resp.setRegisterOrgan(MapUtils.getByExpr(data, "words_result.登记机关.words"));
            return R.ok().setData(resp);
        }

        ARecognizeBusinessLicenseRequest arRecognizeBusinessLicenseRequest = new ARecognizeBusinessLicenseRequest();
        arRecognizeBusinessLicenseRequest.setImageBase64(request.getImageBase64());
        arRecognizeBusinessLicenseRequest.setImageUrl(request.getImageUrl());

        ApiR<RecognizeBusinessLicenseResponse> aR = arRecognizeBusinessLicenseHandle.handle(arRecognizeBusinessLicenseRequest);
        if (aR.isSuccess()) {
            RecognizeBusinessLicenseResponse response = aR.getData();
            if (isSuccessStatusCode(response.getStatusCode())) {
                RecognizeBusinessLicenseResponseBody responseBody = response.getBody();
                RecognizeBusinessLicenseResponseBody.RecognizeBusinessLicenseResponseBodyData responseBodyData = responseBody.getData();
                resp.setCompanyType(responseBodyData.getType());
                resp.setRegisterDate(responseBodyData.getEstablishDate());
                resp.setValidDateEnd(responseBodyData.getValidPeriod());
                resp.setBusiness(responseBodyData.getBusiness());
                resp.setRegisterNumber(responseBodyData.getRegisterNumber());
                resp.setCompanyAddress(responseBodyData.getAddress());
                resp.setRegisterCapital(responseBodyData.getCapital());
                resp.setCompanyName(responseBodyData.getName());
                resp.setCompanyLegalPerson(responseBodyData.getLegalPerson());
            }
        }
        return R.ok().setData(resp);
    }
}
