package com.heyu.api.controller.financial;


import com.heyu.api.baidu.handle.financial.BAirTicketHandle;
import com.heyu.api.baidu.request.financial.BAirTicketRequest;
import com.heyu.api.baidu.response.financial.BAirTicketResp;
import com.heyu.api.baidu.response.financial.BMultiAirTicketResp;
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.financial.AirTicketMultiResp;
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/?_=1740575657628&timestamp=1740577959399#/api?product=AI&project=%E6%96%87%E5%AD%97%E8%AF%86%E5%88%AB&parent=%E8%B4%A2%E5%8A%A1%E7%A5%A8%E6%8D%AEOCR&api=rest%2F2.0%2Focr%2Fv1%2Fair_ticket&method=post
 * <p>
 * 飞机行程单识别
 * <p>
 * <p>
 * 接口描述
 * 支持对飞机行程单的24个字段进行结构化识别，包括电子客票号、印刷序号、姓名、始发站、目的站、航班号、日期、时间、票价、身份证号、承运人、民航发展基金、
 * 保险费、燃油附加费、其他税费、合计金额、填开日期、订票渠道、客票级别、座位等级、销售单位号、签注、免费行李、验证码。 同时，支持单张行程单上的多航班信
 * 息识别。
 */

@Slf4j
@RestController
@RequestMapping("/air/ticket/multi")
@NotIntercept
public class AirTicketMultiController extends BaseController {


    @Autowired
    private BAirTicketHandle bAirTicketHandle;


    @RequestMapping("/recognize")
    @CacheResult
    public R recognize(BAirTicketRequest req) {
        AirTicketMultiResp resp = new AirTicketMultiResp();


        req.setMultiDetect("true");

        ApiR<BAirTicketResp> bR = bAirTicketHandle.handle(req, BMultiAirTicketResp.class);

        if (bR.isSuccess()) {
            BMultiAirTicketResp bAirTicketResp = (BMultiAirTicketResp) bR.getData();
            BMultiAirTicketResp.WordsResultDTO wordsResultDTO = bAirTicketResp.getWordsResult();

            List<String> insurance = new ArrayList<>();
            if (CollectionUtils.isNotEmpty(wordsResultDTO.getInsurance())) {
                for (BMultiAirTicketResp.WordsResultDTO.InsuranceDTO insuranceDTO : wordsResultDTO.getInsurance()) {
                    insurance.add(insuranceDTO.getWord());
                }
            }
            resp.setInsurance(insurance);

            List<String> date = new ArrayList<>();
            if (CollectionUtils.isNotEmpty(wordsResultDTO.getDate())) {
                for (BMultiAirTicketResp.WordsResultDTO.DateDTO dateDTO : wordsResultDTO.getDate()) {
                    date.add(dateDTO.getWord());
                }
            }

            resp.setDate(date);


            List<String> flight = new ArrayList<>();
            if (CollectionUtils.isNotEmpty(wordsResultDTO.getFlight())) {
                for (BMultiAirTicketResp.WordsResultDTO.FlightDTO flightDTO : wordsResultDTO.getFlight()) {
                    flight.add(flightDTO.getWord());
                }
            }

            resp.setFlight(flight);


            List<String> issuedBy = new ArrayList<>();
            if (CollectionUtils.isNotEmpty(wordsResultDTO.getIssuedBy())) {
                for (BMultiAirTicketResp.WordsResultDTO.IssuedByDTO issuedByDTO : wordsResultDTO.getIssuedBy()) {
                    issuedBy.add(issuedByDTO.getWord());
                }
            }

            resp.setIssuedBy(issuedBy);


            List<String> startingStation = new ArrayList<>();

            if (CollectionUtils.isNotEmpty(wordsResultDTO.getStartingStation())) {

                for (BMultiAirTicketResp.WordsResultDTO.StartingStationDTO startingStationDTO : wordsResultDTO.getStartingStation()) {

                    startingStation.add(startingStationDTO.getWord());
                }
            }

            resp.setStartingStation(startingStation);

            List<String> fare = new ArrayList<>();
            if (CollectionUtils.isNotEmpty(wordsResultDTO.getFare())) {
                for (BMultiAirTicketResp.WordsResultDTO.FareDTO fareDTO : wordsResultDTO.getFare()) {
                    fare.add(fareDTO.getWord());
                }
            }
            resp.setFare(fare);

            List<String> ticketRates = new ArrayList<>();
            if (CollectionUtils.isNotEmpty(wordsResultDTO.getTicketRates())) {
                for (BMultiAirTicketResp.WordsResultDTO.TicketRatesDTO ticketRate : wordsResultDTO.getTicketRates()) {
                    ticketRates.add(ticketRate.getWord());
                }
            }
            resp.setTicketRates(ticketRates);


            List<String> serialNumber = new ArrayList<>();
            if (CollectionUtils.isNotEmpty(wordsResultDTO.getSerialNumber())) {
                for (BMultiAirTicketResp.WordsResultDTO.SerialNumberDTO serialNumberDTO : wordsResultDTO.getSerialNumber()) {
                    serialNumber.add(serialNumberDTO.getWord());

                }
            }
            resp.setSerialNumber(serialNumber);

            List<String> ticketNumber = new ArrayList<>();
            if (CollectionUtils.isNotEmpty(wordsResultDTO.getTicketNumber())) {
                for (BMultiAirTicketResp.WordsResultDTO.TicketNumberDTO ticketNumberDTO : wordsResultDTO.getTicketNumber()) {
                    ticketNumber.add(ticketNumberDTO.getWord());
                }
            }
            resp.setTicketNumber(ticketNumber);

            List<String> fuelSurcharge = new ArrayList<>();
            if (CollectionUtils.isNotEmpty(wordsResultDTO.getFuelSurcharge())) {
                for (BMultiAirTicketResp.WordsResultDTO.FuelSurchargeDTO fuelSurchargeDTO : wordsResultDTO.getFuelSurcharge()) {
                    fuelSurcharge.add(fuelSurchargeDTO.getWord());

                }
            }
            resp.setFuelSurcharge(fuelSurcharge);


            List<String> carrier = new ArrayList<>();
            if (CollectionUtils.isNotEmpty(wordsResultDTO.getCarrier())) {
                for (BMultiAirTicketResp.WordsResultDTO.CarrierDTO carrierDTO : wordsResultDTO.getCarrier()) {
                    carrier.add(carrierDTO.getWord());
                }
            }
            resp.setCarrier(carrier);


            List<String> issuedDate = new ArrayList<>();
            if (CollectionUtils.isNotEmpty(wordsResultDTO.getIssuedDate())) {
                for (BMultiAirTicketResp.WordsResultDTO.IssuedDateDTO issuedDateDTO : wordsResultDTO.getIssuedDate()) {
                    issuedDate.add(issuedDateDTO.getWord());
                }
            }
            resp.setIssuedDate(issuedDate);

            List<String> otherTax = new ArrayList<>();
            if (CollectionUtils.isNotEmpty(wordsResultDTO.getOtherTax())) {
                for (String tax : wordsResultDTO.getOtherTax()) {
                    otherTax.add(tax);
                }
            }
            resp.setOtherTax(otherTax);

            List<String> idNum = new ArrayList<>();
            if (CollectionUtils.isNotEmpty(wordsResultDTO.getIdNum())) {
                for (BMultiAirTicketResp.WordsResultDTO.IdNumDTO idNumDTO : wordsResultDTO.getIdNum()) {
                    idNum.add(idNumDTO.getWord());

                }
            }
            resp.setIdNum(idNum);


            List<String> destinationStation = new ArrayList<>();
            if (CollectionUtils.isNotEmpty(wordsResultDTO.getDestinationStation())) {
                for (BMultiAirTicketResp.WordsResultDTO.DestinationStationDTO destinationStationDTO : wordsResultDTO.getDestinationStation()) {
                    destinationStation.add(destinationStationDTO.getWord());

                }
            }
            resp.setDestinationStation(destinationStation);


            List<String> name = new ArrayList<>();
            if (CollectionUtils.isNotEmpty(wordsResultDTO.getName())) {
                for (BMultiAirTicketResp.WordsResultDTO.NameDTO nameDTO : wordsResultDTO.getName()) {
                    name.add(nameDTO.getWord());

                }
            }
            resp.setName(name);

            List<String> time = new ArrayList<>();

            if (CollectionUtils.isNotEmpty(wordsResultDTO.getTime())) {
                for (BMultiAirTicketResp.WordsResultDTO.TimeDTO timeDTO : wordsResultDTO.getTime()) {
                    time.add(timeDTO.getWord());

                }
            }
            resp.setTime(time);


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


        return R.error();
    }

}
