package com.heyu.api.controller.financial;


import com.heyu.api.baidu.handle.financial.BShoppingReceiptHandle;
import com.heyu.api.baidu.request.financial.BShoppingReceiptRequest;
import com.heyu.api.baidu.response.financial.BShoppingReceiptResp;
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.ShoppingReceiptResp;
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=1740579777572#/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%2Fshopping_receipt&method=post
 *购物小票识别
 *
 *
 *接口描述
 * 支持识别各类商场、超市及药店的购物小票，包括店名、小票号码、机器编号、工号、消费日期、消费时间、总金额、找零、币种、实收金额、优惠金额、打印日期、打印时间、明细商品名称、单价、数量、小计金额等信息。
 *
 */


@Slf4j
@RestController
@RequestMapping("/shipping/invoice")
@NotIntercept
public class ShoppingReceiptController extends BaseController {


    @Autowired
    private BShoppingReceiptHandle bShoppingReceiptHandle;


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

        ApiR<BShoppingReceiptResp> bR = bShoppingReceiptHandle.handle(req);
        if (bR.isSuccess()) {
            BShoppingReceiptResp bShoppingReceiptResp = bR.getData();
            List<BShoppingReceiptResp.WordsResultDTO> wordsResultDTO = bShoppingReceiptResp.getWordsResult();

            if (CollectionUtils.isNotEmpty(wordsResultDTO)) {
                for (BShoppingReceiptResp.WordsResultDTO resultDTO : wordsResultDTO) {
                    List<String> totalAmount = new ArrayList<>();                       // 总金额

                    if (CollectionUtils.isNotEmpty(resultDTO.getTotalAmount())) {
                        for (BShoppingReceiptResp.WordsResultDTO.TotalAmountDTO totalAmountDTO : resultDTO.getTotalAmount()) {
                            totalAmount.add(totalAmountDTO.getWord());
                        }
                    }
                    resp.setTotalAmount(totalAmount);


                    List<String> discount = new ArrayList<>();          //优惠/折扣
                    if (CollectionUtils.isNotEmpty(resultDTO.getDiscount())) {
                        for (BShoppingReceiptResp.WordsResultDTO.DiscountDTO discountDTO : resultDTO.getDiscount()) {
                            discount.add(discountDTO.getWord());
                        }
                    }
                    resp.setDiscount(discount);


                    List<String> currency = new ArrayList<>();                  //	币种
                    if (CollectionUtils.isNotEmpty(resultDTO.getCurrency())) {
                        for (BShoppingReceiptResp.WordsResultDTO.CurrencyDTO currencyDTO : resultDTO.getCurrency()) {
                            currency.add(currencyDTO.getWord());
                        }
                    }
                    resp.setCurrency(currency);


                    List<String> receiptNum = new ArrayList<>();                    //小票号码
                    if (CollectionUtils.isNotEmpty(resultDTO.getReceiptNum())) {
                        for (BShoppingReceiptResp.WordsResultDTO.ReceiptNumDTO receiptNumDTO : resultDTO.getReceiptNum()) {
                            receiptNum.add(receiptNumDTO.getWord());
                        }
                    }
                    resp.setReceiptNum(receiptNum);


                    List<String> shopName = new ArrayList<>();              //	店名/超市名字
                    if (CollectionUtils.isNotEmpty(resultDTO.getShopName())) {
                        for (BShoppingReceiptResp.WordsResultDTO.ShopNameDTO shopNameDTO : resultDTO.getShopName()) {
                            shopName.add(shopNameDTO.getWord());

                        }
                    }
                    resp.setShopName(shopName);


                    List<String> consumptionTime = new ArrayList<>();           //消费时间
                    if (CollectionUtils.isNotEmpty(resultDTO.getConsumptionTime())) {
                        for (BShoppingReceiptResp.WordsResultDTO.ConsumptionTimeDTO consumptionTimeDTO : resultDTO.getConsumptionTime()) {
                            consumptionTime.add(consumptionTimeDTO.getWord());
                        }
                    }
                    resp.setConsumptionTime(consumptionTime);

                    List<String> consumptionDate = new ArrayList<>();               //消费日期
                    if (CollectionUtils.isNotEmpty(resultDTO.getConsumptionDate())) {
                        for (BShoppingReceiptResp.WordsResultDTO.ConsumptionDateDTO consumptionDateDTO : resultDTO.getConsumptionDate()) {
                            consumptionDate.add(consumptionDateDTO.getWord());

                        }
                    }
                    resp.setConsumptionDate(consumptionDate);


                    Integer tableRowNum = resultDTO.getTableRowNum();                    //商品明细行数，表示Table中的object个数
                    resp.setTableRowNum(tableRowNum);

                    List<ShoppingReceiptResp.TableDTO> tableList = new ArrayList<>();
                    if (CollectionUtils.isNotEmpty(resultDTO.getTable())) {
                        for (BShoppingReceiptResp.WordsResultDTO.TableDTO tableDTO : resultDTO.getTable()) {

                            ShoppingReceiptResp.TableDTO tab = new ShoppingReceiptResp.TableDTO();
                            tab.setProduct(tableDTO.getProduct().getWord());
                            tab.setQuantity(tableDTO.getQuantity().getWord());
                            tab.setSubtotalAmount(tableDTO.getSubtotalAmount().getWord());
                            tab.setUnitPrice(tableDTO.getUnitPrice().getWord());
                            tableList.add(tab);

                        }
                    }
                    resp.setTable(tableList);


                    List<String> paidAmount = new ArrayList<>();            //实收金额
                    if (CollectionUtils.isNotEmpty(resultDTO.getPaidAmount())) {
                        for (BShoppingReceiptResp.WordsResultDTO.PaidAmountDTO paidAmountDTO : resultDTO.getPaidAmount()) {

                            paidAmount.add(paidAmountDTO.getWord());
                        }
                    }
                    resp.setPaidAmount(paidAmount);


                    List<String> printDate = new ArrayList<>();                     //打印日期
                    if (CollectionUtils.isNotEmpty(resultDTO.getPrintDate())) {
                        for (BShoppingReceiptResp.WordsResultDTO.PrintDateDTO printDateDTO : resultDTO.getPrintDate()) {

                            printDate.add(printDateDTO.getWord());
                        }

                    }
                    resp.setPrintDate(printDate);

                    List<String> machineNum = new ArrayList<>();                //机器编号
                    if (CollectionUtils.isNotEmpty(resultDTO.getMachineNum())) {
                        for (BShoppingReceiptResp.WordsResultDTO.MachineNumDTO machineNumDTO : resultDTO.getMachineNum()) {
                            machineNum.add(machineNumDTO.getWord());

                        }
                    }
                    resp.setMachineNum(machineNum);

                    List<String> change = new ArrayList<>();                    //找零
                    if (CollectionUtils.isNotEmpty(resultDTO.getChange())) {
                        for (BShoppingReceiptResp.WordsResultDTO.ChangeDTO changeDTO : resultDTO.getChange()) {
                            change.add(changeDTO.getWord());

                        }
                    }
                    resp.setChange(change);

                    List<String> employeeNum = new ArrayList<>();                   //工号
                    if (CollectionUtils.isNotEmpty(resultDTO.getEmployeeNum())) {
                        for (BShoppingReceiptResp.WordsResultDTO.EmployeeNumDTO employeeNumDTO : resultDTO.getEmployeeNum()) {


                            employeeNum.add(employeeNumDTO.getWord());
                        }
                    }
                    resp.setEmployeeNum(employeeNum);

                    List<String> printTime = new ArrayList<>();                 //	打印时间

                    if (CollectionUtils.isNotEmpty(resultDTO.getPrintTime())) {
                        for (BShoppingReceiptResp.WordsResultDTO.PrintTimeDTO printTimeDTO : resultDTO.getPrintTime()) {
                            printTime.add(printTimeDTO.getWord());

                        }
                    }
                    resp.setPrintTime(printTime);


                }
            }

        }


        return R.error();
    }


}
