package com.heyu.api.controller.traffic;

import com.heyu.api.baidu.handle.traffic.BWeightNoteHandle;
import com.heyu.api.baidu.request.traffic.BWeightNoteRequest;
import com.heyu.api.baidu.response.traffic.BWeightNoteResp;
import com.heyu.api.controller.BaseController;
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 com.heyu.api.resp.traffic.WeightNoteResp;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.collections.CollectionUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

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


/***
 * https://console.bce.baidu.com/support/?_=1740411162437&timestamp=1743167283288#/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%2Fweight_note&method=post
 *
 * 磅单识别
 *
 * 接口描述
 * 结构化识别磅单的车牌号、打印时间、毛重、皮重、净重、发货单位、收货单位、单号8个关键字段，现阶段仅支持识别印刷体磅单。
 */
@Slf4j
@RestController
@RequestMapping("/weight/note")
@NotIntercept
public class WeightNoteController extends BaseController {

    @Autowired
    private BWeightNoteHandle bWeightNoteHandle;

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

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


        ApiR<BWeightNoteResp> bR = bWeightNoteHandle.handle(request);
        if (bR.isSuccess()) {
            BWeightNoteResp bWeightNoteResp = bR.getData();
            List<BWeightNoteResp.WordsResultDTO> wordsResultDTOS = bWeightNoteResp.getWordsResult();
            if (CollectionUtils.isNotEmpty(wordsResultDTOS)) {
                for (BWeightNoteResp.WordsResultDTO wordsResultDTO : wordsResultDTOS) {

                    WeightNoteResp resp = new WeightNoteResp();


                    List<String> tareWeight = new ArrayList<>();                  //皮重
                    if (CollectionUtils.isNotEmpty(wordsResultDTO.getTareWeight())) {
                        for (BWeightNoteResp.WordsResultDTO.TareWeightDTO tareWeightDTO : wordsResultDTO.getTareWeight()) {
                            tareWeight.add(tareWeightDTO.getWord());
                        }
                    }
                    resp.setTareWeight(tareWeight);

                    List<String> crossWeight = new ArrayList<>();        //毛重
                    if (CollectionUtils.isNotEmpty(wordsResultDTO.getCrossWeight())) {
                        for (BWeightNoteResp.WordsResultDTO.CrossWeightDTO crossWeightDTO : wordsResultDTO.getCrossWeight()) {
                            crossWeight.add(crossWeightDTO.getWord());

                        }
                    }
                    resp.setCrossWeight(crossWeight);


                    List<String> plateNum = new ArrayList<>();              //车牌号
                    if (CollectionUtils.isNotEmpty(wordsResultDTO.getPlateNum())) {
                        for (BWeightNoteResp.WordsResultDTO.PlateNumDTO plateNumDTO : wordsResultDTO.getPlateNum()) {
                            plateNum.add(plateNumDTO.getWord());

                        }
                    }
                    resp.setPlateNum(plateNum);

                    List<String> sendingCompany = new ArrayList<>();//货单位
                    if (CollectionUtils.isNotEmpty(wordsResultDTO.getSendingCompany())) {
                        for (BWeightNoteResp.WordsResultDTO.SendingCompanyDTO sendingCompanyDTO : wordsResultDTO.getSendingCompany()) {
                            sendingCompany.add(sendingCompanyDTO.getWord());

                        }
                    }
                    resp.setSendingCompany(sendingCompany);

                    List<String> deliveryNumber = new ArrayList<>();              //单号
                    if (CollectionUtils.isNotEmpty(wordsResultDTO.getDeliveryNumber())) {
                        for (BWeightNoteResp.WordsResultDTO.DeliveryNumberDTO deliveryNumberDTO : wordsResultDTO.getDeliveryNumber()) {
                            deliveryNumber.add(deliveryNumberDTO.getWord());

                        }
                    }
                    resp.setDeliveryNumber(deliveryNumber);

                    List<String> receivingCompany = new ArrayList<>(); //收货单位
                    if (CollectionUtils.isNotEmpty(wordsResultDTO.getReceivingCompany())) {
                        for (BWeightNoteResp.WordsResultDTO.ReceivingCompanyDTO receivingCompanyDTO : wordsResultDTO.getReceivingCompany()) {
                            receivingCompany.add(receivingCompanyDTO.getWord());

                        }
                    }
                    resp.setReceivingCompany(receivingCompany);

                    List<String> printTime = new ArrayList<>();    //打印时间
                    if (CollectionUtils.isNotEmpty(wordsResultDTO.getPrintTime())) {
                        for (BWeightNoteResp.WordsResultDTO.PrintTimeDTO printTimeDTO : wordsResultDTO.getPrintTime()) {
                            printTime.add(printTimeDTO.getWord());

                        }
                    }
                    resp.setPrintTime(printTime);

                    List<String> netWeight = new ArrayList<>();        //净重
                    if (CollectionUtils.isNotEmpty(wordsResultDTO.getNetWeight())) {
                        for (BWeightNoteResp.WordsResultDTO.NetWeightDTO netWeightDTO : wordsResultDTO.getNetWeight()) {
                            netWeight.add(netWeightDTO.getWord());

                        }
                    }
                    resp.setNetWeight(netWeight);
                    respList.add(resp);


                }

            }
            return R.ok().setData(respList);
        }

        return R.error(bR.getErrorMsg());

    }
}
