package com.heyu.api.controller.traffic;


import com.heyu.api.baidu.handle.traffic.BWaybillHandle;
import com.heyu.api.baidu.request.traffic.BWaybillRequest;
import com.heyu.api.baidu.response.traffic.BWaybillResp;
import com.heyu.api.controller.BaseController;
import com.heyu.api.data.annotation.CacheResult;
import com.heyu.api.data.annotation.CustomPath;
import com.heyu.api.data.utils.ApiR;
import com.heyu.api.data.utils.R;
import com.heyu.api.resp.traffic.WaybillResp;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.collections.CollectionUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import org.springframework.web.bind.annotation.RequestMapping;

import java.util.ArrayList;
import java.util.List;


/**
 * https://console.bce.baidu.com/support/?_=1740411162437&timestamp=1740412723534#/api?product=AI&project=%E6%96%87%E5%AD%97%E8%AF%86%E5%88%AB&parent=%E4%BA%A4%E9%80%9A%E5%9C%BA%E6%99%AFOCR&api=rest%2F2.0%2Focr%2Fv1%2Fwaybill&method=post
 * 快递面单识别
 *
 *
 * 接口描述
 * 支持市面上常见版式的快递面单识别，包括申通/圆通/中通/百世汇通/韵达/顺丰/京东/邮政/极兔/天天等面单版式，结构化识别运单号、收/寄件人姓名、收/寄件人电话、收/寄件人地址等字段。同时支持识别隐私面单。
 *
 *
 */
@Component
@Slf4j
@CustomPath("waybill")
public class WaybillController extends BaseController {


    @Autowired
    private BWaybillHandle bWaybillHandle;


    @RequestMapping("/recognize")
    @CacheResult
    public R recognize(BWaybillRequest request) {

        List<WaybillResp> respList = new ArrayList<>();


        ApiR<BWaybillResp> bR = bWaybillHandle.handle(request);
        if (bR.isSuccess()) {

            BWaybillResp bWaybillResp = bR.getData();
            if (CollectionUtils.isNotEmpty(bWaybillResp.getWordsResult())) {
                for (BWaybillResp.WordsResultDTO wordsResultDTO : bWaybillResp.getWordsResult()) {
                    WaybillResp resp = new WaybillResp();

                    resp.setImageInfo(wordsResultDTO.getImageInfo().getDirection());                 //	图像四方向

                    List<String> barCode = new ArrayList<>();               //条形码


                    if (CollectionUtils.isNotEmpty(wordsResultDTO.getBarCode())) {
                        for (BWaybillResp.WordsResultDTO.BarCodeDTO barCodeDTO : wordsResultDTO.getBarCode()) {

                            barCode.add(barCodeDTO.getWord());
                        }
                    }
                    resp.setBarCode(barCode);

                    List<String> waybillNumber = new ArrayList<>();           //快递运单号

                    if (CollectionUtils.isNotEmpty(wordsResultDTO.getWaybillNumber())) {
                        for (BWaybillResp.WordsResultDTO.WaybillNumberDTO waybillNumberDTO : wordsResultDTO.getWaybillNumber()) {
                            waybillNumber.add(waybillNumberDTO.getWord());
                        }
                    }
                    resp.setWaybillNumber(waybillNumber);

                    List<String> threeSegmentCode = new ArrayList<>();         //三段码
                    if (CollectionUtils.isNotEmpty(wordsResultDTO.getThreeSegmentCode())) {
                        for (BWaybillResp.WordsResultDTO.ThreeSegmentCodeDTO threeSegmentCodeDTO : wordsResultDTO.getThreeSegmentCode()) {
                            threeSegmentCode.add(threeSegmentCodeDTO.getWord());

                        }
                    }
                    resp.setThreeSegmentCode(threeSegmentCode);

                    List<String> recipientName = new ArrayList<>();   //收件人姓名
                    if (CollectionUtils.isNotEmpty(wordsResultDTO.getRecipientName())) {
                        for (BWaybillResp.WordsResultDTO.RecipientNameDTO recipientNameDTO : wordsResultDTO.getRecipientName()) {
                            recipientName.add(recipientNameDTO.getWord());

                        }
                    }
                    resp.setRecipientName(recipientName);

                    List<String> senderName = new ArrayList<>();         //寄件人姓名
                    if (CollectionUtils.isNotEmpty(wordsResultDTO.getSenderName())) {
                        for (BWaybillResp.WordsResultDTO.SenderNameDTO senderNameDTO : wordsResultDTO.getSenderName()) {
                            senderName.add(senderNameDTO.getWord());

                        }
                    }
                    resp.setSenderName(senderName);

                    List<String> recipientAddr = new ArrayList<>();           //收件人地址
                    if (CollectionUtils.isNotEmpty(wordsResultDTO.getRecipientAddr())) {
                        for (BWaybillResp.WordsResultDTO.RecipientAddrDTO recipientAddrDTO : wordsResultDTO.getRecipientAddr()) {
                            recipientAddr.add(recipientAddrDTO.getWord());

                        }
                    }
                    resp.setRecipientAddr(recipientAddr);

                    List<String> senderAddr = new ArrayList<>();             //寄件人地址
                    if (CollectionUtils.isNotEmpty(wordsResultDTO.getSenderAddr())) {
                        for (BWaybillResp.WordsResultDTO.SenderAddrDTO senderAddrDTO : wordsResultDTO.getSenderAddr()) {
                            senderAddr.add(senderAddrDTO.getWord());

                        }
                    }

                    resp.setSenderAddr(senderAddr);


                    List<String> recipientPhone = new ArrayList<>();         //收件人电话
                    if (CollectionUtils.isNotEmpty(wordsResultDTO.getRecipientPhone())) {
                        for (BWaybillResp.WordsResultDTO.RecipientPhoneDTO recipientPhoneDTO : wordsResultDTO.getRecipientPhone()) {
                            recipientPhone.add(recipientPhoneDTO.getWord());

                        }
                    }
                    resp.setRecipientPhone(recipientPhone);


                    List<String> senderPhone = new ArrayList<>(); // 寄件人电话String
                    if (CollectionUtils.isNotEmpty(wordsResultDTO.getSenderPhone())) {
                        for (BWaybillResp.WordsResultDTO.SenderPhoneDTO senderPhoneDTO : wordsResultDTO.getSenderPhone()) {
                            senderPhone.add(senderPhoneDTO.getWord());

                        }
                    }
                    resp.setSenderPhone(senderPhone);

                    List<String> virtualNumber = new ArrayList<>(); //虚拟面单号。当请求参数 is_identify_virtual_waybill = true 时返回该字段
                    if (CollectionUtils.isNotEmpty(wordsResultDTO.getVirtualNumber())) {
                        for (BWaybillResp.WordsResultDTO.SenderPhoneDTO senderPhoneDTO : wordsResultDTO.getVirtualNumber()) {
                            virtualNumber.add(senderPhoneDTO.getWord());
                        }
                    }

                    resp.setVirtualNumber(virtualNumber);

                    List<String> virtualNumberLast = new ArrayList<>(); //隐私面单的4位转接号。当请求参数 is_identify_virtual_waybill = true 时返回该字段
                    if (CollectionUtils.isNotEmpty(wordsResultDTO.getVirtualNumberLast())) {
                        for (BWaybillResp.WordsResultDTO.SenderPhoneDTO senderPhoneDTO : wordsResultDTO.getVirtualNumberLast()) {
                            virtualNumberLast.add(senderPhoneDTO.getWord());
                        }
                    }

                    resp.setVirtualNumberLast(virtualNumberLast);
                    List<String> isVirtualWaybill = new ArrayList<>(); // 此张快递面单是否为隐私面单，“true”代表“是”，“false”代表“否”。当请求参数 is_identify_virtual_waybill = true 时返回该字段
                    if (CollectionUtils.isNotEmpty(wordsResultDTO.getIsVirtualWaybill())) {
                        for (BWaybillResp.WordsResultDTO.SenderPhoneDTO senderPhoneDTO : wordsResultDTO.getIsVirtualWaybill()) {
                            isVirtualWaybill.add(senderPhoneDTO.getWord());
                        }
                    }
                    resp.setIsVirtualWaybill(isVirtualWaybill);

                    respList.add(resp);
                }
            }
            return R.ok().setData(respList);
        }
        return R.error();
    }


}
