package com.heyu.api.controller.financial;


import com.heyu.api.baidu.handle.financial.BOnlineTaxiItineraryHandle;
import com.heyu.api.baidu.request.financial.BOnlineTaxiItineraryRequest;
import com.heyu.api.baidu.response.financial.BOnlineTaxiItineraryResp;
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.OnlineTaxiItineraryResp;
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.ArrayList;
import java.util.List;


/**
 * https://console.bce.baidu.com/support/?_=1740575657628&timestamp=1740579436479#/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%2Fonline_taxi_itinerary&method=post
 * 网约车行程单识别
 *
 * 接口描述
 * 对各大主要服务商的网约车行程单进行结构化识别，包括滴滴打车、花小猪打车、高德地图、曹操出行、阳光出行，支持识别服务商、行程开始时间、
 * 行程结束时间、车型、总金额等16 个关键字段。
 *
 *
 *
 *
 */
@Slf4j
@RestController
@RequestMapping("/online/taxi/itinerary")
@NotIntercept
public class OnlineTaxiItineraryController extends BaseController {


    @Autowired
    private BOnlineTaxiItineraryHandle bOnlineTaxiItineraryHandle;


    @RequestMapping("/recognize")
    @CacheResult
    public R recognize(BOnlineTaxiItineraryRequest req) {

        OnlineTaxiItineraryResp resp = new OnlineTaxiItineraryResp();

        ApiR<BOnlineTaxiItineraryResp> bR = bOnlineTaxiItineraryHandle.handle(req);
        if (bR.isSuccess()) {
            BOnlineTaxiItineraryResp bOnlineTaxiItineraryResp = bR.getData();

            BOnlineTaxiItineraryResp.WordsResultDTO wordsResultDTO = bOnlineTaxiItineraryResp.getWordsResult();
            resp.setTotalFare(wordsResultDTO.getTotalFare());
            resp.setEndTime(wordsResultDTO.getEndTime());
            resp.setPhone(wordsResultDTO.getPhone());
            resp.setServiceProvider(wordsResultDTO.getServiceProvider());
            resp.setStartTime(wordsResultDTO.getStartTime());
            resp.setApplicationDate(wordsResultDTO.getApplicationDate());
            resp.setItemId(wordsResultDTO.getItemId());

            List<OnlineTaxiItineraryResp.ItemsDTO> items = new ArrayList<>();


            for (BOnlineTaxiItineraryResp.WordsResultDTO.ItemsDTO item : wordsResultDTO.getItems()) {

                OnlineTaxiItineraryResp.ItemsDTO itemsDTO = new OnlineTaxiItineraryResp.ItemsDTO();
                itemsDTO.setItemId(item.getItemId());
                itemsDTO.setStartPlace(item.getStartPlace());
                item.setPickupTime(item.getPickupTime());
                item.setCarType(item.getCarType());
                item.setCity(item.getCity());
                item.setDistance(item.getDistance());
                item.setPickupDate(item.getPickupDate());
                item.setDestinationPlace(item.getDestinationPlace());
                item.setFare(item.getFare());
                items.add(itemsDTO);
            }

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

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

    }


}
