/*
 * Copyright (c) 2018-2999 广州亚米信息科技有限公司 All rights reserved.
 *
 * https://www.gz-yami.com/
 *
 * 未经允许，不可做商业用途！
 *
 * 版权所有，侵权必究！
 */

package com.yami.shop.delivery.api.controller;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.yami.shop.bean.app.dto.DeliveryDto;
import com.yami.shop.bean.model.RefundDelivery;
import com.yami.shop.common.exception.GlobalException;
import com.yami.shop.delivery.comment.api.paotui.PaoTuiApi;
import com.yami.shop.delivery.comment.api.paotui.model.request.OrderDetailRequest;
import com.yami.shop.delivery.comment.api.paotui.model.response.OrderStatusResponse;
import com.yami.shop.delivery.comment.model.DeliveryOrder;
import com.yami.shop.delivery.comment.service.DeliveryOrderService;
import com.yami.shop.service.RefundDeliveryService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.AllArgsConstructor;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

@RestController
@RequestMapping("/p/myDelivery")
@Api(tags = "我的物流接口")
@AllArgsConstructor
public class MyDeliveryController {


    private final DeliveryOrderService deliveryOrderService;

    private final RefundDeliveryService refundDeliveryService;
    @Autowired
    private PaoTuiApi paoTuiApi;


    /**
     * 物流查询接口
     */
    @GetMapping("/orderInfo/{orderNumber}")
    @ApiOperation(value = "物流查询接口", notes = "根据orderNumber查询订单所有包裹信息")
    public ResponseEntity<List<DeliveryOrder>> info(@PathVariable("orderNumber") String orderNumber) {
        //查询包裹信息
        List<DeliveryOrder> deliveryOrders = deliveryOrderService.getAndDeliveryItemInfo(orderNumber);
        // 查询物流详情信息
        // 物流公司名称、官网、订单号、物流详情信息
        try {
            for (DeliveryOrder deliveryOrder : deliveryOrders) {
                DeliveryDto deliveryDto;
                // 查询交易单

                // 解析的物流详情明细
                OrderDetailRequest orderDetailRequest = new OrderDetailRequest();
                orderDetailRequest.setOrder_id(deliveryOrder.getDvyFlowId());
                OrderStatusResponse orderStatusResponse = paoTuiApi.deliveryRoute(orderDetailRequest);
                deliveryOrder.setDeliveryDto(orderStatusResponse);
            }
        } catch (Exception e) {
            e.printStackTrace();
            throw new GlobalException("查询出错");
        }
        return ResponseEntity.ok(deliveryOrders);
    }

    /**
     * 退货物流查询接口
     */
    @GetMapping("/refund/{refundSn}")
    @ApiOperation(value = "物流查询接口", notes = "根据orderNumber查询订单所有包裹信息")
    public ResponseEntity<OrderStatusResponse> indfo(@PathVariable("refundSn") String refundSn) {


        RefundDelivery refundDelivery = refundDeliveryService.getOne(new LambdaQueryWrapper<RefundDelivery>()
                .eq(RefundDelivery::getRefundSn, refundSn));
        OrderStatusResponse orderStatusResponse = null;
        try {
            OrderDetailRequest orderDetailRequest = new OrderDetailRequest();
            orderDetailRequest.setOrder_id(refundDelivery.getDeyNu());
            orderStatusResponse = paoTuiApi.deliveryRoute(orderDetailRequest);
        } catch (Exception e) {
            e.printStackTrace();
            throw new GlobalException("查询出错");
        }
        return ResponseEntity.ok(orderStatusResponse);
    }

}
