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

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

import com.yami.shop.bean.model.Order;
import com.yami.shop.bean.model.OrderItem;
import com.yami.shop.common.exception.GlobalException;
import com.yami.shop.common.exception.YamiShopBindException;
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.param.DvyOrderParam;
import com.yami.shop.delivery.comment.param.OrderParam;
import com.yami.shop.delivery.comment.service.DeliveryOrderService;
import com.yami.shop.service.DeliveryService;
import com.yami.shop.service.OrderService;
import lombok.AllArgsConstructor;
import ma.glasnost.orika.MapperFacade;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.ResponseEntity;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * 订单配送信息管理实现
 *
 * @author lhd
 * @version 1.0.0
 * @since 2020-05-15
 */
@Controller
@AllArgsConstructor
@RequestMapping("/platform/orderDelivery")
public class OrderDeliveryController {

    private final OrderService orderService;

    private final DeliveryOrderService deliveryOrderService;

    private final DeliveryService deliveryService;

    private final MapperFacade mapperFacade;

    @Autowired
    private PaoTuiApi paoTuiApi;

    /**
     * 查询订单物流包裹信息
     */
    @PutMapping("/getOrderDeliveries/{orderNumber}")
    public ResponseEntity<List<DeliveryOrder>> getOrderDeliveries(@PathVariable("orderNumber") String orderNumber) {
        List<DeliveryOrder> deliveryOrders = deliveryOrderService.listDetailDelivery(orderNumber);
        return ResponseEntity.ok(deliveryOrders);
    }


    /**
     * 修改订单物流包裹信息
     */
    @PutMapping("/updateOrderDeliveries")
    public ResponseEntity<Void> updateOrderDeliveries(@RequestBody DvyOrderParam dvyOrderParam) {
        List<DeliveryOrder> deliveryOrders = dvyOrderParam.getDeliveryOrders();
        deliveryOrderService.updateOrderDeliveries(deliveryOrders);
        return ResponseEntity.ok().build();
    }


    /**
     * 获取信息
     */
    @GetMapping("/orderInfo/{orderNumber}")
    public ResponseEntity<OrderParam> info(@PathVariable("orderNumber") String orderNumber) {
        Order order = orderService.getOrderDetailByOrderNumberAndShopId(orderNumber, null);
        OrderParam orderParam = mapperFacade.map(order, OrderParam.class);
        orderParam.setScore(orderParam.getOrderItems().stream().mapToInt(OrderItem::getUseScore).sum());
        //查询包裹信息
        List<DeliveryOrder> deliveryOrders = deliveryOrderService.getAndDeliveryItemInfo(orderNumber);
        // 查询物流详情信息
        // 物流公司名称、官网、订单号、物流详情信息
        try {
            for (DeliveryOrder deliveryOrder : deliveryOrders) {

                // 解析的物流详情明细
                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("查询出错");
        }
        orderParam.setDeliveryExpresses(deliveryOrders);
        return ResponseEntity.ok(orderParam);
    }

}
